mirror of
https://github.com/mshick/add-pr-comment.git
synced 2026-01-01 14:49:44 +11:00
feat: allowing action to run on non PR triggers
This commit is contained in:
parent
0dbf59e6db
commit
5aa3ae408c
7 changed files with 116 additions and 29 deletions
50
dist/index.js
vendored
50
dist/index.js
vendored
|
|
@ -1987,6 +1987,27 @@ module.exports = require("os");
|
|||
|
||||
const core = __webpack_require__(470);
|
||||
const github = __webpack_require__(469);
|
||||
const { HttpClient, Headers } = __webpack_require__(539);
|
||||
|
||||
const getPulls = async (repoToken, repo, commitSha) => {
|
||||
const http = new HttpClient("http-client-add-pr-comment");
|
||||
|
||||
const additionalHeaders = {
|
||||
[Headers.Accept]: "application/vnd.github.sailor-v-preview+json",
|
||||
[Headers.Authorization]: `token ${repoToken}`,
|
||||
};
|
||||
|
||||
const response = await http.getJson(
|
||||
`https://api.github.com/repos/${repo}/commits/${commitSha}/pulls`,
|
||||
additionalHeaders
|
||||
);
|
||||
|
||||
const body = await response.readBody();
|
||||
|
||||
core.debug(body);
|
||||
|
||||
return body;
|
||||
};
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
|
|
@ -1998,17 +2019,29 @@ async function run() {
|
|||
core.debug(`input allow-repeats: ${allowRepeats}`);
|
||||
|
||||
const {
|
||||
payload: { pull_request: pullRequest, repository }
|
||||
payload: { pull_request: pullRequest, sha, repository },
|
||||
} = github.context;
|
||||
|
||||
if (!pullRequest) {
|
||||
core.error("this action only works on pull_request events");
|
||||
const { full_name: repoFullName } = repository;
|
||||
|
||||
let issueNumber;
|
||||
|
||||
if (pullRequest && pullRequest.number) {
|
||||
issueNumber = pullRequest.number;
|
||||
} else {
|
||||
// If this is not a pull request, attempt to find a PR matching the sha
|
||||
const pulls = await getPulls(repoToken, repoFullName, sha);
|
||||
issueNumber = pulls.length ? pulls[0].number : null;
|
||||
}
|
||||
|
||||
if (!issueNumber) {
|
||||
core.warning(
|
||||
"this action only works on pull_request events or other commits associated with a pull"
|
||||
);
|
||||
core.setOutput("comment-created", "false");
|
||||
return;
|
||||
}
|
||||
|
||||
const { number: issueNumber } = pullRequest;
|
||||
const { full_name: repoFullName } = repository;
|
||||
const [owner, repo] = repoFullName.split("/");
|
||||
|
||||
const octokit = new github.GitHub(repoToken);
|
||||
|
|
@ -2019,11 +2052,11 @@ async function run() {
|
|||
const { data: comments } = await octokit.issues.listComments({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber
|
||||
issue_number: issueNumber,
|
||||
});
|
||||
|
||||
const filteredComments = comments.filter(
|
||||
c => c.body === message && c.user.login === "github-actions[bot]"
|
||||
(c) => c.body === message && c.user.login === "github-actions[bot]"
|
||||
);
|
||||
|
||||
if (filteredComments.length) {
|
||||
|
|
@ -2037,7 +2070,7 @@ async function run() {
|
|||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
body: message
|
||||
body: message,
|
||||
});
|
||||
|
||||
core.setOutput("comment-created", "true");
|
||||
|
|
@ -7663,6 +7696,7 @@ var HttpCodes;
|
|||
HttpCodes[HttpCodes["RequestTimeout"] = 408] = "RequestTimeout";
|
||||
HttpCodes[HttpCodes["Conflict"] = 409] = "Conflict";
|
||||
HttpCodes[HttpCodes["Gone"] = 410] = "Gone";
|
||||
HttpCodes[HttpCodes["TooManyRequests"] = 429] = "TooManyRequests";
|
||||
HttpCodes[HttpCodes["InternalServerError"] = 500] = "InternalServerError";
|
||||
HttpCodes[HttpCodes["NotImplemented"] = 501] = "NotImplemented";
|
||||
HttpCodes[HttpCodes["BadGateway"] = 502] = "BadGateway";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue