update hidden identifier comment to come last instead of first

This commit is contained in:
Jake Hiller 2020-06-24 10:38:30 -04:00
parent 587f474c3a
commit 1ee5fc7fa0
2 changed files with 27 additions and 14 deletions

View file

@ -9,14 +9,20 @@ it("findPreviousComment", async () => {
user: {
login: "github-actions[bot]"
},
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
body: "previous message\n<!-- Sticky Pull Request Comment -->"
};
const commentWithCustomHeader = {
user: {
login: "github-actions[bot]"
},
body: "<!-- Sticky Pull Request CommentTypeA -->\nprevious message"
body: "previous message\n<!-- Sticky Pull Request CommentTypeA -->"
};
const headerFirstComment = {
user: {
login: "github-actions[bot]"
},
body: "<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message"
}
const otherComments = [
{
user: {
@ -28,14 +34,14 @@ it("findPreviousComment", async () => {
user: {
login: "github-actions[bot]"
},
body: "<!-- Sticky Pull Request CommentTypeB -->\nprevious message"
}
body: "previous message\n<!-- Sticky Pull Request CommentTypeB -->"
},
];
const octokit = {
issues: {
listComments: jest.fn(() =>
Promise.resolve({
data: [commentWithCustomHeader, comment, ...otherComments]
data: [commentWithCustomHeader, comment, headerFirstComment, ...otherComments]
})
)
}
@ -45,6 +51,7 @@ it("findPreviousComment", async () => {
expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe(
commentWithCustomHeader
);
expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(headerFirstComment)
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
});
it("updateComment", async () => {
@ -58,22 +65,22 @@ it("updateComment", async () => {
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "<!-- Sticky Pull Request Comment -->\nhello there"
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA", "<!-- Sticky Pull Request CommentTypeA -->\nhello there")
await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n<!-- Sticky Pull Request CommentTypeA -->")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there\nhello there"
body: "hello there\nhello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
});
it("createComment", async () => {
@ -87,13 +94,13 @@ it("createComment", async () => {
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "<!-- Sticky Pull Request Comment -->\nhello there"
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
});

View file

@ -2,25 +2,31 @@ function headerComment(header) {
return `<!-- Sticky Pull Request Comment${header} -->`;
}
function removeHeaderComment(body, header) {
return body.replace(`\n${header}`, '');
}
export async function findPreviousComment(octokit, repo, issue_number, header) {
const { data: comments } = await octokit.issues.listComments({
...repo,
issue_number
});
const h = headerComment(header);
return comments.find(comment => comment.body.startsWith(h));
return comments.find(comment => comment.body.includes(h));
}
export async function updateComment(octokit, repo, comment_id, body, header, previousBody?) {
const headerIdentifier = headerComment(header);
const updatedBody = previousBody ? `${removeHeaderComment(previousBody, headerIdentifier)}\n${body}` : body;
await octokit.issues.updateComment({
...repo,
comment_id,
body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}`
body: `${updatedBody}\n${headerIdentifier}`
});
}
export async function createComment(octokit, repo, issue_number, body, header) {
await octokit.issues.createComment({
...repo,
issue_number,
body: `${headerComment(header)}\n${body}`
body: `${body}\n${headerComment(header)}`
});
}