Skip to content

Commit

Permalink
Split sonar cloud action to support pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Jul 31, 2024
1 parent c0a961a commit 53d7e26
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master, '*-ci' ]
pull_request:
branches: [ master ]
branches: [ master, '*-ci' ]
workflow_dispatch:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master, '*-ci' ]
pull_request:
branches: [ master ]
branches: [ master, '*-ci' ]
workflow_dispatch:

jobs:
Expand Down
64 changes: 57 additions & 7 deletions .github/workflows/sonar_cloud.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
name: SonarCloud C/C++ CI

on:
push:
branches: [ "master", '*-ci' ]
pull_request:
branches: [ "master" ]
workflow_dispatch:
workflow_run:
workflows: [ 'SonarCloud C/C++ CI Trigger' ]
types: [ completed ]

jobs:
build:
run:
name: sonar_cloud
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
env:
BUILD_WRAPPER_OUT_DIR: build_wrapper

steps:
- name: Checkout
uses: actions/checkout@v4
with:
# checkout out the proper repository in case of pull request
repository: ${{ github.event.workflow_run.head_repository.full_name }}
ref: ${{ github.event.workflow_run.head_branch }}
fetch-depth: 0 # shallow clones should be disabled for a better relevancy of analysis
submodules: recursive

# BEGIN PULL REQUEST

- name: Download pull request number artifact
if: github.event.workflow_run.event == 'pull_request'
uses: dawidd6/action-download-artifact@v6
with:
workflow: sonar_cloud_trigger
run_id: ${{ github.event.workflow_run.id }}
name: pull_request_number

- name: "Read pull request number"
if: github.event.workflow_run.event == 'pull_request'
id: pull_request_number
uses: juliangruber/read-file-action@v1
with:
path: ./pull_request_number.txt

- name: Request GitHub API for pull_request data
if: github.event.workflow_run.event == 'pull_request'
uses: octokit/[email protected]
id: get_pull_request_data
with:
route: GET /repos/${{ github.event.repository.full_name }}/pulls/${{ steps.pull_request_number.outputs.content }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout upstream base branch and checkout pull request head branch
if: github.event.workflow_run.event == 'pull_request'
run: |
git remote add upstream ${{ github.event.repository.clone_url }}
git fetch upstream
git checkout -B ${{ fromJson(steps.get_pull_request_data.outputs.data).base.ref }} \
upstream/${{ fromJson(steps.get_pull_request_data.outputs.data).base.ref }}
git checkout origin/${{ github.event.workflow_run.head_branch }}
git clean -ffdx && git reset --hard HEAD
- name: Set pull request variables
if: github.event.workflow_run.event == 'pull_request'
run: |
echo "PULL_REQUEST_KEY=${{ fromJson(steps.get_pull_request_data.outputs.data).number }}" >> ${GITHUB_ENV}
echo "PULL_REQUEST_BRANCH=${{ fromJson(steps.get_pull_request_data.outputs.data).head.ref }}" >> ${GITHUB_ENV}
echo "PULL_REQUEST_BASE=${{ fromJson(steps.get_pull_request_data.outputs.data).base.ref }}" >> ${GITHUB_ENV}
# END PULL REQUEST

- name: Install sonar-scanner and build-wrapper
uses: SonarSource/sonarcloud-github-c-cpp@v2

Expand Down Expand Up @@ -68,4 +115,7 @@ jobs:
--define sonar.tests="compiler/extensions/cpp/runtime/test,test" \
--define sonar.test.inclusions="compiler/**/test/**/*.h,compiler/**/test/**/.cpp,test/**/*.h,test/**/*.cpp" \
--define sonar.scm.exclusions.disabled="true" \
--define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json"
--define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json" \
--define sonar.pullrequest.key="${{ env.PULL_REQUEST_KEY }}" \
--define sonar.pullrequest.branch="${{ env.PULL_REQUEST_BRANCH }}" \
--define sonar.pullrequest.base="${{ env.PULL_REQUEST_BASE }}"
27 changes: 27 additions & 0 deletions .github/workflows/sonar_cloud_trigger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: SonarCloud C/C++ CI Trigger

on:
push:
branches: [ master, '*-ci' ]
pull_request:
branches: [ master, '*-ci' ]
workflow_dispatch:

jobs:
run:
name: sonar_cloud_trigger
runs-on: ubuntu-latest

# just save the pull request number in case of pull request
steps:
- name: Save pull request number to file
if: github.event_name == 'pull_request'
run: echo ${{ github.event.number }} > pull_request_number.txt

- name: Archive pull request number
if: github.event_name == 'pull_request'
# update when https://github.com/actions/upload-artifact/issues/543 is fixed
uses: actions/upload-artifact@v3
with:
name: pull_request_number
path: pull_request_number.txt

0 comments on commit 53d7e26

Please sign in to comment.