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
This commit is contained in:
Paul Gschwendtner 2020-09-01 10:43:26 +02:00 committed by Alex Rickabaugh
parent 758d0e2045
commit 9e0e763156
1 changed files with 13 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import * as Octokit from '@octokit/rest';
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process'; import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
import {getConfig, getRepoBaseDir, NgDevConfig} from '../config'; import {getConfig, getRepoBaseDir, NgDevConfig} from '../config';
import {info, yellow} from '../console'; import {debug, info, yellow} from '../console';
import {GithubClient} from './github'; import {GithubClient} from './github';
import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls'; 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. */ /** Octokit request parameters object for targeting the configured remote. */
remoteParams = {owner: this.remoteConfig.owner, repo: this.remoteConfig.name}; remoteParams = {owner: this.remoteConfig.owner, repo: this.remoteConfig.name};
/** Git URL that resolves to the configured repository. */ /** 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. */ /** 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. */ /** The OAuth scopes available for the provided Github token. */
private _oauthScopes: Promise<string[]>|null = null; private _cachedOauthScopes: Promise<string[]>|null = null;
/** /**
* Regular expression that matches the provided Github token. Used for * Regular expression that matches the provided Github token. Used for
* sanitizing the token from Git child process output. * sanitizing the token from Git child process output.
@ -60,13 +60,13 @@ export class GitClient {
private _githubTokenRegex: RegExp|null = null; private _githubTokenRegex: RegExp|null = null;
constructor( constructor(
private _githubToken?: string, private _config: Pick<NgDevConfig, 'github'> = getConfig(), public githubToken?: string, private _config: Pick<NgDevConfig, 'github'> = getConfig(),
private _projectRoot = getRepoBaseDir()) { private _projectRoot = getRepoBaseDir()) {
// If a token has been specified (and is not empty), pass it to the Octokit API and // 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 // also create a regular expression that can be used for sanitizing Git command output
// so that it does not print the token accidentally. // so that it does not print the token accidentally.
if (_githubToken != null) { if (githubToken != null) {
this._githubTokenRegex = new RegExp(_githubToken, 'g'); this._githubTokenRegex = new RegExp(githubToken, 'g');
} }
} }
@ -87,10 +87,12 @@ export class GitClient {
* info failed commands. * info failed commands.
*/ */
runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns<string> { runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns<string> {
// To improve the infoging experience in case something fails, we print all executed // To improve the debugging experience in case something fails, we print all executed Git
// Git commands. Note that we do not want to print the token if is contained in the // commands unless the `stdio` is explicitly to `ignore` (which is equivalent to silent).
// command. It's common to share errors with others if the tool failed. // Note that we do not want to print the token if is contained in the command. It's common
info('Executing: git', this.omitGithubTokenFromMessage(args.join(' '))); // 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, { const result = spawnSync('git', args, {
cwd: this._projectRoot, cwd: this._projectRoot,