Skip to content

Commit

Permalink
[cli] Adopt GitHub fine-graned token (#8)
Browse files Browse the repository at this point in the history
* add --fg-token cli flag

* add  prefix to token section
  • Loading branch information
harumaxy authored Dec 21, 2022
1 parent 99b4154 commit 33c0511
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ import * as zx from 'zx';
import { insideDir } from '../utils/filesystem.js';
import { Submodule } from '../utils/submodules.js';

const convertToAuthURL = (url: string, githubToken: string): string => {
const convertToAuthURL = (url: string, githubToken: string, isFineGrained: boolean): string => {
const parsed = GitUrlParse(url);
parsed.token = githubToken;
parsed.token = isFineGrained ? `oauth2:${githubToken}` : githubToken;
return GitUrlParse.stringify(parsed, 'https');
};

type CloneOptions = {
githubToken: string;
isFineGrained: boolean;
depth: number;
submodules: Submodule[];
};
export const clone = async ({ githubToken, depth, submodules }: CloneOptions) => {
export const clone = async ({ githubToken, isFineGrained, depth, submodules }: CloneOptions) => {
const topLevel = (await zx.$`git rev-parse --show-toplevel`).stdout.trim();

for (const submodule of submodules) {
const submoduleDir = path.join(topLevel, submodule.gitModulePath);
const submoduleURL = !!githubToken ? convertToAuthURL(submodule.url, githubToken) : submodule.url;
const submoduleURL = !!githubToken ? convertToAuthURL(submodule.url, githubToken, isFineGrained) : submodule.url;

await zx.$`rm -rf ${submoduleDir}`.catch(() => {
/* ignored */
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const OPTIONS = {
'--paths': 'paths',
'--depth': 'depth',
'--token': 'token',
'--fg-token': 'fgToken',
} as const;
type OptionAlias = keyof typeof OPTIONS;
type Option = typeof OPTIONS[OptionAlias];
Expand Down Expand Up @@ -43,8 +44,12 @@ const main = async () => {
zx.$.verbose = !!parsedOptions.verbose;

let githubToken: string = '';
let isFineGrained: boolean = false;
if (parsedOptions.token?.[0]) {
[githubToken] = parsedOptions.token;
} else if (parsedOptions.fgToken?.[0]) {
[githubToken] = parsedOptions.fgToken;
isFineGrained = true;
} else {
githubToken = process.env.GITHUB_TOKEN || '';
}
Expand All @@ -61,7 +66,7 @@ const main = async () => {
const submodules = await fetchSubmodules({
paths: specificPaths.length > 0 ? specificPaths : null,
});
await clone({ githubToken, depth, submodules });
await clone({ githubToken, isFineGrained, depth, submodules });
};

main().catch((error) => {
Expand Down

0 comments on commit 33c0511

Please sign in to comment.