67f65a9d25
Currently the `GitClient` accepts a generic parameter for determining
whether the `githubToken` should be set or not. This worked fine so far
in terms of distinguishing between an authenticated and
non-authenticated git client instance, but if we intend to conditionally
show methods only for authenticated instances, the generic parameter
is not suitable.
This commit splits up the `GitClient` into two classes. One for
the base logic without any authorization, and a second class that
extends the base logic with authentication logic. i.e. the
`AuthenticatedGitClient`. This allows us to have specific methods only
for the authenticated instance. e.g.
* `hasOauthScopes` has been moved to only exist for authenticated
instances.
* the GraphQL functionality within `gitClient.github` is not
accessible for non-authenticated instances. GraphQL API requires
authentication as per Github.
The initial motiviation for this was that we want to throw if
`hasOAuthScopes` is called without the Octokit instance having
a token configured. This should help avoiding issues as within
3b434ed94d
that prevented the caretaker process momentarily.
Additionally, the Git client has moved from `index.ts` to
`git-client.ts` for better discoverability in the codebase.
PR Close #42468
45 lines
1.8 KiB
TypeScript
45 lines
1.8 KiB
TypeScript
/**
|
|
* @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
|
|
*/
|
|
|
|
import {Argv} from 'yargs';
|
|
|
|
import {error, red, yellow} from '../console';
|
|
|
|
import {AuthenticatedGitClient} from './authenticated-git-client';
|
|
import {GITHUB_TOKEN_GENERATE_URL} from './github-urls';
|
|
|
|
export type ArgvWithGithubToken = Argv<{githubToken: string}>;
|
|
|
|
/** Sets up the `github-token` command option for the given Yargs instance. */
|
|
export function addGithubTokenOption(yargs: Argv): ArgvWithGithubToken {
|
|
return yargs
|
|
// 'github-token' is casted to 'githubToken' to properly set up typings to reflect the key in
|
|
// the Argv object being camelCase rather than kebab case due to the `camel-case-expansion`
|
|
// config: https://github.com/yargs/yargs-parser#camel-case-expansion
|
|
.option('github-token' as 'githubToken', {
|
|
type: 'string',
|
|
description: 'Github token. If not set, token is retrieved from the environment variables.',
|
|
coerce: (token: string) => {
|
|
const githubToken = token || process.env.GITHUB_TOKEN || process.env.TOKEN;
|
|
if (!githubToken) {
|
|
error(red('No Github token set. Please set the `GITHUB_TOKEN` environment variable.'));
|
|
error(red('Alternatively, pass the `--github-token` command line flag.'));
|
|
error(yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`));
|
|
process.exit(1);
|
|
}
|
|
try {
|
|
AuthenticatedGitClient.get();
|
|
} catch {
|
|
AuthenticatedGitClient.configure(githubToken);
|
|
}
|
|
return githubToken;
|
|
},
|
|
})
|
|
.default('github-token' as 'githubToken', '', '<LOCAL TOKEN>');
|
|
}
|