1
|
|
|
<?php |
2
|
|
|
namespace FlexyProject\GitHub\Receiver; |
3
|
|
|
|
4
|
|
|
use FlexyProject\GitHub\AbstractApi; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class give you access to the Repository API. |
9
|
|
|
* |
10
|
|
|
* @link https://developer.github.com/v3/repos/ |
11
|
|
|
* @package FlexyProject\GitHub\Receiver |
12
|
|
|
*/ |
13
|
|
|
class Repositories extends AbstractReceiver |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** Available sub-Receiver */ |
17
|
|
|
const COLLABORATORS = 'Collaborators'; |
18
|
|
|
const COMMENTS = 'Comments'; |
19
|
|
|
const COMMITS = 'Commits'; |
20
|
|
|
const CONTENTS = 'Contents'; |
21
|
|
|
const DEPLOY_KEYS = 'DeployKeys'; |
22
|
|
|
const DEPLOYMENTS = 'Deployments'; |
23
|
|
|
const FORKS = 'Forks'; |
24
|
|
|
const HOOKS = 'Hooks'; |
25
|
|
|
const MERGING = 'Merging'; |
26
|
|
|
const PAGES = 'Pages'; |
27
|
|
|
const RELEASES = 'Releases'; |
28
|
|
|
const STATISTICS = 'Statistics'; |
29
|
|
|
const STATUSES = 'Statuses'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* List repositories for the authenticated user. |
33
|
|
|
* |
34
|
|
|
* @link https://developer.github.com/v3/repos/#list-your-repositories |
35
|
|
|
* |
36
|
|
|
* @param string $type |
37
|
|
|
* @param string $sort |
38
|
|
|
* @param string $direction |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function listYourRepositories(string $type = AbstractApi::TYPE_ALL, |
|
|
|
|
43
|
|
|
string $sort = AbstractApi::SORT_FULL_NAME, |
44
|
|
|
string $direction = AbstractApi::DIRECTION_DESC): array |
45
|
|
|
{ |
46
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/user/repos?:args', |
47
|
|
|
http_build_query(['type' => $type, 'sort' => $sort, 'direction' => $direction]))); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* List public repositories for the specified user. |
52
|
|
|
* |
53
|
|
|
* @link https://developer.github.com/v3/repos/#list-user-repositories |
54
|
|
|
* |
55
|
|
|
* @param string $username |
56
|
|
|
* @param string $type |
57
|
|
|
* @param string $sort |
58
|
|
|
* @param string $direction |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function listUserRepositories(string $username, string $type = AbstractApi::TYPE_OWNER, |
|
|
|
|
63
|
|
|
string $sort = AbstractApi::SORT_FULL_NAME, |
64
|
|
|
string $direction = AbstractApi::DIRECTION_DESC): array |
65
|
|
|
{ |
66
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/users/:username/repos?:args', $username, |
67
|
|
|
http_build_query(['type' => $type, 'sort' => $sort, 'direction' => $direction]))); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* List repositories for the specified org. |
72
|
|
|
* |
73
|
|
|
* @link https://developer.github.com/v3/repos/#list-organization-repositories |
74
|
|
|
* |
75
|
|
|
* @param string $organization |
76
|
|
|
* @param string $type |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function listOrganizationRepositories(string $organization, string $type = AbstractApi::TYPE_ALL): array |
81
|
|
|
{ |
82
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/repos?:args', $organization, |
83
|
|
|
http_build_query(['type' => $type]))); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* List all public repositories |
88
|
|
|
* |
89
|
|
|
* @link https://developer.github.com/v3/repos/#list-all-public-repositories |
90
|
|
|
* |
91
|
|
|
* @param string $since |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function listPublicRepositories(string $since = '1970-01-01'): array |
96
|
|
|
{ |
97
|
|
|
return $this->getApi()->request($this->getApi() |
98
|
|
|
->sprintf('/repositories?:arg', http_build_query(['since', $since]))); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create a new repository for the authenticated user. |
103
|
|
|
* |
104
|
|
|
* @link https://developer.github.com/v3/repos/#create |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @param string $description |
108
|
|
|
* @param string $homepage |
109
|
|
|
* @param bool $private |
110
|
|
|
* @param bool $hasIssues |
111
|
|
|
* @param bool $hasWiki |
112
|
|
|
* @param bool $hasDownloads |
113
|
|
|
* @param int $teamId |
114
|
|
|
* @param bool $autoInit |
115
|
|
|
* @param string $gitignoreTemplate |
116
|
|
|
* @param string $licenseTemplate |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function createRepository(string $name, string $description = '', string $homepage = '', |
|
|
|
|
121
|
|
|
bool $private = false, bool $hasIssues = true, bool $hasWiki = true, |
122
|
|
|
bool $hasDownloads = true, int $teamId = 0, bool $autoInit = false, |
123
|
|
|
string $gitignoreTemplate = '', string $licenseTemplate = ''): array |
124
|
|
|
{ |
125
|
|
|
return $this->getApi()->request(sprintf('/user/repos'), Request::METHOD_POST, [ |
126
|
|
|
'name' => $name, |
127
|
|
|
'description' => $description, |
128
|
|
|
'homepage' => $homepage, |
129
|
|
|
'private' => $private, |
130
|
|
|
'has_issues' => $hasIssues, |
131
|
|
|
'has_wiki' => $hasWiki, |
132
|
|
|
'has_downloads' => $hasDownloads, |
133
|
|
|
'team_id' => $teamId, |
134
|
|
|
'auto_init' => $autoInit, |
135
|
|
|
'gitignore_template' => $gitignoreTemplate, |
136
|
|
|
'license_template' => $licenseTemplate |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Create a new repository in this organization. The authenticated user must be a member of the specified |
142
|
|
|
* organization. |
143
|
|
|
* |
144
|
|
|
* @link https://developer.github.com/v3/repos/#create |
145
|
|
|
* |
146
|
|
|
* @param string $organization |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string $description |
149
|
|
|
* @param string $homepage |
150
|
|
|
* @param bool $private |
151
|
|
|
* @param bool $hasIssues |
152
|
|
|
* @param bool $hasWiki |
153
|
|
|
* @param bool $hasDownloads |
154
|
|
|
* @param int $teamId |
155
|
|
|
* @param bool $autoInit |
156
|
|
|
* @param string $gitignoreTemplate |
157
|
|
|
* @param string $licenseTemplate |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function createOrganizationRepository(string $organization, string $name, string $description = '', |
|
|
|
|
162
|
|
|
string $homepage = '', bool $private = false, bool $hasIssues = true, |
163
|
|
|
bool $hasWiki = true, bool $hasDownloads = true, int $teamId = 0, |
164
|
|
|
bool $autoInit = false, string $gitignoreTemplate = '', |
165
|
|
|
string $licenseTemplate = ''): array |
166
|
|
|
{ |
167
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/repos', $organization), |
168
|
|
|
Request::METHOD_POST, [ |
169
|
|
|
'name' => $name, |
170
|
|
|
'description' => $description, |
171
|
|
|
'homepage' => $homepage, |
172
|
|
|
'private' => $private, |
173
|
|
|
'has_issues' => $hasIssues, |
174
|
|
|
'has_wiki' => $hasWiki, |
175
|
|
|
'has_downloads' => $hasDownloads, |
176
|
|
|
'team_id' => $teamId, |
177
|
|
|
'auto_init' => $autoInit, |
178
|
|
|
'gitignore_template' => $gitignoreTemplate, |
179
|
|
|
'license_template' => $licenseTemplate |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get |
185
|
|
|
* |
186
|
|
|
* @link https://developer.github.com/v3/repos/#get |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function get(): array |
190
|
|
|
{ |
191
|
|
|
return $this->getApi()->request($this->getApi() |
192
|
|
|
->sprintf('/repos/:owner/:repo', $this->getOwner(), $this->getRepo())); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Edit |
197
|
|
|
* |
198
|
|
|
* @link https://developer.github.com/v3/repos/#edit |
199
|
|
|
* |
200
|
|
|
* @param string $name |
201
|
|
|
* @param string $description |
202
|
|
|
* @param string $homepage |
203
|
|
|
* @param bool $private |
204
|
|
|
* @param bool $hasIssues |
205
|
|
|
* @param bool $hasWiki |
206
|
|
|
* @param bool $hasDownloads |
207
|
|
|
* @param string $defaultBranch |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function edit(string $name, string $description = '', string $homepage = '', bool $private = false, |
212
|
|
|
bool $hasIssues = true, bool $hasWiki = true, bool $hasDownloads = true, |
213
|
|
|
string $defaultBranch = ''): array |
214
|
|
|
{ |
215
|
|
|
return $this->getApi()->request($this->getApi() |
216
|
|
|
->sprintf('/repos/:owner/:repo', $this->getOwner(), $this->getRepo()), |
217
|
|
|
Request::METHOD_PATCH, [ |
218
|
|
|
'name' => $name, |
219
|
|
|
'description' => $description, |
220
|
|
|
'homepage' => $homepage, |
221
|
|
|
'private' => $private, |
222
|
|
|
'has_issues' => $hasIssues, |
223
|
|
|
'has_wiki' => $hasWiki, |
224
|
|
|
'has_downloads' => $hasDownloads, |
225
|
|
|
'default_branch' => $defaultBranch |
226
|
|
|
]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* List contributors |
231
|
|
|
* |
232
|
|
|
* @link https://developer.github.com/v3/repos/#list-contributors |
233
|
|
|
* |
234
|
|
|
* @param string $anon |
235
|
|
|
* |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
public function listContributors(string $anon = '0'): array |
239
|
|
|
{ |
240
|
|
|
return $this->getApi()->request($this->getApi() |
241
|
|
|
->sprintf('/repos/:owner/:repo/contributors?:args', $this->getOwner(), |
242
|
|
|
$this->getRepo(), http_build_query(['anon' => $anon]))); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* List languages |
247
|
|
|
* |
248
|
|
|
* @link https://developer.github.com/v3/repos/#list-languages |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function listLanguages(): array |
252
|
|
|
{ |
253
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/languages', $this->getOwner(), |
254
|
|
|
$this->getRepo())); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* List Teams |
259
|
|
|
* |
260
|
|
|
* @link https://developer.github.com/v3/repos/#list-teams |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function listTeams(): array |
264
|
|
|
{ |
265
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/teams', $this->getOwner(), |
266
|
|
|
$this->getRepo())); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* List Tags |
271
|
|
|
* |
272
|
|
|
* @link https://developer.github.com/v3/repos/#list-tags |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
public function listTags(): array |
276
|
|
|
{ |
277
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/tags', $this->getOwner(), |
278
|
|
|
$this->getRepo())); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* List Branches |
283
|
|
|
* |
284
|
|
|
* @link https://developer.github.com/v3/repos/#list-branches |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
public function listBranches(): array |
288
|
|
|
{ |
289
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/branches', $this->getOwner(), |
290
|
|
|
$this->getRepo())); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get Branch |
295
|
|
|
* |
296
|
|
|
* @link https://developer.github.com/v3/repos/#get-branch |
297
|
|
|
* |
298
|
|
|
* @param string $branch |
299
|
|
|
* |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
public function getBranch(string $branch): array |
303
|
|
|
{ |
304
|
|
|
return $this->getApi()->request($this->getApi() |
305
|
|
|
->sprintf('/repos/:owner/:repo/branches/:branch', $this->getOwner(), |
306
|
|
|
$this->getRepo(), $branch)); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Delete a Repository |
311
|
|
|
* |
312
|
|
|
* @link https://developer.github.com/v3/repos/#delete-a-repository |
313
|
|
|
* @return array |
314
|
|
|
*/ |
315
|
|
|
public function deleteRepository(): array |
316
|
|
|
{ |
317
|
|
|
return $this->getApi()->request($this->getApi() |
318
|
|
|
->sprintf('/repos/:owner/:repo', $this->getOwner(), $this->getRepo()), |
319
|
|
|
Request::METHOD_DELETE); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get the contents of a repository's license |
324
|
|
|
* |
325
|
|
|
* @link https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
|
|
public function getRepositoryLicenseContent(): array |
329
|
|
|
{ |
330
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/license', $this->getOwner(), |
331
|
|
|
$this->getRepo())); |
332
|
|
|
} |
333
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.