-
Notifications
You must be signed in to change notification settings - Fork 8
/
github_api.module
327 lines (281 loc) · 8.76 KB
/
github_api.module
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* @file
* GitHub API module functions.
*/
/**
* Implements hook_menu().
*/
function github_api_menu() {
$items = array();
$items['admin/config/development/github_api'] = array(
'title' => 'GitHub API',
'description' => 'Configure the GitHub API.',
'page callback' => 'drupal_get_form',
'page arguments' => array('github_api_settings'),
'access arguments' => array('administer github api'),
'file' => 'includes/github_api.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function github_api_permission() {
return array(
'administer github api' => array(
'title' => t('Administer GitHub API'),
'description' => t('Configure the GitHub API module settings.'),
),
);
}
/**
* Implements hook_flush_caches().
*/
function github_api_flush_caches() {
return array('cache_github_api');
}
/**
* Instantiates a GitHub client object.
*
* @param bool $auth
* If TRUE the client will use authenticated calls.
*
* @return GithubClient
* The instantiated object.
*/
function github_api_client($auth = TRUE) {
composer_manager_register_autoloader();
$cache_obj = NULL;
if (variable_get('github_api_use_cache', TRUE)) {
$cache_obj = new \Github\HttpClient\CachedHttpClient();
$cache_obj->setCache(new DrupalGithubApiHttpClientCacheDrupalNativeCache());
}
$client = new Github\Client($cache_obj);
if ($auth) {
$token = variable_get('github_api_token');
$user = variable_get('github_api_username');
if ($token) {
$client->authenticate($user, $token, GitHub\Client::AUTH_HTTP_PASSWORD);
}
else {
$client->authenticate($user, variable_get('github_api_password'), GitHub\Client::AUTH_HTTP_TOKEN);
}
}
return $client;
}
/**
* Request a GitHub API oAuth token.
*
* @param string $username
* The GitHub username to use for the request.
* @param string $password
* The password for the GitHub user.
* @param string $note
* A note to annotate the token in GitHub.
*
* @return string
* The oAuth token.
*/
function github_api_get_token($username, $password, $note = '') {
composer_manager_register_autoloader();
$client = new Github\Client();
$client->authenticate($username, $password, GitHub\Client::AUTH_HTTP_PASSWORD);
if (!$note) {
$note = variable_get('site_name');
}
$params = array(
'note' => $note,
'note_url' => $GLOBALS['base_url'],
'scopes' => array('user', 'repo'),
);
$response = $client->api('authorizations')->create($params);
if (!empty($response['token'])) {
return $response['token'];
}
}
/**
* Fetches a diff between 2 commits or branches.
*
* @param string $username
* The GitHub username of the repo owner.
* @param string $repository
* The name of the repository.
* @param string $base
* The name or hash to use as the base for the comparison.
* @param string $head
* The name of the head.
*
* @return array
*
* @throws Exception
*/
function github_api_diff($username, $repository, $base, $head) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}
$client = github_api_client();
$response = $client->api('repo')->commits()->compare($username, $repository, $base, $head, 'application/vnd.github.v3.diff');
return $response;
}
/**
* Function to create a branch from a given head.
*
* @param string $username
* The GitHub username of the repo owner.
* @param string $repository
* The name of the repository.
* @param string $source
* The name of the head that the branch will be created from.
* @param string $destination
* The name of the new branch head.
*
* @return array
*
* @throws Exception
*/
function github_api_create_branch($username = NULL, $repository = NULL, $source = NULL, $destination = NULL) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}
$client = github_api_client();
try {
$client->api('current_user')->show();
$references = $client->api('git')->references();
$branch_head = $references->show($username, $repository, 'heads/' . $source);
if (!($references instanceof Github\Api\GitData\References)) {
throw new Exception('Create action failed.');
}
$params = array(
'ref' => 'refs/heads/' . $destination,
'sha' => $branch_head['object']['sha'],
);
return $references->create($username, $repository, $params);
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}
/**
* Function to merge a head into a branch from a given repository.
*
* @param string $username
* The GitHub username of the repo owner.
* @param string $repository
* The name of the repository.
* @param string $source
* The head to merge. This can be a branch name or a commit SHA1.
* @param string $destination
* The name of the base branch that the head will be merged into.
* @param string $message
* Commit message to use for the merge commit. If omitted, a default message
* will be used.
*
* @return array
*
* @throws Exception
*/
function github_api_merge($username = NULL, $repository = NULL, $source = NULL, $destination = NULL, $message = NULL) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}
$client = github_api_client();
try {
$client->api('current_user')->show();
$repo_object = $client->api('repo');
$references = $client->api('git')->references();
$branch_head = $references->show($username, $repository, 'heads/' . $source);
if (!($references instanceof Github\Api\GitData\References) || !($repo_object instanceof Github\Api\Repo)) {
throw new Exception('Merge action failed.');
}
return $repo_object->merge($username, $repository, $destination, $source, $message);
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}
/**
* Function to merge a head into a branch from a given repository.
*
* @param string $username
* The GitHub username of the repo owner.
* @param string $repository
* The name of the repository.
* @param string $source
* The base head to merge or create a new branch. This can be a branch name or
* a commit SHA1.
* @param string $destination
* The name of the new branch or the branch that will be merged into.
* @param string $message
* Commit message to use for the merge commit. If omitted, a default message
* will be used.
*
* @return array
*
* @throws Exception
*/
function github_api_create_or_merge_branch($username = NULL, $repository = NULL, $source = NULL, $destination = NULL, $message = NULL) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}
$client = github_api_client();
try {
$client->api('current_user')->show();
$references = $client->api('git')->references();
if (!($references instanceof Github\Api\GitData\References)) {
throw new Exception('Create or merge action failed.');
}
// Throws not found exception
$branch_source = $references->show($username, $repository, 'heads/' . $source);
try {
$branch_dest = $references->show($username, $repository, 'heads/' . $destination);
}
catch (Exception $exception) {
$params = array(
'ref' => 'refs/heads/' . $destination,
'sha' => $branch_source['object']['sha'],
);
return $references->create($username, $repository, $params);
}
// Finally
$repo_object = $client->api('repo');
if (!($repo_object instanceof Github\Api\Repo)) {
throw new Exception('Merge action failed.');
}
return $repo_object->merge($username, $repository, $destination, $source, $message);
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}
/**
* Rule action to delete a branch.
*
* @param string $username
* The GitHub username of the repo owner.
*
* @param string $repository
* The name of the repository.
*
* @param string $source_branch
* The name of the branch to be deleted.
*
* @throws Exception
*/
function github_api_delete_branch($username = NULL, $repository = NULL, $source_branch = NULL) {
if (!$username) {
$username = variable_get('github_api_default_owner');
}
$client = github_api_client();
try {
$client->api('current_user')->show();
$references = $client->api('git')->references();
if (!($references instanceof Github\Api\GitData\References)) {
throw new Exception('Create action failed.');
}
return $references->remove($username, $repository, 'heads/' . $source_branch);
}
catch (Exception $exception) {
watchdog('github_api', '@exception', array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
}