build: build custom header implement

- c873eaf
- test it
This commit is contained in:
marocchino 2020-02-04 17:24:40 +09:00
parent c873eaf14f
commit 033d20d120
No known key found for this signature in database
GPG key ID: AFF521DBDB122570
5 changed files with 21 additions and 14 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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;

View file

@ -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 }) {

View file

@ -1,3 +1,7 @@
function headerComment(header) {
return `<!-- Sticky Pull Request Comment${header} -->`;
}
export async function findPreviousComment(octokit, repo, issue_number, header) { 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,
@ -20,7 +24,3 @@ export async function createComment(octokit, repo, issue_number, body, header) {
body: `${headerComment(header)}\n${body}` body: `${headerComment(header)}\n${body}`
}); });
} }
function headerComment(header) {
return `<!-- Sticky Pull Request Comment${header} -->`;
}