This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
154 lines (135 loc) · 4.36 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
function commitFile(string $localpath, string $repopath, string $repo, string $branch, String $message, $new = false){
global $client;
global $org;
echo "Commiting $repopath to $repo ... ";
if ($new) {
$client->repository()->contents()->create(
$org,
$repo,
$repopath,
file_get_contents($localpath),
$message,
$branch
);
} else {
$client->repository()->contents()->update(
$org,
$repo,
$repopath,
file_get_contents($localpath),
$message,
$client->repository()->contents()->show($org, $repo, $repopath, "refs/heads/$branch")['sha'],
$branch
);
}
echo "Done.\n";
}
function createBranch(string $repo, string $branch){
global $client;
global $org;
echo "Creating branch $branch in $repo ... ";
try {
$master = $client->repositories()->branches($org, $repo, 'master');
$client->git()->references()->create($org, $repo, [
'ref' => "refs/heads/$branch",
'sha' => $master['commit']['sha'],
]);
echo "Done\n";
} catch (\Github\Exception\RuntimeException $e) {
if ($e->getMessage() === 'Reference already exists') {
echo "Branch already exists, skipping\n";
} elseif ($e->getMessage() === 'Repository was archived so is read-only.') {
echo "Repository was archived so is read-only, skipping\n";
} elseif ($e->getMessage() === 'Not Found') {
echo "Repository was not found (lack of permissions?), skipping\n";
} else {
echo "Unknown error, message was: '{$e->getMessage()}'\n";
throw $e;
}
}
}
function deleteBranch(string $repo, string $branch){
global $client;
global $org;
echo "Deleting branch $branch in $repo ... ";
$client->git()->references()->remove($org, $repo, "heads/$branch");
echo "Done\n";
}
function sendPullRequest(string $repo, string $branch, string $title){
global $client;
global $org;
echo "Sending pull request of $branch in $repo ... ";
try {
$pr = $client->pullRequests()->create($org, $repo, [
'base' => 'master',
'head' => $branch,
'title' => $title,
'body' => file_get_contents('body.txt'),
]);
$prUrl = $pr['html_url'];
} catch (\Github\Exception\ValidationFailedException $e) {
if (strpos($e->getMessage(), 'No commits between') !== false) {
echo $e->getMessage()."\n";
} elseif (strpos($e->getMessage(), 'A pull request already exists') !== false) {
echo $e->getMessage()."\n";
} else {
throw $e;
}
$prUrl = 'NaN';
}
echo "Done: $prUrl\n";
}
function getPullRequestID(string $repo, string $branch){
global $client;
global $org;something from upstream
$prs = $client->pullRequests()->all($org, $repo, [
'head' => "$org:$branch"
]);
if (count($prs) > 1) {
throw new LogicException("There's several PR for this branch: " . join(', ',
array_map(function($pr){return $pr['url'];}, $prs)
)
);
} else {
return $prs[0]['number'];
}
}
function renamePullRequest(string $repo, string $branch, string $title){
global $client;
global $org;
echo "Renaming pull request of $branch in $repo ... ";
$client->pullRequests()->update($org, $repo, getPullRequestID($repo, $branch), [
'title' => $title,
]);
echo "Done.\n";
}
function mergePullRequest(string $repo, string $branch){
global $client;
global $org;
echo "Merging pull request of $branch in $repo ... ";
$client->pullRequests()->merge(
$org,
$repo,
getPullRequestID($repo, $branch),
"Merge $branch",
$client->repositories()->branches($org, $repo, $branch)['commit']['sha']
);
echo "Done.\n";
}
function getDirectoryFiles(string $path): array
{
if (!is_dir($path)) {
return [];
}
$files = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
/** @var SplFileInfo $index */
foreach ($iterator as $index) {
if ($index->isDir()) {
continue;
}
$files[] = $index->getPathname();
}
return $files;
}