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 runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- run: npm ci
- uses: ./ - uses: ./
with: with:
message: | message: |
**Hello!** **Hello PULL!**
🌏 🌏
! !
repo-token: ${{ secrets.GITHUB_TOKEN }} repo-token: ${{ secrets.GITHUB_TOKEN }}

View file

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

50
dist/index.js vendored
View file

@ -1987,6 +1987,27 @@ module.exports = require("os");
const core = __webpack_require__(470); const core = __webpack_require__(470);
const github = __webpack_require__(469); 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() { async function run() {
try { try {
@ -1998,17 +2019,29 @@ async function run() {
core.debug(`input allow-repeats: ${allowRepeats}`); core.debug(`input allow-repeats: ${allowRepeats}`);
const { const {
payload: { pull_request: pullRequest, repository } payload: { pull_request: pullRequest, sha, repository },
} = github.context; } = github.context;
if (!pullRequest) { const { full_name: repoFullName } = repository;
core.error("this action only works on pull_request events");
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"); core.setOutput("comment-created", "false");
return; return;
} }
const { number: issueNumber } = pullRequest;
const { full_name: repoFullName } = repository;
const [owner, repo] = repoFullName.split("/"); const [owner, repo] = repoFullName.split("/");
const octokit = new github.GitHub(repoToken); const octokit = new github.GitHub(repoToken);
@ -2019,11 +2052,11 @@ async function run() {
const { data: comments } = await octokit.issues.listComments({ const { data: comments } = await octokit.issues.listComments({
owner, owner,
repo, repo,
issue_number: issueNumber issue_number: issueNumber,
}); });
const filteredComments = comments.filter( 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) { if (filteredComments.length) {
@ -2037,7 +2070,7 @@ async function run() {
owner, owner,
repo, repo,
issue_number: issueNumber, issue_number: issueNumber,
body: message body: message,
}); });
core.setOutput("comment-created", "true"); core.setOutput("comment-created", "true");
@ -7663,6 +7696,7 @@ var HttpCodes;
HttpCodes[HttpCodes["RequestTimeout"] = 408] = "RequestTimeout"; HttpCodes[HttpCodes["RequestTimeout"] = 408] = "RequestTimeout";
HttpCodes[HttpCodes["Conflict"] = 409] = "Conflict"; HttpCodes[HttpCodes["Conflict"] = 409] = "Conflict";
HttpCodes[HttpCodes["Gone"] = 410] = "Gone"; HttpCodes[HttpCodes["Gone"] = 410] = "Gone";
HttpCodes[HttpCodes["TooManyRequests"] = 429] = "TooManyRequests";
HttpCodes[HttpCodes["InternalServerError"] = 500] = "InternalServerError"; HttpCodes[HttpCodes["InternalServerError"] = 500] = "InternalServerError";
HttpCodes[HttpCodes["NotImplemented"] = 501] = "NotImplemented"; HttpCodes[HttpCodes["NotImplemented"] = 501] = "NotImplemented";
HttpCodes[HttpCodes["BadGateway"] = 502] = "BadGateway"; HttpCodes[HttpCodes["BadGateway"] = 502] = "BadGateway";

View file

@ -1,5 +1,26 @@
const core = require("@actions/core"); const core = require("@actions/core");
const github = require("@actions/github"); 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() { async function run() {
try { try {
@ -11,17 +32,29 @@ async function run() {
core.debug(`input allow-repeats: ${allowRepeats}`); core.debug(`input allow-repeats: ${allowRepeats}`);
const { const {
payload: { pull_request: pullRequest, repository } payload: { pull_request: pullRequest, sha, repository },
} = github.context; } = github.context;
if (!pullRequest) { const { full_name: repoFullName } = repository;
core.error("this action only works on pull_request events");
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"); core.setOutput("comment-created", "false");
return; return;
} }
const { number: issueNumber } = pullRequest;
const { full_name: repoFullName } = repository;
const [owner, repo] = repoFullName.split("/"); const [owner, repo] = repoFullName.split("/");
const octokit = new github.GitHub(repoToken); const octokit = new github.GitHub(repoToken);
@ -32,11 +65,11 @@ async function run() {
const { data: comments } = await octokit.issues.listComments({ const { data: comments } = await octokit.issues.listComments({
owner, owner,
repo, repo,
issue_number: issueNumber issue_number: issueNumber,
}); });
const filteredComments = comments.filter( 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) { if (filteredComments.length) {
@ -50,7 +83,7 @@ async function run() {
owner, owner,
repo, repo,
issue_number: issueNumber, issue_number: issueNumber,
body: message body: message,
}); });
core.setOutput("comment-created", "true"); core.setOutput("comment-created", "true");

12
package-lock.json generated
View file

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

View file

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