1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DevBoardLib\GithubApiFacade\Repo; |
4
|
|
|
|
5
|
|
|
use DevBoardLib\GithubCore\Repo\GithubRepo; |
6
|
|
|
use Github\Client; |
7
|
|
|
use Github\ResultPager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class PaginatedKnpLabsRepoFacade. |
11
|
|
|
*/ |
12
|
|
|
class PaginatedKnpLabsRepoFacade implements RepoFacade |
13
|
|
|
{ |
14
|
|
|
private $client; |
15
|
|
|
private $githubRepo; |
16
|
|
|
|
17
|
|
|
private $paginator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PaginatedKnpLabsRepoFacade constructor. |
21
|
|
|
* |
22
|
|
|
* @param $client |
23
|
|
|
* @param $githubRepo |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Client $client, GithubRepo $githubRepo) |
26
|
|
|
{ |
27
|
|
|
$this->client = $client; |
28
|
|
|
$this->githubRepo = $githubRepo; |
29
|
|
|
|
30
|
|
|
$this->paginator = new ResultPager($this->client); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Fetches GithubRepo details. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function fetchDetails() |
39
|
|
|
{ |
40
|
|
|
return $this->getRepoApi() |
|
|
|
|
41
|
|
|
->show( |
42
|
|
|
$this->githubRepo->getOwner(), |
|
|
|
|
43
|
|
|
$this->githubRepo->getName() |
|
|
|
|
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Fetches GithubBranch details. |
49
|
|
|
* |
50
|
|
|
* @param $branchName |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function fetchBranch($branchName) |
55
|
|
|
{ |
56
|
|
|
return $this->getRepoApi() |
|
|
|
|
57
|
|
|
->branches( |
58
|
|
|
$this->githubRepo->getOwner(), |
59
|
|
|
$this->githubRepo->getName(), |
60
|
|
|
$branchName |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @return array */ |
65
|
|
|
public function fetchAllBranches() |
66
|
|
|
{ |
67
|
|
|
$results = []; |
68
|
|
|
|
69
|
|
|
foreach ($this->fetchAllBranchNames() as $branchData) { |
70
|
|
|
$results[] = $this->fetchBranch($branchData['name']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $results; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @return array */ |
77
|
|
View Code Duplication |
public function fetchAllBranchNames() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$parameters = [ |
80
|
|
|
$this->githubRepo->getOwner(), |
81
|
|
|
$this->githubRepo->getName(), |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
return $this->paginator->fetchAll($this->getRepoApi(), 'branches', $parameters); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @return array */ |
88
|
|
|
public function fetchAllTags() |
89
|
|
|
{ |
90
|
|
|
$results = []; |
91
|
|
|
|
92
|
|
|
foreach ($this->fetchAllTagNames() as $tagData) { |
93
|
|
|
$tagData['commit'] = $this->fetchCommit($tagData['commit']['sha']); |
94
|
|
|
$results[] = $tagData; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $results; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @return array */ |
101
|
|
View Code Duplication |
public function fetchAllTagNames() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$parameters = [ |
104
|
|
|
$this->githubRepo->getOwner(), |
105
|
|
|
$this->githubRepo->getName(), |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
return $this->paginator->fetchAll($this->getRepoApi(), 'tags', $parameters); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Fetches GithubCommit details. |
113
|
|
|
* |
114
|
|
|
* @param $commitSha |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function fetchCommit($commitSha) |
119
|
|
|
{ |
120
|
|
|
return $this->getRepoApi()->commits() |
|
|
|
|
121
|
|
|
->show( |
122
|
|
|
$this->githubRepo->getOwner(), |
123
|
|
|
$this->githubRepo->getName(), |
124
|
|
|
$commitSha |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Fetches GithubCommit status. |
130
|
|
|
* |
131
|
|
|
* @param $commitSha |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function fetchCommitStatus($commitSha) |
136
|
|
|
{ |
137
|
|
|
return $this->getRepoApi()->statuses() |
|
|
|
|
138
|
|
|
->combined( |
139
|
|
|
$this->githubRepo->getOwner(), |
140
|
|
|
$this->githubRepo->getName(), |
141
|
|
|
$commitSha |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fetches list of GithubCommit statuses. |
147
|
|
|
* |
148
|
|
|
* @param $commitSha |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function fetchCommitStatuses($commitSha) |
153
|
|
|
{ |
154
|
|
|
return $this->getRepoApi()->statuses() |
|
|
|
|
155
|
|
|
->show( |
156
|
|
|
$this->githubRepo->getOwner(), |
157
|
|
|
$this->githubRepo->getName(), |
158
|
|
|
$commitSha |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** @return array */ |
163
|
|
View Code Duplication |
public function fetchAllPullRequests() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$parameters = [ |
166
|
|
|
$this->githubRepo->getOwner(), |
167
|
|
|
$this->githubRepo->getName(), |
168
|
|
|
['state' => 'all'], |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
return $this->paginator->fetchAll($this->getPullRequestApi(), 'all', $parameters); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @return array */ |
175
|
|
View Code Duplication |
public function fetchAllMilestones() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$parameters = [ |
178
|
|
|
$this->githubRepo->getOwner(), |
179
|
|
|
$this->githubRepo->getName(), |
180
|
|
|
['state' => 'all'], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
return $this->paginator->fetchAll($this->getMilestonesApi(), 'all', $parameters); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns all issues. |
188
|
|
|
* |
189
|
|
|
* IMPORTANT: |
190
|
|
|
* - GitHub API v3 returns both issues and pullRequests together so filtering needs to be done |
191
|
|
|
* - Pull requests have 'pull_request' key. |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
public function fetchAllIssues() |
196
|
|
|
{ |
197
|
|
|
$results = []; |
198
|
|
|
|
199
|
|
|
foreach ($this->fetchAllIssuesAndPullRequests() as $result) { |
200
|
|
|
if (!array_key_exists('pull_request', $result)) { |
201
|
|
|
$results[] = $result; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $results; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** @return array */ |
209
|
|
View Code Duplication |
public function fetchAllIssuesAndPullRequests() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$parameters = [ |
212
|
|
|
$this->githubRepo->getOwner(), |
213
|
|
|
$this->githubRepo->getName(), |
214
|
|
|
['state' => 'all'], |
215
|
|
|
]; |
216
|
|
|
|
217
|
|
|
return $this->paginator->fetchAll($this->getIssueApi(), 'all', $parameters); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return \Github\Api\ApiInterface |
222
|
|
|
*/ |
223
|
|
|
private function getRepoApi() |
224
|
|
|
{ |
225
|
|
|
return $this->client->api('repository'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return \Github\Api\ApiInterface |
230
|
|
|
*/ |
231
|
|
|
private function getIssueApi() |
232
|
|
|
{ |
233
|
|
|
return $this->client->api('issues'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return \Github\Api\Issue\Milestones |
238
|
|
|
*/ |
239
|
|
|
private function getMilestonesApi() |
240
|
|
|
{ |
241
|
|
|
return $this->getIssueApi()->milestones(); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return \Github\Api\PullRequest |
246
|
|
|
*/ |
247
|
|
|
private function getPullRequestApi() |
248
|
|
|
{ |
249
|
|
|
return $this->client->api('pull_requests'); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for function calls that miss required arguments.