Skip to content

Commit 682de67

Browse files
authored
Merge pull request #52 from marocchino/custom-comment-header
feat: add ability to set custom header
2 parents ee22346 + 033d20d commit 682de67

File tree

8 files changed

+72
-27
lines changed

8 files changed

+72
-27
lines changed

.github/workflows/comment_on_pull_request.yml

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ jobs:
1010
- uses: ./
1111
with:
1212
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
header: FromPR
1314
message: |
1415
Test ${{ github.sha }} is successfully ended.
16+
This is message from PR.

.github/workflows/comment_on_push.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ jobs:
1717
number: ${{ steps.finder.outputs.pr }}
1818
message: |
1919
Test ${{ github.sha }} is successfully ended.
20+
This is message from push.

__tests__/comment.test.ts

+38-7
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

+3
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

lib/comment.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
const HEADER = "<!-- Sticky Pull Request Comment -->";
13-
function findPreviousComment(octokit, repo, issue_number) {
12+
function headerComment(header) {
13+
return `<!-- Sticky Pull Request Comment${header} -->`;
14+
}
15+
function findPreviousComment(octokit, repo, issue_number, header) {
1416
return __awaiter(this, void 0, void 0, function* () {
1517
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
16-
return comments.find(comment => comment.body.startsWith(HEADER));
18+
const h = headerComment(header);
19+
return comments.find(comment => comment.body.startsWith(h));
1720
});
1821
}
1922
exports.findPreviousComment = findPreviousComment;
20-
function updateComment(octokit, repo, comment_id, body) {
23+
function updateComment(octokit, repo, comment_id, body, header) {
2124
return __awaiter(this, void 0, void 0, function* () {
22-
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${HEADER}\n${body}` }));
25+
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${headerComment(header)}\n${body}` }));
2326
});
2427
}
2528
exports.updateComment = updateComment;
26-
function createComment(octokit, repo, issue_number, body) {
29+
function createComment(octokit, repo, issue_number, body, header) {
2730
return __awaiter(this, void 0, void 0, function* () {
28-
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${HEADER}\n${body}` }));
31+
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${headerComment(header)}\n${body}` }));
2932
});
3033
}
3134
exports.createComment = createComment;

lib/main.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ function run() {
3131
try {
3232
const repo = github_1.context.repo;
3333
const body = core.getInput("message", { required: true });
34+
const header = core.getInput("header", { required: false }) || "";
3435
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
3536
const octokit = new github_1.GitHub(githubToken);
36-
const previous = yield comment_1.findPreviousComment(octokit, repo, number);
37+
const previous = yield comment_1.findPreviousComment(octokit, repo, number, header);
3738
if (previous) {
38-
yield comment_1.updateComment(octokit, repo, previous.id, body);
39+
yield comment_1.updateComment(octokit, repo, previous.id, body, header);
3940
}
4041
else {
41-
yield comment_1.createComment(octokit, repo, number, body);
42+
yield comment_1.createComment(octokit, repo, number, body, header);
4243
}
4344
}
4445
catch ({ message }) {

src/comment.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
const HEADER = "<!-- Sticky Pull Request Comment -->";
1+
function headerComment(header) {
2+
return `<!-- Sticky Pull Request Comment${header} -->`;
3+
}
24

3-
export async function findPreviousComment(octokit, repo, issue_number) {
5+
export async function findPreviousComment(octokit, repo, issue_number, header) {
46
const { data: comments } = await octokit.issues.listComments({
57
...repo,
68
issue_number
79
});
8-
return comments.find(comment => comment.body.startsWith(HEADER));
10+
const h = headerComment(header);
11+
return comments.find(comment => comment.body.startsWith(h));
912
}
10-
export async function updateComment(octokit, repo, comment_id, body) {
13+
export async function updateComment(octokit, repo, comment_id, body, header) {
1114
await octokit.issues.updateComment({
1215
...repo,
1316
comment_id,
14-
body: `${HEADER}\n${body}`
17+
body: `${headerComment(header)}\n${body}`
1518
});
1619
}
17-
export async function createComment(octokit, repo, issue_number, body) {
20+
export async function createComment(octokit, repo, issue_number, body, header) {
1821
await octokit.issues.createComment({
1922
...repo,
2023
issue_number,
21-
body: `${HEADER}\n${body}`
24+
body: `${headerComment(header)}\n${body}`
2225
});
2326
}

src/main.ts

+4-3
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)