Github integration.

This commit is contained in:
Dan Cryer 2013-05-14 16:37:54 +01:00
parent 9ea3a22864
commit dfeac39be3
2 changed files with 121 additions and 17 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;
}
}

View file

@ -23,6 +23,20 @@
<script>
<?php
$gh = \b8\Registry::getInstance()->get('github_app', null);
if($gh)
{
print 'window.github_app_id = ' . json_encode($gh['id']) . ';' . PHP_EOL;
}
if(!empty($token))
{
print 'window.github_token = ' . json_encode($token) . ';' . PHP_EOL;
}
?>
$(document).ready(function()
{
$('#element-reference').change(function()
@ -39,13 +53,56 @@ $(document).ready(function()
'bb_anon': /https\:\/\/bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
};
for(var i in acceptable) {
if(val.match(acceptable[i])) {
el.val(val.replace(acceptable[i], '$1'));
}
}
});
$('#element-type').change(function()
{
if(!window.github_app_id || $(this).val() != 'github' || window.github_token) {
return;
}
// Show sign in with Github button.
var el = $('#element-reference');
var rtn = <?= json_encode((empty($_SERVER['HTTPS']) ? 'http' : 'https') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); ?>;
var url = 'https://github.com/login/oauth/authorize?client_id=' + window.github_app_id + '&scope=repo&redirect_uri=' + rtn;
var btn = $('<a>').addClass('btn btn-inverse').text('Sign in with Github').attr('href', url);
el.after(btn);
el.remove();
});
$('#element-github').change(function()
{
var val = $('#element-github').val();
if(val != 'choose') {
$('#element-type').val('github');
$('#element-reference').val(val);
$('label[for=element-reference]').hide();
$('label[for=element-type]').hide();
$('#element-reference').hide();
$('#element-type').hide();
$('#element-token').val(window.github_token);
if(!$('#element-title').val()) {
$('#element-title').val(val);
}
}
else {
$('label[for=element-reference]').show();
$('label[for=element-type]').show();
$('#element-reference').show();
$('#element-type').show();
$('#element-reference').val('');
$('#element-token').val('');
}
});
});
</script>