summaryrefslogtreecommitdiffstats
path: root/pift.module
blob: 9327ebcc6f195ab98eeaba39b7301ffc448e71ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php

/**
 * @file
 * Integrates into project to provide an automated testing hub.
 *
 * @author Jimmy Berry ("boombatower", http://drupal.org/user/214218)
 */

/*
 * Variables loaded as constants.
 */
define('PIFT_FREQUENCY', variable_get('pift_frequency', -1));
define('PIFT_LAST', variable_get('pift_last', 0));
define('PIFT_KEY', variable_get('pift_key', ''));
define('PIFT_SERVER', variable_get('pift_server', ''));
define('PIFT_DESCRIPTION', variable_get('pift_description', ''));
define('PIFT_FOLLOWUP_FAIL', variable_get('pift_followup_fail', 0));
define('PIFT_FOLLOWUP_RETEST', variable_get('pift_followup_retest', 0));
define('PIFT_REGEX', variable_get('pift_regex', '/(\.diff|\.patch)$/'));
define('PIFT_PID', variable_get('pift_pid', 3060));  // TODO: Drupal.org specific, to expediate the D7 port
define('PIFT_RETEST', variable_get('pift_retest', 24 * 60 * 60));
define('PIFT_DELETE', variable_get('pift_delete', FALSE));
define('PIFT_LAST_RETRIEVE', variable_get('pift_last_retrieve', 1));

/**
 * Maximum number of items of the same type to transfer in a single XML-RPC
 * request.
 */
define('PIFT_XMLRPC_MAX', 50);

/**
 * Maximum number of batches to send during a single cron run.
 */
define('PIFT_XMLRPC_MAX_BATCHES', 3);

/*
 * Test type codes.
 */
define('PIFT_TYPE_RELEASE', 1);
define('PIFT_TYPE_FILE', 2);

/*
 * Test status codes.
 */
define('PIFT_STATUS_QUEUE', 1);
define('PIFT_STATUS_SENT', 2);
define('PIFT_STATUS_FAIL', 3);
define('PIFT_STATUS_PASS', 4);

if (!defined('PIFR_RESPONSE_ACCEPTED')) {
  /*
   * PIFR XML-RPC response codes.
   */
  define('PIFR_RESPONSE_ACCEPTED', 1);
  define('PIFR_RESPONSE_INVALID_SERVER', 2);
  define('PIFR_RESPONSE_DENIED', 3);

  /*
   * Project type codes.
   */
  define('PIFR_SERVER_PROJECT_TYPE_CORE', 1);
  define('PIFR_SERVER_PROJECT_TYPE_MODULE', 2);
}

/*
 * Load required includes.
 */
module_load_include('project.inc', 'pift');
module_load_include('test.inc', 'pift');

/**
 * Implements hook_menu().
 */
function pift_menu() {
  $items = array();

  $items['admin/project/pift'] = array(
    'title' => 'Automated testing',
    'description' => 'Configure the automated testing project issue integration.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pift_admin_settings_form'),
    'access arguments' => array('administer projects'),
    'file' => 'pift.admin.inc',
  );
  $items['pift/retest/%'] = array(
    'title' => 'Request retest of a file or branch',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pift_pages_retest_confirm_form', 2),
    'access arguments' => array('pift re-test files'),
    'file' => 'pift.pages.inc',
    'type' => MENU_CALLBACK,
  );
  $items['pift/delete/%'] = array(
    'title' => 'Request deletion of a test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pift_pages_delete_test_confirm_form', 2),
    'access arguments' => array('pift re-test files'),
    'file' => 'pift.pages.inc',
    'type' => MENU_CALLBACK,
  );

// TODO: The plan was to remove the testing-status tab for the D7 Port, and
// add it back in with Conduit.  However, we need somewhere to store the
// 'enable testing' checkbox, so leaving this in temporarily until the Conduit
// piece is available.
  $items['node/%node/qa-settings'] = array(
    'title' => t('Automated Testing'),
    'access callback' => 'pift_results_visibility',
    'access arguments' => array(1),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pift_pages_project_issue_settings', 1),
    'file' => 'pift.pages.inc',
    'weight' => '5',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Access callback to determine whether the Automated Testing tab is visible.
 */
function pift_results_visibility($node) {
  // Only display the tab on projects with releases
  if (project_node_is_project($node)) {
    if ($node->field_project_has_releases[$node->language][0]['value']) {
      // Ensure user has access to enable testing on the node
      if (user_access('pift enable project testing')) {
        return TRUE;
      }
    }
  }
  return FALSE;
}

/**
 * Implements hook_permission().
 */
function pift_permission() {
  return array(
    'pift re-test files' => array(
      'title' => t('pift re-test files'),
      'description' => t('Request a file to be re-tested'),
    ),
    'pift enable project testing' => array(
      'title' => t('pift enable project testing'),
      'description' => t('Enable testing on a project'),
    ),
    'pift access project testing tab' => array(
      'title' => t('pift access project testing tab'),
      'description' => t('Access the testing tab on projects'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function pift_theme() {
  return array(
    'pift_attachments' => array(
      'variables' => array(
        'files' => array(),
        'closed' => FALSE,
      ),
      'file' => 'pift.pages.inc',
    ),
    'pift_auto_followup' => array(
      'variables' => array(
        'type' => '',
        'nid' => 0,
        'cid' => 0,
        'filename' => '',
      ),
    ),
  );
}

/**
 * Implements hook_init().
 */
function pift_init() {
  drupal_add_css(drupal_get_path('module', 'pift') . '/pift.css');
}

/**
 * Implements hook_cron().
 */
function pift_cron() {
  if (PIFT_DELETE) {
    // An issue comment or node has been deleted, remove related test entries.
    pift_test_delete_files();
    variable_set('pift_delete', FALSE);
  }

  // Check if sending is enabled and that the sending frequency has elapsed.
  $time = REQUEST_TIME;
  if (PIFT_FREQUENCY != -1 && $time > PIFT_LAST + PIFT_FREQUENCY) {
    module_load_include('cron.inc', 'pift');

    // Requeue all tests that have passed the re-test interval.
//    pift_cron_retest(); TODO fix query.

    // Send a batch of queued tests.
    pift_cron_queue_batch();

    // Retrieve any results that have occured since last cron run.
    pift_cron_retrieve_results();

    // Store current time as last run.
    variable_set('pift_last', $time);
  }
}

/**
 * Implements hook_versioncontrol_code_arrival().
 */
function pift_versioncontrol_code_arrival(VersioncontrolRepository $repository, VersioncontrolEvent $event) {
  // Ignore events for disabled projects and non-Git repos.
  if (!$repository instanceof VersioncontrolGitRepository || !pift_project_enabled($repository->project_nid)) {
    return;
  }

  module_load_include('cron.inc', 'pift');
  $api_versions = pift_core_api_versions();

  $branch_names = array();
  foreach ($event as $ref) {
    if (VERSIONCONTROL_GIT_REFTYPE_BRANCH === $ref->reftype) {
      $branch_names[] = $ref->refname;
    }
  }

  $rids = pift_cron_get_release($repository->project_nid, $branch_names);
  foreach ($rids as $rid) {
    // Ensure that one of the compatibility terms is present on the release node.
    $release = node_load($rid);
    $found = FALSE;
    // TODO: Update loop, since $release->taxonomy will not exist
    foreach ($api_versions as $api_version) {
      if (array_key_exists($api_version, $release->taxonomy)) {
        // Compatible term found, continue processing.
        $test_id = db_query('SELECT test_id
                                          FROM {pift_test}
                                          WHERE type = :type
                                          AND id = :id', array(':type' => PIFT_TYPE_RELEASE, ':id' => $rid))->fetchField();

        // If existing test for release, queue it, otherwise add a new test.
        if ($test_id) {
          pift_test_requeue($test_id);
        }
        else if ($test_id !== 0) {
          pift_test_add(PIFT_TYPE_RELEASE, $rid);
        }
        break;
      }
    }
  }
}

/**
 * Helper fuction to build a list of releases for this project's repository.
 *
 * @param $node The project node object
 * @param $quiet Silence error messages
 * @return array List of available labels with corresponding release nodes
 */
function pift_get_releases($node, $quiet = FALSE) {
  $valid = pift_valid_prefix_list();
  $branches = array();
  $topbranches = array();
  if (project_node_is_project($node)) {
    $result = db_query("Select b.name from {versioncontrol_release_labels} a
       join {versioncontrol_labels} b on a.label_id = b.label_id
       where a.project_nid = :nid", array(':nid' => $node->nid));
    foreach ($result as $data) {
      // Filter out any branches < 6 (not accepted by PIFT)
      // Move '.x' releases to the top
      if (in_array(substr($data->name, 0, 3), $valid)) {
        if (substr($data->name, strlen($data->name) - 2, 2) == '.x') {
          $topbranches[$data->name] = $data->name;
        }
        else {
          $branches[$data->name] = $data->name;
        }
      }
    }
    // Sort to get the highest branch on top
    uasort($branches, 'version_compare');
    uasort($topbranches, 'version_compare');
    $branches = array_merge($branches, $topbranches);
    $branches = pift_array_reverse($branches);

    if (empty($branches) && !$quiet) {
      drupal_set_message(t('No releases found for the given project.'), 'error', FALSE);
    }
  }
  return $branches;
}

/**
 * Returns a listing of valid project release prefixes (ie. 6.x, 7.x, 8.x)
 */
function pift_valid_prefix_list() {
  $terms = array();
  $tids = variable_get('pift_core', array());
  foreach ($tids as $key => $value) {
    if (!empty($tids[$key])) {
      $term = taxonomy_term_load($key);
      $terms[$key] = $term->name;
    }
  }
  return $terms;
}


/**
 * Alternative function for the php standard array_reverse()
 *
 * As array_reverse() would damage numerical array keys, from
 * http://drupal.org/node/1074220.  Code copied from project_git_instructions
 *
 * Borrowed from http://php.net/manual/en/function.array-reverse.php#102492
 */
function pift_array_reverse($array) {
  $array_key = array_keys($array);
  $array_value = array_values($array);

  $array_return = array();
  for ($i = 1, $size_of_array = sizeof($array_key); $i <= $size_of_array; $i++) {
    $array_return[$array_key[$size_of_array -$i]] = $array_value[$size_of_array -$i];
  }

  return $array_return;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function pift_form_project_issue_node_form_alter(&$form, $form_state, $form_id) {
    module_load_include('pages.inc', 'pift');
    pift_pages_description_add($form, $form_state, $form_id);
}

/**
 * Implements hook_node_view().
 *
 * TODO:  This approach is no longer valid after the project* changes. The
 * new approach will be to implement a field formatter on the field_issue_files
 * field, and add the pift testing results via this field formatter; which
 * should be much cleaner than removing and injecting the file attachments
 * table as was done in D6.
 */
//function pift_node_view($node, $view_mode = 'full') {
//  if (pift_node_is_interesting($node)) {
//    if (!$a3 && pift_project_enabled($node->project_issue['pid'])) { // Full view.
//      $files = pift_test_get_files_node($node->nid);
//      $status = $node->project_issue['sid'];
//      $node->content['pift_files'] = array(
//        '#value' => '<div id="pift-results-' . $node->nid . '">' .
//                        theme('pift_attachments', array('files' => $files, 'closed' => $status)) . '</div>',
//        '#weight' => 50,
//      );
//      unset($node->content['files']); // Remove old attachments table.
//    }
//  }
//}

/**
 * Implements hook_node_insert().
 */
function pift_node_insert($node) {
  if (pift_node_is_interesting($node)) {
    if (!empty($node->field_issue_files)) {
      if (pift_test_check_criteria_issue($node)) {
        pift_test_add_files($node->field_issue_files);
      }
    }
  }
}

/**
 * Implements hook_node_update().
 */
function pift_node_update($node) {
  if (pift_node_is_interesting($node)) {
    if ($node->field_issue_files != $node->original->field_issue_files) {
      if (!empty($node->field_issue_files)) {
        if (pift_test_check_criteria_issue($node)) {
          pift_test_add_files($node->field_issue_files);
        }
      }
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function pift_node_delete($node) {
  if (pift_node_is_interesting($node)) {
    // Flag pift that a project or issue node was deleted.
    if (project_node_is_project($node)) {
      // Remove this project from the pift_project table
      db_delete('pift_project')->condition('nid', $node->nid)->execute();
    }
    variable_set('pift_delete', TRUE);
  }
}

/**
 * Check if a node is a project node or issue node with file attached.
 *
 * Used in hook_node_*() to filter out irrelevant nodes.
 */
function pift_node_is_interesting($node) {
  return project_node_is_project($node) || (project_issue_node_is_issue($node) && !empty($node->field_issue_files));
}

/**
 * Cleanup the inconsistent project_issue property placement.
 *
 * TODO:  Confirm this approach is no longer valid after the project* changes.
 *
 * In order to remove the need to a bunch of conditions all over PIFT, convert
 * the inconsistent node format to the one used everywhere else. The
 * inconsistent format is only found during node creation, after a node has
 * been created and hook_load() is used the properties are prefixed by
 * project_issue.
 *
 *
 *
 * @param object $node Node to convert.
 * @return object Properly formatted node.
 * @link http://drupal.org/node/519562
 */
//function pift_nodeapi_clean($node) {
//  $node->project_issue = array();
//
//  $fields = array('pid', 'rid', 'component', 'category', 'priority', 'assigned', 'sid');
//  foreach ($fields as $field) {
//    $node->project_issue[$field] = $node->$field;
//  }
//
//  return $node;
//}

/**
 * Implements hook_comment_view().
 *
 * TODO:  This approach is no longer valid after the project* changes. The
 * new approach will be to implement a field formatter on the field_issue_files
 * field and comments, and add the pift testing results via this field
 * formatter; which should be much cleaner than removing and injecting the file
 * attachments table as was done in D6.
 */
//function pift_comment_view($comment) {
//  if ($node = pift_comment_is_interesting($comment)) {
//    if (!empty($comment->files) && pift_project_enabled($node->project_issue['pid'])) {
//      // Remove comment_upload attachments table and generate new one.
//      $comment->comment = preg_replace('/<table class="comment-upload-attachments">.*?<\/table>/s', '', $comment->comment);
//      $files = pift_test_get_files_comment($comment->cid);
//      $status = $node->project_issue['sid'];
//      $comment->comment .= '<div id="pift-results-' . $comment->nid . '-' . $comment->cid . '">' .
//                              theme('pift_attachments', array('files' => $files, 'closed' => $status)) . '</div>';
//    }
//  }
//}

/**
 * Implements hook_comment_insert().
 *
 * TODO:  This approach is no longer valid after the project* changes. Files
 * and patches are now attached to the node directly.
 */
//function pift_comment_insert($comment) {
//  if ($node = pift_comment_is_interesting($comment)) {
//    if (pift_test_check_criteria_issue($node)) {
//      if (!empty($comment->files)) {
//        // Add attachments to this comment to the send queue.
//        $files = comment_upload_load_files($comment['cid']);
//        pift_test_add_files($files);
//      }
//      // Add previously submitted files if issue state changes.
//      pift_test_add_previous_files($comment['nid']);
//    }
//  }
//}

/**
 * Implements hook_comment_delete().
 *
 * TODO:  Determine if we want to remove patches when the associated comment is
 * deleted ... this may now be redundant, since we can simply delete the file
 * from the field_issue_files field.
 */
//function pift_comment_delete($comment) {
//  if (pift_comment_is_interesting($comment)) {
//    variable_set('pift_delete', TRUE);
//  }
//}

/**
 * Check that comment is attached to a project_issue node.
 *
 * TODO:  This is only called from three functions, all of which have been
 * commented out as part of the D7 port.
 */
//function pift_comment_is_interesting($comment) {
//  if (($node = node_load($comment->nid)) && $node->type == 'project_issue') {
//    return $node;
//  }
//  return FALSE;
//}

/**
 * Theme the auto followup comments.
 *
 * @param string $type Type of following, either: 'retest' or 'fail'.
 * @param integer $nid Node ID containting the failed test.
 * @param integer $cid Comment ID, if applicable, containing the failed test.
 * @param string $filename Name of file.
 * @return string HTML output.
 */
function theme_pift_auto_followup($variables) {
  // TODO: Validate whether these variables are still valid after the project*
  // changes introduced with the D7 port.
  $type = $variables['type'];
  $nid = $variables['nid'];
  $cid = $variables['cid'];
  $filename = $variables['filename'];
  $args = array(
    '@id' => "pift-results-$nid",
    '@filename' => $filename,
  );

  if ($type == 'retest') {
    if ($cid) {
      $comment = comment_load($cid);
      $args['@cid'] = $comment->cid;
      $args['@comment'] = $comment->subject;
      return t('<a href="#comment-@cid">@comment</a>: <a href="#@id">@filename</a> queued for re-testing.', $args);
    }
    return t('<a href="#@id">@filename</a> queued for re-testing.', $args);
  }
  elseif ($type == 'fail') {
    return t('The last submitted patch, <a href="#@id">@filename</a>, failed testing.', $args);
  }
  return '';
}

/**
 * Get the core compatible API version term IDs.
 *
 * @return array Associative array of core compatible API version term IDs.
 */
function pift_core_api_versions() {
  return array_filter(variable_get('pift_core', array()));
}

/**
 * Load the core release for the given API term ID.
 *
 * @param integer $api_tid Drupal core API compatibility term ID, of the
 *   vocabulary defined by _project_release_get_api_vid.
 * @return Drupal core release NID.
 * @see _project_release_get_api_vid()
 */
function pift_core_api_release($api_tid) {
  static $api_releases = array();
  if (!isset($api_branches[$api_tid])) {
    $api_vocabulary = taxonomy_vocabulary_load(variable_get('project_release_api_vocabulary', ''));
    $taxonomy_field = 'taxonomy_vocabulary_' . $api_vocabulary->machine_name;

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'project_release')   // TODO: Drupal.org specific
      ->fieldCondition('field_release_project', 'target_id', PIFT_PID, '=')
      ->fieldCondition($taxonomy_field, 'tid', $api_tid)
      ->propertyOrderBy('nid', 'DESC')
      ->range(0,1);
    $result = $query->execute();

    if (isset($result['node'])) {
      $api_releases[$api_tid] = reset(array_keys($result['node']));
    }
  }

  return $api_releases[$api_tid];
}

/**
 * Return a list of active project release compatibility terms in the system.
 */
function pift_compatibility_list() {
  $compatibility_list = array();

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', variable_get('project_release_api_vocabulary', -1))
    ->fieldCondition('field_release_recommended', 'value', '1');
  $result = $query->execute();

  if (isset($result['taxonomy_term'])) {
    $term_ids = array_keys($result['taxonomy_term']);
    $terms = entity_load('taxonomy_term', $term_ids);
  }

  foreach ($terms as $key => $term) {
    $compatibility_list[$key] = $term->name;
  }

  return $compatibility_list;
}