1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hubph; |
4
|
|
|
|
5
|
|
|
use Consolidation\Config\ConfigInterface; |
6
|
|
|
use Hubph\Internal\EventLogger; |
7
|
|
|
|
8
|
|
|
class HubphAPI |
9
|
|
|
{ |
10
|
|
|
protected $config; |
11
|
|
|
protected $token; |
12
|
|
|
protected $gitHubAPI; |
13
|
|
|
protected $eventLogger; |
14
|
|
|
protected $as = 'default'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* HubphAPI constructor |
18
|
|
|
*/ |
19
|
|
|
public function __construct(ConfigInterface $config) |
20
|
|
|
{ |
21
|
|
|
$this->config = $config; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function startLogging($filename) |
25
|
|
|
{ |
26
|
|
|
$this->stopLogging(); |
27
|
|
|
$this->eventLogger = new EventLogger($filename); |
28
|
|
|
$this->eventLogger->start(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function stopLogging() |
32
|
|
|
{ |
33
|
|
|
if ($this->eventLogger) { |
34
|
|
|
$this->eventLogger->stop(); |
35
|
|
|
} |
36
|
|
|
$this->eventLogger = null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setAs($as) |
40
|
|
|
{ |
41
|
|
|
if ($as != $this->as) { |
42
|
|
|
$this->as = $as; |
43
|
|
|
$this->token = false; |
44
|
|
|
$this->gitHubAPI = false; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function whoami() |
49
|
|
|
{ |
50
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
51
|
|
|
$authenticated = $gitHubAPI->api('current_user')->show(); |
52
|
|
|
return $authenticated; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function prCreate($org, $project, $title, $body, $base, $head) |
56
|
|
|
{ |
57
|
|
|
$params = [ |
58
|
|
|
'title' => $title, |
59
|
|
|
'body' => $body, |
60
|
|
|
'base' => $base, |
61
|
|
|
'head' => $head, |
62
|
|
|
]; |
63
|
|
|
$response = $this->gitHubAPI()->api('pull_request')->create($org, $project, $params); |
64
|
|
|
$this->logEvent(__FUNCTION__, [$org, $project], $params, $response); |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function prClose($org, $project, PullRequests $prs) |
69
|
|
|
{ |
70
|
|
|
foreach ($prs->prNumbers() as $n) { |
71
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
72
|
|
|
$gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function prMerge($org, $project, PullRequests $prs, $message, $mergeMethod = 'squash', $title = null) |
77
|
|
|
{ |
78
|
|
|
// First, check to see if all of the pull requests can be merged, |
79
|
|
|
// and collect the sha hash of the head of the branch. |
80
|
|
|
$allClean = true; |
|
|
|
|
81
|
|
|
$shas = []; |
82
|
|
|
foreach ($prs->prNumbers() as $n) { |
83
|
|
|
$pullRequest = $this->gitHubAPI()->api('pull_request')->show($org, $project, $n); |
84
|
|
|
$is_clean = $pullRequest['mergeable'] && $pullRequest['mergeable_state'] == 'clean'; |
85
|
|
|
if (!$is_clean) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
$shas[$pullRequest['id']] = $pullRequest['head']['sha']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Merge all of the pull requests |
92
|
|
|
foreach ($shas as $id => $sha) { |
93
|
|
|
$response = $this->gitHubAPI()->api('pull_request')->merge($org, $project, $id, $message, $sha, $mergeMethod, $title); |
94
|
|
|
$this->logEvent(__FUNCTION__, [$org, $project], [$id, $message, $sha, $mergeMethod, $title], $response); |
95
|
|
|
} |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* prCheck determines whether there are any open PRs that already exist |
101
|
|
|
* that satisfy any of the provided $vids. |
102
|
|
|
* |
103
|
|
|
* @param string $projectWithOrg org/project to check |
104
|
|
|
* @param VersionIdentifiers $vids |
105
|
|
|
* @return [int $status, PullRequests $prs] status of PRs, and a list of PR numbers |
|
|
|
|
106
|
|
|
* - If $status is 0, then the caller should go ahead and create a new PR. |
107
|
|
|
* The existing pull requests that would be superceded by the new PR are |
108
|
|
|
* returned in the second parameter. These PRs could all be closed. |
109
|
|
|
* - If $status is >0, then there is no need to create a new PR, as there |
110
|
|
|
* are already existing PRs that are equivalent to the one that would |
111
|
|
|
* be open. The equivalent PRs are returned in the second parameter. |
112
|
|
|
*/ |
113
|
|
|
public function prCheck($projectWithOrg, VersionIdentifiers $vids) |
114
|
|
|
{ |
115
|
|
|
// Find all of the PRs that contain any vid |
116
|
|
|
$existingPRs = $this->existingPRs($projectWithOrg, $vids); |
117
|
|
|
|
118
|
|
|
// Check to see if there are PRs matching all of the vids/vvals. |
119
|
|
|
$titles = $existingPRs->titles(); |
120
|
|
|
$status = $vids->allExist($titles); |
121
|
|
|
|
122
|
|
|
return [$status, $existingPRs]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function addTokenAuthentication($url) |
126
|
|
|
{ |
127
|
|
|
$token = $this->gitHubToken(); |
128
|
|
|
if (!$token) { |
129
|
|
|
return $url; |
130
|
|
|
} |
131
|
|
|
$projectAndOrg = $this->projectAndOrgFromUrl($url); |
132
|
|
|
return "https://{$token}:[email protected]/{$projectAndOrg}.git"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
protected function projectAndOrgFromUrl($remote) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$remote = preg_replace('#^git@[^:]*:#', '', $remote); |
138
|
|
|
$remote = preg_replace('#^[^:]*://[^/]*/#', '', $remote); |
139
|
|
|
$remote = preg_replace('#\.git$#', '', $remote); |
140
|
|
|
|
141
|
|
|
return $remote; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function existingPRs($projectWithOrg, VersionIdentifiers $vids) |
145
|
|
|
{ |
146
|
|
|
$preamble = $vids->getPreamble(); |
147
|
|
|
$q = "repo:$projectWithOrg in:title is:pr state:open $preamble"; |
148
|
|
|
$result = new PullRequests(); |
149
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
150
|
|
|
$searchResults = $gitHubAPI->api('search')->issues($q); |
151
|
|
|
$result->addSearchResults($searchResults, $vids->pattern()); |
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function allPRs($projectWithOrg) |
157
|
|
|
{ |
158
|
|
|
$q = "repo:$projectWithOrg in:title is:pr state:open"; |
159
|
|
|
$result = new PullRequests(); |
160
|
|
|
$searchResults = $this->gitHubAPI()->api('search')->issues($q); |
161
|
|
|
$result->addSearchResults($searchResults); |
162
|
|
|
|
163
|
|
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Pass an event of note to the event logger |
168
|
|
|
* @param string $event_name |
169
|
|
|
* @param array $args |
170
|
|
|
* @param array $params |
171
|
|
|
* @param array $response |
172
|
|
|
*/ |
173
|
|
|
protected function logEvent($event_name, $args, $params, $response) |
174
|
|
|
{ |
175
|
|
|
if ($this->eventLogger) { |
176
|
|
|
$this->eventLogger->log($event_name, $args, $params, $response); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Authenticate and then return the gitHub API object. |
182
|
|
|
*/ |
183
|
|
|
public function gitHubAPI() |
184
|
|
|
{ |
185
|
|
|
if (!$this->gitHubAPI) { |
186
|
|
|
$token = $this->gitHubToken(); |
187
|
|
|
|
188
|
|
|
$this->gitHubAPI = new \Github\Client(); |
189
|
|
|
$this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN); |
190
|
|
|
} |
191
|
|
|
return $this->gitHubAPI; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Look up the GitHub token set either via environment variable or in the |
196
|
|
|
* auth-token cache directory. |
197
|
|
|
*/ |
198
|
|
|
public function gitHubToken() |
199
|
|
|
{ |
200
|
|
|
if (!$this->token) { |
201
|
|
|
$this->token = $this->getGitHubToken(); |
202
|
|
|
} |
203
|
|
|
return $this->token; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function getGitHubToken() |
207
|
|
|
{ |
208
|
|
|
$as = $this->as; |
209
|
|
|
$token = null; |
210
|
|
|
if ($as == 'default') { |
211
|
|
|
$as = $this->getConfig()->get("github.default-user"); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// First preference: There is a 'path' component in preferences |
215
|
|
|
// pointing to a file containing the token. |
216
|
|
|
$github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path"); |
217
|
|
|
if (file_exists($github_token_cache)) { |
218
|
|
|
$token = trim(file_get_contents($github_token_cache)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Second preference: There is an environment variable that begins |
222
|
|
|
// with an uppercased version of the 'as' string followed by '_TOKEN' |
223
|
|
|
if (!$token) { |
|
|
|
|
224
|
|
|
$env_name = strtoupper(str_replace('-', '_', $as)) . '_TOKEN'; |
225
|
|
|
$token = getenv($env_name); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// If we read in a token from one of the preferred locations, then |
229
|
|
|
// set the GITHUB_TOKEN environment variable and return it. |
230
|
|
|
if ($token) { |
231
|
|
|
putenv("GITHUB_TOKEN=$token"); |
232
|
|
|
return $token; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// Fallback: authenticate to whatever 'GITHUB_TOKEN' is already set to. |
236
|
|
|
return getenv('GITHUB_TOKEN'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function getConfig() |
240
|
|
|
{ |
241
|
|
|
return $this->config; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.