1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hubph; |
4
|
|
|
|
5
|
|
|
use Consolidation\Config\ConfigInterface; |
6
|
|
|
|
7
|
|
|
class HubphAPI |
8
|
|
|
{ |
9
|
|
|
protected $config; |
10
|
|
|
protected $gitHubAPI; |
11
|
|
|
protected $as = 'default'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HubphAPI constructor |
15
|
|
|
*/ |
16
|
|
|
public function __construct(ConfigInterface $config) |
17
|
|
|
{ |
18
|
|
|
$this->config = $config; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setAs($as) |
22
|
|
|
{ |
23
|
|
|
if ($as != $this->as) { |
24
|
|
|
$this->as = $as; |
25
|
|
|
$this->gitHubAPI = false; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function whoami() |
30
|
|
|
{ |
31
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
32
|
|
|
$authenticated = $gitHubAPI->api('current_user')->show(); |
33
|
|
|
return $authenticated; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function prClose($org, $project, $number) |
37
|
|
|
{ |
38
|
|
|
foreach ((array)$number as $n) { |
39
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
40
|
|
|
$gitHubAPI->api('pull_request')->update($org, $project, $n, ['state' => 'closed']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function prCheck($projectWithOrg, $vids) |
45
|
|
|
{ |
46
|
|
|
$gitHubAPI = $this->gitHubAPI(); |
47
|
|
|
|
48
|
|
|
// Find all of the PRs that contain any vid |
49
|
|
|
$existingPRs = $this->existingPRs($gitHubAPI, $projectWithOrg, $vids); |
50
|
|
|
|
51
|
|
|
// Check to see if there are PRs matching all of the vids/vvals. |
52
|
|
|
// If so, exit with a message and do nothing. |
53
|
|
|
$titles = $existingPRs->titles(); |
54
|
|
|
if ($vids->allExist($titles)) { |
55
|
|
|
return [2, "Pull requests already exist; nothing more to do."]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Check to see if there are PRs matching SOME of the vids (with |
59
|
|
|
// or without the matching vvals). If so, close all that match. |
60
|
|
|
if ($existingPRs->isEmpty()) { |
61
|
|
|
return [0, "No open pull requests that need to be closed."]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return [0, $existingPRs->prNumbers()]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function existingPRs($gitHubAPI, $projectWithOrg, $vids) |
68
|
|
|
{ |
69
|
|
|
$base_q = "repo:$projectWithOrg in:title is:pr state:open"; |
70
|
|
|
$result = new PullRequests(); |
71
|
|
|
|
72
|
|
|
foreach ($vids->ids() as $vid) { |
73
|
|
|
// TODO: we could exit early if $result already contains all $vid/$vval values |
74
|
|
|
|
75
|
|
|
$q = "$base_q $vid"; |
76
|
|
|
$searchResults = $gitHubAPI->api('search')->issues($q); |
77
|
|
|
|
78
|
|
|
$result->addSearchResults($searchResults); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Authenticate and then return the gitHub API object. |
86
|
|
|
*/ |
87
|
|
|
public function gitHubAPI() |
88
|
|
|
{ |
89
|
|
|
if (!$this->gitHubAPI) { |
90
|
|
|
$token = $this->gitHubToken(); |
91
|
|
|
|
92
|
|
|
$this->gitHubAPI = new \Github\Client(); |
93
|
|
|
$this->gitHubAPI->authenticate($token, null, \Github\Client::AUTH_HTTP_TOKEN); |
94
|
|
|
} |
95
|
|
|
return $this->gitHubAPI; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Look up the GitHub token set either via environment variable or in the |
100
|
|
|
* auth-token cache directory. |
101
|
|
|
*/ |
102
|
|
|
public function gitHubToken() |
103
|
|
|
{ |
104
|
|
|
$as = $this->as; |
105
|
|
|
if ($as == 'default') { |
106
|
|
|
$as = $this->getConfig()->get("github.default-user"); |
107
|
|
|
} |
108
|
|
|
$github_token_cache = $this->getConfig()->get("github.personal-auth-token.$as.path"); |
109
|
|
|
if (file_exists($github_token_cache)) { |
110
|
|
|
$token = trim(file_get_contents($github_token_cache)); |
111
|
|
|
putenv("GITHUB_TOKEN=$token"); |
112
|
|
|
} else { |
113
|
|
|
$token = getenv('GITHUB_TOKEN'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $token; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getConfig() |
120
|
|
|
{ |
121
|
|
|
return $this->config; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|