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

Try to update earliest matching comment #37

Merged
merged 1 commit into from
Jul 14, 2023
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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions src/helpers/update-comment-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ export class UpdateCommentHelper {
@inject(Octokit)
private readonly octokit: Octokit;

private static readonly BOT_TYPE =
"Bot";

/**
* Updates a GH comment previously created by the github-actions bot with a new comment
* Updates a GH comment with a new comment if needed
*
* @param searchRegex regex used to determine which comment to edit
* @param comment the new comment content
Expand Down Expand Up @@ -72,12 +69,26 @@ export class UpdateCommentHelper {
response: RestEndpointMethodTypes["issues"]["listComments"]["response"],
searchRegex: RegExp
): ArrayType<IssuesListCommentsResponseData> | undefined {
return response.data.find((comment) => {
return (
comment.user.type === UpdateCommentHelper.BOT_TYPE
&& searchRegex.test(comment.body)
);
const matches = response.data.filter((comment) => {
return searchRegex.test(comment.body);
});
// comments are sorted by ascending id
return this.getEarliestComment(matches);
}

private getEarliestComment(
comments: IssuesListCommentsResponseData
): ArrayType<IssuesListCommentsResponseData> | undefined {
if (comments.length === 0) {
return undefined;
}
let earliest = comments[0];
for (let i = 1; i < comments.length; i++) {
if (new Date(comments[i].created_at) < new Date(earliest.created_at)) {
earliest = comments[i];
}
}
return earliest;
}

private async updateCommentById(
Expand Down
2 changes: 1 addition & 1 deletion src/logic/handle-pull-request-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class HandlePullRequestLogic implements Logic {
const comment = `${HandlePullRequestLogic.MESSAGE_PREFIX}: [![Contribute](${badgeUrl})](${targetUrl})`;

const updatedExisting = await this.updateCommentHelper.updateComment(
new RegExp(`^${HandlePullRequestLogic.MESSAGE_PREFIX}.*`),
new RegExp(`^${HandlePullRequestLogic.MESSAGE_PREFIX}.*$`),
comment, payload
);

Expand Down
96 changes: 72 additions & 24 deletions tests/helpers/update-comment-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Test Helper UpdateCommentHelper", () => {
container.bind(Octokit).toConstantValue(octokit);
const updateCommentHelper = container.get(UpdateCommentHelper);
await updateCommentHelper.updateComment(
/^(Hello world)*./g,
/^(Hello world)*.$/,
"my new comment",
payload
);
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("Test Helper UpdateCommentHelper", () => {
// regex does not match comment body
await expect(
updateCommentHelper.updateComment(
/^(regex does not match).*/g,
/^(regex does not match).*$/,
"my comment",
payload
)
Expand All @@ -92,20 +92,54 @@ describe("Test Helper UpdateCommentHelper", () => {
expect(octokit.issues.updateComment).toBeCalledTimes(0);
});

test("comment not found due to no comments by bot", async () => {
test("comment body is the same as new comment content", async () => {
const octokit = setComments([
{
id: 1234,
user: {
type: "Bot",
},
body: "Hello world!",
},
]);
container.bind(Octokit).toConstantValue(octokit);
const updateCommentHelper = container.get(UpdateCommentHelper);

await expect(
updateCommentHelper.updateComment(
/^(Hello world)*.$/,
"Hello world!", // same comment content
payload
)
).resolves.toBe(true);

expect(octokit.issues.updateComment).toBeCalledTimes(0);
});

test("update the earliest matching comment", async () => {
const octokit = setComments([
{
id: 1234,
user: {
type: "User",
},
created_at: "2022-05-09T19:54:41Z",
body: "Hello world!",
},
{
id: 1235,
user: {
type: "Bot",
},
created_at: "2022-05-09T19:54:41Z",
body: "Hello world!",
},
{
id: 1236,
user: {
type: "Organization",
},
created_at: "2022-06-09T19:54:41Z",
body: "Hello world!",
},
]);
Expand All @@ -114,22 +148,36 @@ describe("Test Helper UpdateCommentHelper", () => {

await expect(
updateCommentHelper.updateComment(
/^(Hello world)*./g,
"my comment",
/^(Hello world)*.$/,
"Hello world!!!!!", // different comment content
payload
)
).resolves.toBe(false);
).resolves.toBe(true);

expect(octokit.issues.updateComment).toBeCalledTimes(0);
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 1234,
owner: payload.repository.owner.login,
repo: payload.repository.name,
body: "Hello world!!!!!",
});
});

test("comment body is the same as new comment content", async () => {
test("update the earliset matching comment 2", async () => {
const octokit = setComments([
{
id: 1234,
user: {
type: "Bot",
type: "User",
},
created_at: "2023-05-09T19:54:41Z",
body: "Hello world!",
},
{
id: 1235,
user: {
type: "Organization",
},
created_at: "2023-04-09T19:54:41Z",
body: "Hello world!",
},
]);
Expand All @@ -138,52 +186,52 @@ describe("Test Helper UpdateCommentHelper", () => {

await expect(
updateCommentHelper.updateComment(
/^(Hello world)*./g,
"Hello world!", // same comment content
/^(Hello world)*.$/,
"Hello world!!!!!", // different comment content
payload
)
).resolves.toBe(true);

expect(octokit.issues.updateComment).toBeCalledTimes(0);
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 1235,
owner: payload.repository.owner.login,
repo: payload.repository.name,
body: "Hello world!!!!!",
});
});

test("update the correct existing comment", async () => {
test("update the earliset matching comment 3", async () => {
const octokit = setComments([
{
id: 1234,
user: {
type: "User",
},
created_at: "2023-05-09T19:54:41Z",
body: "Hello world!",
},
{
id: 1235,
user: {
type: "Bot",
},
body: "Hello world!",
},
{
id: 1236,
user: {
type: "Organization",
},
body: "Hello world!",
created_at: "2023-04-09T19:54:41Z",
body: "A different comment",
},
]);
container.bind(Octokit).toConstantValue(octokit);
const updateCommentHelper = container.get(UpdateCommentHelper);

await expect(
updateCommentHelper.updateComment(
/^(Hello world)*./g,
/^(Hello world)*.$/,
"Hello world!!!!!", // different comment content
payload
)
).resolves.toBe(true);

expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 1235,
comment_id: 1234,
owner: payload.repository.owner.login,
repo: payload.repository.name,
body: "Hello world!!!!!",
Expand All @@ -203,7 +251,7 @@ describe("Test Helper UpdateCommentHelper", () => {
const updateCommentHelper = container.get(UpdateCommentHelper);
await expect(
updateCommentHelper.updateComment(
/^(Hello world)*./g,
/^(Hello world)*.$/,
"Hello world!",
payload
)
Expand Down