Skip to content

Commit

Permalink
feat: allow unauthentiated GitHub API usage (#4666)
Browse files Browse the repository at this point in the history
* feat: allow unauthentiated GitHub API usage

* refactor: move into a helper method
  • Loading branch information
chingor13 authored Nov 8, 2022
1 parent 24bb713 commit 4dd4b8d
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions packages/mono-repo-publish/src/bin/mono-repo-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as yargs from 'yargs';
import * as core from '../main';
import {Octokit} from '@octokit/rest';

interface CommonArgs {
'pr-url': string;
Expand Down Expand Up @@ -46,7 +47,6 @@ function parseCommonArgs(yargs: yargs.Argv): yargs.Argv<CommonArgs> {
return process.env.APP_ID_PATH;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
demand: true,
})
.option('private-key-path', {
describe: 'path to a file containing the GitHub private key',
Expand All @@ -55,7 +55,6 @@ function parseCommonArgs(yargs: yargs.Argv): yargs.Argv<CommonArgs> {
return process.env.GITHUB_PRIVATE_KEY_PATH;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
demand: true,
})
.option('installation-id-path', {
describe: 'path to a file containing the GitHub installation ID',
Expand All @@ -64,7 +63,6 @@ function parseCommonArgs(yargs: yargs.Argv): yargs.Argv<CommonArgs> {
return process.env.INSTALLATION_ID_PATH;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
demand: true,
})
.option('exclude-files', {
describe: 'glob of paths to exclude',
Expand All @@ -84,21 +82,8 @@ const publishCommand: yargs.CommandModule<{}, PublishArgs> = {
});
},
async handler(argv) {
const appIdPath = argv['app-id-path'];
const privateKeyPath = argv['private-key-path'];
const installationIdPath = argv['installation-id-path'];

if (!appIdPath || !privateKeyPath || !installationIdPath) {
throw Error(
'Need to set all of APP_ID_PATH, GITHUB_PRIVATE_KEY_PATH, INSTALLATION_ID_PATH'
);
}
const octokit = buildOctokit(argv);
const pr = core.parseURL(argv['pr-url']);
const octokit = core.getOctokitInstance(
appIdPath,
privateKeyPath,
installationIdPath
);
if (!pr) {
throw Error(`Could not find PR from ${argv.prUrl}`);
}
Expand All @@ -122,21 +107,8 @@ const publishCustomCommand: yargs.CommandModule<{}, PublishCustomArgs> = {
});
},
async handler(argv) {
const appIdPath = argv['app-id-path'];
const privateKeyPath = argv['private-key-path'];
const installationIdPath = argv['installation-id-path'];

if (!appIdPath || !privateKeyPath || !installationIdPath) {
throw Error(
'Need to set all of APP_ID_PATH, GITHUB_PRIVATE_KEY_PATH, INSTALLATION_ID_PATH'
);
}
const octokit = buildOctokit(argv);
const pr = core.parseURL(argv['pr-url']);
const octokit = core.getOctokitInstance(
appIdPath,
privateKeyPath,
installationIdPath
);
if (!pr) {
throw Error(`Could not find PR from ${argv.prUrl}`);
}
Expand All @@ -149,6 +121,24 @@ const publishCustomCommand: yargs.CommandModule<{}, PublishCustomArgs> = {
},
};

function buildOctokit(argv: CommonArgs): Octokit {
const appIdPath = argv['app-id-path'];
const privateKeyPath = argv['private-key-path'];
const installationIdPath = argv['installation-id-path'];
if (!appIdPath || !privateKeyPath || !installationIdPath) {
console.warn(
'Missing one of APP_ID_PATH, GITHUB_PRIVATE_KEY_PATH, INSTALLATION_ID_PATH. Using unauthenticated client.'
);
return new Octokit();
} else {
return core.getOctokitInstance(
appIdPath,
privateKeyPath,
installationIdPath
);
}
}

// Get testing repo that touches submodules that we would want to publish
// Once we have the list, actually calling npm publish on those modules
export const parser = yargs
Expand Down

0 comments on commit 4dd4b8d

Please sign in to comment.