Skip to content

Commit a39dbd1

Browse files
dfcoffinclaude
andcommitted
ci: Fix SonarCloud for fork PRs and harden CI workflows
- Add fork guard to ci.yml SonarCloud job to prevent failures when SONAR_TOKEN is unavailable on fork PRs - Add permissions (least-privilege) and concurrency (cancel stale runs) to pr-checks.yml - Remove duplicate sonarcloud-pr job from pr-checks.yml (handled by ci.yml for internal PRs, sonar-fork-pr.yml for fork PRs) - Remove continue-on-error from spotless and OWASP checks - Update sonar-fork-pr.yml with permissions, Maven cache, upstream fetch for proper diff, and separate build/analyze steps Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a413312 commit a39dbd1

3 files changed

Lines changed: 95 additions & 44 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ jobs:
8686
name: SonarCloud Analysis
8787
runs-on: ubuntu-latest
8888
needs: build-and-test
89+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
8990

9091
steps:
9192
- name: Checkout code

.github/workflows/pr-checks.yml

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ on:
44
pull_request:
55
types: [opened, synchronize, reopened]
66

7+
permissions:
8+
contents: read
9+
pull-requests: read
10+
11+
concurrency:
12+
group: pr-checks-${{ github.event.pull_request.number }}
13+
cancel-in-progress: true
14+
715
env:
816
JAVA_VERSION: '25'
917
MAVEN_OPTS: -Xmx3072m
@@ -46,52 +54,9 @@ jobs:
4654

4755
- name: Check code formatting
4856
run: mvn spotless:check
49-
continue-on-error: true
5057

5158
- name: Run quick tests
5259
run: mvn test -pl openespi-common,openespi-datacustodian
5360

5461
- name: Check for security vulnerabilities
55-
run: mvn org.owasp:dependency-check-maven:check -DfailBuildOnCVSS=8
56-
continue-on-error: true
57-
58-
sonarcloud-pr:
59-
name: SonarCloud PR Analysis
60-
runs-on: ubuntu-latest
61-
if: github.event.pull_request.head.repo.full_name == github.repository
62-
63-
steps:
64-
- name: Checkout code
65-
uses: actions/checkout@v4
66-
with:
67-
fetch-depth: 0
68-
69-
- name: Set up JDK 25
70-
uses: actions/setup-java@v4
71-
with:
72-
java-version: ${{ env.JAVA_VERSION }}
73-
distribution: 'temurin'
74-
cache: 'maven'
75-
76-
- name: Cache SonarCloud packages
77-
uses: actions/cache@v4
78-
with:
79-
path: ~/.sonar/cache
80-
key: ${{ runner.os }}-sonar
81-
restore-keys: ${{ runner.os }}-sonar
82-
83-
- name: Build modules for SonarCloud
84-
run: mvn clean verify -pl openespi-common,openespi-datacustodian,openespi-thirdparty -am
85-
86-
- name: Analyze with SonarCloud
87-
env:
88-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
90-
run: |
91-
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
92-
-Dsonar.projectKey=GreenButtonAlliance_OpenESPI-GreenButton-Java \
93-
-Dsonar.organization=greenbuttonalliance \
94-
-Dsonar.host.url=https://sonarcloud.io \
95-
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }} \
96-
-Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }} \
97-
-Dsonar.pullrequest.base=${{ github.event.pull_request.base.ref }}
62+
run: mvn org.owasp:dependency-check-maven:check -DfailBuildOnCVSS=8
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: SonarCloud Fork PR Analysis
2+
3+
on:
4+
workflow_run:
5+
workflows: ["CI/CD Pipeline"]
6+
types: [completed]
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
sonarcloud-fork:
14+
name: SonarCloud Analysis (Fork PR)
15+
runs-on: ubuntu-latest
16+
if: >
17+
github.event.workflow_run.event == 'pull_request' &&
18+
github.event.workflow_run.conclusion == 'success' &&
19+
github.event.workflow_run.head_repository.full_name != github.repository
20+
21+
steps:
22+
- name: Checkout fork PR code
23+
uses: actions/checkout@v4
24+
with:
25+
repository: ${{ github.event.workflow_run.head_repository.full_name }}
26+
ref: ${{ github.event.workflow_run.head_sha }}
27+
fetch-depth: 0
28+
29+
- name: Fetch base branch for diff
30+
run: |
31+
git remote add upstream https://github.com/${{ github.repository }}.git
32+
git fetch upstream
33+
34+
- name: Get PR number
35+
uses: actions/github-script@v7
36+
id: pr-number
37+
with:
38+
script: |
39+
const pulls = await github.rest.pulls.list({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
head: `${context.payload.workflow_run.head_repository.owner.login}:${context.payload.workflow_run.head_branch}`,
43+
state: 'open'
44+
});
45+
return pulls.data[0]?.number || '';
46+
result-encoding: string
47+
48+
- name: Set up JDK 25
49+
uses: actions/setup-java@v4
50+
with:
51+
java-version: '25'
52+
distribution: 'temurin'
53+
cache: 'maven'
54+
55+
- name: Cache Maven packages
56+
uses: actions/cache@v4
57+
with:
58+
path: ~/.m2/repository
59+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
60+
restore-keys: |
61+
${{ runner.os }}-maven-
62+
63+
- name: Cache SonarCloud packages
64+
uses: actions/cache@v4
65+
with:
66+
path: ~/.sonar/cache
67+
key: ${{ runner.os }}-sonar
68+
restore-keys: ${{ runner.os }}-sonar
69+
70+
- name: Build modules
71+
run: mvn clean verify -pl openespi-common,openespi-datacustodian,openespi-thirdparty -am
72+
73+
- name: Analyze with SonarCloud
74+
env:
75+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
77+
run: |
78+
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
79+
-Dsonar.projectKey=GreenButtonAlliance_OpenESPI-GreenButton-Java \
80+
-Dsonar.organization=greenbuttonalliance \
81+
-Dsonar.host.url=https://sonarcloud.io \
82+
-Dsonar.pullrequest.key=${{ steps.pr-number.outputs.result }} \
83+
-Dsonar.pullrequest.branch=${{ github.event.workflow_run.head_branch }} \
84+
-Dsonar.pullrequest.base=${{ github.event.workflow_run.pull_requests[0].base.ref || 'main' }} \
85+
-Dsonar.coverage.jacoco.xmlReportPaths=**/target/site/jacoco/jacoco.xml

0 commit comments

Comments
 (0)