mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2025-12-14 12:31:14 +00:00
refactor: extract api wrapper
This commit is contained in:
parent
97da04af99
commit
fd286f3230
2 changed files with 30 additions and 23 deletions
21
src/comment.ts
Normal file
21
src/comment.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
export async function findPreviousComment(octokit, repo, issue_number) {
|
||||
const { data: comments } = await octokit.issues.listComments({
|
||||
...repo,
|
||||
issue_number
|
||||
});
|
||||
return comments.find(comment => comment.user.login === "github-actions[bot]");
|
||||
}
|
||||
export async function updateComment(octokit, repo, comment_id, body) {
|
||||
await octokit.issues.updateComment({
|
||||
...repo,
|
||||
comment_id,
|
||||
body
|
||||
});
|
||||
}
|
||||
export async function createComment(octokit, repo, issue_number, body) {
|
||||
await octokit.issues.createComment({
|
||||
...repo,
|
||||
issue_number,
|
||||
body
|
||||
});
|
||||
}
|
||||
32
src/main.ts
32
src/main.ts
|
|
@ -1,40 +1,26 @@
|
|||
import * as core from "@actions/core";
|
||||
import { context, GitHub } from "@actions/github";
|
||||
|
||||
import { findPreviousComment, createComment, updateComment } from "./comment";
|
||||
async function run() {
|
||||
try {
|
||||
const repo = context.repo;
|
||||
const issue_number = context?.payload?.pull_request?.number;
|
||||
if (!issue_number) {
|
||||
const number = context?.payload?.pull_request?.number;
|
||||
const body = core.getInput("message");
|
||||
const githubToken = core.getInput("GITHUB_TOKEN");
|
||||
if (!number) {
|
||||
core.setFailed("This action only works for pull_request");
|
||||
return;
|
||||
}
|
||||
const body = core.getInput("message");
|
||||
const githubToken = core.getInput("GITHUB_TOKEN");
|
||||
if (!body || !githubToken) {
|
||||
core.setFailed("invalid input: please check your workflow");
|
||||
return;
|
||||
}
|
||||
const octokit = new GitHub(githubToken);
|
||||
const { data: comments } = await octokit.issues.listComments({
|
||||
...repo,
|
||||
issue_number
|
||||
});
|
||||
const myComment = comments.find(
|
||||
comment => comment.user.login === "github-actions[bot]"
|
||||
);
|
||||
if (myComment) {
|
||||
await octokit.issues.updateComment({
|
||||
...repo,
|
||||
comment_id: myComment.id,
|
||||
body
|
||||
});
|
||||
const previous = await findPreviousComment(octokit, repo, number);
|
||||
if (previous) {
|
||||
await updateComment(octokit, repo, previous.id, body);
|
||||
} else {
|
||||
await octokit.issues.createComment({
|
||||
...repo,
|
||||
issue_number,
|
||||
body
|
||||
});
|
||||
await createComment(octokit, repo, number, body);
|
||||
}
|
||||
} catch ({ message }) {
|
||||
core.setFailed(message);
|
||||
|
|
|
|||
Loading…
Reference in a new issue