feat: allowing action to run on non PR triggers

This commit is contained in:
Michael Shick 2020-04-08 16:39:41 -07:00
parent 0dbf59e6db
commit 5aa3ae408c
No known key found for this signature in database
GPG key ID: ADF5BC9704BB4A61
7 changed files with 116 additions and 29 deletions

22
.github/workflows/test-master.yml vendored Normal file
View file

@ -0,0 +1,22 @@
name: "test-local"
on:
push:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Add Comment
uses: ./
with:
message: |
**Hello MASTER!**
🌏
!
repo-token: ${{ secrets.GITHUB_TOKEN }}
allow-repeats: false

View file

@ -7,13 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: npm ci
- uses: ./
with:
message: |
**Hello!**
**Hello PULL!**
🌏
!
repo-token: ${{ secrets.GITHUB_TOKEN }}

View file

@ -10,7 +10,7 @@ inputs:
allow-repeats:
description: "allow messages to be repeated"
required: false
default: false
default: "false"
branding:
icon: message-circle
color: purple

50
dist/index.js vendored
View file

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

View file

@ -1,5 +1,26 @@
const core = require("@actions/core");
const github = require("@actions/github");
const { HttpClient, Headers } = require("@actions/http-client");
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 {
@ -11,17 +32,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);
@ -32,11 +65,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) {
@ -50,7 +83,7 @@ async function run() {
owner,
repo,
issue_number: issueNumber,
body: message
body: message,
});
core.setOutput("comment-created", "true");

12
package-lock.json generated
View file

@ -20,9 +20,9 @@
}
},
"@actions/http-client": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.6.tgz",
"integrity": "sha512-LGmio4w98UyGX33b/W6V6Nx/sQHRXZ859YlMkn36wPsXPB82u8xTVlA/Dq2DXrm6lEq9RVmisRJa1c+HETAIJA==",
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.7.tgz",
"integrity": "sha512-PY3ys/XH5WMekkHyZhYSa/scYvlE5T/TV/T++vABHuY5ZRgtiBZkn2L2tV5Pv/xDCl59lSZb9WwRuWExDyAsSg==",
"requires": {
"tunnel": "0.0.6"
}
@ -203,9 +203,9 @@
"integrity": "sha512-uM4mnmsIIPK/yeO+42F2RQhGUIs39K2RFmugcJANppXe6J1nvH87PvzPZYpza7Xhhs8Yn9yIAVdLZ84z61+0xQ=="
},
"@zeit/ncc": {
"version": "0.22.0",
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.0.tgz",
"integrity": "sha512-zaS6chwztGSLSEzsTJw9sLTYxQt57bPFBtsYlVtbqGvmDUsfW7xgXPYofzFa1kB9ur2dRop6IxCwPnWLBVCrbQ==",
"version": "0.22.1",
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.1.tgz",
"integrity": "sha512-Qq3bMuonkcnV/96jhy9SQYdh39NXHxNMJ1O31ZFzWG9n52fR2DLtgrNzhj/ahlEjnBziMLGVWDbaS9sf03/fEw==",
"dev": true
},
"acorn": {

View file

@ -28,10 +28,11 @@
"homepage": "https://github.com/mshick/add-pr-comment#readme",
"dependencies": {
"@actions/core": "^1.2.3",
"@actions/github": "^2.1.1"
"@actions/github": "^2.1.1",
"@actions/http-client": "^1.0.7"
},
"devDependencies": {
"@zeit/ncc": "^0.22.0",
"@zeit/ncc": "^0.22.1",
"eslint": "^6.8.0"
}
}