Skip to content

Commit

Permalink
Post results for pull requests events under PR head, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwgmeligmeyling committed Jun 17, 2020
1 parent 2e85390 commit f355d71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```
Expand All @@ -72,16 +72,33 @@ And do not forget to enable XML output for the Maven plugin:
</build>
```

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:
* [checkstyle-github-action](https://github.com/jwgmeligmeyling/checkstyle-github-action)
* [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
```
Expand Down
8 changes: 6 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ async function createCheck(
numErrors: number
): Promise<void> {
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)
Expand All @@ -69,7 +75,7 @@ async function createCheck(
if (!existingCheckRun) {
const createRequest = {
...context.repo,
head_sha: context.sha,
head_sha: sha,
name,
status: <const>'completed',
conclusion: numErrors === 0 ? <const>'success' : <const>'neutral',
Expand Down

0 comments on commit f355d71

Please sign in to comment.