Skip to content

Commit

Permalink
Show formatted options to the user (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Oct 16, 2023
1 parent 9e6695a commit 993d280
Show file tree
Hide file tree
Showing 31 changed files with 815 additions and 617 deletions.
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@
{
"type": "node",
"request": "launch",
"name": "yarn start (entrypoint.cli.ts)",
"name": "Run (basic)",
"program": "${workspaceRoot}/src/entrypoint.cli.ts",
"runtimeArgs": ["-r", "ts-node/register/transpile-only"],
"args": ["--repo=elastic/kibana"],
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Run (many args)",
"program": "${workspaceRoot}/src/entrypoint.cli.ts",
"runtimeArgs": ["-r", "ts-node/register/transpile-only"],
"args": ["--dryRun", "--repo=elastic/kibana", "--maxNumber=100"],

"console": "integratedTerminal"
},
{
"name": "Jest",
"type": "node",
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,29 @@
"@types/core-js": "^2.5.5",
"@types/dedent": "^0.7.0",
"@types/inquirer": "^8.2.6",
"@types/jest": "^29.5.4",
"@types/lodash": "^4.14.197",
"@types/node": "^20.5.4",
"@types/safe-json-stringify": "^1.1.2",
"@types/yargs": "^17.0.24",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"eslint": "^8.47.0",
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.199",
"@types/node": "^20.8.6",
"@types/safe-json-stringify": "^1.1.3",
"@types/yargs": "^17.0.28",
"@types/yargs-parser": "^21.0.1",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.2.3",
"eslint-plugin-prettier": "^5.0.0",
"graphql-config": "^5.0.2",
"eslint-plugin-jest": "^27.4.2",
"eslint-plugin-prettier": "^5.0.1",
"graphql-config": "^5.0.3",
"husky": "^8.0.3",
"jest": "^29.6.4",
"jest": "^29.7.0",
"jest-snapshot-serializer-ansi": "^1.0.0",
"lint-staged": "^14.0.1",
"nock": "^13.3.3",
"prettier": "^3.0.2",
"nock": "^13.3.4",
"prettier": "^3.0.3",
"strip-ansi": "^6.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "5.1.6"
"typescript": "5.2.2"
}
}
8 changes: 7 additions & 1 deletion src/backportRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
getOptionsFromCliArgs,
OptionsFromCliArgs,
} from './options/cliArgs';
import { getOptions, ValidConfigOptions } from './options/options';
import {
getActiveOptionsFormatted,
getOptions,
ValidConfigOptions,
} from './options/options';
import { runSequentially, Result } from './runSequentially';

export type BackportAbortResponse = {
Expand Down Expand Up @@ -103,6 +107,8 @@ export async function backportRun({
logger.info('Backporting options', options);
spinner.stop();

consoleLog(getActiveOptionsFormatted(options));

const commitsSpan = apm.startSpan(`Get commits`);
commits = await getCommits(options);
commitsSpan?.setLabel('commit_count', commits.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function getCommitsWithoutBackports({
const commitsInConflictingPaths = await fetchCommitsByAuthor({
...options,
author: null, // retrieve commits across all authors
maxNumber: 50,
dateSince: null,
dateUntil: commit.sourceCommit.committedDate,
commitPaths: conflictingFiles,
Expand Down
23 changes: 17 additions & 6 deletions src/lib/cherrypickAndCreateTargetPullRequest/waitForCherrypick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,23 @@ async function cherrypickAndHandleConflicts({
.map((f) => f.relative)
.slice(0, 50);

const commitsWithoutBackports = await getCommitsWithoutBackports({
options,
commit,
targetBranch,
conflictingFiles: conflictingFilesRelative,
});
let commitsWithoutBackports: Awaited<
ReturnType<typeof getCommitsWithoutBackports>
>;

try {
commitsWithoutBackports = await getCommitsWithoutBackports({
options,
commit,
targetBranch,
conflictingFiles: conflictingFilesRelative,
});
} catch (e) {
commitsWithoutBackports = [];
if (e instanceof Error) {
logger.warn(`Unable to fetch commits without backports: ${e.message}`);
}
}

if (!options.interactive) {
throw new BackportError({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/FetchPullRequestId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export async function fetchPullRequestId(
},
});

return prResponse.repository.pullRequest.id;
return prResponse.data.data.repository.pullRequest.id;
}
3 changes: 2 additions & 1 deletion src/lib/github/v4/apiRequestV4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('apiRequestV4', () => {
});

it('should return correct response', async () => {
expect(res).toEqual({ viewer: { login: 'sqren' } });
// @ts-expect-error
expect(res.data.data).toEqual({ viewer: { login: 'sqren' } });
});

it('should call with correct args', async () => {
Expand Down
21 changes: 2 additions & 19 deletions src/lib/github/v4/apiRequestV4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,9 @@ type ApiRequestOptions = {
variables?: Variables;
};

// Define the implementation signature
export async function apiRequestV4<DataResponse>(
opts: ApiRequestOptions,
): Promise<DataResponse>;

// Overload for returning AxiosResponse with fullResponse: true
// eslint-disable-next-line
export async function apiRequestV4<DataResponse>(
opts: ApiRequestOptions & { fullResponse: true },
): Promise<AxiosResponse<GithubV4Response<DataResponse>, any>>;

// Define the implementation
// eslint-disable-next-line
export async function apiRequestV4<DataResponse>(
opts: ApiRequestOptions & { fullResponse?: boolean },
): Promise<DataResponse | AxiosResponse<GithubV4Response<DataResponse>, any>> {
): Promise<AxiosResponse<GithubV4Response<DataResponse>, any>> {
const {
githubApiBaseUrlV4 = 'https://api.github.com/graphql',
accessToken,
Expand Down Expand Up @@ -90,11 +77,7 @@ export async function apiRequestV4<DataResponse>(

span?.setOutcome('success');

if (opts.fullResponse) {
return response;
}

return response.data.data;
return response;
} catch (e) {
span?.setOutcome('failure');

Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/disablePullRequestAutoMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ export async function disablePullRequestAutoMerge(
},
});

return res.disablePullRequestAutoMerge.pullRequest?.number;
return res.data.data.disablePullRequestAutoMerge.pullRequest?.number;
}
2 changes: 1 addition & 1 deletion src/lib/github/v4/enablePullRequestAutoMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function enablePullRequestAutoMerge(
},
});

return res.enablePullRequestAutoMerge.pullRequest?.number;
return res.data.data.enablePullRequestAutoMerge.pullRequest?.number;
}

export function parseGithubError(e: GithubV4Exception<any>) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/fetchAuthorId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ export async function fetchAuthorId({
variables: { author },
});

return res.user.id;
return res.data.data.user.id;
}
9 changes: 5 additions & 4 deletions src/lib/github/v4/fetchCommits/fetchCommitByPullNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export async function fetchCommitsByPullNumber(options: {
}
`;

let res: CommitByPullNumberResponse;
let data: CommitByPullNumberResponse;
try {
res = await apiRequestV4<CommitByPullNumberResponse>({
const res = await apiRequestV4<CommitByPullNumberResponse>({
githubApiBaseUrlV4,
accessToken,
query,
Expand All @@ -75,11 +75,12 @@ export async function fetchCommitsByPullNumber(options: {
pullNumber,
},
});
data = res.data.data;
} catch (e) {
res = swallowMissingConfigFileException<CommitByPullNumberResponse>(e);
data = swallowMissingConfigFileException<CommitByPullNumberResponse>(e);
}

const pullRequestNode = res.repository.pullRequest;
const pullRequestNode = data.repository.pullRequest;
if (!pullRequestNode) {
throw new BackportError(`The PR #${pullNumber} does not exist`);
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib/github/v4/fetchCommits/fetchCommitBySha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export async function fetchCommitBySha(options: {
${SourceCommitWithTargetPullRequestFragment}
`;

let res: CommitsByShaResponse;
let data: CommitsByShaResponse;
try {
res = await apiRequestV4<CommitsByShaResponse>({
const res = await apiRequestV4<CommitsByShaResponse>({
githubApiBaseUrlV4,
accessToken,
query,
Expand All @@ -52,11 +52,12 @@ export async function fetchCommitBySha(options: {
sha,
},
});
data = res.data.data;
} catch (e) {
res = swallowMissingConfigFileException<CommitsByShaResponse>(e);
data = swallowMissingConfigFileException<CommitsByShaResponse>(e);
}

const sourceCommit = res.repository.object;
const sourceCommit = data.repository.object;
if (!sourceCommit) {
throw new BackportError(
`No commit found on branch "${sourceBranch}" with sha "${sha}"`,
Expand Down
12 changes: 10 additions & 2 deletions src/lib/github/v4/fetchCommits/fetchCommitsByAuthor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
SourceCommitWithTargetPullRequestFragment,
parseSourceCommit,
} from '../../../sourceCommit/parseSourceCommit';
import { apiRequestV4 } from '../apiRequestV4';
import { GithubV4Exception, apiRequestV4 } from '../apiRequestV4';
import { fetchAuthorId } from '../fetchAuthorId';

async function fetchByCommitPath({
Expand Down Expand Up @@ -92,13 +92,21 @@ async function fetchByCommitPath({
};

try {
return await apiRequestV4<CommitByAuthorResponse>({
const res = await apiRequestV4<CommitByAuthorResponse>({
githubApiBaseUrlV4,
accessToken,
query,
variables,
});
return res.data.data;
} catch (e) {
if (e instanceof GithubV4Exception) {
if (e.githubResponse.status === 502 && maxNumber > 50) {
throw new BackportError(
`The GitHub API returned a 502 error. Try reducing the number of commits to display: "--max-number 20"`,
);
}
}
return swallowMissingConfigFileException<CommitByAuthorResponse>(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function fetchCommitsForRebaseAndMergeStrategy(
},
});

const pullRequestNode = res.repository.pullRequest;
const pullRequestNode = res.data.data.repository.pullRequest;

if (!pullRequestNode?.mergeCommit) {
throw new Error('Pull request is not merged');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ export async function fetchPullRequestsBySearchQuery(options: {
maxNumber,
};

let res;
let data: ResponseData;
try {
res = await apiRequestV4<ResponseData>({
const res = await apiRequestV4<ResponseData>({
githubApiBaseUrlV4,
accessToken,
query,
variables,
});
data = res.data.data;
} catch (e) {
res = swallowMissingConfigFileException<ResponseData>(e);
data = swallowMissingConfigFileException<ResponseData>(e);
}

const commits = res.search.nodes.map((pullRequestNode) => {
const commits = data.search.nodes.map((pullRequestNode) => {
const sourceCommit = pullRequestNode.mergeCommit;
return parseSourceCommit({ options, sourceCommit });
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/fetchExistingPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function fetchExistingPullRequest({
});

const existingPullRequest =
res.repository.ref?.associatedPullRequests.edges[0];
res.data.data.repository.ref?.associatedPullRequests.edges[0];

if (!existingPullRequest) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/fetchPullRequestAutoMergeMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export async function fetchPullRequestAutoMergeMethod(
},
});

return res.repository.pullRequest?.autoMergeRequest?.mergeMethod;
return res.data.data.repository.pullRequest?.autoMergeRequest?.mergeMethod;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export async function getOptionsFromGithub(options: {
accessToken,
query,
variables: { repoOwner, repoName },
fullResponse: true,
});

throwIfInsufficientPermissions(res);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/github/v4/getRepoOwnerAndNameFromGitRemotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export async function getRepoOwnerAndNameFromGitRemotes({
},
});

const { data } = res.data;

return {
repoName: res.repository.name,
repoName: data.repository.name,
// get the original owner (not the fork owner)
repoOwner: res.repository.isFork
? res.repository.parent.owner.login
: res.repository.owner.login,
repoOwner: data.repository.isFork
? data.repository.parent.owner.login
: data.repository.owner.login,
};
} catch (e) {
if (e instanceof GithubV4Exception) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/github/v4/validateTargetBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function validateTargetBranch({
variables: { repoOwner, repoName, branchName },
});

if (!res.repository.ref) {
if (!res.data.data.repository.ref) {
spinner.fail(`The branch "${branchName}" does not exist`);
throw new BackportError({
code: 'invalid-branch-exception',
Expand Down
Loading

0 comments on commit 993d280

Please sign in to comment.