-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_api.php
115 lines (95 loc) · 2.87 KB
/
github_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Call REST API, using file_get_contents
* and a stream_context for POST data or additional headers.
*
* Does not use CURL as libcurl has disabled https for PUT
* in many installations.
*/
function api($url, $post=FALSE, $headers=array()) {
$context_opt = array();
if (strpos($url, ' ') !== FALSE) {
list($verb, $url) = explode(' ', $url);
}
else {
$verb = 'GET';
}
//$scheme = parse_url($url, PHP_URL_SCHEME);
$scheme = 'http'; // This needs to be "http" even for "https"
$context_opt = array($scheme => array());
if ($post) {
$context_opt[$scheme] = array(
'method' => 'POST',
'content' => json_encode($post),
);
$headers[] = 'Content-Type: application/json';
}
if ($verb != 'GET') {
$context_opt[$scheme]['method'] = $verb;
}
$headers[] = 'Accept: application/json';
$headers[] = 'User-Agent: ' . config('github.app_name');
if(!empty($_SESSION['access_token'])) {
$headers[] = 'Authorization: Bearer ' . $_SESSION['access_token'];
}
$context_opt[$scheme] += array(
'header' => join('', array_map(function ($h) {
return "$h\r\n";
}, $headers))
);
$context = stream_context_create($context_opt);
return json_decode(file_get_contents($url, /*include_path*/FALSE, $context));
}
class Github {
function __construct($token, $username, $repo) {
$this->token = $token;
$this->username = $username;
$this->repo = $repo;
$this->url_base = config('github.api_url_base');
}
function mkdir($path) {
return $this->save($path . '/empty', '-- empty file --');
}
function save($path, $content, $sha = NULL) {
// Create _posts, following
// http://mdswanson.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
$params = array(
'path' => "{$path}",
'message' => "save {$path}",
'content' => base64_encode($content),
'branch' => 'gh-pages',
);
if ($sha) {
$params['sha'] = $sha;
}
$info = api(
'PUT ' . $this->url_base . "repos/{$this->username}/{$this->repo}/contents/{$path}",
$params,
array('Authorization: Bearer ' . $this->token)
);
}
function branches() {
return api(
$this->url_base . "repos/{$this->username}/{$this->repo}/branches",
/*post*/FALSE,
array('Authorization: Bearer ' . $this->token)
);
}
function createBranch($branch, $branch_from_sha) {
return api(
$this->url_base . "repos/{$this->username}/{$this->repo}/git/refs",
/*post*/array(
'ref' => 'refs/head/' . $branch,
'sha' => $branch_from_sha,
),
array('Authorization: Bearer ' . $this->token)
);
}
function trees() {
return api(
$this->url_base . "repos/{$this->username}/{$this->repo}/git/trees/gh-pages?recursive=1",
/*post*/FALSE,
array('Authorization: Bearer ' . $this->token)
);
}
}