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

Providing more feedback when failing to merge when conditions are met #1562

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
44 changes: 31 additions & 13 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.

44 changes: 31 additions & 13 deletions lib/mergePullRequest.js

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

45 changes: 31 additions & 14 deletions src/mergePullRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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 { error, setFailed, setOutput, warning } from '@actions/core';
import { RequestError } from '@octokit/request-error';
import { getMergeMethod } from './getMergeMethod';

Expand All @@ -10,31 +10,30 @@ export async function checkIfPullRequestMerged(
pullRequestNumber: number,
octokit: Octokit & Api,
) {
let response: RequestError['response'];
try {
response = await octokit.rest.pulls.checkIfMerged({
const { status } = await octokit.rest.pulls.checkIfMerged({
owner,
repo,
pull_number: pullRequestNumber,
});
if (response.status === 204) {
if (status === 204) {
return true;
} else {
return false;
}
} catch (error) {
if (error instanceof RequestError) {
if (error.status === 204) {
} catch (requestError) {
if (requestError instanceof RequestError) {
if (requestError.status === 204) {
return true;
} else if (error.status === 404) {
} else if (requestError.status === 404) {
return false;
} else {
throw new Error(
`Failed to check if pull request is merged: [${error.status}] ${error.message}`,
`Failed to check if pull request is merged: [${requestError.status}] ${requestError.message}`,
);
}
} else {
throw error;
throw requestError;
}
}
}
Expand All @@ -54,10 +53,10 @@ export async function mergePullRequest(
merge_method: mergeMethod,
});
setOutput('skipped', false);
} catch (error) {
if (error instanceof RequestError) {
} catch (requestError) {
if (requestError instanceof RequestError) {
warning(
`Failed to merge pull request: [${error.status}] ${error.message}`,
`Failed to merge pull request: [${requestError.status}] ${requestError.message}`,
);

// If it's merged by someone else in a race condition we treat it as skipped,
Expand All @@ -68,9 +67,27 @@ export async function mergePullRequest(
pullRequestNumber,
octokit,
);
if (merged) {
try {
const { data: pullRequest } = await octokit.rest.pulls.get({
owner,
repo,
pull_number: pullRequestNumber,
});
warning(
`This Pull Request has been merged by: ${pullRequest.merged_by?.login} (${pullRequest.merged_by?.html_url})`,
);
} catch {
warning(`This Pull Request has been merged by unknown user.`);
}
} else {
// If it's not merged by someone else in a race condition then we treat it as a real error.
error(`This Pull Request remains unmerged.`);
setFailed(`Failed to merge this Pull Request when conditions are met.`);
}
setOutput('skipped', !merged);
} else {
throw error;
throw requestError;
}
}
}