feat: add ability to set custom header

This commit is contained in:
marocchino 2020-02-04 17:19:50 +09:00
parent ee22346016
commit c873eaf14f
No known key found for this signature in database
GPG key ID: AFF521DBDB122570
4 changed files with 56 additions and 18 deletions

View file

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

View file

@ -14,13 +14,14 @@ async function run() {
try {
const repo = context.repo;
const body = core.getInput("message", { required: true });
const header = core.getInput("header", { required: false }) || "";
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = new GitHub(githubToken);
const previous = await findPreviousComment(octokit, repo, number);
const previous = await findPreviousComment(octokit, repo, number, header);
if (previous) {
await updateComment(octokit, repo, previous.id, body);
await updateComment(octokit, repo, previous.id, body, header);
} else {
await createComment(octokit, repo, number, body);
await createComment(octokit, repo, number, body, header);
}
} catch ({ message }) {
core.setFailed(message);