From 5aa3ae408c29648463d90ffafc8791d475b14222 Mon Sep 17 00:00:00 2001 From: Michael Shick Date: Wed, 8 Apr 2020 16:39:41 -0700 Subject: [PATCH] feat: allowing action to run on non PR triggers --- .github/workflows/test-master.yml | 22 +++++++++ .github/workflows/{test.yml => test-pr.yml} | 5 +-- action.yml | 2 +- dist/index.js | 50 +++++++++++++++++---- index.js | 49 ++++++++++++++++---- package-lock.json | 12 ++--- package.json | 5 ++- 7 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/test-master.yml rename .github/workflows/{test.yml => test-pr.yml} (86%) diff --git a/.github/workflows/test-master.yml b/.github/workflows/test-master.yml new file mode 100644 index 0000000..ac23c4c --- /dev/null +++ b/.github/workflows/test-master.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test-pr.yml similarity index 86% rename from .github/workflows/test.yml rename to .github/workflows/test-pr.yml index e044271..aa9b551 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test-pr.yml @@ -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 }} diff --git a/action.yml b/action.yml index 1eda09c..e0d27b8 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/dist/index.js b/dist/index.js index 5050208..abb2563 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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"; diff --git a/index.js b/index.js index fe1f760..d333d2c 100644 --- a/index.js +++ b/index.js @@ -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"); diff --git a/package-lock.json b/package-lock.json index 7d4835a..2e56a15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { diff --git a/package.json b/package.json index ec09cc0..9f740c3 100644 --- a/package.json +++ b/package.json @@ -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" } }