GitHub - update authentication flow for branch protection (#181924)

* GitHub - tweak authentication request

* Add tracing messages
This commit is contained in:
Ladislau Szomoru
2023-05-09 20:10:00 +02:00
committed by GitHub
parent d4632bb740
commit 3dd678d2b0
2 changed files with 40 additions and 19 deletions

View File

@@ -10,6 +10,8 @@ import { Octokit } from '@octokit/rest';
import { httpsOverHttp } from 'tunnel';
import { URL } from 'url';
export class AuthenticationError extends Error { }
function getAgent(url: string | undefined = process.env.HTTPS_PROXY): Agent {
if (!url) {
return globalAgent;
@@ -57,25 +59,30 @@ export function getOctokit(): Promise<Octokit> {
let _octokitGraphql: Promise<graphql> | undefined;
export function getOctokitGraphql(): Promise<graphql> {
export async function getOctokitGraphql(): Promise<graphql> {
if (!_octokitGraphql) {
_octokitGraphql = getSession()
.then(async session => {
const token = session.accessToken;
const { graphql } = await import('@octokit/graphql');
try {
const session = await authentication.getSession('github', scopes, { createIfNone: false });
return graphql.defaults({
headers: {
authorization: `token ${token}`
},
request: {
agent: getAgent()
}
});
}).then(null, async err => {
_octokitGraphql = undefined;
throw err;
if (!session) {
throw new AuthenticationError('No GitHub authentication session available.');
}
const token = session.accessToken;
const { graphql } = await import('@octokit/graphql');
return graphql.defaults({
headers: {
authorization: `token ${token}`
},
request: {
agent: getAgent()
}
});
} catch (err) {
_octokitGraphql = undefined;
throw err;
}
}
return _octokitGraphql;