使用 bitbucket REST API做个展示项目提交动态的WP模板页面
最近用上bitbucket了,github也有在用,不过它不允许免费用户创建private仓库。
bitbucket支持 Git和Mercurial两种类型的repo.且带Project management功能(Issue tracking+ Wiki),网页如同github一样,保持着简洁的特性。
bitbucket REST APIs 文档:http://confluence.atlassian.com/display/BITBUCKET/Using+the+bitbucket+REST+APIs
我这里只用到了Changesets REST resource。
使用举例:
$ curl https://api.bitbucket.org/1.0/repositories/jespern/django-piston/changesets
{
"count": 219,
"start": "tip",
"limit": 15,
"changesets": [
{
"node": "21a24da68710",
"files": [
{
"type": "modified",
"file": "piston/oauth.py"
}
],
"author": "jespern",
"timestamp": "2009-09-08 12:49:43",
"branch": "default",
"message": "oauth 1.0a spec ready oauth.py",
"revision": 204,
"size": 4166
},
...
}
默认是返回json数据。
接口调用方式:
GET /repositories/USERNAME/REPO_SLUG/changesets
USERNAME: The owner username.
REPO_SLUG: The slug of the repository.
另外,还提供两个参数start 和 limit .
如,只取2条:
$ curl https://api.bitbucket.org/1.0/repositories/jespern/django-piston/changesets?limit=2
Authentication(认证)
对于public项目,以上这个调用是无需认证的,但是对于private repo,要获取数据就必须认证了。
认证方式可以采用HTTP BASIC AUTH 或 OAuth 1.0a . 为了避免要加载一个OAuth 类库,我这里就采用HTTP BASIC AUTH认证了。
3600* 1 )
{
unlink($bitbucket_cache_file);
}
else
{
$changesets = unserialize( base64_decode(file_get_contents($bitbucket_cache_file) ) );
}
}
if( !is_array($changesets) || count($changesets) < 1 )
{
//set up required options
$ch = curl_init();
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 3 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 3 );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, 'WordPress/' . $wp_version . '; ihacklog-WP-bitbucket-page-template-v1.0' );
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch,CURLOPT_USERPWD, $userlogin. ':' . $password);
$data = curl_exec($ch);
curl_close($ch);
if( $data )
{
$arr = json_decode($data);
$changesets = $arr->changesets;
}
file_put_contents($bitbucket_cache_file,base64_encode(serialize($changesets) ) );
}
foreach( $changesets as $change )
{
$change_url = sprintf('https://bitbucket.org/%1$s/%2$s/changeset/%3$s',$repo_admin,$repo, $change->node);
$raw_author = explode(' ',$change->raw_author);
echo '';
echo ''. $change->node . '';
echo '' . gmdate('Y-m-d H:i:s',strtotime($change->utctimestamp) + 3600*8) .'';
echo ' ';
echo '
';
}
}
?>
>
Not Found
Sorry, but you are looking for something that is not here.
All Comments (0)