2020-05-05 18:37:31 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-05-05 18:37:31 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-05-27 18:39:31 -04:00
|
|
|
import * as Octokit from '@octokit/rest';
|
|
|
|
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
|
2020-05-05 18:37:31 -04:00
|
|
|
|
2020-06-15 12:20:05 -04:00
|
|
|
import {getConfig, getRepoBaseDir, NgDevConfig} from '../config';
|
2020-09-01 04:43:26 -04:00
|
|
|
import {debug, info, yellow} from '../console';
|
2020-07-24 11:59:12 -04:00
|
|
|
import {GithubClient} from './github';
|
2020-09-01 04:39:35 -04:00
|
|
|
import {getRepositoryGitUrl, GITHUB_TOKEN_GENERATE_URL, GITHUB_TOKEN_SETTINGS_URL} from './github-urls';
|
2020-05-27 18:39:31 -04:00
|
|
|
|
|
|
|
/** Github response type extended to include the `x-oauth-scopes` headers presence. */
|
|
|
|
type RateLimitResponseWithOAuthScopeHeader = Octokit.Response<Octokit.RateLimitGetResponse>&{
|
|
|
|
headers: {'x-oauth-scopes': string};
|
|
|
|
};
|
|
|
|
|
2020-06-24 18:40:15 -04:00
|
|
|
/** Describes a function that can be used to test for given Github OAuth scopes. */
|
|
|
|
export type OAuthScopeTestFunction = (scopes: string[], missing: string[]) => void;
|
|
|
|
|
2020-05-27 18:39:31 -04:00
|
|
|
/** Error for failed Git commands. */
|
|
|
|
export class GitCommandError extends Error {
|
|
|
|
constructor(client: GitClient, public args: string[]) {
|
|
|
|
// Errors are not guaranteed to be caught. To ensure that we don't
|
|
|
|
// accidentally leak the Github token that might be used in a command,
|
|
|
|
// we sanitize the command that will be part of the error message.
|
|
|
|
super(`Command failed: git ${client.omitGithubTokenFromMessage(args.join(' '))}`);
|
|
|
|
}
|
2020-05-05 18:37:31 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:39:31 -04:00
|
|
|
/**
|
2020-09-09 08:44:14 -04:00
|
|
|
* Common client for performing Git interactions with a given remote.
|
2020-05-27 18:39:31 -04:00
|
|
|
*
|
2020-08-31 12:24:12 -04:00
|
|
|
* Takes in two optional arguments:
|
2020-09-09 08:44:14 -04:00
|
|
|
* `githubToken`: the token used for authentication in Github interactions, by default empty
|
2020-05-27 18:39:31 -04:00
|
|
|
* allowing readonly actions.
|
2020-09-09 08:44:14 -04:00
|
|
|
* `config`: The dev-infra configuration containing information about the remote. By default
|
|
|
|
* the dev-infra configuration is loaded with its Github configuration.
|
2020-05-27 18:39:31 -04:00
|
|
|
**/
|
|
|
|
export class GitClient {
|
2020-10-28 13:07:48 -04:00
|
|
|
/** Whether verbose logging of Git actions should be used. */
|
|
|
|
static LOG_COMMANDS = true;
|
2020-09-09 08:44:14 -04:00
|
|
|
/** Short-hand for accessing the default remote configuration. */
|
2020-05-27 18:39:31 -04:00
|
|
|
remoteConfig = this._config.github;
|
|
|
|
/** Octokit request parameters object for targeting the configured remote. */
|
|
|
|
remoteParams = {owner: this.remoteConfig.owner, repo: this.remoteConfig.name};
|
2020-09-01 04:39:35 -04:00
|
|
|
/** Git URL that resolves to the configured repository. */
|
2020-09-01 04:43:26 -04:00
|
|
|
repoGitUrl = getRepositoryGitUrl(this.remoteConfig, this.githubToken);
|
2020-05-27 18:39:31 -04:00
|
|
|
/** Instance of the authenticated Github octokit API. */
|
2020-09-01 04:43:26 -04:00
|
|
|
github = new GithubClient(this.githubToken);
|
2020-05-27 18:39:31 -04:00
|
|
|
|
|
|
|
/** The OAuth scopes available for the provided Github token. */
|
2020-09-01 04:43:26 -04:00
|
|
|
private _cachedOauthScopes: Promise<string[]>|null = null;
|
2020-06-05 17:32:42 -04:00
|
|
|
/**
|
|
|
|
* Regular expression that matches the provided Github token. Used for
|
|
|
|
* sanitizing the token from Git child process output.
|
|
|
|
*/
|
|
|
|
private _githubTokenRegex: RegExp|null = null;
|
2020-05-27 18:39:31 -04:00
|
|
|
|
|
|
|
constructor(
|
2020-09-01 04:43:26 -04:00
|
|
|
public githubToken?: string, private _config: Pick<NgDevConfig, 'github'> = getConfig(),
|
2020-07-24 11:59:12 -04:00
|
|
|
private _projectRoot = getRepoBaseDir()) {
|
2020-06-05 17:32:42 -04:00
|
|
|
// 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.
|
2020-09-01 04:43:26 -04:00
|
|
|
if (githubToken != null) {
|
|
|
|
this._githubTokenRegex = new RegExp(githubToken, 'g');
|
2020-06-05 17:32:42 -04:00
|
|
|
}
|
2020-05-27 18:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Executes the given git command. Throws if the command fails. */
|
|
|
|
run(args: string[], options?: SpawnSyncOptions): Omit<SpawnSyncReturns<string>, 'status'> {
|
|
|
|
const result = this.runGraceful(args, options);
|
|
|
|
if (result.status !== 0) {
|
|
|
|
throw new GitCommandError(this, args);
|
|
|
|
}
|
|
|
|
// Omit `status` from the type so that it's obvious that the status is never
|
|
|
|
// non-zero as explained in the method description.
|
|
|
|
return result as Omit<SpawnSyncReturns<string>, 'status'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spawns a given Git command process. Does not throw if the command fails. Additionally,
|
|
|
|
* if there is any stderr output, the output will be printed. This makes it easier to
|
2020-08-26 16:49:43 -04:00
|
|
|
* info failed commands.
|
2020-05-27 18:39:31 -04:00
|
|
|
*/
|
|
|
|
runGraceful(args: string[], options: SpawnSyncOptions = {}): SpawnSyncReturns<string> {
|
2020-09-01 04:43:26 -04:00
|
|
|
// To improve the debugging experience in case something fails, we print all executed Git
|
2020-10-28 13:07:48 -04:00
|
|
|
// commands to better understand the git actions occuring. Depending on the command being
|
|
|
|
// executed, this debugging information should be logged at different logging levels.
|
|
|
|
const printFn = (!GitClient.LOG_COMMANDS || options.stdio === 'ignore') ? debug : info;
|
2020-10-10 15:02:47 -04:00
|
|
|
// Note that we do not want to print the token if it is contained in the command. It's common
|
2020-09-01 04:43:26 -04:00
|
|
|
// to share errors with others if the tool failed, and we do not want to leak tokens.
|
|
|
|
printFn('Executing: git', this.omitGithubTokenFromMessage(args.join(' ')));
|
2020-05-27 18:39:31 -04:00
|
|
|
|
|
|
|
const result = spawnSync('git', args, {
|
|
|
|
cwd: this._projectRoot,
|
|
|
|
stdio: 'pipe',
|
|
|
|
...options,
|
|
|
|
// Encoding is always `utf8` and not overridable. This ensures that this method
|
|
|
|
// always returns `string` as output instead of buffers.
|
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.stderr !== null) {
|
|
|
|
// Git sometimes prints the command if it failed. This means that it could
|
|
|
|
// potentially leak the Github token used for accessing the remote. To avoid
|
|
|
|
// printing a token, we sanitize the string before printing the stderr output.
|
|
|
|
process.stderr.write(this.omitGithubTokenFromMessage(result.stderr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Whether the given branch contains the specified SHA. */
|
|
|
|
hasCommit(branchName: string, sha: string): boolean {
|
|
|
|
return this.run(['branch', branchName, '--contains', sha]).stdout !== '';
|
|
|
|
}
|
|
|
|
|
2020-06-25 10:15:14 -04:00
|
|
|
/** Gets the currently checked out branch or revision. */
|
|
|
|
getCurrentBranchOrRevision(): string {
|
|
|
|
const branchName = this.run(['rev-parse', '--abbrev-ref', 'HEAD']).stdout.trim();
|
|
|
|
// If no branch name could be resolved. i.e. `HEAD` has been returned, then Git
|
|
|
|
// is currently in a detached state. In those cases, we just want to return the
|
|
|
|
// currently checked out revision/SHA.
|
|
|
|
if (branchName === 'HEAD') {
|
|
|
|
return this.run(['rev-parse', 'HEAD']).stdout.trim();
|
|
|
|
}
|
|
|
|
return branchName;
|
2020-05-27 18:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets whether the current Git repository has uncommitted changes. */
|
|
|
|
hasUncommittedChanges(): boolean {
|
|
|
|
return this.runGraceful(['diff-index', '--quiet', 'HEAD']).status !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Whether the repo has any local changes. */
|
|
|
|
hasLocalChanges(): boolean {
|
2020-06-05 17:49:45 -04:00
|
|
|
return this.runGraceful(['diff-index', '--quiet', 'HEAD']).status !== 0;
|
2020-05-27 18:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sanitizes a given message by omitting the provided Github token if present. */
|
|
|
|
omitGithubTokenFromMessage(value: string): string {
|
2020-06-05 17:32:42 -04:00
|
|
|
// If no token has been defined (i.e. no token regex), we just return the
|
|
|
|
// value as is. There is no secret value that needs to be omitted.
|
|
|
|
if (this._githubTokenRegex === null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return value.replace(this._githubTokenRegex, '<TOKEN>');
|
2020-05-27 18:39:31 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 19:49:07 -04:00
|
|
|
/**
|
|
|
|
* Checks out a requested branch or revision, optionally cleaning the state of the repository
|
|
|
|
* before attempting the checking. Returns a boolean indicating whether the branch or revision
|
|
|
|
* was cleanly checked out.
|
|
|
|
*/
|
|
|
|
checkout(branchOrRevision: string, cleanState: boolean): boolean {
|
|
|
|
if (cleanState) {
|
|
|
|
// Abort any outstanding ams.
|
|
|
|
this.runGraceful(['am', '--abort'], {stdio: 'ignore'});
|
|
|
|
// Abort any outstanding cherry-picks.
|
|
|
|
this.runGraceful(['cherry-pick', '--abort'], {stdio: 'ignore'});
|
|
|
|
// Abort any outstanding rebases.
|
|
|
|
this.runGraceful(['rebase', '--abort'], {stdio: 'ignore'});
|
|
|
|
// Clear any changes in the current repo.
|
|
|
|
this.runGraceful(['reset', '--hard'], {stdio: 'ignore'});
|
|
|
|
}
|
|
|
|
return this.runGraceful(['checkout', branchOrRevision], {stdio: 'ignore'}).status === 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:39:31 -04:00
|
|
|
/**
|
|
|
|
* Assert the GitClient instance is using a token with permissions for the all of the
|
|
|
|
* provided OAuth scopes.
|
|
|
|
*/
|
2020-06-24 18:40:15 -04:00
|
|
|
async hasOauthScopes(testFn: OAuthScopeTestFunction): Promise<true|{error: string}> {
|
2020-05-27 18:39:31 -04:00
|
|
|
const scopes = await this.getAuthScopesForToken();
|
2020-06-24 18:40:15 -04:00
|
|
|
const missingScopes: string[] = [];
|
|
|
|
// Test Github OAuth scopes and collect missing ones.
|
|
|
|
testFn(scopes, missingScopes);
|
2020-05-27 18:39:31 -04:00
|
|
|
// If no missing scopes are found, return true to indicate all OAuth Scopes are available.
|
|
|
|
if (missingScopes.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preconstructed error message to log to the user, providing missing scopes and
|
|
|
|
* remediation instructions.
|
|
|
|
**/
|
|
|
|
const error =
|
|
|
|
`The provided <TOKEN> does not have required permissions due to missing scope(s): ` +
|
|
|
|
`${yellow(missingScopes.join(', '))}\n\n` +
|
|
|
|
`Update the token in use at:\n` +
|
2020-09-01 04:39:35 -04:00
|
|
|
` ${GITHUB_TOKEN_SETTINGS_URL}\n\n` +
|
|
|
|
`Alternatively, a new token can be created at: ${GITHUB_TOKEN_GENERATE_URL}\n`;
|
2020-05-27 18:39:31 -04:00
|
|
|
|
|
|
|
return {error};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-15 12:20:05 -04:00
|
|
|
* Retrieve the OAuth scopes for the loaded Github token.
|
2020-05-27 18:39:31 -04:00
|
|
|
**/
|
2020-10-10 15:02:47 -04:00
|
|
|
private getAuthScopesForToken() {
|
2020-05-27 18:39:31 -04:00
|
|
|
// If the OAuth scopes have already been loaded, return the Promise containing them.
|
2020-09-01 04:39:35 -04:00
|
|
|
if (this._cachedOauthScopes !== null) {
|
|
|
|
return this._cachedOauthScopes;
|
2020-05-27 18:39:31 -04:00
|
|
|
}
|
|
|
|
// OAuth scopes are loaded via the /rate_limit endpoint to prevent
|
|
|
|
// usage of a request against that rate_limit for this lookup.
|
2020-09-01 04:39:35 -04:00
|
|
|
return this._cachedOauthScopes = this.github.rateLimit.get().then(_response => {
|
2020-05-27 18:39:31 -04:00
|
|
|
const response = _response as RateLimitResponseWithOAuthScopeHeader;
|
|
|
|
const scopes: string = response.headers['x-oauth-scopes'] || '';
|
|
|
|
return scopes.split(',').map(scope => scope.trim());
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 18:37:31 -04:00
|
|
|
}
|