Skip to content

Commit

Permalink
Implemented #13: Implement integration with GitHub
Browse files Browse the repository at this point in the history
- Updated code of getting an issue data.
  • Loading branch information
andkirby committed Dec 1, 2015
1 parent 04f87a0 commit c6ff614
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 15 deletions.
17 changes: 17 additions & 0 deletions LibHooks/config/commit-msg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@
</default>
</allowed>
</jira>
<github>
<allowed>
<!--Commit message type: default-->
<default>
<open>1</open>
</default>
</allowed>
</github>
</status>
</issue>
</IssueStatus>
Expand Down Expand Up @@ -186,6 +194,15 @@
<Bug>bug</Bug>
</default>
</jira>
<github>
<default>
<!--JIRA issue-type map to general issue-type-->
<!-- Tasks -->
<enhancement>task</enhancement>
<!-- Bugs -->
<bug>bug</bug>
</default>
</github>
</tracker>
</type>
</issue>
Expand Down
105 changes: 90 additions & 15 deletions LibHooks/lib/PreCommit/Issue/GitHubAdapter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace PreCommit\Issue;

use \Github\Client as Api;
use Github\Client as Api;
use Zend\Cache\Storage\Adapter\Filesystem as CacheAdapter;

/**
Expand Down Expand Up @@ -35,13 +35,37 @@ class GitHubAdapter extends AdapterAbstract implements AdapterInterface
*/
protected $_issueKey;

/**
* Allowed labels which can be used to determine issue type
*
* @var array
*/
protected $_labelTypes = array(
'bug', 'enhancement'
);

/**
* Default issue type (label)
*
* @var array
*/
protected $_defaultLabelType = 'enhancement';

/**
* GitHub API client
*
* @var Api
*/
protected $_api;

/**
* Set issue key
*
* @param string $issueKey
*/
public function __construct($issueKey)
{
parent::__construct($issueKey);
$this->_issueKey = (string)$issueKey;
}

Expand All @@ -54,15 +78,20 @@ public function __construct($issueKey)
protected function _getIssue()
{
if (null === $this->_issue) {
$this->_issue = $this->_getCachedResult($this->_issueKey);
$this->_issue = $this->_getCachedResult(
$this->_getCacheKey()
);
if (!$this->_issue) {
$this->_issue = $this->_loadIssueData();
if (!$this->_issue) {
throw new Exception(
"Issue not {$this->_issueKey} found.", self::EXCEPTION_CODE_ISSUE_NOT_FOUND
"Issue not {$this->_issueKey} found.",
self::EXCEPTION_CODE_ISSUE_NOT_FOUND
);
}
$this->_cacheResult($this->_issueKey, $this->_issue);
$this->_cacheResult(
$this->_getCacheKey(), $this->_issue
);
}
}
return $this->_issue;
Expand All @@ -79,11 +108,22 @@ protected function _getIssueNumber()
}

//region Caching methods
/**
* Get cache key
*
* @return string
*/
protected function _getCacheKey()
{
return $this->_getVendorName() . '-' . $this->_getRepositoryName()
. '-' . $this->_getIssueNumber();
}

/**
* Write summary to cache file
*
* @param string $key
* @param array $result
* @param array $result
* @return $this
*/
protected function _cacheResult($key, $result)
Expand Down Expand Up @@ -143,7 +183,8 @@ protected function _getCache()
*/
public function getSummary()
{
return $this->_getIssue()->getSummary();
$issue = $this->_getIssue();
return $issue['title'];
}

/**
Expand All @@ -153,7 +194,8 @@ public function getSummary()
*/
public function getKey()
{
return $this->_getIssue()->getKey();
$issue = $this->_getIssue();
return $issue['number'];
}

/**
Expand All @@ -163,7 +205,15 @@ public function getKey()
*/
public function getOriginalType()
{
return $this->_getIssue()->getIssueType();
$issue = $this->_getIssue();
if (!empty($issue['labels'])) {
foreach ($issue['labels'] as $label) {
if (in_array($label['name'], $this->_labelTypes)) {
return $label['name'];
}
}
}
return $this->_defaultLabelType;
}

/**
Expand All @@ -173,7 +223,8 @@ public function getOriginalType()
*/
public function getStatus()
{
return $this->_normalizeName($this->_getIssue()->getStatus());
$issue = $this->_getIssue();
return $this->_normalizeName($issue['state']);
}

/**
Expand All @@ -183,7 +234,7 @@ public function getStatus()
*/
public function ignoreIssue()
{
$this->_cacheResult($this->_issueKey, array());
$this->_cacheResult($this->_getCacheKey(), array());
return $this;
}
//endregion
Expand All @@ -201,8 +252,8 @@ protected function _loadIssueData()
throw new Exception('Connection params not fully set.');
}
return $this->_getApi()->issue()->show(
$this->_getConfig()->getNode('github/name'),
$this->_getConfig()->getNode('github/repository'),
$this->_getVendorName(),
$this->_getRepositoryName(),
$this->_getIssueNumber()
);
}
Expand All @@ -214,7 +265,11 @@ protected function _loadIssueData()
*/
protected function _getApi()
{
return new Api();
if ($this->_api === null) {
$this->_api = new Api();
$this->_api->authenticate('andkirby', 'gigaleon33');
}
return $this->_api;
}

/**
Expand All @@ -224,8 +279,28 @@ protected function _getApi()
*/
protected function _canRequest()
{
return $this->_getConfig()->getNode('tracker/github/name')
&& $this->_getConfig()->getNode('tracker/github/repository');
return $this->_getVendorName()
&& $this->_getRepositoryName();
}
//endregion

/**
* Get vendor name
*
* @return string
*/
protected function _getVendorName()
{
return $this->_getConfig()->getNode('tracker/github/name');
}

/**
* Get repository name
*
* @return string
*/
protected function _getRepositoryName()
{
return $this->_getConfig()->getNode('tracker/github/repository');
}
}

0 comments on commit c6ff614

Please sign in to comment.