|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hubph; |
|
4
|
|
|
|
|
5
|
|
|
use Consolidation\Config\ConfigInterface; |
|
6
|
|
|
|
|
7
|
|
|
class HubphAPI |
|
8
|
|
|
{ |
|
9
|
|
|
protected $config; |
|
10
|
|
|
protected $token; |
|
11
|
|
|
protected $gitHubAPI; |
|
12
|
|
|
protected $as = 'default'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* HubphAPI constructor |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(ConfigInterface $config) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->config = $config; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function startLogging($filename) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->stopLogging(); |
|
25
|
|
|
$this->eventLogger = new EventLogger($filename); |
|
|
|
|
|
|
26
|
|
|
$this->eventLogger->start(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function stopLogging() |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->eventLogger) { |
|
32
|
|
|
$this->eventLogger->stop(); |
|
33
|
|
|
} |
|
34
|
|
|
$this->eventLogger = null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function setAs($as) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($as != $this->as) { |
|
40
|
|
|
$this->as = $as; |
|
41
|
|
|
$this->token = false; |
|
42
|
|
|
$this->gitHubAPI = false; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function whoami() |
|
47
|
|
|
{ |
|
48
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
|
49
|
|
|
$authenticated = $gitHubAPI->api('current_user')->show(); |
|
|
|
|
|
|
50
|
|
|
return $authenticated; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function prCreate($org, $project, $title, $body, $base, $head) |
|
54
|
|
|
{ |
|
55
|
|
|
$params = [ |
|
56
|
|
|
'title' => $title, |
|
57
|
|
|
'body' => $body, |
|
58
|
|
|
'base' => $base, |
|
59
|
|
|
'head' => $head, |
|
60
|
|
|
]; |
|
61
|
|
|
$response = $this->gitHubAPI()->api('pull_request')->create($org, $project, $params); |
|
|
|
|
|
|
62
|
|
|
$this->logEvent(__FUNCTION__, [$org, $project], $params, $response); |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function prClose($org, $project, $number) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ((array)$number as $n) { |
|
69
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
|
70
|
|
|
$gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function prCheck($projectWithOrg, $vids) |
|
75
|
|
|
{ |
|
76
|
|
|
// Find all of the PRs that contain any vid |
|
77
|
|
|
$existingPRs = $this->existingPRs($projectWithOrg, $vids); |
|
78
|
|
|
|
|
79
|
|
|
// Check to see if there are PRs matching all of the vids/vvals. |
|
80
|
|
|
// If so, exit with a message and do nothing. |
|
81
|
|
|
$titles = $existingPRs->titles(); |
|
82
|
|
|
if ($vids->allExist($titles)) { |
|
83
|
|
|
return [2, "Pull requests already exist; nothing more to do."]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Check to see if there are PRs matching SOME of the vids (with |
|
87
|
|
|
// or without the matching vvals). If so, close all that match. |
|
88
|
|
|
if ($existingPRs->isEmpty()) { |
|
89
|
|
|
return [0, "No open pull requests that need to be closed."]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return [0, $existingPRs->prNumbers()]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function addTokenAuthentication($url) |
|
96
|
|
|
{ |
|
97
|
|
|
$token = $this->gitHubToken(); |
|
98
|
|
|
if (!$token) { |
|
99
|
|
|
return $url; |
|
100
|
|
|
} |
|
101
|
|
|
$projectAndOrg = $this->projectAndOrgFromUrl($url); |
|
102
|
|
|
return "https://{$token}:[email protected]/{$projectAndOrg}.git"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
protected function projectAndOrgFromUrl($remote) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$remote = preg_replace('#^git@[^:]*:#', '', $remote); |
|
108
|
|
|
$remote = preg_replace('#^[^:]*://[^/]*/#', '', $remote); |
|
109
|
|
|
$remote = preg_replace('#\.git$#', '', $remote); |
|
110
|
|
|
|
|
111
|
|
|
return $remote; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function existingPRs($projectWithOrg, $vids) |
|
115
|
|
|
{ |
|
116
|
|
|
$preamble = $vids->getPreamble(); |
|
117
|
|
|
$q = "repo:$projectWithOrg in:title is:pr state:open $preamble"; |
|
118
|
|
|
$result = new PullRequests(); |
|
119
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
|
120
|
|
|
$searchResults = $gitHubAPI->api('search')->issues($q); |
|
|
|
|
|
|
121
|
|
|
$result->addSearchResults($searchResults, $vids->pattern()); |
|
122
|
|
|
|
|
123
|
|
|
return $result; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function allPRs($projectWithOrg) |
|
127
|
|
|
{ |
|
128
|
|
|
$q = "repo:$projectWithOrg in:title is:pr state:open"; |
|
129
|
|
|
$result = new PullRequests(); |
|
130
|
|
|
$searchResults = $this->gitHubAPI()->api('search')->issues($q); |
|
|
|
|
|
|
131
|
|
|
$result->addSearchResults($searchResults); |
|
132
|
|
|
|
|
133
|
|
|
return $result; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Pass an event of note to the event logger |
|
138
|
|
|
* @param string $event_name |
|
139
|
|
|
* @param array $args |
|
140
|
|
|
* @param array $params |
|
141
|
|
|
* @param array $response |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function logEvent($event_name, $args, $params, $response) |
|
144
|
|
|
{ |
|
145
|
|
|
if ($this->eventLogger) { |
|
146
|
|
|
$this->eventLogger->log($event_name, $args, $params, $response); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Authenticate and then return the gitHub API object. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function gitHubAPI() |
|
154
|
|
|
{ |
|
155
|
|
|
if (!$this->gitHubAPI) { |
|
156
|
|
|
$token = $this->gitHubToken(); |
|
157
|
|
|
|
|
158
|
|
|
$this->gitHubAPI = new \Github\Client(); |
|
159
|
|
|
$this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN); |
|
160
|
|
|
} |
|
161
|
|
|
return $this->gitHubAPI; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Look up the GitHub token set either via environment variable or in the |
|
166
|
|
|
* auth-token cache directory. |
|
167
|
|
|
*/ |
|
168
|
|
|
public function gitHubToken() |
|
169
|
|
|
{ |
|
170
|
|
|
if (!$this->token) { |
|
171
|
|
|
$this->token = $this->getGitHubToken(); |
|
172
|
|
|
} |
|
173
|
|
|
return $this->token; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
protected function getGitHubToken() |
|
177
|
|
|
{ |
|
178
|
|
|
$as = $this->as; |
|
179
|
|
|
if ($as == 'default') { |
|
180
|
|
|
$as = $this->getConfig()->get("github.default-user"); |
|
181
|
|
|
} |
|
182
|
|
|
$github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path"); |
|
183
|
|
|
if (file_exists($github_token_cache)) { |
|
184
|
|
|
$token = trim(file_get_contents($github_token_cache)); |
|
185
|
|
|
putenv("GITHUB_TOKEN=$token"); |
|
186
|
|
|
} else { |
|
187
|
|
|
$token = getenv('GITHUB_TOKEN'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $token; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
protected function getConfig() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->config; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: