Github integration.

This commit is contained in:
Dan Cryer 2013-05-14 16:37:54 +01:00
commit dfeac39be3
2 changed files with 114 additions and 10 deletions

View file

@ -94,18 +94,34 @@ class ProjectController extends b8\Controller
$pub = file_get_contents($id . '.pub');
$prv = file_get_contents($id);
$values = array('key' => $prv);
$values = array('key' => $prv, 'pubkey' => $pub);
}
$form = $this->projectForm($values);
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
{
$gh = \b8\Registry::getInstance()->get('github_app');
$code = $this->getParam('code', null);
if(!is_null($code))
{
$http = new \b8\HttpClient();
$resp = $http->post('https://github.com/login/oauth/access_token', array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code));
if($resp['success'])
{
parse_str($resp['body'], $resp);
$_SESSION['github_token'] = $resp['access_token'];
}
}
$view = new b8\View('ProjectForm');
$view->type = 'add';
$view->project = null;
$view->form = $form;
$view->key = $pub;
$view->token = $_SESSION['github_token'] ? $_SESSION['github_token'] : null;
return $view->render();
}
@ -171,27 +187,40 @@ class ProjectController extends b8\Controller
$form->setMethod('POST');
$form->setAction('/project/' . $type);
$form->addField(new Form\Element\Csrf('csrf'));
$form->addField(new Form\Element\Hidden('token'));
$form->addField(new Form\Element\Hidden('pubkey'));
$field = new Form\Element\Select('type');
$field->setRequired(true);
$field->setPattern('^(github|bitbucket)');
$field->setOptions(array('choose' => 'Select repository type...', 'github' => 'Github', 'bitbucket' => 'Bitbucket'));
$field->setLabel('Where is your project hosted?');
$field->setClass('span4');
$form->addField($field);
if(isset($_SESSION['github_token']))
{
$field = new Form\Element\Select('github');
$field->setPattern('[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+');
$field->setLabel('Choose a Github repository:');
$field->setClass('span4');
$field->setOptions($this->getGithubRepositories());
$form->addField($field);
}
$field = new Form\Element\Text('reference');
$field->setRequired(true);
$field->setPattern('[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+');
$field->setLabel('Repository Name / URL:');
$field->setClass('span4');
$form->addField($field);
$field = new Form\Element\Text('title');
$field->setRequired(true);
$field->setLabel('Project Title');
$field->setClass('span4');
$form->addField($field);
$field = new Form\Element\Select('type');
$field->setRequired(true);
$field->setOptions(array('github' => 'Github', 'bitbucket' => 'Bitbucket'));
$field->setLabel('Where is your project hosted?');
$field->setClass('span4');
$form->addField($field);
$field = new Form\Element\Text('reference');
$field->setRequired(true);
$field->setPattern('[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+');
$field->setLabel('Repository Name on Github / Bitbucket (e.g. block8/phpci)');
$field->setClass('span4');
$form->addField($field);
$field = new Form\Element\TextArea('key');
$field->setRequired(false);
$field->setLabel('Private key to use to access repository (leave blank to use anonymous HTTP repository access)');
@ -207,4 +236,22 @@ class ProjectController extends b8\Controller
$form->setValues($values);
return $form;
}
protected function getGithubRepositories()
{
$http = new \b8\HttpClient();
$res = $http->get('https://api.github.com/user/repos', array('type' => 'all', 'access_token' => $_SESSION['github_token']));
$rtn = array();
$rtn['choose'] = 'Select a repository...';
if($res['success'])
{
foreach($res['body'] as $repo)
{
$rtn[$repo['full_name']] = $repo['full_name'];
}
}
return $rtn;
}
}