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 {
|
||||
await octokitWithAuth.request(
|
||||
await octokitWithAuth().request(
|
||||
`POST /repos/${ owner }/${ name }/milestones`,
|
||||
{
|
||||
title: nextMilestone,
|
||||
|
|
|
@ -5,7 +5,8 @@ import { getLatestGithubReleaseVersion } from '../repo';
|
|||
|
||||
jest.mock( '../api', () => {
|
||||
return {
|
||||
graphqlWithAuth: jest.fn().mockResolvedValue( {
|
||||
graphqlWithAuth: () =>
|
||||
jest.fn().mockResolvedValue( {
|
||||
repository: {
|
||||
releases: {
|
||||
nodes: [
|
||||
|
|
|
@ -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( {
|
||||
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: {
|
||||
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 ),
|
||||
} );
|
||||
|
||||
return octokitWithAuthInstance;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue