mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2025-12-15 20:56:31 +00:00
Merge pull request #52 from marocchino/custom-comment-header
feat: add ability to set custom header
This commit is contained in:
commit
682de6733d
8 changed files with 72 additions and 27 deletions
|
|
@ -10,5 +10,7 @@ jobs:
|
||||||
- uses: ./
|
- uses: ./
|
||||||
with:
|
with:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
header: FromPR
|
||||||
message: |
|
message: |
|
||||||
Test ${{ github.sha }} is successfully ended.
|
Test ${{ github.sha }} is successfully ended.
|
||||||
|
This is message from PR.
|
||||||
|
|
|
||||||
1
.github/workflows/comment_on_push.yml
vendored
1
.github/workflows/comment_on_push.yml
vendored
|
|
@ -17,3 +17,4 @@ jobs:
|
||||||
number: ${{ steps.finder.outputs.pr }}
|
number: ${{ steps.finder.outputs.pr }}
|
||||||
message: |
|
message: |
|
||||||
Test ${{ github.sha }} is successfully ended.
|
Test ${{ github.sha }} is successfully ended.
|
||||||
|
This is message from push.
|
||||||
|
|
|
||||||
|
|
@ -11,23 +11,40 @@ it("findPreviousComment", async () => {
|
||||||
},
|
},
|
||||||
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
|
body: "<!-- Sticky Pull Request Comment -->\nprevious message"
|
||||||
};
|
};
|
||||||
const otherComment = {
|
const commentWithCustomHeader = {
|
||||||
user: {
|
user: {
|
||||||
login: "some-user"
|
login: "github-actions[bot]"
|
||||||
},
|
},
|
||||||
body: "lgtm"
|
body: "<!-- Sticky Pull Request CommentTypeA -->\nprevious message"
|
||||||
};
|
};
|
||||||
|
const otherComments = [
|
||||||
|
{
|
||||||
|
user: {
|
||||||
|
login: "some-user"
|
||||||
|
},
|
||||||
|
body: "lgtm"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
user: {
|
||||||
|
login: "github-actions[bot]"
|
||||||
|
},
|
||||||
|
body: "<!-- Sticky Pull Request CommentTypeB -->\nprevious message"
|
||||||
|
}
|
||||||
|
];
|
||||||
const octokit = {
|
const octokit = {
|
||||||
issues: {
|
issues: {
|
||||||
listComments: jest.fn(() =>
|
listComments: jest.fn(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
data: [otherComment, comment]
|
data: [commentWithCustomHeader, comment, ...otherComments]
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(await findPreviousComment(octokit, repo, 123)).toBe(comment);
|
expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment);
|
||||||
|
expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe(
|
||||||
|
commentWithCustomHeader
|
||||||
|
);
|
||||||
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
|
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
|
||||||
});
|
});
|
||||||
it("updateComment", async () => {
|
it("updateComment", async () => {
|
||||||
|
|
@ -37,12 +54,19 @@ it("updateComment", async () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
await updateComment(octokit, repo, 456, "hello there")
|
await updateComment(octokit, repo, 456, "hello there", "")
|
||||||
).toBeUndefined();
|
).toBeUndefined();
|
||||||
expect(octokit.issues.updateComment).toBeCalledWith({
|
expect(octokit.issues.updateComment).toBeCalledWith({
|
||||||
comment_id: 456,
|
comment_id: 456,
|
||||||
body: "<!-- Sticky Pull Request Comment -->\nhello there"
|
body: "<!-- Sticky Pull Request Comment -->\nhello there"
|
||||||
});
|
});
|
||||||
|
expect(
|
||||||
|
await updateComment(octokit, repo, 456, "hello there", "TypeA")
|
||||||
|
).toBeUndefined();
|
||||||
|
expect(octokit.issues.updateComment).toBeCalledWith({
|
||||||
|
comment_id: 456,
|
||||||
|
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it("createComment", async () => {
|
it("createComment", async () => {
|
||||||
const octokit = {
|
const octokit = {
|
||||||
|
|
@ -51,10 +75,17 @@ it("createComment", async () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
expect(
|
expect(
|
||||||
await createComment(octokit, repo, 456, "hello there")
|
await createComment(octokit, repo, 456, "hello there", "")
|
||||||
).toBeUndefined();
|
).toBeUndefined();
|
||||||
expect(octokit.issues.createComment).toBeCalledWith({
|
expect(octokit.issues.createComment).toBeCalledWith({
|
||||||
issue_number: 456,
|
issue_number: 456,
|
||||||
body: "<!-- Sticky Pull Request Comment -->\nhello there"
|
body: "<!-- Sticky Pull Request Comment -->\nhello there"
|
||||||
});
|
});
|
||||||
|
expect(
|
||||||
|
await createComment(octokit, repo, 456, "hello there", "TypeA")
|
||||||
|
).toBeUndefined();
|
||||||
|
expect(octokit.issues.createComment).toBeCalledWith({
|
||||||
|
issue_number: 456,
|
||||||
|
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@ name: "Sticky Pull Request Comment"
|
||||||
description: "Create comment on pull request, if exists update that comment."
|
description: "Create comment on pull request, if exists update that comment."
|
||||||
author: "marocchino"
|
author: "marocchino"
|
||||||
inputs:
|
inputs:
|
||||||
|
header:
|
||||||
|
description: "Header to determine if the comment is to be updated, not shown on screen"
|
||||||
|
required: false
|
||||||
message:
|
message:
|
||||||
description: "comment message"
|
description: "comment message"
|
||||||
required: true
|
required: true
|
||||||
|
|
|
||||||
|
|
@ -9,23 +9,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const HEADER = "<!-- Sticky Pull Request Comment -->";
|
function headerComment(header) {
|
||||||
function findPreviousComment(octokit, repo, issue_number) {
|
return `<!-- Sticky Pull Request Comment${header} -->`;
|
||||||
|
}
|
||||||
|
function findPreviousComment(octokit, repo, issue_number, header) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
|
const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
|
||||||
return comments.find(comment => comment.body.startsWith(HEADER));
|
const h = headerComment(header);
|
||||||
|
return comments.find(comment => comment.body.startsWith(h));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.findPreviousComment = findPreviousComment;
|
exports.findPreviousComment = findPreviousComment;
|
||||||
function updateComment(octokit, repo, comment_id, body) {
|
function updateComment(octokit, repo, comment_id, body, header) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${HEADER}\n${body}` }));
|
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${headerComment(header)}\n${body}` }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.updateComment = updateComment;
|
exports.updateComment = updateComment;
|
||||||
function createComment(octokit, repo, issue_number, body) {
|
function createComment(octokit, repo, issue_number, body, header) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${HEADER}\n${body}` }));
|
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${headerComment(header)}\n${body}` }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.createComment = createComment;
|
exports.createComment = createComment;
|
||||||
|
|
|
||||||
|
|
@ -31,14 +31,15 @@ function run() {
|
||||||
try {
|
try {
|
||||||
const repo = github_1.context.repo;
|
const repo = github_1.context.repo;
|
||||||
const body = core.getInput("message", { required: true });
|
const body = core.getInput("message", { required: true });
|
||||||
|
const header = core.getInput("header", { required: false }) || "";
|
||||||
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
||||||
const octokit = new github_1.GitHub(githubToken);
|
const octokit = new github_1.GitHub(githubToken);
|
||||||
const previous = yield comment_1.findPreviousComment(octokit, repo, number);
|
const previous = yield comment_1.findPreviousComment(octokit, repo, number, header);
|
||||||
if (previous) {
|
if (previous) {
|
||||||
yield comment_1.updateComment(octokit, repo, previous.id, body);
|
yield comment_1.updateComment(octokit, repo, previous.id, body, header);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
yield comment_1.createComment(octokit, repo, number, body);
|
yield comment_1.createComment(octokit, repo, number, body, header);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch ({ message }) {
|
catch ({ message }) {
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,26 @@
|
||||||
const HEADER = "<!-- Sticky Pull Request Comment -->";
|
function headerComment(header) {
|
||||||
|
return `<!-- Sticky Pull Request Comment${header} -->`;
|
||||||
|
}
|
||||||
|
|
||||||
export async function findPreviousComment(octokit, repo, issue_number) {
|
export async function findPreviousComment(octokit, repo, issue_number, header) {
|
||||||
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.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({
|
await octokit.issues.updateComment({
|
||||||
...repo,
|
...repo,
|
||||||
comment_id,
|
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({
|
await octokit.issues.createComment({
|
||||||
...repo,
|
...repo,
|
||||||
issue_number,
|
issue_number,
|
||||||
body: `${HEADER}\n${body}`
|
body: `${headerComment(header)}\n${body}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,14 @@ async function run() {
|
||||||
try {
|
try {
|
||||||
const repo = context.repo;
|
const repo = context.repo;
|
||||||
const body = core.getInput("message", { required: true });
|
const body = core.getInput("message", { required: true });
|
||||||
|
const header = core.getInput("header", { required: false }) || "";
|
||||||
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
||||||
const octokit = new GitHub(githubToken);
|
const octokit = new GitHub(githubToken);
|
||||||
const previous = await findPreviousComment(octokit, repo, number);
|
const previous = await findPreviousComment(octokit, repo, number, header);
|
||||||
if (previous) {
|
if (previous) {
|
||||||
await updateComment(octokit, repo, previous.id, body);
|
await updateComment(octokit, repo, previous.id, body, header);
|
||||||
} else {
|
} else {
|
||||||
await createComment(octokit, repo, number, body);
|
await createComment(octokit, repo, number, body, header);
|
||||||
}
|
}
|
||||||
} catch ({ message }) {
|
} catch ({ message }) {
|
||||||
core.setFailed(message);
|
core.setFailed(message);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue