-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgit.php
327 lines (264 loc) · 8.29 KB
/
git.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
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
class git {
private $_course;
private $_assignment;
private $_user;
private $_group;
private $_table = 'assignment_github_repos';
private $_server = array();
private $_api = array();
private $_service_list = array();
/**
* @param integer $course
* @param integer $assignment
* @param integer $user
* @param integer $group
*/
public function __construct($course, $assignment, $user = 0, $group = 0) {
global $CFG, $ASSIGNMENT_GITHUB;
$this->_course = $course;
$this->_assignment = $assignment;
$this->_user = $user;
$this->_group = $group;
foreach($ASSIGNMENT_GITHUB->server as $server => $cfg) {
$this->_server[$cfg['domain']] = $server;
$this->_api[$server] = $cfg['service'];
}
}
/**
* Parse the git repository service site accoding to the url
*
* @param string $url is a git repository url
* @return string if we support the site, else null
*/
public function parse_git_server($url) {
if (!$url) {
return null;
}
// cut protocol: http, git, ssh ...
$url_no_protocal = preg_replace('/https?:\/\/([^@]+@)?|https?:\/\/|git@|git:\/\/|ssh:\/\//i', '', $url);
$param_list = preg_split('/[\/:]/', $url_no_protocal);
$site = $param_list[0];
foreach($this->_server as $domain => $server) {
if (preg_match('/'.$domain.'$/i', $site)) {
return $server;
}
}
return null;
}
/**
* Get the api object
*
* @param string $server
* @return object if we have the api class, else null
*/
public function get_api_service($server) {
if (!array_key_exists($server, $this->_api)) {
return null;
}
if (array_key_exists($server, $this->_service_list)) {
return $this->_service_list[$server];
}
$class_name = $this->_api[$server];
include_once(ASSIGNMENT_GITHUB_MODULES . $class_name . '.php');
$this->_service_list[$server] = new $class_name();
return $this->_service_list[$server];
}
public function get_api_service_by_url($url) {
$server = $this->parse_git_server($url);
return $this->get_api_service($server);
}
/**
* Get user's repository in database
*
* @param integer $userid
* @return object
*/
public function get_by_user($userid) {
$conditions = array(
'course' => $this->_course,
'assignment' => $this->_assignment,
'userid' => intval($userid),
'groupid' => 0,
);
$result = $this->get_records($conditions);
if (!$result || !is_array($result)) {
return false;
}
return array_pop($result);
}
/**
* Get group's repository in database
*
* @param integer $groupid
* @return object
*/
public function get_by_group($groupid) {
$conditions = array(
'course' => $this->_course,
'assignment' => $this->_assignment,
'userid' => 0,
'groupid' => intval($groupid),
);
$result = $this->get_records($conditions);
if (!$result || !is_array($result)) {
return false;
}
return array_pop($result);
}
/**
* List all repos of an assignment
*
* @param integer $mode is group mode
* @return array|false
*/
public function list_all($mode) {
$conditions = array(
'course' => $this->_course,
'assignment' => $this->_assignment,
);
if ($mode == SEPARATEGROUPS || $mode == VISIBLEGROUPS) {
$conditions['userid'] = 0;
$key = 'groupid';
} else if ($mode == NOGROUPS) {
$conditions['groupid'] = 0;
$key = 'userid';
} else {
throw new Exception(get_string('unknowntype', 'assignment_github'));
return false;
}
$result = $this->get_records($conditions);
if (!$result || !is_array($result)) {
return false;
}
$repos = array();
foreach($result as $v) {
$repos[$v->$key] = $v;
}
return $repos;
}
/**
* Get git repository records from database
*
* @param array $conditions
* @param string $sort
* @param array $fields
* @param integer $limitfrom
* @param integer $limitnum
* @return array
*/
private function get_records($conditions, $sort='', $fields=array(), $limitfrom=0, $limitnum=0) {
global $DB;
if(!$fields) {
$fields = '*';
} else {
$fields = implode($fileds, ',');
}
try {
return $DB->get_records($this->_table, $conditions, $sort, $fields, $limitfrom, $limitnum);
} catch(Exception $e) {
return false;
}
}
private function fetch_repo_info($url) {
$server = $this->parse_git_server($url);
if (!$server) {
throw new Exception(get_string('unknownserver', 'assignment_github'));
return false;
}
$service = $this->get_api_service($server);
if (!$service) {
throw new Exception(get_string('serviceerror', 'assignment_github'));
return false;
}
try {
$repo = $service->get_repo_info($url);
if (!$repo) {
throw new Exception(get_string('reponotfind', 'assignment_github'));
return false;
}
} catch (Exception $e) {
throw new Exception(get_string('serviceerror', 'assignment_github'));
}
$repo['server'] = $server;
return $repo;
}
public function add_repo($url, $type) {
try {
$repo = $this->fetch_repo_info($url);
} catch(Exception $e) {
throw new Exception($e->getMessage());
return false;
}
$data = array(
'repo' => $repo['name'],
'server' => $repo['server'],
'url' => $url,
'is_public' => $repo['public'],
);
return $this->add_record($data, $type);
}
private function add_record($data, $type) {
global $DB;
if (is_array($data)) {
$data = (object)$data;
} else if (!is_object($data)) {
return false;
}
$data->course = $this->_course;
$data->assignment = $this->_assignment;
if ($type == NOGROUPS) {
$data->userid = $this->_user;
$data->groupid = 0;
} else if ($type == SEPARATEGROUPS || $type == VISIBLEGROUPS) {
$data->userid = 0;
$data->groupid = $this->_group;
} else {
throw new Exception(get_string('unknowntype', 'assignment_github'));
return false;
}
$data->created = time();
$data->created_user = $this->_user;
try {
return $DB->insert_record($this->_table, $data);
} catch(Exception $e) {
throw new Exception(get_string('serviceerror', 'assignment_github'));
return false;
}
}
public function update_repo($id, $url) {
try {
$repo = $this->fetch_repo_info($url);
} catch(Exception $e) {
throw new Exception($e->getMessage());
return false;
}
$data = array(
'id' => $id,
'repo' => $repo['name'],
'server' => $repo['server'],
'url' => $url,
'is_public' => $repo['public'],
);
return $this->update_record($data);
}
private function update_record($data) {
global $DB;
if (is_array($data)) {
$data = (object)$data;
} else if (!is_object($data)) {
return false;
}
if (!$data->id) {
return false;
}
$data->updated = time();
$data->updated_user = $this->_user;
try {
return $DB->update_record($this->_table, $data);
} catch(Exception $e) {
throw new Exception(get_string('serviceerror', 'assignment_github'));
return false;
}
}
}