Skip to content

Commit

Permalink
More useful error message for 422 errors
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
hmarr committed Mar 15, 2021
1 parent bf844d1 commit e21ecf1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
12 changes: 9 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5890,12 +5890,18 @@ function approve(token, context) {
switch (error.status) {
case 401:
core.setFailed(`${error.message}. Please check that the \`github-token\` input ` +
`parameter is set correctly.`);
"parameter is set correctly.");
break;
case 403:
core.setFailed(`${error.message}. In some cases, the GitHub token used for actions triggered ` +
`from \`pull_request\` events are read-only, which can cause this problem. ` +
`Switching to the \`pull_request_target\` event typically resolves this issue.`);
"from `pull_request` events are read-only, which can cause this problem. " +
"Switching to the `pull_request_target` event typically resolves this issue.");
break;
case 422:
core.setFailed(`${error.message}. This typically happens when you try to approve the pull ` +
"request with the same user account that created the pull request. Try using " +
"the built-in `${{ secrets.GITHUB_TOKEN }}` token, or if you're using a personal " +
"access token, use one that belongs to a dedicated bot account.");
break;
default:
core.setFailed(`Error (code ${error.status}): ${error.message}`);
Expand Down
12 changes: 12 additions & 0 deletions src/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ test("when the token doesn't have write permissions", async () => {
);
});

test("when a user tries to approve their own pull request", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(422, { message: "Unprocessable Entity" });

await approve("gh-tok", ghContext());

expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("same user account")
);
});

function ghContext(): Context {
const ctx = new Context();
ctx.payload = {
Expand Down
14 changes: 11 additions & 3 deletions src/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ export async function approve(token: string, context: Context) {
case 401:
core.setFailed(
`${error.message}. Please check that the \`github-token\` input ` +
`parameter is set correctly.`
"parameter is set correctly."
);
break;
case 403:
core.setFailed(
`${error.message}. In some cases, the GitHub token used for actions triggered ` +
`from \`pull_request\` events are read-only, which can cause this problem. ` +
`Switching to the \`pull_request_target\` event typically resolves this issue.`
"from `pull_request` events are read-only, which can cause this problem. " +
"Switching to the `pull_request_target` event typically resolves this issue."
);
break;
case 422:
core.setFailed(
`${error.message}. This typically happens when you try to approve the pull ` +
"request with the same user account that created the pull request. Try using " +
"the built-in `${{ secrets.GITHUB_TOKEN }}` token, or if you're using a personal " +
"access token, use one that belongs to a dedicated bot account."
);
break;
default:
Expand Down

0 comments on commit e21ecf1

Please sign in to comment.