mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2025-12-17 05:28:28 +00:00
commit
f850d3d060
5 changed files with 100 additions and 25 deletions
55
__tests__/comment.test.ts
Normal file
55
__tests__/comment.test.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import {
|
||||||
|
findPreviousComment,
|
||||||
|
createComment,
|
||||||
|
updateComment
|
||||||
|
} from "../src/comment";
|
||||||
|
const repo = {};
|
||||||
|
const body = "some message";
|
||||||
|
it("findPreviousComment", async () => {
|
||||||
|
const comment = {
|
||||||
|
user: {
|
||||||
|
login: "github-actions[bot]"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const otherComment = {
|
||||||
|
user: {
|
||||||
|
login: "some-user"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const octokit = {
|
||||||
|
issues: {
|
||||||
|
listComments: jest.fn(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
data: [otherComment, comment]
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(await findPreviousComment(octokit, repo, 123)).toBe(comment);
|
||||||
|
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
|
||||||
|
});
|
||||||
|
it("updateComment", async () => {
|
||||||
|
const octokit = {
|
||||||
|
issues: {
|
||||||
|
updateComment: jest.fn(() => Promise.resolve())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
expect(await updateComment(octokit, repo, 456, body)).toBeUndefined();
|
||||||
|
expect(octokit.issues.updateComment).toBeCalledWith({
|
||||||
|
comment_id: 456,
|
||||||
|
body
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("createComment", async () => {
|
||||||
|
const octokit = {
|
||||||
|
issues: {
|
||||||
|
createComment: jest.fn(() => Promise.resolve())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
expect(await createComment(octokit, repo, 456, body)).toBeUndefined();
|
||||||
|
expect(octokit.issues.createComment).toBeCalledWith({
|
||||||
|
issue_number: 456,
|
||||||
|
body
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
test("create a comment when no comment", async () => {});
|
|
||||||
test("update a comment when comment is exists", async () => {});
|
|
||||||
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 * as core from "@actions/core";
|
||||||
import { context, GitHub } from "@actions/github";
|
import { context, GitHub } from "@actions/github";
|
||||||
|
import { findPreviousComment, createComment, updateComment } from "./comment";
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
const repo = context.repo;
|
const repo = context.repo;
|
||||||
const issue_number = context?.payload?.pull_request?.number;
|
const number = context?.payload?.pull_request?.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(githubToken);
|
const octokit = new GitHub(githubToken);
|
||||||
const { data: comments } = await octokit.issues.listComments({
|
const previous = await findPreviousComment(octokit, repo, number);
|
||||||
...repo,
|
if (previous) {
|
||||||
issue_number
|
await updateComment(octokit, repo, previous.id, body);
|
||||||
});
|
|
||||||
const myComment = comments.find(
|
|
||||||
comment => comment.user.login === "github-actions[bot]"
|
|
||||||
);
|
|
||||||
if (myComment) {
|
|
||||||
await octokit.issues.updateComment({
|
|
||||||
...repo,
|
|
||||||
comment_id: myComment.id,
|
|
||||||
body
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
await octokit.issues.createComment({
|
await createComment(octokit, repo, number, body);
|
||||||
...repo,
|
|
||||||
issue_number,
|
|
||||||
body
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch ({ message }) {
|
} catch ({ message }) {
|
||||||
core.setFailed(message);
|
core.setFailed(message);
|
||||||
|
|
|
||||||
15
wallaby.js
Normal file
15
wallaby.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
module.exports = function(wallaby) {
|
||||||
|
return {
|
||||||
|
files: ["src/**/*.js?(x)", "!src/**/*.spec.ts?(x)"],
|
||||||
|
tests: ["__tests__/**/*.test.ts?(x)"],
|
||||||
|
|
||||||
|
env: {
|
||||||
|
type: "node",
|
||||||
|
runner: "node"
|
||||||
|
},
|
||||||
|
|
||||||
|
testFramework: "jest",
|
||||||
|
|
||||||
|
debug: true
|
||||||
|
};
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue