gitlab_time_tracker-8.x-1.x-dev/gitlab_time_tracker.module

gitlab_time_tracker.module
<?php

/**
 * @file
 * Contains gitlab_time_tracker.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Datetime\DateTimePlus;
use \Drupal\node\Entity\Node;

/**
 * Implements hook_help().
 */
function gitlab_time_tracker_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the gitlab_time_tracker module.
    case 'help.page.gitlab_time_tracker':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Timetracking information fetcher') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_cron().
 */
function gitlab_time_tracker_cron() {
  \Drupal::service('gitlab_time_tracker.migration_controller')->migrateUsers();
  \Drupal::service('gitlab_time_tracker.migration_controller')->migrateProjects();

  // Migrate milestones.
  $projects = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'project']);
  foreach ($projects as $project) {
    if (!is_null($project->field_gitlab_id)) {
      \Drupal::service('gitlab_time_tracker.migration_controller')->migrateMilestones(
        $project->field_gitlab_id->value
      );
    }
  }

}

/**
 * Syncronize timetracks for bunch of issue.
 */
function gitlab_time_tracker_migrate_timetracks($issues) {
  $nodes = Node::loadMultiple($issues);
  foreach ($nodes as $node) {

    if (!gitlab_time_tracker_issue_is_synced($node)) {
      \Drupal::service('gitlab_time_tracker.migration_controller')->migrateTimeTrack(
        $node->field_project->entity->field_gitlab_id->value,
        $node->field_gitlab_iid->value,
        FALSE
      );

      $now = new DateTimePlus();
      $node->field_children_sync_date = $now->format("Y-m-d\TH:i:s");
      $node->save();
    }
  }
}

/**
 * Predicate: Is issue fresh (all notes syncronized)?
 */
function gitlab_time_tracker_issue_is_synced($node) {
  $updated_at = $node->field_last_synchronization_date->value;
  $synced_at = $node->field_children_sync_date->value;

  try {
    $updated_at_boxed = DateTimePlus::createFromFormat("Y-m-d\TH:i:s", $updated_at);
    $synced_at_boxed = DateTimePlus::createFromFormat("Y-m-d\TH:i:s", $synced_at)->modify("-1 week");

    return $updated_at_boxed < $synced_at_boxed;
  }
  catch (InvalidArgumentException $exception) {
    // Something wrong at date parsing. We need to re-sync.
    return FALSE;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc