Skip to content

Commit

Permalink
Merge pull request civicrm#32 from totten/master-git-footnote-up2
Browse files Browse the repository at this point in the history
CRM-12612 - For two-words commits (eg "fix CRM-12612"), put JIRA title i...
  • Loading branch information
totten committed Sep 13, 2013
2 parents 01fd08b + 549d40f commit c785c2c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
53 changes: 36 additions & 17 deletions git-footnote/src/CRM/GitFootnote/JiraFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,46 @@ public function __construct($wordPattern, $url, $jiraApi = NULL) {
}

public function filter(CommitMessage $message) {
$words = $this->parseWords(trim($message->getMessage(), "\r\n\t "));
if (count($words) == 1) {
$message->setMessage($this->filterStandaloneWord($words[0]));
} else {
parent::filter($message);
// If message is a single line with 1-2 real words and 1 JIRA issue,
// then use filterShortMessage
$trimmedMessage = trim($message->getMessage(), "\r\n\t ");
if (substr_count($trimmedMessage, "\n") == 0) {
$words = $this->parseWords($trimmedMessage);
if (count($words) >= 1 && count($words) <= 3) {
$issueKeys = array_filter($words, array($this, 'isIssueKey'));
if (count($issueKeys) == 1) {
$message->setMessage($this->filterShortMessage($words));
return;
}
}
}

// Otherwise, use standard filter+footnotes
parent::filter($message);
}

/**
* Given a single-word commit, filter the one word
* Given a short commit message with single issue reference, add
* the JIRA title to summary line.
*
* @param $word
* @param array $words
* @return string
*/
public function filterStandaloneWord($word) {
if (preg_match($this->wordPattern, $word)) {
$issue = $this->getIssue($word);
if ($issue) {
return ($word . ' - ' . $issue->getSummary() . "\n\n" . $this->createIssueUrl($word));
}
else {
return ($word . ' - ' . $this->createIssueUrl($word));
public function filterShortMessage($words) {
$suffix = '';
foreach ($words as $word) {
if ($this->isIssueKey($word)) {
$issue = $this->getIssue($word);
if ($issue) {
$suffix = ' - ' . $issue->getSummary() . "\n\n" . $this->createIssueUrl($word);
}
else {
$suffix = ' - ' . $this->createIssueUrl($word);
}
break;
}
}
return $word;
return implode('', $words) . $suffix;
}

/**
Expand All @@ -56,7 +71,7 @@ public function filterStandaloneWord($word) {
* @return mixed
*/
public function filterWord(CommitMessage $message, $word) {
if (preg_match($this->wordPattern, $word)) {
if ($this->isIssueKey($word)) {
$issue = $this->getIssue($word);
if ($issue) {
$title = $word . ': ' . $issue->getSummary();
Expand Down Expand Up @@ -97,4 +112,8 @@ protected function getIssue($key) {
protected function createIssueUrl($issueKey) {
return $this->url . '/browse/' . $issueKey;
}

protected function isIssueKey($word) {
return preg_match($this->wordPattern, $word);
}
}
38 changes: 30 additions & 8 deletions git-footnote/tests/CRM/GitFootnote/JiraFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,41 @@ function offlineCases() {
"Hello",
array()
);
$cases[] = array(
"Hello world",
"Hello world",
array()
);
$cases[] = array(
"CRM-1234",
"CRM-1234 - http://example.com/jira/browse/CRM-1234",
array()
);
$cases[] = array(
"Hello CRM-1234",
"Hello CRM-1234",
"Hello world CRM-1234",
"Hello world CRM-1234",
array("CRM-1234:\n http://example.com/jira/browse/CRM-1234")
);
$cases[] = array(
" Hello CRM-1234",
" Hello CRM-1234",
" Hello world CRM-1234",
" Hello world CRM-1234",
array("CRM-1234:\n http://example.com/jira/browse/CRM-1234")
);
$cases[] = array(
"Hello CRM-1234! ",
"Hello CRM-1234! ",
"Hello world CRM-1234! ",
"Hello world CRM-1234! ",
array("CRM-1234:\n http://example.com/jira/browse/CRM-1234")
);
$cases[] = array(
"CRM-1234 Hello",
"CRM-1234 Hello",
"CRM-1234 Hello world",
"CRM-1234 Hello world",
array("CRM-1234:\n http://example.com/jira/browse/CRM-1234")
);
$cases[] = array(
"fix CRM-1234",
"fix CRM-1234 - http://example.com/jira/browse/CRM-1234",
array(),
);
$cases[] = array(
"CRM-1234 CRM-567",
"CRM-1234 CRM-567",
Expand Down Expand Up @@ -126,6 +136,18 @@ function onlineCases() {
array(),
array($webResponses['CRM-1234'], $webResponses['schema'])
);
$cases[] = array(
"CRM-1234 fix",
"CRM-1234 fix - Four digit ticket\n\nhttp://example.com/jira/browse/CRM-1234",
array(),
array($webResponses['CRM-1234'], $webResponses['schema'])
);
$cases[] = array(
"fix CRM-1234",
"fix CRM-1234 - Four digit ticket\n\nhttp://example.com/jira/browse/CRM-1234",
array(),
array($webResponses['CRM-1234'], $webResponses['schema'])
);
$cases[] = array(
"CRM-1234 - Foo",
"CRM-1234 - Foo",
Expand Down

0 comments on commit c785c2c

Please sign in to comment.