-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathRepoSplitCommand.php
More file actions
491 lines (431 loc) · 16.9 KB
/
Copy pathRepoSplitCommand.php
File metadata and controls
491 lines (431 loc) · 16.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Dev\Command;
use Google\Cloud\Dev\Component;
use Google\Cloud\Dev\GitHub;
use Google\Cloud\Dev\Packagist;
use Google\Cloud\Dev\ReleaseNotes;
use Google\Cloud\Dev\RunShell;
use Google\Cloud\Dev\Split;
use Google\Cloud\Dev\SplitInstall;
use GuzzleHttp\BodySummarizer;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* A Symfony command for executing subtree splits.
*
* @internal
*/
class RepoSplitCommand extends Command
{
const PARENT_TAG_NAME = 'https://github.com/%s/releases/tag/%s';
const EXEC_DIR = '.split';
const TOKEN_ENV = 'GH_OAUTH_TOKEN';
private const PHP_TEAM = RepoComplianceCommand::PHP_TEAM;
private string $rootPath;
/**
* @param string $rootPath The path to the repository root directory.
*/
public function __construct($rootPath)
{
$this->rootPath = realpath($rootPath);
parent::__construct();
}
/**
* Command configuration.
*
* @return void
*/
protected function configure()
{
$this->setName('repo:split')
->setAliases(['split'])
->setDescription('Split subtree and push to various remotes.')
->addArgument(
'repo',
InputArgument::REQUIRED,
'The parent repository, in the form of `organization/repository`'
)
->addArgument(
'parent',
InputArgument::REQUIRED,
'The parent repository tag to which each component is linked.'
)
->addOption(
'token',
't',
InputOption::VALUE_OPTIONAL,
sprintf(
'A Github Auth Token. If not provided, uses value of environment variable `%s`.',
self::TOKEN_ENV
)
)
->addOption(
'packagist-username',
'',
InputOption::VALUE_REQUIRED,
'A packagist username. If provided, new packages will automatically be submitted ' .
'via the packagist API.',
)
->addOption(
'packagist-token',
'',
InputOption::VALUE_REQUIRED,
'A Packagist API Auth Token. If provided, new packages will automatically be ' .
'submitted via the packagist API.',
)
->addOption(
'packagist-safe-token',
'',
InputOption::VALUE_REQUIRED,
'A Packagist API Auth Token. This token can only be used for package updates, and ' .
'is not very sensitive if leaked.',
)
->addOption(
'splitsh',
null,
InputOption::VALUE_OPTIONAL,
'Path to a pre-compiled splitsh-lite binary.'
)
->addOption(
'component',
'c',
InputOption::VALUE_OPTIONAL,
'Split and release only a single component.'
)
->addOption(
'update-release-notes',
'',
InputOption::VALUE_NONE,
'Update the release notes if the release already exists.'
)
->addOption(
'component-branch',
'',
InputOption::VALUE_OPTIONAL,
'Specify the branch of the component to push to.'
);
}
/**
* Execute Split and Release process.
*
* @param InputInterface $input The Symfony input handler.
* @param OutputInterface $output The Symfony output handler.
* @return int The exit code.
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("<info>[INFO]</info>: Parent repository: " . $input->getArgument('repo'));
$output->writeln("<info>[INFO]</info>: Parent tag: " . $input->getArgument('parent'));
$execDir = $this->rootPath . '/' . self::EXEC_DIR;
$token = $this->githubToken($input->getOption('token'));
$shell = new RunShell();
$guzzle = $this->guzzleClient();
$github = $this->githubClient($output, $shell, $guzzle, $token);
$split = $this->splitWrapper($output, $shell);
$packagist = null;
if ($packagistUsername = $input->getOption('packagist-username')) {
if (!$packagistToken = $input->getOption('packagist-token')) {
throw new \InvalidArgumentException('A packagist token must be provided if a username is provided.');
}
$packagistSafeToken = $input->getOption('packagist-safe-token');
$packagist = new Packagist($guzzle, $packagistUsername, $packagistToken, $packagistSafeToken, $output);
}
@mkdir($execDir);
if (!$splitBinaryPath = $input->getOption('splitsh')) {
throw new \InvalidArgumentException('A splitsh binary path must be provided.');
}
$changelog = $github->getChangelog(
$input->getArgument('repo'),
$input->getArgument('parent')
);
$releaseNotes = new ReleaseNotes($changelog);
$branchToPush = $input->getOption('component-branch');
if ($componentId = $input->getOption('component')) {
$components = [new Component($componentId)];
} else {
// Build a list of components to update based on the release notes
$components = [];
foreach (Component::getComponents() as $component) {
if ($releaseNotes->get($component->getId())) {
$components[] = $component;
}
}
}
$parentTagSource = sprintf(
self::PARENT_TAG_NAME,
$input->getArgument('repo'),
$input->getArgument('parent')
);
$errors = [];
foreach ($components as $component) {
$res = $this->processComponent(
$output,
$github,
$split,
$releaseNotes,
$component,
$splitBinaryPath,
$parentTagSource,
$input->getOption('update-release-notes'),
$packagist,
$branchToPush
);
if (!$res) {
$errors[] = $component->getId();
}
$output->writeln('');
$output->writeln('');
}
if ($errors) {
$output->writeln('<error>[ERROR]</error>: One or more components reported an error.');
$output->writeln('Please correct errors and try again.');
$output->writeln('Error component(s): ' . implode(', ', $errors));
return 1;
}
return 0;
}
/**
* Process release for a given component.
*
* Checks if the tag exists on the remote target, splits the component,
* pushes to the remote target and creates a github release.
*
* @param OutputInterface $output Allows writing to cli.
* @param GitHub A GitHub API wrapper.
* @param Split A Splitsh wrapper.
* @param ReleaseNotes The parsed release notes.
* @param array $component The component data.
* @param string $splitBinaryPath The path to the splitsh binary.
* @param string $parentTagSource The URI to the parent tag.
* @param ?Packagist $packagist The Packagist API object (if configured).
* @param ?string $branchToPush The branch to which the component will be pushed.
* @return bool
*/
private function processComponent(
OutputInterface $output,
GitHub $github,
Split $split,
ReleaseNotes $releaseNotes,
Component $component,
$splitBinaryPath,
$parentTagSource,
$updateReleaseNotes,
?Packagist $packagist,
?string $branchToPush
) {
$output->writeln('');
$tagName = 'v' . $component->getPackageVersion();
$repoName = $component->getRepoName();
$componentId = $component->getId();
$isAlreadyTagged = $github->doesTagExist($repoName, $tagName);
if (is_null($branchToPush)) {
$branchToPush = $github->getDefaultBranch($repoName) ?: 'main';
}
// If the repo is empty, it's new and we don't want to force-push.
$isTargetEmpty = $github->isTargetEmpty($repoName);
$output->writeln(sprintf(
'<comment>%s</comment>: Starting on component. Target version <info>%s</info>',
$componentId,
$tagName
));
$this->writeDiv($output);
if ($isAlreadyTagged) {
$output->writeln(sprintf(
'Version <info>%s</info> already exists on target <info>%s</info>',
$tagName,
$repoName
));
if ($updateReleaseNotes && ($notes = $releaseNotes->get($componentId)) && ($version = $releaseNotes->getVersion($componentId))) {
$github->updateReleaseNotes($repoName, 'v' . $version, $notes);
$output->writeln(sprintf('<comment>[info]</comment> Release notes updated for version <info>%s</info>.', $version));
} else {
$output->writeln('<comment>[info]</comment> Skipping.');
}
return true;
}
$output->writeln(sprintf(
'<comment>%s</comment>: Running splitsh',
$componentId
));
$splitBranch = $split->execute($splitBinaryPath, $this->rootPath, $component->getName());
if ($splitBranch) {
$output->writeln(sprintf('Split succeeded, branch <info>%s</info> created.', $splitBranch));
} else {
$output->writeln('<error>Split failed!</error>');
return false;
}
$output->writeln('');
$output->writeln(sprintf(
'<comment>%s</comment>: Push to github target %s',
$componentId,
$repoName
));
$res = $github->push($repoName, $splitBranch, $branchToPush, $isTargetEmpty);
if ($res[0]) {
$output->writeln(sprintf('<comment>%s</comment>: Push succeeded.', $componentId));
} else {
$output->writeln(sprintf('<error>%s</error>: Push failed.', $componentId));
return false;
}
$output->writeln('');
$output->writeln('<comment>[info]</comment> Creating GitHub tag.');
$notes = $releaseNotes->get($componentId);
if (!$notes) {
$notes = sprintf(
'For release notes, please see the [associated Google Cloud PHP release](%s).',
$parentTagSource
);
}
$res = $github->createRelease(
$repoName,
$tagName,
$component->getPackageName() . ' ' . $tagName,
$notes
);
if ($res) {
$output->writeln(sprintf('<comment>%s</comment>: Tag succeeded.', $componentId));
} else {
$output->writeln(sprintf('<error>%s</error>: Tag failed.', $componentId));
return false;
}
// This is the first release!
if ($tagName === 'v0.1.0') {
// Ensure issues/projects/wiki/pages/discussion are disabled and the repo is public
$ret = $github->updateRepoDetails($repoName, [
'has_issues' => false,
'has_projects' => false,
'has_wiki' => false,
'has_pages' => false,
'has_discussions' => false,
'visibility' => 'public',
]);
if ($ret) {
$output->writeln(sprintf('<comment>%s</comment>: Disabled repo issues/projects/etc for first release.', $componentId));
} else {
$output->writeln(sprintf('<error>%s</error>: Unable to update repo details.', $componentId));
return false;
}
// Submit the new package to Packagist and add a webhook to update it
if ($packagist) {
$output->writeln('<comment>[info]</comment> Creating Packagist package.');
$res = $packagist->submitPackage('https://github.com/' . $repoName);
if ($res) {
$output->writeln(sprintf('<comment>%s</comment>: Packagist package created.', $componentId));
} else {
$output->writeln(sprintf('<error>%s</error>: Unable to create Packagist package.', $componentId));
return false;
}
// use the safe API token if possible
$apiToken = $packagist->getSafeApiToken() ?: $packagist->getApiToken();
if ($github->addWebhook($repoName, $packagist->getWebhookUrl(), $apiToken)) {
$output->writeln(sprintf('<comment>%s</comment>: Packagist webhook package created.', $componentId));
} else {
$output->writeln(sprintf('<error>%s</error>: Unable to create Packagist webhook.', $componentId));
return false;
}
}
// Add team permission
$ret = $github->updateTeamPermission('googleapis', self::PHP_TEAM, $repoName, 'admin');
if ($ret) {
$output->writeln(sprintf('<comment>%s</comment>: Added "%s" as admin.', $componentId, self::PHP_TEAM));
} else {
$output->writeln(sprintf('<error>%s</error>: Unable to add "%s" as admin.', $componentId, self::PHP_TEAM));
return false;
}
}
return true;
}
/**
* Fetch the GitHub auth token from the user or environment.
*
* @param string|null $userToken The user token, if provided, else `null`.
* @return string
* @throws \RuntimeException If no user token was given and the environment
* variable is not set.
*/
protected function githubToken($userToken)
{
$token = $userToken ?: getenv(self::TOKEN_ENV);
if (!$token) {
throw new \RuntimeException(sprintf(
'Could not find GitHub auth token. Please set the environment ' .
'variable `%s` or pass token as console argument.',
self::TOKEN_ENV
));
}
return $token;
}
/**
* Initialize the Split wrapper.
*
* You can override this method in unit tests.
*
* @param OutputInterface $output Allows writing to cli.
* @param RunShell $shell A wrapper for executing shell commands.
* @return Split
*/
protected function splitWrapper(OutputInterface $output, RunShell $shell)
{
$output->writeln('<comment>[info]</comment> Instantiating Splitsh Wrapper.');
return new Split($shell);
}
/**
* Initialize the Github client.
*
* You can override this method in unit tests.
*
* @param OutputInterface $output Allows writing to cli.
* @param RunShell $shell A wrapper for executing shell commands.
* @param Client $guzzle A guzzle client for executing HTTP requests.
* @param string $token A Github auth token.
* @return GitHub
*/
protected function githubClient(OutputInterface $output, RunShell $shell, Client $guzzle, $token)
{
$output->writeln('<comment>[info]</comment> Instantiating GitHub API Wrapper.');
return new GitHub($shell, $guzzle, $token, $output);
}
/**
* Create a Guzzle client.
*
* You can override this method in unit tests.
*
* @return Client
*/
protected function guzzleClient()
{
// Create a new HTTP Errors middleware with a body summarizer of length
// 240 characters (the default is 120)
$httpErrorsMiddleware = Middleware::httpErrors(new BodySummarizer(240));
$stack = HandlerStack::create();
$stack->remove('http_errors');
$stack->unshift($httpErrorsMiddleware, 'http_errors');
return new Client(['handler' => $stack]);
}
private function writeDiv(OutputInterface $output)
{
$output->writeln('--------');
}
}