-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove_multidev.php
executable file
·60 lines (53 loc) · 1.86 KB
/
remove_multidev.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* @file
* Removes all multidev environments whose corresponding PR is not open.
*/
$github_token = $argv[1];
$github_user = $argv[2];
$repo = $argv[3];
exec("terminus build-env:list ithaca-www '^pr-'", $envs);
for ($i = 3; $i < count($envs) - 1; $i++) {
$pieces = explode(' ', $envs[$i]);
$branch_name = $pieces[2];
print_r("Checking $branch_name environment.");
$number = getPRNumberFromEnv($branch_name);
if ($number) {
$pr_data = getPRObject($number, $github_token, $github_user, $repo);
// print_r($pr_data['state']);
if (!isset($pr_data['state']) || $pr_data['state'] != 'open') {
//Remove the environment
print_r("There is no pull request numbered $number or it is not open. Proceeding to remove.");
exec("terminus multidev:delete ithaca-www.$branch_name --yes --delete-branch", $output, $result);
if (!$result) {
print_r("Removed $branch_name environment from Pantheon as there is not an open PR for it.");
}
else {
print_r("There was a problem trying to remove $branch_name environment. Exit code $result.");
}
}
else {
print_r("$branch_name will be kept.");
}
}
else {
print_r("Cannot identify a pull request number from $branch_name.");
}
}
function getPRNumberFromEnv($name) {
$pieces = explode('-', $name);
if(isset($pieces[1]) && is_numeric($pieces[1])) {
return $pieces[1];
}
return 0;
}
function getPRObject($pr_number, $github_token, $github_user, $repo) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://api.github.com/repos/$repo/pulls/$pr_number");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERPWD, "$github_user:$github_token");
curl_setopt($ch, CURLOPT_USERAGENT, "$github_user");
$output=json_decode(curl_exec($ch), TRUE);
curl_close($ch);
return $output;
}