Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of merge API call failure #1517

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/getCheckRuns.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 39 additions & 18 deletions lib/mergePullRequest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ async function handlePullRequest(pullRequestNumber: number) {
const mergeMethod = getMergeMethod();
info(`Merging with merge method: ${mergeMethod}`);
await mergePullRequest(owner, repo, pullRequestNumber, mergeMethod, octokit);
setOutput('skipped', false);
}

async function run(): Promise<void> {
Expand Down
62 changes: 43 additions & 19 deletions src/mergePullRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Octokit } from '@octokit/core';
import type { Api } from '@octokit/plugin-rest-endpoint-methods/dist-types/types';
import { setOutput, warning } from '@actions/core';
import { RequestError } from '@octokit/request-error';
import { getMergeMethod } from './getMergeMethod';

Expand All @@ -16,21 +17,26 @@ export async function checkIfPullRequestMerged(
repo,
pull_number: pullRequestNumber,
});
if (response.status === 204) {
return true;
} else {
return false;
}
} catch (error) {
if (error instanceof RequestError) {
response = error.response;
if (error.status === 204) {
return true;
} else if (error.status === 404) {
return false;
} else {
throw new Error(
`Failed to check if pull request is merged: [${error.status}] ${error.message}`,
);
}
} else {
throw error;
}
}

if (response?.status === 204) {
return true;
} else if (response?.status === 404) {
return false;
} else {
throw new Error(
`Failed to check if pull request is merged: ${response?.status}`,
);
}
}

export async function mergePullRequest(
Expand All @@ -40,13 +46,31 @@ export async function mergePullRequest(
mergeMethod: ReturnType<typeof getMergeMethod>,
octokit: Octokit & Api,
) {
const response = await octokit.rest.pulls.merge({
owner,
repo,
pull_number: pullRequestNumber,
merge_method: mergeMethod,
});
if (response.status !== 200) {
throw new Error(`Failed to merge pull request: ${response.status}`);
try {
await octokit.rest.pulls.merge({
owner,
repo,
pull_number: pullRequestNumber,
merge_method: mergeMethod,
});
setOutput('skipped', false);
} catch (error) {
if (error instanceof RequestError) {
warning(
`Failed to merge pull request: [${error.status}] ${error.message}`,
);

// If it's merged by someone else in a race condition we treat it as skipped,
// because it's the same as someone else merged it before we try.
const merged = await checkIfPullRequestMerged(
owner,
repo,
pullRequestNumber,
octokit,
);
setOutput('skipped', !merged);
} else {
throw error;
}
}
}