Skip to content

Commit 169e865

Browse files
authored
Fix sonarqube using base branch on fork for detecting new code in pr (matrix-org#2394)
* Fix sonarqube using base branch on fork for detecting new code in pr * Add comment * Tweak comment * Fix origin vs upstream * Stop wrongly using github.action_repository * Fix condition, we can add upstream always
1 parent 8803d2b commit 169e865

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Must only be called from a workflow_run in the context of the upstream repo
12
name: SonarCloud
23
on:
34
workflow_call:
@@ -35,13 +36,6 @@ on:
3536
type: string
3637
required: false
3738
description: The base branch of the PR if this workflow is being triggered due to one
38-
39-
# Org specific parameters
40-
main_branch:
41-
type: string
42-
required: false
43-
description: The default branch of the repository
44-
default: "develop"
4539
secrets:
4640
SONAR_TOKEN:
4741
required: true
@@ -57,13 +51,20 @@ jobs:
5751
ref: ${{ inputs.head_branch }} # checkout commit that triggered this workflow
5852
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
5953

60-
# Fetch develop so that Sonar can identify new issues in PR builds
61-
- name: "📕 Fetch ${{ inputs.main_branch }}"
62-
if: inputs.head_branch != inputs.main_branch
63-
run: git rev-parse HEAD && git fetch origin ${{ inputs.main_branch }}:${{ inputs.main_branch }} && git status && git rev-parse HEAD
54+
# Fetch base branch from the upstream repo so that Sonar can identify new code in PR builds
55+
- name: "📕 Fetch upstream base branch"
56+
# workflow_call retains the github context of the caller, so `repositoryUrl` will be upstream always due
57+
# to it running on `workflow_run` which is called from the context of the target repo and not the fork.
58+
if: inputs.base_branch
59+
run: |
60+
git remote add upstream ${{ github.repositoryUrl }}
61+
git rev-parse HEAD
62+
git fetch upstream ${{ inputs.base_branch }}:${{ inputs.base_branch }}
63+
git status
64+
git rev-parse HEAD
6465
6566
# There's a 'download artifact' action, but it hasn't been updated for the workflow_run action
66-
# (https://github.com/actions/download-artifact/issues/60) so instead we get this mess:
67+
# (https://github.com/actions/download-artifact/issues/60) so instead we get this alternative:
6768
- name: "📥 Download Coverage Report"
6869
uses: dawidd6/action-download-artifact@v2
6970
if: inputs.coverage_workflow_name

0 commit comments

Comments
 (0)