-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtag.php
More file actions
377 lines (359 loc) · 11.9 KB
/
Copy pathtag.php
File metadata and controls
377 lines (359 loc) · 11.9 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
if(count($argv) < 3) {
die("tag.php \$branchname \$tag [\$historic_date]\n");
}
$originalBranch = $argv[1];
$tag = $argv[2];
if (!preg_match('/^v[1-9]\d+\.\d+\.\d+(?:(?:alpha|beta|rc)\d+)?$/', $tag)) {
die('Invalid tag format. It should be vXX.Y.Z[alphaX|betaX|rcX]');
}
// to the rescue, when tagging a release was forgotten
// call it with the date of the release tar ball. This can be received from the download server via, e.g.
// ls --full-time /var/www/html/server/prereleases | grep 25.0.3rc1
// or use the datetime of the merge commit of the version bump PR
$historic = null;
if (isset($argv[3])) {
if (1 !== preg_match('/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] UTC$/', $argv[3])) {
die("Invalid historic date $argv[3], expected format: '2023-01-21 09:42:23 UTC', UTC is required");
}
$historic = $argv[3];
}
switch($originalBranch) {
case 'stable21':
// keep them in sync with the ones from brancher/branch.php
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/example-files',
'nextcloud/files_pdfviewer',
'nextcloud/files_rightclick',
'nextcloud/files_videoplayer',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/text',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable22':
case 'stable23':
case 'stable24':
// keep them in sync with the ones from brancher/branch.php
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/circles',
'nextcloud/example-files',
'nextcloud/files_pdfviewer',
'nextcloud/files_rightclick',
'nextcloud/files_videoplayer',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/text',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable25':
case 'stable26':
case 'stable27':
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/bruteforcesettings',
'nextcloud/circles',
'nextcloud/example-files',
'nextcloud/files_pdfviewer',
'nextcloud/files_rightclick',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/related_resources',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/suspicious_login',
'nextcloud/text',
'nextcloud/twofactor_totp',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable28':
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/bruteforcesettings',
'nextcloud/circles',
'nextcloud/example-files',
'nextcloud/files_pdfviewer',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/related_resources',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/suspicious_login',
'nextcloud/text',
'nextcloud/twofactor_totp',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable29':
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/app_api',
'nextcloud/bruteforcesettings',
'nextcloud/circles',
'nextcloud/example-files',
'nextcloud/files_downloadlimit',
'nextcloud/files_pdfviewer',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/related_resources',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/suspicious_login',
'nextcloud/text',
'nextcloud/twofactor_totp',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable30':
case 'stable31':
case 'stable32':
case 'stable33':
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/app_api',
'nextcloud/bruteforcesettings',
'nextcloud/circles',
'nextcloud/documentation',
'nextcloud/example-files',
'nextcloud/files_downloadlimit',
'nextcloud/files_pdfviewer',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/related_resources',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/suspicious_login',
'nextcloud/text',
'nextcloud/twofactor_nextcloud_notification',
'nextcloud/twofactor_totp',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
case 'stable34':
case 'master':
$repositories = [
'nextcloud/server',
'nextcloud/3rdparty',
'nextcloud/activity',
'nextcloud/app_api',
'nextcloud/bruteforcesettings',
'nextcloud/circles',
'nextcloud/documentation',
'nextcloud/example-files',
'nextcloud/files_downloadlimit',
'nextcloud/files_pdfviewer',
'nextcloud/files_lock',
'nextcloud/firstrunwizard',
'nextcloud/logreader',
'nextcloud/nextcloud_announcements',
'nextcloud/notifications',
'nextcloud/office',
'nextcloud/password_policy',
'nextcloud/photos',
'nextcloud/privacy',
'nextcloud/recommendations',
'nextcloud/related_resources',
'nextcloud/serverinfo',
'nextcloud/survey_client',
'nextcloud/suspicious_login',
'nextcloud/text',
'nextcloud/twofactor_nextcloud_notification',
'nextcloud/twofactor_totp',
'nextcloud/updater',
'nextcloud/viewer',
'nextcloud-gmbh/support',
];
break;
default:
die("Branch not found :(\n");
}
// Check if gh CLI is available
$exitCode = 0;
exec('which gh 2>/dev/null', $devNull, $exitCode);
$ghAvailable = $exitCode === 0;
unset($devNull, $exitCode);
if ($ghAvailable) {
fwrite(STDERR, '[Info] Using gh API for tagging (fast mode)' . PHP_EOL);
} else {
fwrite(STDERR, '[Info] gh CLI not found, falling back to git clone' . PHP_EOL);
}
/**
* Tag a repository via gh release create + delete (keeps the tag).
* Always tags current branch HEAD.
*
* @return bool true on success
*/
function tagViaGh(string $repo, string $branch, string $tag): bool {
// Check if release already exists (e.g. server creates its own release separately)
$exitCode = 0;
exec(sprintf(
'gh release view %s --repo %s 2>/dev/null',
escapeshellarg($tag), escapeshellarg($repo)
), $viewOutput, $exitCode);
$releaseExists = $exitCode === 0;
$exitCode = 0;
exec(sprintf(
'gh release create %s --repo %s --target %s --title %s --notes "" 2>&1',
escapeshellarg($tag), escapeshellarg($repo), escapeshellarg($branch), escapeshellarg($tag)
), $output, $exitCode);
if ($exitCode !== 0) {
// If release already existed, tag already exists too — that's fine
if ($releaseExists) {
fwrite(STDERR, '[OK] ' . $repo . ' already has release ' . $tag . ', skipping' . PHP_EOL);
return true;
}
fwrite(STDERR, '[Warning] gh: could not create release for ' . $repo . ': ' . trim(implode('', $output)) . PHP_EOL);
return false;
}
// Only delete the release if we just created it
sleep(1);
$exitCode = 0;
exec(sprintf(
'gh release delete %s --repo %s --yes 2>&1',
escapeshellarg($tag), escapeshellarg($repo)
), $deleteOutput, $exitCode);
if ($exitCode !== 0) {
fwrite(STDERR, '[Warning] gh: release created but could not delete for ' . $repo . ': ' . trim(implode('', $deleteOutput)) . PHP_EOL);
}
fwrite(STDERR, '[OK] ' . $repo . ' tagged ' . $tag . ' (via gh)' . PHP_EOL);
return true;
}
// use proper temp location on dev machines, assuming it's memdisc, to not wear out physical storage
$workDir = gethostname() === 'client-builder' ? __DIR__ : null;
// for historic commits we need to pull some more history to be able to find the relevant commit
$depthMode = '--depth=1';
$committerDate = '';
if ($historic) {
$historicDateTime = new DateTime($historic);
$sixWeeks = new DateInterval('P6W');
$historicDateTime->sub($sixWeeks);
$depthMode = sprintf('--shallow-since="%s"', $historicDateTime->format('Y-m-d H:i:s e'));
unset($historicDateTime);
unset($sixWeeks);
}
foreach($repositories as $repo) {
// Try gh API first
if ($ghAvailable && !$historic) {
if (tagViaGh($repo, $originalBranch, $tag)) {
continue;
}
fwrite(STDERR, '[Info] Falling back to git clone for ' . $repo . PHP_EOL);
}
// Fallback: clone + signed tag
if ($workDir === null) {
$workDir = trim(shell_exec('mktemp -d'));
fwrite(STDERR, '[Debug] Work dir is: ' . $workDir . PHP_EOL);
}
$name = explode('/', $repo)[1];
$SSH_OPTIONS = '';
if ($name === 'support' && gethostname() === 'client-builder') {
$SSH_OPTIONS = "GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa.support-app -o IdentitiesOnly=yes'";
}
$branchOpt = $originalBranch === 'master' || $originalBranch === 'main' ? '' : ' --branch='.escapeshellarg($originalBranch);
// Clone the repository and checkout the required branch
fwrite(STDERR, '[Debug] cd ' . $workDir . ' && ' . $SSH_OPTIONS . ' git clone ' . $depthMode . $branchOpt . ' git@github.com:' . $repo . PHP_EOL);
shell_exec('cd ' . $workDir . ' && ' . $SSH_OPTIONS . ' git clone ' . $depthMode . $branchOpt . ' git@github.com:' . $repo);
// checkout historic commit if applicable
if ($historic) {
if (!is_dir($workDir . '/' . $name)) {
// we end up here, with a failed clone, when there were no commits in our time range. We redo with depth=42.
// 42 for good luck. 1 Might bring too new commits.
shell_exec('cd ' . $workDir . ' && ' . $SSH_OPTIONS . ' git clone --depth=42' . $branchOpt . ' git@github.com:' . $repo);
}
$commitHash = trim(shell_exec('cd ' . $workDir . '/' . $name . ' && git log -n1 --format=%H --until="' . $historic . '"'));
if ($commitHash === "") {
shell_exec('cd ' . $workDir . ' && rm -rf ' . $name);
// we end up here, when there were no old enough commits in our time range! We redo with depth=42.
// 42 for good luck.
shell_exec('cd ' . $workDir . ' && ' . $SSH_OPTIONS . ' git clone --depth=42' . $branchOpt . ' git@github.com:' . $repo);
$commitHash = trim(shell_exec('cd ' . $workDir . '/' . $name . ' && git log -n1 --format=%H --until="' . $historic . '"'));
}
if (strlen($commitHash) !== 40) {
fwrite(STDERR, '[Error] unexpected commit length, aborting. Hash was ' . $commitHash . PHP_EOL);
exit(1);
}
shell_exec('cd ' . $workDir . '/' . $name . '/$repo && git checkout ' . $commitHash);
// use the date of the commit
if ($committerDate === '') {
// utilize commit date of the latest server commit, which should be the merge commit of the version bump.
// Requires that server repo is always tagged first!
$commitDate = trim(shell_exec('cd ' . $workDir . '/' . $name . ' && git show --format=%aD | head -1'));
$committerDate = sprintf('GIT_COMMITTER_DATE="%s"', $commitDate);
}
}
// Create a signed tag
shell_exec('cd ' . $workDir . '/' . $name . ' && ' . $committerDate . ' git tag -s ' . $tag . ' -m \'' . $tag . '\'');
// Push the signed tag
shell_exec('cd ' . $workDir . '/' . $name . ' && ' . $SSH_OPTIONS . ' git push origin ' . $tag);
// Delete repository
shell_exec('cd ' . $workDir . ' && rm -rf ' . $name);
}