Skip to content

Commit 9e0e763

Browse files
devversionalxhub
authored andcommitted
refactor(dev-infra): do not print git commands in silent mode (angular#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 angular#38656
1 parent 758d0e2 commit 9e0e763

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

dev-infra/utils/git/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as Octokit from '@octokit/rest';
1010
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
1111

1212
import {getConfig, getRepoBaseDir, NgDevConfig} from '../config';
13-
import {info, yellow} from '../console';
13+
import {debug, info, yellow} from '../console';
1414
import {GithubClient} from './github';
1515
import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls';
1616

@@ -47,26 +47,26 @@ export class GitClient {
4747
/** Octokit request parameters object for targeting the configured remote. */
4848
remoteParams = {owner: this.remoteConfig.owner, repo: this.remoteConfig.name};
4949
/** Git URL that resolves to the configured repository. */
50-
repoGitUrl = getRepositoryGitUrl(this.remoteConfig, this._githubToken);
50+
repoGitUrl = getRepositoryGitUrl(this.remoteConfig, this.githubToken);
5151
/** Instance of the authenticated Github octokit API. */
52-
github = new GithubClient(this._githubToken);
52+
github = new GithubClient(this.githubToken);
5353

5454
/** The OAuth scopes available for the provided Github token. */
55-
private _oauthScopes: Promise<string[]>|null = null;
55+
private _cachedOauthScopes: Promise<string[]>|null = null;
5656
/**
5757
* Regular expression that matches the provided Github token. Used for
5858
* sanitizing the token from Git child process output.
5959
*/
6060
private _githubTokenRegex: RegExp|null = null;
6161

6262
constructor(
63-
private _githubToken?: string, private _config: Pick<NgDevConfig, 'github'> = getConfig(),
63+
public githubToken?: string, private _config: Pick<NgDevConfig, 'github'> = getConfig(),
6464
private _projectRoot = getRepoBaseDir()) {
6565
// If a token has been specified (and is not empty), pass it to the Octokit API and
6666
// also create a regular expression that can be used for sanitizing Git command output
6767
// so that it does not print the token accidentally.
68-
if (_githubToken != null) {
69-
this._githubTokenRegex = new RegExp(_githubToken, 'g');
68+
if (githubToken != null) {
69+
this._githubTokenRegex = new RegExp(githubToken, 'g');
7070
}
7171
}
7272

@@ -87,10 +87,12 @@ export class GitClient {
8787
* info failed commands.
8888
*/
8989
runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns<string> {
90-
// To improve the infoging experience in case something fails, we print all executed
91-
// Git commands. Note that we do not want to print the token if is contained in the
92-
// command. It's common to share errors with others if the tool failed.
93-
info('Executing: git', this.omitGithubTokenFromMessage(args.join(' ')));
90+
// To improve the debugging experience in case something fails, we print all executed Git
91+
// commands unless the `stdio` is explicitly to `ignore` (which is equivalent to silent).
92+
// Note that we do not want to print the token if is contained in the command. It's common
93+
// to share errors with others if the tool failed, and we do not want to leak tokens.
94+
const printFn = options.stdio !== 'ignore' ? info : debug;
95+
printFn('Executing: git', this.omitGithubTokenFromMessage(args.join(' ')));
9496

9597
const result = spawnSync('git', args, {
9698
cwd: this._projectRoot,

0 commit comments

Comments
 (0)