Skip to content

Commit c873eaf

Browse files
committed
feat: add ability to set custom header
1 parent ee22346 commit c873eaf

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

__tests__/comment.test.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,40 @@ it("findPreviousComment", async () => {
1111
},
1212
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
1313
};
14-
const otherComment = {
14+
const commentWithCustomHeader = {
1515
user: {
16-
login: "some-user"
16+
login: "github-actions[bot]"
1717
},
18-
body: "lgtm"
18+
body: "<!-- Sticky Pull Request CommentTypeA -->\nprevious message"
1919
};
20+
const otherComments = [
21+
{
22+
user: {
23+
login: "some-user"
24+
},
25+
body: "lgtm"
26+
},
27+
{
28+
user: {
29+
login: "github-actions[bot]"
30+
},
31+
body: "<!-- Sticky Pull Request CommentTypeB -->\nprevious message"
32+
}
33+
];
2034
const octokit = {
2135
issues: {
2236
listComments: jest.fn(() =>
2337
Promise.resolve({
24-
data: [otherComment, comment]
38+
data: [commentWithCustomHeader, comment, ...otherComments]
2539
})
2640
)
2741
}
2842
};
2943

30-
expect(await findPreviousComment(octokit, repo, 123)).toBe(comment);
44+
expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment);
45+
expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe(
46+
commentWithCustomHeader
47+
);
3148
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
3249
});
3350
it("updateComment", async () => {
@@ -37,12 +54,19 @@ it("updateComment", async () => {
3754
}
3855
};
3956
expect(
40-
await updateComment(octokit, repo, 456, "hello there")
57+
await updateComment(octokit, repo, 456, "hello there", "")
4158
).toBeUndefined();
4259
expect(octokit.issues.updateComment).toBeCalledWith({
4360
comment_id: 456,
4461
body: "<!-- Sticky Pull Request Comment -->\nhello there"
4562
});
63+
expect(
64+
await updateComment(octokit, repo, 456, "hello there", "TypeA")
65+
).toBeUndefined();
66+
expect(octokit.issues.updateComment).toBeCalledWith({
67+
comment_id: 456,
68+
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
69+
});
4670
});
4771
it("createComment", async () => {
4872
const octokit = {
@@ -51,10 +75,17 @@ it("createComment", async () => {
5175
}
5276
};
5377
expect(
54-
await createComment(octokit, repo, 456, "hello there")
78+
await createComment(octokit, repo, 456, "hello there", "")
5579
).toBeUndefined();
5680
expect(octokit.issues.createComment).toBeCalledWith({
5781
issue_number: 456,
5882
body: "<!-- Sticky Pull Request Comment -->\nhello there"
5983
});
84+
expect(
85+
await createComment(octokit, repo, 456, "hello there", "TypeA")
86+
).toBeUndefined();
87+
expect(octokit.issues.createComment).toBeCalledWith({
88+
issue_number: 456,
89+
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
90+
});
6091
});

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: "Sticky Pull Request Comment"
22
description: "Create comment on pull request, if exists update that comment."
33
author: "marocchino"
44
inputs:
5+
header:
6+
description: "Header to determine if the comment is to be updated, not shown on screen"
7+
required: false
58
message:
69
description: "comment message"
710
required: true

src/comment.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
const HEADER = "<!-- Sticky Pull Request Comment -->";
2-
3-
export async function findPreviousComment(octokit, repo, issue_number) {
1+
export async function findPreviousComment(octokit, repo, issue_number, header) {
42
const { data: comments } = await octokit.issues.listComments({
53
...repo,
64
issue_number
75
});
8-
return comments.find(comment => comment.body.startsWith(HEADER));
6+
const h = headerComment(header);
7+
return comments.find(comment => comment.body.startsWith(h));
98
}
10-
export async function updateComment(octokit, repo, comment_id, body) {
9+
export async function updateComment(octokit, repo, comment_id, body, header) {
1110
await octokit.issues.updateComment({
1211
...repo,
1312
comment_id,
14-
body: `${HEADER}\n${body}`
13+
body: `${headerComment(header)}\n${body}`
1514
});
1615
}
17-
export async function createComment(octokit, repo, issue_number, body) {
16+
export async function createComment(octokit, repo, issue_number, body, header) {
1817
await octokit.issues.createComment({
1918
...repo,
2019
issue_number,
21-
body: `${HEADER}\n${body}`
20+
body: `${headerComment(header)}\n${body}`
2221
});
2322
}
23+
24+
function headerComment(header) {
25+
return `<!-- Sticky Pull Request Comment${header} -->`;
26+
}

src/main.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ async function run() {
1414
try {
1515
const repo = context.repo;
1616
const body = core.getInput("message", { required: true });
17+
const header = core.getInput("header", { required: false }) || "";
1718
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
1819
const octokit = new GitHub(githubToken);
19-
const previous = await findPreviousComment(octokit, repo, number);
20+
const previous = await findPreviousComment(octokit, repo, number, header);
2021
if (previous) {
21-
await updateComment(octokit, repo, previous.id, body);
22+
await updateComment(octokit, repo, previous.id, body, header);
2223
} else {
23-
await createComment(octokit, repo, number, body);
24+
await createComment(octokit, repo, number, body, header);
2425
}
2526
} catch ({ message }) {
2627
core.setFailed(message);

0 commit comments

Comments
 (0)