forked from roots/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-repo.php
executable file
·231 lines (199 loc) · 6.1 KB
/
update-repo.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace Roots\WordPressSelfUpdate;
require_once __DIR__ . '/update-wp-releases.php';
require_once __DIR__ . '/build-branch.php';
function getWPReleases()
{
$wpReleasesPath = getcwd() . '/wp-releases.json';
if (file_exists($wpReleasesPath)) {
return json_decode(file_get_contents($wpReleasesPath));
}
return collectTags();
}
function tempdir()
{
$tempfile = tempnam(sys_get_temp_dir(), 'roots-wp-');
if (file_exists($tempfile)) {
unlink($tempfile);
}
if (!mkdir($tempfile) && !is_dir($tempfile)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempfile));
}
return $tempfile;
}
function isVersion($string)
{
return preg_match('/^[0-9.]+$/', $string);
}
function isGithubToken($string) {
return preg_match('/^[a-z0-9]+$/i', $string);
}
function run($cmd) {
$code = null;
system($cmd, $code);
return $code === 0;
}
function getGitRemote() {
$githubToken = getenv('GITHUB_TOKEN');
if (!isGithubToken($githubToken)) {
throw new \RuntimeException("refusing to proceed with possibly invalid GITHUB TOKEN $githubToken");
}
return "https://[email protected]/roots/wordpress.git";
}
function gitTagExists($tag)
{
if (!isVersion($tag)) {
throw new \RuntimeException("refusing to pass '$tag' to the shell");
}
$ref = escapeshellarg("refs/tags/$tag");
return run("git show-ref --tags --quiet --verify -- $ref");
}
function configureGitRepo() {
return (
run('git config user.name "Roots Ladybug"') &&
run('git config user.email "[email protected]"')
);
}
function createGitBranchFromDir($dir, $version)
{
if (!isVersion($version)) {
throw new \RuntimeException("refusing to pass '$version' to the shell");
}
$safeVersion = escapeshellarg($version);
$remote = escapeshellarg(getGitRemote());
$branch = escapeshellarg("$version-branch");
$prev = getcwd();
if (!chdir($dir)) {
throw new \RuntimeException("couldn't switch to $dir");
}
try {
if (!run('git init')) {
throw new \RuntimeException("failed to git init in $dir");
}
if (!configureGitRepo()) {
throw new \RuntimeException("could not set git info for $dir");
}
if (!run("git checkout -b $branch")) {
throw new \RuntimeException("failed to git create branch $safeVersion");
}
if (
!run('git add .') ||
!run("git commit -a -m $safeVersion")
) {
throw new \RuntimeException("failed to commit $safeVersion");
}
if (!run("git push $remote $branch")) {
throw new \RuntimeException("failed to push $safeVersion to $remote");
}
if (
!run("git tag $safeVersion") ||
!run("git push $remote $safeVersion")
) {
throw new \RuntimeException("failed to tag $safeVersion");
}
} finally {
chdir($prev);
}
return true;
}
function updateMasterBranch($dir, $version, $zipURL) {
$remote = escapeshellarg(getGitRemote());
$safeDir = escapeshellarg($dir);
$prev = getcwd();
if (!run("git clone --single-branch -b master $remote $safeDir")) {
throw new \RuntimeException("failed to clone git repo in $dir");
}
if (!chdir($dir)) {
throw new \RuntimeException("couldn't switch to $dir");
}
try {
$built = buildBranch($version, $zipURL, $dir);
if (!$built) {
throw new \RuntimeException("failed to build out master branch with $version");
}
if (!run('git add .')) {
throw new \RuntimeException("can't add anything to index");
}
if (run('git diff-index --quiet HEAD')) {
echo "master is already up to date\n";
return true;
}
if (!configureGitRepo()) {
throw new \RuntimeException("could not set git info for $dir");
}
$commitMessage = escapeshellarg("updates to $version");
if (!run("git commit -a -m $commitMessage")) {
throw new \RuntimeException("failed to commit $version to master");
}
if (!run("git push $remote master")) {
throw new \RuntimeException("failed to push $version to $remote");
}
} finally {
chdir($prev);
}
return true;
}
function validateRelease($release) {
if (empty($release)) {
throw new \RuntimeException('bogus release');
}
$version = $release->name;
if (empty($version)) {
throw new \RuntimeException('encountered release with no name');
}
if (!isVersion($version)) {
throw new \RuntimeException("tag '$version' does not look like a version number");
}
if (!$release->zipball_url) {
throw new \RuntimeException("tag '$version' does does not have a zip url");
}
return true;
}
function pushTags()
{
if (!run('git fetch --tags ' . escapeshellarg(getGitRemote()))) {
throw new \RuntimeException('could not fetch tags');
}
$stagingDir = tempdir();
$releases = getWPReleases();
foreach ($releases as $release) {
$version = $release->name;
validateRelease($release);
if (gitTagExists($version)) {
echo "already have a tag for '$version'\n";
continue;
}
$tagDir = "$stagingDir/$version";
if (!mkdir($tagDir) && !is_dir($tagDir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tagDir));
}
$built = buildBranch($version, $release->zipball_url, $tagDir);
if (!$built) {
throw new \RuntimeException("failed to build out $version");
}
$pushed = createGitBranchFromDir($tagDir, $version);
if (!$pushed) {
throw new \RuntimeException("failed to push $tagDir to remote");
}
echo "pushed $version successfully\n";
}
$latestRelease = $releases[0];
validateRelease($latestRelease);
if (!updateMasterBranch("$stagingDir/master", $latestRelease->name, $latestRelease->zipball_url)) {
throw new \RuntimeException("failed to update master branch");
}
echo "updated master successfully\n";
return true;
}
// if run on cli
if ($argv && $argv[0] && realpath($argv[0]) === __FILE__) {
$usage = "usage: update-repo.php\n";
$args = array_slice($argv, 1);
if (count($args) === 1 && in_array($args[0], ['-h', '--help'])) {
echo $usage;
exit(0);
}
$result = pushTags();
fwrite(STDERR, ($result ? 'success!' : 'failure') . "\n");
exit($result ? 0 : 1);
}