1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework GitHub Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package\Repositories; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* GitHub API Repositories Contents class for the Joomla Framework. |
15
|
|
|
* |
16
|
|
|
* These API methods let you retrieve the contents of files within a repository as Base64 encoded content. |
17
|
|
|
* See media types for requesting raw or other formats. |
18
|
|
|
* |
19
|
|
|
* @documentation http://developer.github.com/v3/repos/contents |
20
|
|
|
* |
21
|
|
|
* @since 1.0 |
22
|
|
|
*/ |
23
|
|
|
class Contents extends AbstractPackage |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Get the README. |
27
|
|
|
* |
28
|
|
|
* This method returns the preferred README for a repository. |
29
|
|
|
* |
30
|
|
|
* @param string $owner The name of the owner of the GitHub repository. |
31
|
|
|
* @param string $repo The name of the GitHub repository. |
32
|
|
|
* @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
33
|
|
|
* |
34
|
|
|
* @since 1.0 |
35
|
|
|
* |
36
|
|
|
* @return object |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getReadme($owner, $repo, $ref = '') |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
// Build the request path. |
41
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/readme'; |
42
|
|
|
|
43
|
|
|
if ($ref) |
44
|
|
|
{ |
45
|
|
|
$path .= '?ref=' . $ref; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Send the request. |
49
|
|
|
return $this->processResponse( |
50
|
|
|
$this->client->get($this->fetchUrl($path)) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get contents. |
56
|
|
|
* |
57
|
|
|
* This method returns the contents of any file or directory in a repository. |
58
|
|
|
* |
59
|
|
|
* @param string $owner The name of the owner of the GitHub repository. |
60
|
|
|
* @param string $repo The name of the GitHub repository. |
61
|
|
|
* @param string $path The content path. |
62
|
|
|
* @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
63
|
|
|
* |
64
|
|
|
* @since 1.0 |
65
|
|
|
* |
66
|
|
|
* @return object |
67
|
|
|
*/ |
68
|
|
|
public function get($owner, $repo, $path, $ref = '') |
69
|
|
|
{ |
70
|
|
|
// Build the request path. |
71
|
|
|
$rPath = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; |
72
|
|
|
|
73
|
|
|
if ($ref) |
74
|
|
|
{ |
75
|
|
|
$rPath .= '?ref=' . $ref; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Send the request. |
79
|
|
|
return $this->processResponse( |
80
|
|
|
$this->client->get($this->fetchUrl($rPath)) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get archive link. |
86
|
|
|
* |
87
|
|
|
* This method will return a 302 to a URL to download a tarball or zipball archive for a repository. |
88
|
|
|
* Please make sure your HTTP framework is configured to follow redirects or you will need to use the |
89
|
|
|
* Location header to make a second GET request. |
90
|
|
|
* |
91
|
|
|
* Note: For private repositories, these links are temporary and expire quickly. |
92
|
|
|
* |
93
|
|
|
* To follow redirects with curl, use the -L switch: |
94
|
|
|
* curl -L https://api.github.com/repos/pengwynn/octokit/tarball > octokit.tar.gz |
95
|
|
|
* |
96
|
|
|
* @param string $owner The name of the owner of the GitHub repository. |
97
|
|
|
* @param string $repo The name of the GitHub repository. |
98
|
|
|
* @param string $archive_format Either tarball or zipball. |
99
|
|
|
* @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. |
100
|
|
|
* |
101
|
|
|
* @throws \UnexpectedValueException |
102
|
|
|
* @since 1.0 |
103
|
|
|
* |
104
|
|
|
* @return object |
105
|
|
|
*/ |
106
|
|
|
public function getArchiveLink($owner, $repo, $archive_format = 'zipball', $ref = '') |
107
|
|
|
{ |
108
|
|
|
if (false == in_array($archive_format, array('tarball', 'zipball'))) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
throw new \UnexpectedValueException('Archive format must be either "tarball" or "zipball".'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Build the request path. |
114
|
|
|
$path = '/repos/' . $owner . '/' . $repo . '/' . $archive_format; |
115
|
|
|
|
116
|
|
|
if ($ref) |
117
|
|
|
{ |
118
|
|
|
$path .= '?ref=' . $ref; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Send the request. |
122
|
|
|
return $this->processResponse( |
123
|
|
|
$this->client->get($this->fetchUrl($path)), |
124
|
|
|
302 |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create a file. |
130
|
|
|
* |
131
|
|
|
* This method creates a new file in a repository. |
132
|
|
|
* |
133
|
|
|
* Optional Parameters |
134
|
|
|
* The author section is optional and is filled in with the committer information if omitted. |
135
|
|
|
* If the committer information is omitted, the authenticated user’s information is used. |
136
|
|
|
* |
137
|
|
|
* You must provide values for both name and email, whether you choose to use author or committer. |
138
|
|
|
* Otherwise, you’ll receive a 500 status code. |
139
|
|
|
* |
140
|
|
|
* @param string $owner The owner of the repository. |
141
|
|
|
* @param string $repo The repository name. |
142
|
|
|
* @param string $path The content path. |
143
|
|
|
* @param string $message The commit message. |
144
|
|
|
* @param string $content The new file content, Base64 encoded. |
145
|
|
|
* @param string $branch The branch name. If not provided, uses the repository’s |
146
|
|
|
* default branch (usually master). |
147
|
|
|
* @param string $authorName The name of the author of the commit |
148
|
|
|
* @param string $authorEmail The email of the author of the commit |
149
|
|
|
* @param string $committerName The name of the committer of the commit |
150
|
|
|
* @param string $committerEmail The email of the committer of the commit |
151
|
|
|
* |
152
|
|
|
* @throws \UnexpectedValueException |
153
|
|
|
* |
154
|
|
|
* @return object |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function create($owner, $repo, $path, $message, $content, $branch = 'master', |
|
|
|
|
157
|
|
|
$authorName = '', $authorEmail = '', $committerName = '', $committerEmail = '') |
158
|
|
|
{ |
159
|
|
|
// Build the request path. |
160
|
|
|
$route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; |
161
|
|
|
|
162
|
|
|
$data = array( |
163
|
|
|
'message' => $message, |
164
|
|
|
'content' => $content, |
165
|
|
|
'branch' => $branch |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
if ($authorName) |
169
|
|
|
{ |
170
|
|
|
if (!$authorEmail) |
171
|
|
|
{ |
172
|
|
|
throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data['author'] = array( |
176
|
|
|
'name' => $authorName, |
177
|
|
|
'email' => $authorEmail |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($committerName) |
182
|
|
|
{ |
183
|
|
|
if (!$committerEmail) |
184
|
|
|
{ |
185
|
|
|
throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$data['committer'] = array( |
189
|
|
|
'name' => $committerName, |
190
|
|
|
'email' => $committerEmail |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data)), 201); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Update a file. |
199
|
|
|
* |
200
|
|
|
* This method updates a file in a repository. |
201
|
|
|
* |
202
|
|
|
* Optional Parameters |
203
|
|
|
* The author section is optional and is filled in with the committer information if omitted. |
204
|
|
|
* If the committer information is omitted, the authenticated user’s information is used. |
205
|
|
|
* |
206
|
|
|
* You must provide values for both name and email, whether you choose to use author or committer. |
207
|
|
|
* Otherwise, you’ll receive a 500 status code. |
208
|
|
|
* |
209
|
|
|
* @param string $owner The owner of the repository. |
210
|
|
|
* @param string $repo The repository name. |
211
|
|
|
* @param string $path The content path. |
212
|
|
|
* @param string $message The commit message. |
213
|
|
|
* @param string $content The new file content, Base64 encoded. |
214
|
|
|
* @param string $sha The blob SHA of the file being replaced. |
215
|
|
|
* @param string $branch The branch name. If not provided, uses the repository’s |
216
|
|
|
* default branch (usually master). |
217
|
|
|
* @param string $authorName The name of the author of the commit |
218
|
|
|
* @param string $authorEmail The email of the author of the commit |
219
|
|
|
* @param string $committerName The name of the committer of the commit |
220
|
|
|
* @param string $committerEmail The email of the committer of the commit |
221
|
|
|
* |
222
|
|
|
* @throws \UnexpectedValueException |
223
|
|
|
* |
224
|
|
|
* @return object |
225
|
|
|
*/ |
226
|
|
View Code Duplication |
public function update($owner, $repo, $path, $message, $content, $sha, $branch = 'master', |
|
|
|
|
227
|
|
|
$authorName = '', $authorEmail = '', $committerName = '', $committerEmail = '') |
228
|
|
|
{ |
229
|
|
|
// Build the request path. |
230
|
|
|
$route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; |
231
|
|
|
|
232
|
|
|
$data = array( |
233
|
|
|
'message' => $message, |
234
|
|
|
'content' => $content, |
235
|
|
|
'sha' => $sha, |
236
|
|
|
'branch' => $branch |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
if ($authorName) |
240
|
|
|
{ |
241
|
|
|
if (!$authorEmail) |
242
|
|
|
{ |
243
|
|
|
throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$data['author'] = array( |
247
|
|
|
'name' => $authorName, |
248
|
|
|
'email' => $authorEmail |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if ($committerName) |
253
|
|
|
{ |
254
|
|
|
if (!$committerEmail) |
255
|
|
|
{ |
256
|
|
|
throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$data['committer'] = array( |
260
|
|
|
'name' => $committerName, |
261
|
|
|
'email' => $committerEmail |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data))); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Delete a file. |
270
|
|
|
* |
271
|
|
|
* This method deletes a file in a repository. |
272
|
|
|
* |
273
|
|
|
* @param string $owner The owner of the repository. |
274
|
|
|
* @param string $repo The repository name. |
275
|
|
|
* @param string $path The content path. |
276
|
|
|
* @param string $message The commit message. |
277
|
|
|
* @param string $sha The blob SHA of the file being replaced. |
278
|
|
|
* @param string $branch The branch name. If not provided, uses the repository’s |
279
|
|
|
* default branch (usually master). |
280
|
|
|
* @param string $authorName The name of the author of the commit |
281
|
|
|
* @param string $authorEmail The email of the author of the commit |
282
|
|
|
* @param string $committerName The name of the committer of the commit |
283
|
|
|
* @param string $committerEmail The email of the committer of the commit |
284
|
|
|
* |
285
|
|
|
* @throws \UnexpectedValueException |
286
|
|
|
* |
287
|
|
|
* @return object |
288
|
|
|
*/ |
289
|
|
View Code Duplication |
public function delete($owner, $repo, $path, $message, $sha, $branch = 'master', |
|
|
|
|
290
|
|
|
$authorName = '', $authorEmail = '', $committerName = '', $committerEmail = '') |
291
|
|
|
{ |
292
|
|
|
// Build the request path. |
293
|
|
|
$route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; |
294
|
|
|
|
295
|
|
|
$data = array( |
296
|
|
|
'message' => $message, |
297
|
|
|
'sha' => $sha, |
298
|
|
|
'branch' => $branch |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
if ($authorName) |
302
|
|
|
{ |
303
|
|
|
if (!$authorEmail) |
304
|
|
|
{ |
305
|
|
|
throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$data['author'] = array( |
309
|
|
|
'name' => $authorName, |
310
|
|
|
'email' => $authorEmail |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
if ($committerName) |
315
|
|
|
{ |
316
|
|
|
if (!$committerEmail) |
317
|
|
|
{ |
318
|
|
|
throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$data['committer'] = array( |
322
|
|
|
'name' => $committerName, |
323
|
|
|
'email' => $committerEmail |
324
|
|
|
); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this->processResponse( |
328
|
|
|
$this->client->delete( |
329
|
|
|
$this->fetchUrl($route), |
330
|
|
|
array(), null, json_encode($data) |
331
|
|
|
) |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
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.