forked from backdrop-contrib/githubapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubapi.module
More file actions
246 lines (208 loc) · 6.99 KB
/
githubapi.module
File metadata and controls
246 lines (208 loc) · 6.99 KB
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
<?php
/**
* This module provides GitHub API integration.
*
* @author Gor Martsen <gor@me.com>
*/
// URL Github API.
define('GITHUB_API_URI', 'https://api.github.com');
// URL Github.
define('GITHUB_URI', 'https://github.com');
define('GITHUB_SCOPE', 'user:email,repo,admin:repo_hook,read:org,admin:org_hook');
define('GITHUB_HOOK_NAME', 'web');
define('GITHUB_PAYLOAD_PATH', 'githubapi/payload');
define('GITHUB_PAYLOAD_CONTENT_TYPE', 'json');
define('GITHUB_API_USER_AGENT', 'Backdrop CMS GitHub API module');
/**
* Implements hook_config_info().
*/
function githubapi_config_info() {
$prefixes['githubapi.settings'] = array(
'label' => t('github API settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_menu().
*/
function githubapi_menu() {
$items = array();
$items['admin/config/system/githubapi'] = array(
'title' => 'github API settings',
'page callback' => 'githubapi_repos',
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'githubapi.page.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/system/githubapi/list'] = array(
'title' => 'Repos list',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/githubapi/settings'] = array(
'title' => 'github API settings',
'page callback' => 'backdrop_get_form',
'page arguments' => array('githubapi_admin_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'description' => 'Github API configuration.',
'file' => 'githubapi.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/system/githubapi/%/delete'] = array(
'title' => 'Remove hook',
'page callback' => 'backdrop_get_form',
'page arguments' => array('githubapi_hook_remove', 4),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'description' => 'Github API configuration.',
'file' => 'githubapi.page.inc',
'type' => MENU_CALLBACK,
);
$items['githubapi/register'] = array(
'type' => MENU_CALLBACK,
'title' => 'Connect to Github',
'page callback' => 'githubapi_get_access_token',
'access callback' => TRUE,
'file' => 'githubapi.admin.inc',
);
$items['githubapi/payload'] = array(
'type' => MENU_CALLBACK,
'title' => 'Payload callback',
'page callback' => 'githubapi_payload_process',
'access callback' => TRUE,
);
return $items;
}
function gitlc_github_rid_load($rid){
return db_select('githubapi_repositories', 'gr')
->fields('gr')
->condition('id', $rid)
->execute()
->fetchAssoc();
}
/**
* Implements https://developer.github.com/webhooks/#payloads.
*/
function githubapi_payload_process() {
if (isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
if (empty($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
backdrop_add_http_header('Status', '403 Signature required');
exit('Signature required for this request.');
}
$event_name = $_SERVER['HTTP_X_GITHUB_EVENT'];
$received_json = file_get_contents('php://input', TRUE);
if (!$event = backdrop_json_decode($received_json)) {
backdrop_add_http_header('Status', '403 Empty or broken data received');
exit('Empty or broken data received.');
}
$repository_name = '';
if(isset($event['repository']) && isset($event['repository']['name'])){
$repository_name = $event['repository']['name'];
}
$owner_name = '';
if(isset($event['repository']['owner'])){
if(isset($event['repository']['owner']['login'])){
$owner_name = $event['repository']['owner']['login'];
}
if(isset($event['repository']['owner']['name'])){
$owner_name = $event['repository']['owner']['name'];
}
}
if($event_name == 'membership'){
if(isset($event['organization']['login'])){
$owner_name = $event['organization']['login'];
}
}
if($event_name == 'ping'){
if(isset($event['organization']['login'])){
$owner_name = $event['organization']['login'];
}
}
if(empty($owner_name)){
backdrop_add_http_header('Status', '403 Unsupported Event type.');
exit('We did not find owner information.');
}
if(empty($repository_name)){
$repo = db_select('githubapi_repositories', 'gr')
->fields('gr')
->condition('name', "*")
->condition('owner', $owner_name)
->execute()
->fetchAssoc();
$repo['name'] = '';
if(empty($repo)){
backdrop_add_http_header('Status', '403 We do not process this repo.');
exit('We did not find repo information.');
}
}else{
$repo = db_select('githubapi_repositories', 'gr')
->fields('gr')
->condition('name', $repository_name)
->condition('owner', $owner_name)
->execute()
->fetchAssoc();
if(empty($repo)){
$repo = db_select('githubapi_repositories', 'gr')
->fields('gr')
->condition('name', "*")
->condition('owner', $owner_name)
->execute()
->fetchAssoc();
$repo['name'] = $repository_name;
}
if(empty($repo)){
backdrop_add_http_header('Status', '403 We do not process this repo.');
exit('We did not find repo information.');
}
}
list($algorithm, $expected_hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2);
$actual_hash = hash_hmac($algorithm, $received_json, $repo['secret']);
if ($expected_hash !== $actual_hash) {
backdrop_add_http_header('Status', '403 Invalid signature');
exit('The provided secret key did not match the server key.');
}
if ($event_name) {
$sender = $event['sender'];
$repository = $event['repository'];
unset($event['repository']);
unset($event['sender']);
$record = array(
'type' => $event_name,
'sender' => $sender,
'rid' => $repo['id'],
'data' => $event,
'timestamp' => REQUEST_TIME,
);
backdrop_write_record('githubapi_payload', $record);
module_invoke_all('githubapi_payload', $event_name, $record, $repo);
backdrop_add_http_header('Status', '200 Success');
exit('Payload was processed.');
}
}else{
backdrop_add_http_header('Status', '403 Bad request');
exit('HTTP_X_GITHUB_EVENT has to be provided.');
}
}
function githubapi_get_class($repo){
module_load_include('inc', 'githubapi', 'githubapi_class');
$config = config('githubapi.settings');
$token = settings_get('githubapi_token');
if(empty($token)){
$token = $config->get('token');
}
$githubapi = new GitHubAPI();
$githubapi->setOwnerName($repo['owner']);
$githubapi->setRepoName($repo['name']);
$githubapi->setToken($token);
return $githubapi;
}
function githubapi_get_token(){
$config = config('githubapi.settings');
$token = settings_get('githubapi_token');
if(empty($token)){
$token = $config->get('token');
}
return $token;
}