From f355d7120a8fb344490f806336c61151be98d03c Mon Sep 17 00:00:00 2001 From: Jan-Willem Gmelig Meyling Date: Wed, 17 Jun 2020 23:30:07 +0200 Subject: [PATCH] Post results for pull requests events under PR head, fixes #2 --- README.md | 21 +++++++++++++++++++-- dist/index.js | 8 ++++++-- src/main.ts | 10 ++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a861b25..cc1b781 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ jobs: ${{ runner.os }}-maven- - name: Build with Maven run: mvn -B verify pmd:pmd - - uses: jwgmeligmeyling/pmd-github-action@v1 + - uses: jwgmeligmeyling/pmd-github-action@master with: path: '**/pmd.xml' ``` @@ -72,6 +72,17 @@ And do not forget to enable XML output for the Maven plugin: ``` +Please make sure that by default workflows on `pull_request` events checkout [`refs/pull/:prNumber/merge`](https://help.github.com/en/actions/reference/events-that-trigger-workflows) instead of the head of the pull request. +Due to this, line numbers for the generated violations may not align with the actual line numbers to which they are displayed on the `HEAD`. +As it is, there is not really a sensible way to run this action on the merge commit of the pull request, because the result would be posted to an unnamed workflow for an otherwise invisible commit. +Even for `pull_request` events there is the possibility to checkout the pull request head instead. In order to do so, change your `checkout` action accordingly: + +```yaml +- uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} +``` + ## Other relevant actions This is a Github Action in a series of other GitHub Actions. Similar actions include: @@ -79,9 +90,15 @@ This is a Github Action in a series of other GitHub Actions. Similar actions inc * [pmd-github-action](https://github.com/jwgmeligmeyling/pmd-github-action) * [spotbugs-github-action](https://github.com/jwgmeligmeyling/spotbugs-github-action) +## Known limitations +Due to GitHub API limitations, we cannot specify to which Workflow Run (or underlying Check Suite) a newly created Check Run should be associated. +As a result, workflows that trigger on several types of events, might push results under another event than the action was run in. +For more information, see: https://github.com/jwgmeligmeyling/pmd-github-action/issues/4 + ## Contributing -Install the dependencies +Install the dependencies: + ```bash $ npm install ``` diff --git a/dist/index.js b/dist/index.js index 8a3da7c..a6592a1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8458,11 +8458,15 @@ function run() { function createCheck(name, title, annotations, numErrors) { return __awaiter(this, void 0, void 0, function* () { const octokit = github_1.getOctokit(core.getInput(constants_1.Inputs.Token)); - const req = Object.assign(Object.assign({}, github_1.context.repo), { ref: github_1.context.sha }); + let sha = github_1.context.sha; + if (github_1.context.payload.pull_request) { + sha = github_1.context.payload.pull_request.head.sha; + } + const req = Object.assign(Object.assign({}, github_1.context.repo), { ref: sha }); const res = yield octokit.checks.listForRef(req); const existingCheckRun = res.data.check_runs.find(check => check.name === name); if (!existingCheckRun) { - const createRequest = Object.assign(Object.assign({}, github_1.context.repo), { head_sha: github_1.context.sha, name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral', output: { + const createRequest = Object.assign(Object.assign({}, github_1.context.repo), { head_sha: sha, name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral', output: { title, summary: `${numErrors} violation(s) found`, annotations diff --git a/src/main.ts b/src/main.ts index cf72f04..9a7cab5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,9 +56,15 @@ async function createCheck( numErrors: number ): Promise { const octokit = getOctokit(core.getInput(Inputs.Token)) + let sha = context.sha + + if (context.payload.pull_request) { + sha = context.payload.pull_request.head.sha + } + const req = { ...context.repo, - ref: context.sha + ref: sha } const res = await octokit.checks.listForRef(req) @@ -69,7 +75,7 @@ async function createCheck( if (!existingCheckRun) { const createRequest = { ...context.repo, - head_sha: context.sha, + head_sha: sha, name, status: 'completed', conclusion: numErrors === 0 ? 'success' : 'neutral',