#498156: integrate with triggers, token and the activity module.

From: damz <damz@damz-dev.local>


---
 versioncontrol-backend.inc |   19 ++++++
 versioncontrol.module      |  129 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 148 insertions(+), 0 deletions(-)

diff --git versioncontrol-backend.inc versioncontrol-backend.inc
index bea8b77..626fbc7 100644
--- versioncontrol-backend.inc
+++ versioncontrol-backend.inc
@@ -353,6 +353,25 @@ function versioncontrol_insert_operation($operation, &$operation_items) {
     'insert', $operation, $operation_items
   );
 
+  // Trigger the actions.
+  $operation_map = array(
+    VERSIONCONTROL_OPERATION_COMMIT => 'commit',
+    VERSIONCONTROL_OPERATION_BRANCH => 'branch',
+    VERSIONCONTROL_OPERATION_TAG => 'tag',
+  );
+
+  if (isset($operation_map[$operation['type']])) {
+    $op_name = $operation_map[$operation['type']];
+    $aids = _trigger_get_hook_aids('versioncontrol', $op_name);
+    $context = array(
+      'hook' => 'versioncontrol',
+      'op' => $op_name,
+      // TODO: remove when possible...
+      'versioncontrol' => (object) $operation,
+    );
+    actions_do(array_keys($aids), $operation, $context);
+  }
+
   // This one too, as there is also an update function & hook for it.
   // Pretend that the labels didn't exist beforehand.
   $labels = $operation['labels'];
diff --git versioncontrol.module versioncontrol.module
index f61ff74..8af850e 100644
--- versioncontrol.module
+++ versioncontrol.module
@@ -461,6 +461,135 @@ function versioncontrol_perm() {
   );
 }
 
+/**
+ * Implementation of hook_hook_info().
+ *
+ * Expose triggers to Drupal.
+ */
+function versioncontrol_hook_info() {
+  return array(
+    'versioncontrol' => array(
+      'versioncontrol' => array(
+        'commit' => array(
+          'runs when' => t('A new commit is made on a repository'),
+        ),
+        'branch' => array(
+          'runs when' => t('A new branch is created on a repository'),
+        ),
+        'tag' => array(
+          'runs when' => t('A new tag is created on a repository'),
+        ),
+      ),
+    ),
+  );
+}
+
+/**
+ * Implementation of hook_activity_info().
+ */
+function versioncontrol_activity_info() {
+  $info = (object) array(
+    'api' => 2,
+    'name' => 'versioncontrol',
+    'object_type' => 'versioncontrol',
+    'objects' => array(
+      'author' => 'versioncontrol',
+    ),
+    'hooks' => array(
+      'versioncontrol' => array('commit', 'branch', 'tag'),
+    ),
+  );
+  return $info;
+}
+
+/**
+ * Implementation of hook_token_values().
+ */
+function versioncontrol_token_values($type, $object = NULL, $options = array()) {
+  $values = array();
+  if ($type == 'versioncontrol') {
+    $operation = $object;
+    $values['message']          = check_plain($operation->message);
+
+    $account = !empty($operation->uid) ? user_load($operation->uid) : drupal_anonymous_user();
+    if (empty($account->uid)) {
+      $account->name = variable_get('anonymous', t('Anonymous'));
+      $account->mail = '';
+    }
+    $values['author-uid']       = $operation->uid;
+    $values['author-name']      = check_plain($account->name);
+    $values['author-name-raw']  = $account->name;
+    $values['author-mail']      = check_plain($account->mail);
+    $values['author-mail-raw']  = $account->mail;
+      
+    if (isset($operation->date)) {
+      $date = (int) $operation->date;
+      $values['yyyy']           = date('Y', $date);
+      $values['yy']             = date('y', $date);
+      $values['month']          = date('F', $date);
+      $values['mon']            = date('M', $date);
+      $values['mm']             = date('m', $date);
+      $values['m']              = date('n', $date);
+      $values['ww']             = date('W', $date);
+      $values['date']           = date('N', $date);
+      $values['day']            = date('l', $date);
+      $values['ddd']            = date('D', $date);
+      $values['dd']             = date('d', $date);
+      $values['d']              = date('j', $date);
+    }
+
+    $values['revision']         = $operation->revision;
+
+    if (!empty($operation->labels)) {
+      $labels = array(
+        VERSIONCONTROL_OPERATION_BRANCH => array(),
+        VERSIONCONTROL_OPERATION_TAG => array(),
+      );
+      foreach ($operation->labels as $label) {
+        $labels[$label['type']][] = $label['name'];
+      }
+      $values['branches']      = check_plain(implode(', ', $labels[VERSIONCONTROL_OPERATION_BRANCH]));
+      $values['tags']          = check_plain(implode(', ', $labels[VERSIONCONTROL_OPERATION_TAG]));
+    }
+    else {
+      $values['branches']      = '';
+      $values['tags']          = '';
+    }
+  }
+  return $values;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function versioncontrol_token_list($type = 'all') {
+  if ($type == 'versioncontrol' || $type == 'all') {
+    $tokens['versioncontrol'] = array(
+      'message' => t("Message attached to the operation"),
+      'author-uid' => t("Operation author's user id"),
+      'author-name' => t("Operation author's name"),
+      'author-name-raw' => t("Operation author's name. WARNING - raw user input"),
+      'author-mail' => t("Operation author's e-mail"),
+      'author-mail-row' => t("Operation author's e-mail. WARNING - raw user input"),
+      'yyyy' => t("Operation creation year (four digit)"),
+      'yy' => t("Operation creation year (two digit)"),
+      'month' => t("Operation creation month (full word)"),
+      'mon' => t("Operation creation month (abbreviated)"),
+      'mm' => t("Operation creation month (two digit, zero padded)"),
+      'm' => t("Operation creation month (one or two digit)"),
+      'ww' => t("Operation creation week (two digit)"),
+      'date' => t("Operation creation date (day of month)"),
+      'day' => t("Operation creation day (full word)"),
+      'ddd' => t("Operation creation day (abbreviation)"),
+      'dd' => t("Operation creation day (two digit, zero-padded)"),
+      'd' => t("Operation creation day (one or two digit)"),
+      'revision' => t("Operation revision"),
+      'branches' => t("Branches affected by the operation"),
+      'tags' => t("Tags affected by the operation"),
+    );
+    return $tokens;
+  }
+}
 
 // API functions start here.
 
