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 {
await octokitWithAuth.request(
await octokitWithAuth().request(
`POST /repos/${ owner }/${ name }/milestones`,
{
title: nextMilestone,

View File

@ -5,46 +5,47 @@ import { getLatestGithubReleaseVersion } from '../repo';
jest.mock( '../api', () => {
return {
graphqlWithAuth: jest.fn().mockResolvedValue( {
repository: {
releases: {
nodes: [
{
tagName: 'nightly',
isLatest: false,
},
{
tagName: 'wc-beta-tester-99.99.0',
isLatest: false,
},
{
tagName: '1.0.0',
isLatest: false,
},
{
tagName: '1.1.0',
isLatest: false,
},
{
tagName: '1.2.0',
isLatest: false,
},
{
tagName: '2.0.0',
isLatest: false,
},
{
tagName: '2.0.1',
isLatest: true,
},
{
tagName: '1.0.1',
isLatest: false,
},
],
graphqlWithAuth: () =>
jest.fn().mockResolvedValue( {
repository: {
releases: {
nodes: [
{
tagName: 'nightly',
isLatest: false,
},
{
tagName: 'wc-beta-tester-99.99.0',
isLatest: false,
},
{
tagName: '1.0.0',
isLatest: false,
},
{
tagName: '1.1.0',
isLatest: false,
},
{
tagName: '1.2.0',
isLatest: false,
},
{
tagName: '2.0.0',
isLatest: false,
},
{
tagName: '2.0.1',
isLatest: true,
},
{
tagName: '1.0.1',
isLatest: false,
},
],
},
},
},
} ),
} ),
};
} );

View File

@ -1,20 +1,52 @@
/**
* External dependencies
*/
import { graphql } from '@octokit/graphql';
import { graphql as gql } from '@octokit/graphql';
import { Octokit } from 'octokit';
import { graphql } from '@octokit/graphql/dist-types/types';
/**
* Internal dependencies
*/
import { getEnvVar } from '../environment';
export const graphqlWithAuth = graphql.defaults( {
headers: {
authorization: `Bearer ${ getEnvVar( 'GITHUB_TOKEN', true ) }`,
},
} );
let graphqlWithAuthInstance;
let octokitWithAuthInstance;
export const octokitWithAuth = new Octokit( {
auth: getEnvVar( 'GITHUB_TOKEN', true ),
} );
/**
* 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: {
authorization: `Bearer ${ getEnvVar( 'GITHUB_TOKEN', true ) }`,
},
} );
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 ),
} );
return octokitWithAuthInstance;
};

View File

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