Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow unauthentiated GitHub API usage #4666

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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