2013-05-16 00:47:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2013-05-16 03:16:56 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
2013-05-16 00:47:37 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Model\Build;
|
2013-05-16 16:40:40 +02:00
|
|
|
|
2013-05-16 00:47:37 +02:00
|
|
|
use PHPCI\Model\Build\RemoteGitBuild;
|
|
|
|
|
|
|
|
/**
|
2013-05-16 03:30:48 +02:00
|
|
|
* Github Build Model
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Core
|
|
|
|
*/
|
2013-05-16 00:47:37 +02:00
|
|
|
class GithubBuild extends RemoteGitBuild
|
|
|
|
{
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get link to commit from another source (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function getCommitLink()
|
|
|
|
{
|
|
|
|
return 'https://github.com/' . $this->getProject()->getReference() . '/commit/' . $this->getCommitId();
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get link to branch from another source (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function getBranchLink()
|
|
|
|
{
|
|
|
|
return 'https://github.com/' . $this->getProject()->getReference() . '/tree/' . $this->getBranch();
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Send status updates to any relevant third parties (i.e. Github)
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
public function sendStatusPostback()
|
|
|
|
{
|
2013-10-11 22:51:23 +02:00
|
|
|
$token = \b8\Config::getInstance()->get('phpci.github.token');
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-10-11 22:51:23 +02:00
|
|
|
if (empty($token)) {
|
2013-05-16 16:40:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-10-11 22:51:23 +02:00
|
|
|
$project = $this->getProject();
|
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
$url = 'https://api.github.com/repos/'.$project->getReference().'/statuses/'.$this->getCommitId();
|
|
|
|
$http = new \b8\HttpClient();
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
switch($this->getStatus())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
$status = 'pending';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$status = 'success';
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
$status = 'failure';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$status = 'error';
|
|
|
|
break;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-10-10 02:30:40 +02:00
|
|
|
$phpciUrl = \b8\Config::getInstance()->get('phpci.url');
|
2013-05-16 16:40:40 +02:00
|
|
|
$params = array( 'state' => $status,
|
2013-10-10 02:30:40 +02:00
|
|
|
'target_url' => $phpciUrl . '/build/view/' . $this->getId());
|
2013-05-16 16:40:40 +02:00
|
|
|
$headers = array(
|
2013-10-11 22:51:23 +02:00
|
|
|
'Authorization: token ' . $token,
|
2013-05-16 16:40:40 +02:00
|
|
|
'Content-Type: application/x-www-form-urlencoded'
|
|
|
|
);
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
$http->setHeaders($headers);
|
2013-10-10 02:30:40 +02:00
|
|
|
$http->request('POST', $url, json_encode($params));
|
2013-05-16 16:40:40 +02:00
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Get the URL to be used to clone this remote repository.
|
|
|
|
*/
|
2013-05-16 16:40:40 +02:00
|
|
|
protected function getCloneUrl()
|
|
|
|
{
|
|
|
|
$key = trim($this->getProject()->getGitKey());
|
2013-05-16 00:47:37 +02:00
|
|
|
|
2013-05-16 16:40:40 +02:00
|
|
|
if (!empty($key)) {
|
|
|
|
return 'git@github.com:' . $this->getProject()->getReference() . '.git';
|
|
|
|
} else {
|
|
|
|
return 'https://github.com/' . $this->getProject()->getReference() . '.git';
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 13:28:27 +02:00
|
|
|
|
|
|
|
public function getCommitMessage()
|
|
|
|
{
|
|
|
|
$rtn = $this->data['commit_message'];
|
|
|
|
|
2014-05-01 17:53:29 +02:00
|
|
|
$reference = $this->getProject()->getReference();
|
|
|
|
$commitLink = '<a target="_blank" href="https://github.com/' . $reference . '/issues/$1">#$1</a>';
|
|
|
|
$rtn = preg_replace('/\#([0-9]+)/', $commitLink, $rtn);
|
2014-04-25 13:28:27 +02:00
|
|
|
$rtn = preg_replace('/\@([a-zA-Z0-9_]+)/', '<a target="_blank" href="https://github.com/$1">@$1</a>', $rtn);
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileLinkTemplate()
|
|
|
|
{
|
|
|
|
$link = 'https://github.com/' . $this->getProject()->getReference() . '/';
|
|
|
|
$link .= 'blob/' . $this->getBranch() . '/';
|
|
|
|
$link .= '{FILE}';
|
|
|
|
$link .= '#L{LINE}';
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
2013-05-16 00:47:37 +02:00
|
|
|
}
|