Merge pull request #5 from marocchino/3-comment-header

comment header
This commit is contained in:
marocchino 2019-11-23 10:50:50 +09:00 committed by GitHub
commit 79063050ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 20 deletions

2
.gitignore vendored
View file

@ -91,3 +91,5 @@ typings/
# DynamoDB Local files # DynamoDB Local files
.dynamodb/ .dynamodb/
.vscode/

View file

@ -4,17 +4,18 @@ import {
updateComment updateComment
} from "../src/comment"; } from "../src/comment";
const repo = {}; const repo = {};
const body = "some message";
it("findPreviousComment", async () => { it("findPreviousComment", async () => {
const comment = { const comment = {
user: { user: {
login: "github-actions[bot]" login: "github-actions[bot]"
} },
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
}; };
const otherComment = { const otherComment = {
user: { user: {
login: "some-user" login: "some-user"
} },
body: "lgtm"
}; };
const octokit = { const octokit = {
issues: { issues: {
@ -35,10 +36,12 @@ it("updateComment", async () => {
updateComment: jest.fn(() => Promise.resolve()) updateComment: jest.fn(() => Promise.resolve())
} }
}; };
expect(await updateComment(octokit, repo, 456, body)).toBeUndefined(); expect(
await updateComment(octokit, repo, 456, "hello there")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({ expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
body body: "<!-- Sticky Pull Request Comment -->\nhello there"
}); });
}); });
it("createComment", async () => { it("createComment", async () => {
@ -47,9 +50,11 @@ it("createComment", async () => {
createComment: jest.fn(() => Promise.resolve()) createComment: jest.fn(() => Promise.resolve())
} }
}; };
expect(await createComment(octokit, repo, 456, body)).toBeUndefined(); expect(
await createComment(octokit, repo, 456, "hello there")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({ expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
body body: "<!-- Sticky Pull Request Comment -->\nhello there"
}); });
}); });

31
lib/comment.js Normal file
View file

@ -0,0 +1,31 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const HEADER = "<!-- Sticky Pull Request Comment -->";
function findPreviousComment(octokit, repo, issue_number) {
return __awaiter(this, void 0, void 0, function* () {
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
return comments.find(comment => comment.body.startsWith(HEADER));
});
}
exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body) {
return __awaiter(this, void 0, void 0, function* () {
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${HEADER}\n${body}` }));
});
}
exports.updateComment = updateComment;
function createComment(octokit, repo, issue_number, body) {
return __awaiter(this, void 0, void 0, function* () {
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${HEADER}\n${body}` }));
});
}
exports.createComment = createComment;

View file

@ -18,31 +18,30 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const github_1 = require("@actions/github"); const github_1 = require("@actions/github");
const comment_1 = require("./comment");
function run() { function run() {
var _a, _b, _c; var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const repo = github_1.context.repo; const repo = github_1.context.repo;
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; 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;
if (!issue_number) { const body = core.getInput("message");
const githubToken = core.getInput("GITHUB_TOKEN");
if (!number) {
core.setFailed("This action only works for pull_request"); core.setFailed("This action only works for pull_request");
return; return;
} }
const body = core.getInput("message");
const githubToken = core.getInput("GITHUB_TOKEN");
if (!body || !githubToken) { if (!body || !githubToken) {
core.setFailed("invalid input: please check your workflow"); core.setFailed("invalid input: please check your workflow");
return; return;
} }
const octokit = new github_1.GitHub(githubToken); const octokit = new github_1.GitHub(githubToken);
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number })); const previous = yield comment_1.findPreviousComment(octokit, repo, number);
const myComment = comments.find(comment => comment.user.login === "github-actions[bot]"); if (previous) {
if (myComment) { yield comment_1.updateComment(octokit, repo, previous.id, body);
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id: myComment.id, body }));
} }
else { else {
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, yield comment_1.createComment(octokit, repo, number, body);
body }));
} }
} }
catch ({ message }) { catch ({ message }) {

View file

@ -1,21 +1,23 @@
const HEADER = "<!-- Sticky Pull Request Comment -->";
export async function findPreviousComment(octokit, repo, issue_number) { export async function findPreviousComment(octokit, repo, issue_number) {
const { data: comments } = await octokit.issues.listComments({ const { data: comments } = await octokit.issues.listComments({
...repo, ...repo,
issue_number issue_number
}); });
return comments.find(comment => comment.user.login === "github-actions[bot]"); return comments.find(comment => comment.body.startsWith(HEADER));
} }
export async function updateComment(octokit, repo, comment_id, body) { export async function updateComment(octokit, repo, comment_id, body) {
await octokit.issues.updateComment({ await octokit.issues.updateComment({
...repo, ...repo,
comment_id, comment_id,
body body: `${HEADER}\n${body}`
}); });
} }
export async function createComment(octokit, repo, issue_number, body) { export async function createComment(octokit, repo, issue_number, body) {
await octokit.issues.createComment({ await octokit.issues.createComment({
...repo, ...repo,
issue_number, issue_number,
body body: `${HEADER}\n${body}`
}); });
} }