Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
hmarr committed Feb 3, 2024
1 parent 5c6d6d8 commit 37b1c4c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"prettier": "^3.2.4",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
}
}
},
"prettier": {}
}
68 changes: 34 additions & 34 deletions src/approve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ function mockOctokit(
method: "get" | "post" | "put" | "delete",
path: string,
status: number,
body: any
body: any,
) {
let isDone = false;
mockServer.use(
http[method](`https://api.github.com${path}`, () => {
isDone = true;
return HttpResponse.json(body, { status: status ?? 200 });
})
}),
);
return { isDone: () => isDone };
}
Expand All @@ -47,14 +47,14 @@ const apiMocks = {
"get",
"/repos/hmarr/test/pulls/101",
status ?? 200,
body ?? { head: { sha: "24c5451bbf1fb09caa3ac8024df4788aff4d4974" } }
body ?? { head: { sha: "24c5451bbf1fb09caa3ac8024df4788aff4d4974" } },
),
getReviews: (status?: number, body?: any) =>
mockOctokit(
"get",
"/repos/hmarr/test/pulls/101/reviews",
status ?? 200,
body ?? []
body ?? [],
),
createReview: () =>
mockOctokit("post", "/repos/hmarr/test/pulls/101/reviews", 200, {}),
Expand All @@ -71,7 +71,7 @@ test("a review is successfully created with a PAT", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -87,7 +87,7 @@ test("a review is successfully created with an Actions token", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -104,7 +104,7 @@ test("when a review is successfully created with message", async () => {
context: ghContext(),
reviewMessage: "Review body",
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -120,7 +120,7 @@ test("when a review is successfully created using pull-request-number", async ()
"post",
"/repos/hmarr/test/pulls/102/reviews",
200,
{ id: 1 }
{ id: 1 },
);

expect(
Expand All @@ -129,7 +129,7 @@ test("when a review is successfully created using pull-request-number", async ()
context: ghContext(),
prNumber: 102,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();

expect(createReview.isDone()).toBe(true);
Expand All @@ -152,14 +152,14 @@ test("when a review has already been approved by current user", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();

expect(createReview.isDone()).toBe(false);
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining(
"Current user already approved pull request #101, nothing to do"
)
"Current user already approved pull request #101, nothing to do",
),
);
});

Expand All @@ -181,7 +181,7 @@ test("when a review is pending", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -204,7 +204,7 @@ test("when a review is dismissed", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand Down Expand Up @@ -232,7 +232,7 @@ test("when a review is dismissed, but an earlier review is approved", async () =
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -255,7 +255,7 @@ test("when a review is not approved", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -278,7 +278,7 @@ test("when a review is commented", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -301,7 +301,7 @@ test("when a review has already been approved by another user", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -324,7 +324,7 @@ test("when a review has already been approved by unknown user", async () => {
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -351,7 +351,7 @@ test("when a review has been previously approved by user and but requests a re-r
context: ghContext(),
prNumber: 101,
octokitOpts: { request: fetch },
})
}),
).toBeTruthy();
expect(createReview.isDone()).toBe(true);
});
Expand All @@ -363,11 +363,11 @@ test("without a pull request", async () => {
token: "gh-tok",
context: new Context(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(createReview.isDone()).toBe(false);
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("Make sure you're triggering this")
expect.stringContaining("Make sure you're triggering this"),
);
});

Expand All @@ -382,11 +382,11 @@ test("when the token is invalid", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(createReview.isDone()).toBe(false);
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("`github-token` input parameter")
expect.stringContaining("`github-token` input parameter"),
);
});

Expand All @@ -403,10 +403,10 @@ test("when the token doesn't have write permissions", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("pull_request_target")
expect.stringContaining("pull_request_target"),
);
});

Expand All @@ -423,10 +423,10 @@ test("when a user tries to approve their own pull request", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("same user account")
expect.stringContaining("same user account"),
);
});

Expand All @@ -441,11 +441,11 @@ test("when pull request does not exist or the token doesn't have access", async
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(createReview.isDone()).toBe(false);
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("doesn't have access")
expect.stringContaining("doesn't have access"),
);
});

Expand All @@ -462,10 +462,10 @@ test("when the token is read-only", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("are read-only")
expect.stringContaining("are read-only"),
);
});

Expand All @@ -482,10 +482,10 @@ test("when the token doesn't have write access to the repository", async () => {
token: "gh-tok",
context: ghContext(),
octokitOpts: { request: fetch },
})
}),
).toBeFalsy();
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("doesn't have access")
expect.stringContaining("doesn't have access"),
);
});

Expand Down
18 changes: 9 additions & 9 deletions src/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function approve({
if (!prNumber) {
core.setFailed(
"Event payload missing `pull_request` key, and no `pull-request-number` provided as input." +
"Make sure you're triggering this action on the `pull_request` or `pull_request_target` events."
"Make sure you're triggering this action on the `pull_request` or `pull_request_target` events.",
);
return false;
}
Expand Down Expand Up @@ -61,18 +61,18 @@ export async function approve({
// we need to create a new review. Review requests mean that existing "APPROVED" reviews
// don't count towards the mergeability of the PR.
const outstandingReviewRequest = pr.requested_reviewers?.some(
(reviewer) => reviewer.login == login
(reviewer) => reviewer.login == login,
);

if (alreadyReviewed && !outstandingReviewRequest) {
core.info(
`Current user already approved pull request #${prNumber}, nothing to do`
`Current user already approved pull request #${prNumber}, nothing to do`,
);
return false;
}

core.info(
`Pull request #${prNumber} has not been approved yet, creating approving review`
`Pull request #${prNumber} has not been approved yet, creating approving review`,
);
await client.rest.pulls.createReview({
owner: context.repo.owner,
Expand All @@ -88,29 +88,29 @@ export async function approve({
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."
"Switching to the `pull_request_target` event typically resolves this issue.",
);
break;
case 404:
core.setFailed(
`${error.message}. This typically means the token you're using doesn't have ` +
"access to this repository. Use the built-in `${{ secrets.GITHUB_TOKEN }}` token " +
"or review the scopes assigned to your personal access token."
"or review the scopes assigned to your personal access token.",
);
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."
"access token, use one that belongs to a dedicated bot account.",
);
break;
default:
Expand All @@ -131,7 +131,7 @@ export async function approve({
}

async function getLoginForToken(
client: InstanceType<typeof GitHub>
client: InstanceType<typeof GitHub>,
): Promise<string> {
try {
const { data: user } = await client.rest.users.getAuthenticated();
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function prNumber(): number {
if (!github.context.payload.pull_request) {
throw new Error(
"This action must be run using a `pull_request` event or " +
"have an explicit `pull-request-number` provided"
"have an explicit `pull-request-number` provided",
);
}
return github.context.payload.pull_request.number;
Expand Down

0 comments on commit 37b1c4c

Please sign in to comment.