Don't generate singletons for Octokit and GraphQL until they're used at runtime. (#38268)

* Move API instance generation into functions to ensure they don't run on import.

* Dont use extraneous variables, call singleton fns directly.

* Get rid of accidental change
This commit is contained in:
Sam Seay 2023-05-16 10:41:37 +12:00 committed by GitHub
parent e860e1f21f
commit f943bebc15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 56 deletions

View File

@ -89,7 +89,7 @@ export const milestoneCommand = new Command( 'milestone' )
} }
try { try {
await octokitWithAuth.request( await octokitWithAuth().request(
`POST /repos/${ owner }/${ name }/milestones`, `POST /repos/${ owner }/${ name }/milestones`,
{ {
title: nextMilestone, title: nextMilestone,

View File

@ -5,7 +5,8 @@ import { getLatestGithubReleaseVersion } from '../repo';
jest.mock( '../api', () => { jest.mock( '../api', () => {
return { return {
graphqlWithAuth: jest.fn().mockResolvedValue( { graphqlWithAuth: () =>
jest.fn().mockResolvedValue( {
repository: { repository: {
releases: { releases: {
nodes: [ nodes: [

View File

@ -1,20 +1,52 @@
/** /**
* External dependencies * External dependencies
*/ */
import { graphql } from '@octokit/graphql'; import { graphql as gql } from '@octokit/graphql';
import { Octokit } from 'octokit'; import { Octokit } from 'octokit';
import { graphql } from '@octokit/graphql/dist-types/types';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { getEnvVar } from '../environment'; import { getEnvVar } from '../environment';
export const graphqlWithAuth = graphql.defaults( { let graphqlWithAuthInstance;
let octokitWithAuthInstance;
/**
* Returns a graphql instance with auth headers, throws an Exception if
* `GITHUB_TOKEN` env var is not present.
*
* @return graphql instance
*/
export const graphqlWithAuth = (): graphql => {
if ( graphqlWithAuthInstance ) {
return graphqlWithAuthInstance;
}
graphqlWithAuthInstance = gql.defaults( {
headers: { headers: {
authorization: `Bearer ${ getEnvVar( 'GITHUB_TOKEN', true ) }`, authorization: `Bearer ${ getEnvVar( 'GITHUB_TOKEN', true ) }`,
}, },
} ); } );
export const octokitWithAuth = new Octokit( { return graphqlWithAuthInstance;
};
/**
* Returns an Octokit instance with auth headers, throws an Exception if
* `GITHUB_TOKEN` env var is not present.
*
* @return graphql instance
*/
export const octokitWithAuth = (): Octokit => {
if ( octokitWithAuthInstance ) {
return octokitWithAuthInstance;
}
octokitWithAuthInstance = new Octokit( {
auth: getEnvVar( 'GITHUB_TOKEN', true ), auth: getEnvVar( 'GITHUB_TOKEN', true ),
} ); } );
return octokitWithAuthInstance;
};

View File

@ -7,7 +7,6 @@ import { Repository } from '@octokit/graphql-schema';
* Internal dependencies * Internal dependencies
*/ */
import { graphqlWithAuth, octokitWithAuth } from './api'; import { graphqlWithAuth, octokitWithAuth } from './api';
import { Logger } from '../logger';
import { PullRequestEndpointResponse } from './types'; import { PullRequestEndpointResponse } from './types';
export const getLatestGithubReleaseVersion = async ( options: { export const getLatestGithubReleaseVersion = async ( options: {
@ -16,7 +15,7 @@ export const getLatestGithubReleaseVersion = async ( options: {
} ): Promise< string > => { } ): Promise< string > => {
const { owner, name } = options; const { owner, name } = options;
const data = await graphqlWithAuth< { repository: Repository } >( ` const data = await graphqlWithAuth()< { repository: Repository } >( `
{ {
repository(owner: "${ owner }", name: "${ name }") { repository(owner: "${ owner }", name: "${ name }") {
releases( releases(
@ -45,8 +44,9 @@ export const doesGithubBranchExist = async (
nextReleaseBranch: string nextReleaseBranch: string
): Promise< boolean > => { ): Promise< boolean > => {
const { owner, name } = options; const { owner, name } = options;
try { try {
const branchOnGithub = await octokitWithAuth.request( const branchOnGithub = await octokitWithAuth().request(
'GET /repos/{owner}/{repo}/branches/{branch}', 'GET /repos/{owner}/{repo}/branches/{branch}',
{ {
owner, owner,
@ -74,7 +74,7 @@ export const getRefFromGithubBranch = async (
source: string source: string
): Promise< string > => { ): Promise< string > => {
const { owner, name } = options; const { owner, name } = options;
const { repository } = await graphqlWithAuth< { const { repository } = await graphqlWithAuth()< {
repository: Repository; repository: Repository;
} >( ` } >( `
{ {
@ -105,7 +105,7 @@ export const createGithubBranch = async (
ref: string ref: string
): Promise< void > => { ): Promise< void > => {
const { owner, name } = options; const { owner, name } = options;
await octokitWithAuth.request( 'POST /repos/{owner}/{repo}/git/refs', { await octokitWithAuth().request( 'POST /repos/{owner}/{repo}/git/refs', {
owner, owner,
repo: name, repo: name,
ref: `refs/heads/${ branch }`, ref: `refs/heads/${ branch }`,
@ -121,7 +121,7 @@ export const deleteGithubBranch = async (
branch: string branch: string
): Promise< void > => { ): Promise< void > => {
const { owner, name } = options; const { owner, name } = options;
await octokitWithAuth.request( await octokitWithAuth().request(
'DELETE /repos/{owner}/{repo}/git/refs/heads/{ref}', 'DELETE /repos/{owner}/{repo}/git/refs/heads/{ref}',
{ {
owner, owner,
@ -152,7 +152,7 @@ export const createPullRequest = async ( options: {
body: string; body: string;
} ): Promise< PullRequestEndpointResponse[ 'data' ] > => { } ): Promise< PullRequestEndpointResponse[ 'data' ] > => {
const { head, base, owner, name, title, body } = options; const { head, base, owner, name, title, body } = options;
const pullRequest = await octokitWithAuth.request( const pullRequest = await octokitWithAuth().request(
'POST /repos/{owner}/{repo}/pulls', 'POST /repos/{owner}/{repo}/pulls',
{ {
owner, owner,
@ -163,6 +163,7 @@ export const createPullRequest = async ( options: {
base, base,
} }
); );
//@ts-ignore There is a type mismatch between the graphql schema and the response. pullRequest.data.head.repo.has_discussions is a boolean, but the graphql schema doesn't have that field. //@ts-ignore There is a type mismatch between the graphql schema and the response. pullRequest.data.head.repo.has_discussions is a boolean, but the graphql schema doesn't have that field.
return pullRequest.data; return pullRequest.data;
}; };