Skip to content

Commit 7906305

Browse files
authored
Merge pull request #5 from marocchino/3-comment-header
comment header
2 parents f850d3d + 1cfcb23 commit 7906305

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ typings/
9191

9292
# DynamoDB Local files
9393
.dynamodb/
94+
95+
.vscode/

__tests__/comment.test.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import {
44
updateComment
55
} from "../src/comment";
66
const repo = {};
7-
const body = "some message";
87
it("findPreviousComment", async () => {
98
const comment = {
109
user: {
1110
login: "github-actions[bot]"
12-
}
11+
},
12+
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
1313
};
1414
const otherComment = {
1515
user: {
1616
login: "some-user"
17-
}
17+
},
18+
body: "lgtm"
1819
};
1920
const octokit = {
2021
issues: {
@@ -35,10 +36,12 @@ it("updateComment", async () => {
3536
updateComment: jest.fn(() => Promise.resolve())
3637
}
3738
};
38-
expect(await updateComment(octokit, repo, 456, body)).toBeUndefined();
39+
expect(
40+
await updateComment(octokit, repo, 456, "hello there")
41+
).toBeUndefined();
3942
expect(octokit.issues.updateComment).toBeCalledWith({
4043
comment_id: 456,
41-
body
44+
body: "<!-- Sticky Pull Request Comment -->\nhello there"
4245
});
4346
});
4447
it("createComment", async () => {
@@ -47,9 +50,11 @@ it("createComment", async () => {
4750
createComment: jest.fn(() => Promise.resolve())
4851
}
4952
};
50-
expect(await createComment(octokit, repo, 456, body)).toBeUndefined();
53+
expect(
54+
await createComment(octokit, repo, 456, "hello there")
55+
).toBeUndefined();
5156
expect(octokit.issues.createComment).toBeCalledWith({
5257
issue_number: 456,
53-
body
58+
body: "<!-- Sticky Pull Request Comment -->\nhello there"
5459
});
5560
});

lib/comment.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const HEADER = "<!-- Sticky Pull Request Comment -->";
13+
function findPreviousComment(octokit, repo, issue_number) {
14+
return __awaiter(this, void 0, void 0, function* () {
15+
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
16+
return comments.find(comment => comment.body.startsWith(HEADER));
17+
});
18+
}
19+
exports.findPreviousComment = findPreviousComment;
20+
function updateComment(octokit, repo, comment_id, body) {
21+
return __awaiter(this, void 0, void 0, function* () {
22+
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${HEADER}\n${body}` }));
23+
});
24+
}
25+
exports.updateComment = updateComment;
26+
function createComment(octokit, repo, issue_number, body) {
27+
return __awaiter(this, void 0, void 0, function* () {
28+
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${HEADER}\n${body}` }));
29+
});
30+
}
31+
exports.createComment = createComment;

lib/main.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,30 @@ var __importStar = (this && this.__importStar) || function (mod) {
1818
Object.defineProperty(exports, "__esModule", { value: true });
1919
const core = __importStar(require("@actions/core"));
2020
const github_1 = require("@actions/github");
21+
const comment_1 = require("./comment");
2122
function run() {
2223
var _a, _b, _c;
2324
return __awaiter(this, void 0, void 0, function* () {
2425
try {
2526
const repo = github_1.context.repo;
26-
const issue_number = (_c = (_b = (_a = github_1.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number;
27-
if (!issue_number) {
27+
const number = (_c = (_b = (_a = github_1.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number;
28+
const body = core.getInput("message");
29+
const githubToken = core.getInput("GITHUB_TOKEN");
30+
if (!number) {
2831
core.setFailed("This action only works for pull_request");
2932
return;
3033
}
31-
const body = core.getInput("message");
32-
const githubToken = core.getInput("GITHUB_TOKEN");
3334
if (!body || !githubToken) {
3435
core.setFailed("invalid input: please check your workflow");
3536
return;
3637
}
3738
const octokit = new github_1.GitHub(githubToken);
38-
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
39-
const myComment = comments.find(comment => comment.user.login === "github-actions[bot]");
40-
if (myComment) {
41-
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id: myComment.id, body }));
39+
const previous = yield comment_1.findPreviousComment(octokit, repo, number);
40+
if (previous) {
41+
yield comment_1.updateComment(octokit, repo, previous.id, body);
4242
}
4343
else {
44-
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number,
45-
body }));
44+
yield comment_1.createComment(octokit, repo, number, body);
4645
}
4746
}
4847
catch ({ message }) {

src/comment.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
const HEADER = "<!-- Sticky Pull Request Comment -->";
2+
13
export async function findPreviousComment(octokit, repo, issue_number) {
24
const { data: comments } = await octokit.issues.listComments({
35
...repo,
46
issue_number
57
});
6-
return comments.find(comment => comment.user.login === "github-actions[bot]");
8+
return comments.find(comment => comment.body.startsWith(HEADER));
79
}
810
export async function updateComment(octokit, repo, comment_id, body) {
911
await octokit.issues.updateComment({
1012
...repo,
1113
comment_id,
12-
body
14+
body: `${HEADER}\n${body}`
1315
});
1416
}
1517
export async function createComment(octokit, repo, issue_number, body) {
1618
await octokit.issues.createComment({
1719
...repo,
1820
issue_number,
19-
body
21+
body: `${HEADER}\n${body}`
2022
});
2123
}

0 commit comments

Comments
 (0)