Skip to content

Commit 5029299

Browse files
Post results for pull request events under PR head
1 parent 1d4a6e2 commit 5029299

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
${{ runner.os }}-maven-
4949
- name: Build with Maven
5050
run: mvn -B verify checkstyle:checkstyle
51-
- uses: jwgmeligmeyling/checkstyle-github-action@v1
51+
- uses: jwgmeligmeyling/checkstyle-github-action@master
5252
with:
5353
path: '**/checkstyle-result.xml'
5454
```
@@ -70,13 +70,29 @@ And do not forget to enable XML output for the Maven plugin:
7070
</build>
7171
```
7272

73+
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.
74+
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`.
75+
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.
76+
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:
77+
78+
```yaml
79+
- uses: actions/checkout@v2
80+
with:
81+
ref: ${{ github.event.pull_request.head.sha }}
82+
```
83+
7384
## Other relevant actions
7485
This is a Github Action in a series of other GitHub Actions. Similar actions include:
7586
7687
* [checkstyle-github-action](https://github.com/jwgmeligmeyling/checkstyle-github-action)
7788
* [pmd-github-action](https://github.com/jwgmeligmeyling/pmd-github-action)
7889
* [spotbugs-github-action](https://github.com/jwgmeligmeyling/spotbugs-github-action)
7990
91+
## Known limitations
92+
Due to GitHub API limitations, we cannot specify to which Workflow Run (or underlying Check Suite) a newly created Check Run should be associated.
93+
As a result, workflows that trigger on several types of events, might push results under another event than the action was run in.
94+
For more information, see: https://github.com/jwgmeligmeyling/checkstyle-github-action/issues/2
95+
8096
## Contributing
8197
8298
Install the dependencies

dist/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8476,11 +8476,15 @@ function createCheck(name, title, annotations, numErrors, conclusion) {
84768476
return __awaiter(this, void 0, void 0, function* () {
84778477
core.info(`Uploading ${annotations.length} / ${numErrors} annotations to GitHub as ${name} with conclusion ${conclusion}`);
84788478
const octokit = github_2.getOctokit(core.getInput(constants_1.Inputs.Token));
8479-
const req = Object.assign(Object.assign({}, github_2.context.repo), { ref: github_2.context.sha });
8479+
let sha = github_2.context.sha;
8480+
if (github_2.context.payload.pull_request) {
8481+
sha = github_2.context.payload.pull_request.head.sha;
8482+
}
8483+
const req = Object.assign(Object.assign({}, github_2.context.repo), { ref: sha });
84808484
const res = yield octokit.checks.listForRef(req);
84818485
const existingCheckRun = res.data.check_runs.find(check => check.name === name);
84828486
if (!existingCheckRun) {
8483-
const createRequest = Object.assign(Object.assign({}, github_2.context.repo), { head_sha: github_2.context.sha, conclusion,
8487+
const createRequest = Object.assign(Object.assign({}, github_2.context.repo), { head_sha: sha, conclusion,
84848488
name, status: 'completed', output: {
84858489
title,
84868490
summary: `${numErrors} violation(s) found`,

src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ async function createCheck(
9595
`Uploading ${annotations.length} / ${numErrors} annotations to GitHub as ${name} with conclusion ${conclusion}`
9696
)
9797
const octokit = getOctokit(core.getInput(Inputs.Token))
98+
let sha = context.sha
99+
100+
if (context.payload.pull_request) {
101+
sha = context.payload.pull_request.head.sha
102+
}
103+
98104
const req = {
99105
...context.repo,
100-
ref: context.sha
106+
ref: sha
101107
}
102108

103109
const res = await octokit.checks.listForRef(req)
@@ -108,7 +114,7 @@ async function createCheck(
108114
if (!existingCheckRun) {
109115
const createRequest = {
110116
...context.repo,
111-
head_sha: context.sha,
117+
head_sha: sha,
112118
conclusion,
113119
name,
114120
status: <const>'completed',

0 commit comments

Comments
 (0)