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:
parent
e860e1f21f
commit
f943bebc15
|
@ -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,
|
||||||
|
|
|
@ -5,46 +5,47 @@ import { getLatestGithubReleaseVersion } from '../repo';
|
||||||
|
|
||||||
jest.mock( '../api', () => {
|
jest.mock( '../api', () => {
|
||||||
return {
|
return {
|
||||||
graphqlWithAuth: jest.fn().mockResolvedValue( {
|
graphqlWithAuth: () =>
|
||||||
repository: {
|
jest.fn().mockResolvedValue( {
|
||||||
releases: {
|
repository: {
|
||||||
nodes: [
|
releases: {
|
||||||
{
|
nodes: [
|
||||||
tagName: 'nightly',
|
{
|
||||||
isLatest: false,
|
tagName: 'nightly',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: 'wc-beta-tester-99.99.0',
|
{
|
||||||
isLatest: false,
|
tagName: 'wc-beta-tester-99.99.0',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: '1.0.0',
|
{
|
||||||
isLatest: false,
|
tagName: '1.0.0',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: '1.1.0',
|
{
|
||||||
isLatest: false,
|
tagName: '1.1.0',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: '1.2.0',
|
{
|
||||||
isLatest: false,
|
tagName: '1.2.0',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: '2.0.0',
|
{
|
||||||
isLatest: false,
|
tagName: '2.0.0',
|
||||||
},
|
isLatest: false,
|
||||||
{
|
},
|
||||||
tagName: '2.0.1',
|
{
|
||||||
isLatest: true,
|
tagName: '2.0.1',
|
||||||
},
|
isLatest: true,
|
||||||
{
|
},
|
||||||
tagName: '1.0.1',
|
{
|
||||||
isLatest: false,
|
tagName: '1.0.1',
|
||||||
},
|
isLatest: false,
|
||||||
],
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
} ),
|
||||||
} ),
|
|
||||||
};
|
};
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
headers: {
|
let octokitWithAuthInstance;
|
||||||
authorization: `Bearer ${ getEnvVar( 'GITHUB_TOKEN', true ) }`,
|
|
||||||
},
|
|
||||||
} );
|
|
||||||
|
|
||||||
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;
|
||||||
|
};
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue