Skip to content

Commit

Permalink
refactor: check GH auth token env var in getGitHubAuthToken (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 committed Aug 10, 2023
1 parent 9da8198 commit 7ac4cc0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ci/ci-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ program
.trim();

const octokit = new Octokit({
auth: process.env.ELECTRON_BUILD_TOOLS_GH_AUTH || (await getGitHubAuthToken(['repo'])),
auth: await getGitHubAuthToken(['repo']),
});

const ref = options.ref ? parseRef(options.ref) : currentRef;
Expand Down
2 changes: 1 addition & 1 deletion src/e-backport.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program
}

const octokit = new Octokit({
auth: process.env.ELECTRON_BUILD_TOOLS_GH_AUTH || (await getGitHubAuthToken(['repo'])),
auth: await getGitHubAuthToken(['repo']),
});
const { data: user } = await octokit.users.getAuthenticated();
const { data: pr } = await octokit.pulls.get({
Expand Down
2 changes: 1 addition & 1 deletion src/e-cherry-pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ program
targetBranch = tmp;
}
const octokit = new Octokit({
auth: process.env.ELECTRON_BUILD_TOOLS_GH_AUTH || (await getGitHubAuthToken(['repo'])),
auth: await getGitHubAuthToken(['repo']),
});
try {
const {
Expand Down
4 changes: 2 additions & 2 deletions src/e-gh-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const d = require('debug')('build-tools:gh-auth');
const program = require('commander');

const { getGitHubAuthToken } = require('./utils/github-auth');
const { createGitHubAuthToken } = require('./utils/github-auth');
const { fatal } = require('./utils/logging');

program
Expand All @@ -12,7 +12,7 @@ program
.allowExcessArguments(false)
.action(async ({ shell }) => {
try {
const token = await getGitHubAuthToken(['repo']);
const token = await createGitHubAuthToken(['repo']);
if (shell) {
console.log(`export ELECTRON_BUILD_TOOLS_GH_AUTH="${token}"`);
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/github-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { createOAuthDeviceAuth } = require('@octokit/auth-oauth-device');
const ELECTRON_BUILD_TOOLS_GITHUB_CLIENT_ID = '03581ca0d21228704ab3';

async function getGitHubAuthToken(scopes = []) {
if (process.env.ELECTRON_BUILD_TOOLS_GH_AUTH) {
return process.env.ELECTRON_BUILD_TOOLS_GH_AUTH;
}

try {
const gh = spawn('gh', ['auth', 'status', '--show-token']);
const done = new Promise((resolve, reject) => {
Expand All @@ -23,6 +27,10 @@ async function getGitHubAuthToken(scopes = []) {
} catch (e) {
// fall through to fetching the token through oauth
}
return await createGitHubAuthToken(scopes);
}

async function createGitHubAuthToken(scopes = []) {
const auth = createOAuthDeviceAuth({
clientType: 'oauth-app',
clientId: ELECTRON_BUILD_TOOLS_GITHUB_CLIENT_ID,
Expand All @@ -40,5 +48,6 @@ async function getGitHubAuthToken(scopes = []) {
}

module.exports = {
createGitHubAuthToken,
getGitHubAuthToken,
};

0 comments on commit 7ac4cc0

Please sign in to comment.