Skip to content

Commit 29c2b7d

Browse files
committed
Leave the code scanning report off by default
The report needs the security-events: write permission, which the default token does not have, so with it on by default every workflow that uses the action would have to add a permissions block, and one that did not would skip the upload on every run. The code-scanning input now defaults to false; a workflow that wants the alert sets it to true and grants the permission, as the README shows. The example workflow loses the permissions block it no longer needs. Test Action turns the report on in both jobs: with the permission in the mode=changed job, where the upload has to go through, and without it on the first run of the platform matrix, where it has to be skipped without failing the check.
1 parent 00ed0e4 commit 29c2b7d

4 files changed

Lines changed: 14 additions & 15 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ jobs:
1515
permissions:
1616
contents: read
1717
pull-requests: read # gh pr diff
18-
security-events: write # the action reports its version check to code scanning
18+
security-events: write # the code scanning upload
1919
steps:
2020
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2121
with:
2222
fetch-depth: 0
2323
- uses: ./
2424
with:
2525
mode: changed
26+
code-scanning: true # with the permission: the upload has to go through
2627

2728
# The action and the hook on every platform they support, with a file that is formatted and one that
2829
# is not. The repository itself has no Java files, so the tests write their own.
2930
test-platforms:
3031
name: Test on ${{ matrix.os }}
3132
runs-on: ${{ matrix.os }}
3233
permissions:
33-
contents: read # no security-events: the action has to do without the code scanning upload
34+
contents: read # no security-events: the code scanning upload has to be skipped, not fail the check
3435
strategy:
3536
fail-fast: false
3637
matrix:
@@ -48,6 +49,7 @@ jobs:
4849
uses: ./
4950
with:
5051
mode: all
52+
code-scanning: true # without the permission, see above
5153

5254
- name: Write a file that is not formatted
5355
run: printf 'class Unformatted{int x;}\n' > Unformatted.java

‎README.md‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ v2 is for open-java-format 2.98.0.2 and later, which has a single style. A workf
2222
|-------|----------|---------|-------------|
2323
| `version` | yes | `2.98.0.6` | Version of the open-java-format native binary, one of those on [Maven Central](https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/) |
2424
| `mode` | no | `changed` | `changed` — only files from PR or push; `all` — every `.java` file in repo |
25-
| `code-scanning` | no | `true` | Report the version check to [code scanning](#code-scanning) as well; `false` keeps it to the log warning |
25+
| `code-scanning` | no | `false` | Also report the version check to [code scanning](#code-scanning); needs the `security-events: write` permission |
2626

2727
## How `mode: changed` works
2828

@@ -34,7 +34,7 @@ v2 is for open-java-format 2.98.0.2 and later, which has a single style. A workf
3434

3535
- If all files are formatted correctly → passes ✅
3636
- If any file has formatting issues → lists unformatted files and fails ❌
37-
- If a newer open-java-format release is out → warns in the job log, and raises a [code scanning alert](#code-scanning) when the job allows it
37+
- If a newer open-java-format release is out → warns in the job log, and raises a [code scanning alert](#code-scanning) when that is turned on
3838

3939
Files are checked in the open-java-format style, the formatter's only style. When the check fails, fix locally with a native binary or the runnable jar from the [open-java-format releases](https://github.com/openjavaformat/open-java-format/releases):
4040

@@ -90,9 +90,9 @@ jobs:
9090

9191
## Code scanning
9292

93-
The action compares the `version` it runs with the latest [open-java-format release](https://github.com/openjavaformat/open-java-format/releases) and warns in the job log when a newer one is out. A log warning is easy to miss, so the check is also reported to code scanning: the action uploads a SARIF file with one alert while the version is behind, and with none once it is current, which closes the alert. The alert shows up under **Security → Code scanning** and in the **Code scanning results** check on pull requests, and points at the line of the workflow that names the version.
93+
The action compares the `version` it runs with the latest [open-java-format release](https://github.com/openjavaformat/open-java-format/releases) and warns in the job log when a newer one is out. A log warning is easy to miss, so the check can also be reported to code scanning: the action then uploads a SARIF file with one alert while the version is behind, and with none once it is current, which closes the alert. The alert shows up under **Security → Code scanning** and in the **Code scanning results** check on pull requests, and points at the line of the workflow that names the version.
9494

95-
The upload needs the `security-events: write` permission, which a job gets like this:
95+
This is off by default, because the upload needs the `security-events: write` permission, which the default token does not have. To turn it on, set `code-scanning: true` and give the job the permission:
9696

9797
```yaml
9898
jobs:
@@ -101,18 +101,19 @@ jobs:
101101
permissions:
102102
contents: read
103103
pull-requests: read # mode: changed reads the files of the pull request
104-
security-events: write # the action reports its version check to code scanning
104+
security-events: write # the code scanning upload
105105
steps:
106106
- uses: actions/checkout@v7
107107
108108
- uses: openjavaformat/open-java-format-action@v2
109109
with:
110110
version: '2.98.0.6'
111+
code-scanning: true
111112
```
112113

113-
Without the permission the upload is skipped with a line in the log, the check passes as before, and the log warning stays. Code scanning is free on public repositories; a private repository needs [GitHub Code Security](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-security-and-analysis-settings-for-your-repository) enabled. Pull requests from forks run with a read-only token and get the log warning only. To stop the upload altogether, set `code-scanning: false`.
114+
The `permissions` block replaces the default permissions of the job, so it has to name everything else the job needs, such as `contents: read` for the checkout. If the permission is missing anyway, the upload is skipped with a line in the log, the check passes as before, and the log warning stays. Code scanning is free on public repositories; a private repository needs [GitHub Code Security](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-security-and-analysis-settings-for-your-repository) enabled. Pull requests from forks run with a read-only token and get the log warning only.
114115

115-
One report per job is enough. When the job runs on a matrix, enable it on one leg only:
116+
One report per job is enough. When the job runs on a matrix, turn it on for one leg only:
116117

117118
```yaml
118119
with:

‎action.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ inputs:
1616
required: false
1717
default: 'changed'
1818
code-scanning:
19-
description: 'Report the version check to code scanning as well: "true" raises an alert under Security → Code scanning while a newer open-java-format is out, "false" keeps the check to the log warning. The upload needs the security-events: write permission and is skipped without it.'
19+
description: 'Also report the version check to code scanning: "true" raises an alert under Security → Code scanning while a newer open-java-format is out. Off by default, because the upload needs the security-events: write permission; without it the upload is skipped.'
2020
required: false
21-
default: 'true'
21+
default: 'false'
2222

2323
runs:
2424
using: 'composite'

‎example-usage.yml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ jobs:
1010
format-check:
1111
name: Check Java formatting
1212
runs-on: ubuntu-latest
13-
permissions:
14-
contents: read
15-
pull-requests: read # mode: changed reads the files of the pull request
16-
security-events: write # the action reports its version check to code scanning
1713

1814
steps:
1915
- uses: actions/checkout@v7

0 commit comments

Comments
 (0)