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