feat(dev-infra): prevent `git push` from being called in dryRun mode (#41387)

Update GitClient to prevent the `push` command from being run in dryMode.

PR Close #41387
This commit is contained in:
Joey Perrott 2021-04-01 13:49:09 -07:00 committed by atscott
parent f23406462b
commit 2cef385e43
3 changed files with 41 additions and 1 deletions

View File

@ -434,6 +434,31 @@ function addGithubTokenOption(yargs) {
.default('github-token', '', '<LOCAL TOKEN>');
}
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/** Whether the current environment is in dry run mode. */
function isDryRun() {
return process.env['DRY_RUN'] !== undefined;
}
/** Error to be thrown when a function or method is called in dryRun mode and shouldn't be. */
var DryRunError = /** @class */ (function (_super) {
tslib.__extends(DryRunError, _super);
function DryRunError() {
var _this = _super.call(this, 'Cannot call this function in dryRun mode.') || this;
// Set the prototype explicitly because in ES5, the prototype is accidentally lost due to
// a limitation in down-leveling.
// https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work.
Object.setPrototypeOf(_this, DryRunError.prototype);
return _this;
}
return DryRunError;
}(Error));
/**
* @license
* Copyright Google LLC All Rights Reserved.
@ -604,6 +629,12 @@ var GitClient = /** @class */ (function () {
*/
GitClient.prototype.runGraceful = function (args, options) {
if (options === void 0) { options = {}; }
/** The git command to be run. */
var gitCommand = args[0];
if (isDryRun() && gitCommand === 'push') {
debug("\"git push\" is not able to be run in dryRun mode.");
throw new DryRunError();
}
// To improve the debugging experience in case something fails, we print all executed Git
// commands to better understand the git actions occuring. Depending on the command being
// executed, this debugging information should be logged at different logging levels.

View File

@ -9,7 +9,7 @@
import {Argv} from 'yargs';
/**
* Add a --dry-run flag to the available options for the yargs argv object. When present, sets an
* Add a --dry-run flag to the available options for the yargs argv object. When present, sets an
* environment variable noting dry run mode.
*/
export function addDryRunFlag<T>(args: Argv<T>) {

View File

@ -11,6 +11,7 @@ import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
import {getConfig, getRepoBaseDir, NgDevConfig} from '../config';
import {debug, info, yellow} from '../console';
import {DryRunError, isDryRun} from '../dry-run';
import {GithubClient} from './github';
import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls';
@ -89,6 +90,14 @@ export class GitClient {
* info failed commands.
*/
runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns<string> {
/** The git command to be run. */
const gitCommand = args[0];
if (isDryRun() && gitCommand === 'push') {
debug(`"git push" is not able to be run in dryRun mode.`);
throw new DryRunError();
}
// To improve the debugging experience in case something fails, we print all executed Git
// commands to better understand the git actions occuring. Depending on the command being
// executed, this debugging information should be logged at different logging levels.