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 setAs($as) |
23
|
|
|
{ |
24
|
|
|
if ($as != $this->as) { |
25
|
|
|
$this->as = $as; |
26
|
|
|
$this->token = false; |
27
|
|
|
$this->gitHubAPI = false; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function whoami() |
32
|
|
|
{ |
33
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
34
|
|
|
$authenticated = $gitHubAPI->api('current_user')->show(); |
35
|
|
|
return $authenticated; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function prClose($org, $project, $number) |
39
|
|
|
{ |
40
|
|
|
foreach ((array)$number as $n) { |
41
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
42
|
|
|
$gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function prCheck($projectWithOrg, $vids) |
47
|
|
|
{ |
48
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
49
|
|
|
|
50
|
|
|
// Find all of the PRs that contain any vid |
51
|
|
|
$existingPRs = $this->existingPRs($gitHubAPI, $projectWithOrg, $vids); |
52
|
|
|
|
53
|
|
|
// Check to see if there are PRs matching all of the vids/vvals. |
54
|
|
|
// If so, exit with a message and do nothing. |
55
|
|
|
$titles = $existingPRs->titles(); |
56
|
|
|
if ($vids->allExist($titles)) { |
57
|
|
|
return [2, "Pull requests already exist; nothing more to do."]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Check to see if there are PRs matching SOME of the vids (with |
61
|
|
|
// or without the matching vvals). If so, close all that match. |
62
|
|
|
if ($existingPRs->isEmpty()) { |
63
|
|
|
return [0, "No open pull requests that need to be closed."]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [0, $existingPRs->prNumbers()]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addTokenAuthentication($url) |
70
|
|
|
{ |
71
|
|
|
$token = $this->gitHubToken(); |
72
|
|
|
if (!$token) { |
73
|
|
|
return $url; |
74
|
|
|
} |
75
|
|
|
$projectAndOrg = $this->projectAndOrgFromUrl($url); |
76
|
|
|
return "https://{$token}:[email protected]/{$projectAndOrg}.git"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
protected function projectAndOrgFromUrl($remote) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$remote = preg_replace('#^git@[^:]*:#', '', $remote); |
82
|
|
|
$remote = preg_replace('#^[^:]*://[^/]/#', '', $remote); |
83
|
|
|
$remote = preg_replace('#\.git$#', '', $remote); |
84
|
|
|
|
85
|
|
|
return $remote; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function existingPRs($gitHubAPI, $projectWithOrg, $vids) |
89
|
|
|
{ |
90
|
|
|
$base_q = "repo:$projectWithOrg in:title is:pr state:open"; |
91
|
|
|
$result = new PullRequests(); |
92
|
|
|
|
93
|
|
|
foreach ($vids->ids() as $vid) { |
94
|
|
|
// TODO: we could exit early if $result already contains all $vid/$vval values |
95
|
|
|
|
96
|
|
|
$q = "$base_q $vid"; |
97
|
|
|
$searchResults = $gitHubAPI->api('search')->issues($q); |
98
|
|
|
|
99
|
|
|
$result->addSearchResults($searchResults); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Authenticate and then return the gitHub API object. |
107
|
|
|
*/ |
108
|
|
|
public function gitHubAPI() |
109
|
|
|
{ |
110
|
|
|
if (!$this->gitHubAPI) { |
111
|
|
|
$token = $this->gitHubToken(); |
112
|
|
|
|
113
|
|
|
$this->gitHubAPI = new \Github\Client(); |
114
|
|
|
$this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN); |
115
|
|
|
} |
116
|
|
|
return $this->gitHubAPI; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Look up the GitHub token set either via environment variable or in the |
121
|
|
|
* auth-token cache directory. |
122
|
|
|
*/ |
123
|
|
|
public function gitHubToken() |
124
|
|
|
{ |
125
|
|
|
if (!$this->token) { |
126
|
|
|
$this->token = $this->getGitHubToken(); |
127
|
|
|
} |
128
|
|
|
return $this->token; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getGitHubToken() |
132
|
|
|
{ |
133
|
|
|
$as = $this->as; |
134
|
|
|
if ($as == 'default') { |
135
|
|
|
$as = $this->getConfig()->get("github.default-user"); |
136
|
|
|
} |
137
|
|
|
$github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path"); |
138
|
|
|
if (file_exists($github_token_cache)) { |
139
|
|
|
$token = trim(file_get_contents($github_token_cache)); |
140
|
|
|
putenv("GITHUB_TOKEN=$token"); |
141
|
|
|
} else { |
142
|
|
|
$token = getenv('GITHUB_TOKEN'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $token; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function getConfig() |
149
|
|
|
{ |
150
|
|
|
return $this->config; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.