From 9e0e7631562e7f5e6ed49d5080b0eeba759f3b5e Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 1 Sep 2020 10:43:26 +0200 Subject: [PATCH] refactor(dev-infra): do not print git commands in silent mode (#38656) The git client respects the `SpawnSyncOptions` when a command is executed. Currently it does not hide the command info messages when commands are run in silent mode. We fix this as part of this commit, so that the command info is only printed to `debug` if `stdio` is set to `ignore`. Additonally, the github token is made public so that it can be used by commands if other repositories like forks are targeted. PR Close #38656 --- dev-infra/utils/git/index.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/dev-infra/utils/git/index.ts b/dev-infra/utils/git/index.ts index c533c75575..1322d62652 100644 --- a/dev-infra/utils/git/index.ts +++ b/dev-infra/utils/git/index.ts @@ -10,7 +10,7 @@ import * as Octokit from '@octokit/rest'; import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process'; import {getConfig, getRepoBaseDir, NgDevConfig} from '../config'; -import {info, yellow} from '../console'; +import {debug, info, yellow} from '../console'; import {GithubClient} from './github'; import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls'; @@ -47,12 +47,12 @@ export class GitClient { /** Octokit request parameters object for targeting the configured remote. */ remoteParams = {owner: this.remoteConfig.owner, repo: this.remoteConfig.name}; /** Git URL that resolves to the configured repository. */ - repoGitUrl = getRepositoryGitUrl(this.remoteConfig, this._githubToken); + repoGitUrl = getRepositoryGitUrl(this.remoteConfig, this.githubToken); /** Instance of the authenticated Github octokit API. */ - github = new GithubClient(this._githubToken); + github = new GithubClient(this.githubToken); /** The OAuth scopes available for the provided Github token. */ - private _oauthScopes: Promise|null = null; + private _cachedOauthScopes: Promise|null = null; /** * Regular expression that matches the provided Github token. Used for * sanitizing the token from Git child process output. @@ -60,13 +60,13 @@ export class GitClient { private _githubTokenRegex: RegExp|null = null; constructor( - private _githubToken?: string, private _config: Pick = getConfig(), + public githubToken?: string, private _config: Pick = getConfig(), private _projectRoot = getRepoBaseDir()) { // If a token has been specified (and is not empty), pass it to the Octokit API and // also create a regular expression that can be used for sanitizing Git command output // so that it does not print the token accidentally. - if (_githubToken != null) { - this._githubTokenRegex = new RegExp(_githubToken, 'g'); + if (githubToken != null) { + this._githubTokenRegex = new RegExp(githubToken, 'g'); } } @@ -87,10 +87,12 @@ export class GitClient { * info failed commands. */ runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns { - // To improve the infoging experience in case something fails, we print all executed - // Git commands. Note that we do not want to print the token if is contained in the - // command. It's common to share errors with others if the tool failed. - info('Executing: git', this.omitGithubTokenFromMessage(args.join(' '))); + // To improve the debugging experience in case something fails, we print all executed Git + // commands unless the `stdio` is explicitly to `ignore` (which is equivalent to silent). + // Note that we do not want to print the token if is contained in the command. It's common + // to share errors with others if the tool failed, and we do not want to leak tokens. + const printFn = options.stdio !== 'ignore' ? info : debug; + printFn('Executing: git', this.omitGithubTokenFromMessage(args.join(' '))); const result = spawnSync('git', args, { cwd: this._projectRoot,