From 6eceaca8aca37cd2192bba7215f884d03ea74138 Mon Sep 17 00:00:00 2001 From: Keith Wall Date: Wed, 19 Jun 2024 09:23:26 +0100 Subject: [PATCH] Enable sonar in junit5 ext repo (#335) * Enable sonar in junit5 ext repo * Update .github/workflows/sonar.yaml Co-authored-by: Sam Barker Signed-off-by: Keith Wall * addressing review comments; --------- Signed-off-by: Keith Wall Co-authored-by: Sam Barker --- .github/workflows/maven.yml | 32 ++++++++++++++- .github/workflows/sonar.yaml | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/sonar.yaml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index e0e43266..574f7c62 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -37,14 +37,44 @@ jobs: with: java-version: 17 distribution: 'zulu' - cache: 'maven' + cache: maven + + - name: Cache SonarCloud packages + uses: actions/cache@v4 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar - name: 'Build junit extension with maven tests' + if: github.ref_name != 'main' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: mvn -B clean verify -Pci + - name: 'Build junit extension maven project on main' + if: github.event_name == 'push' && github.ref_name == 'main' + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: mvn -B clean verify -Pci org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar -Dsonar.projectKey=kroxylicious_kroxylicious-junit5-extension + - name: 'Run tests in containers' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: TEST_CLUSTER_EXECUTION_MODE=CONTAINER KAFKA_VERSION=latest mvn -B clean verify -Dsurefire.failIfNoSpecifiedTests=false -Dtest=KafkaClusterTest + - name: Save PR number to file + if: github.event_name == 'pull_request' && ${{ matrix.os }} == 'ubuntu-latest' + run: echo ${{ github.event.number }} > PR_NUMBER.txt + + - name: Archive PR number + if: github.event_name == 'pull_request' && ${{ matrix.os }} == 'ubuntu-latest' + uses: actions/upload-artifact@v4 + with: + name: PR_NUMBER + path: PR_NUMBER.txt + - name: Archive container logs uses: actions/upload-artifact@v4 if: failure() diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml new file mode 100644 index 00000000..3a507aaa --- /dev/null +++ b/.github/workflows/sonar.yaml @@ -0,0 +1,78 @@ +name: Sonar Scan + +on: + workflow_run: + workflows: [Build] + types: [completed] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }} + cancel-in-progress: true + +jobs: + Sonar: + runs-on: ubuntu-latest + if: github.event.workflow_run && github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' + steps: + - name: echo event + run: cat $GITHUB_EVENT_PATH + + - name: Download PR number artifact + uses: dawidd6/action-download-artifact@v5 + with: + workflow: Build + run_id: ${{ github.event.workflow_run.id }} + name: PR_NUMBER + + - name: Read PR_NUMBER.txt + id: pr_number + uses: juliangruber/read-file-action@v1 + with: + path: ./PR_NUMBER.txt + + - name: Request GitHub API for PR data + uses: octokit/request-action@v2.x + id: get_pr_data + with: + route: GET /repos/{full_name}/pulls/{number} + number: ${{ steps.pr_number.outputs.content }} + full_name: ${{ github.event.repository.full_name }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - uses: actions/checkout@v4 + with: + repository: ${{ github.event.workflow_run.head_repository.full_name }} + ref: ${{ github.event.workflow_run.head_branch }} + fetch-depth: 0 + + - name: Checkout base branch + run: | + git remote add upstream ${{ github.event.repository.clone_url }} + git fetch upstream + git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} + git checkout ${{ github.event.workflow_run.head_branch }} + git clean -ffdx && git reset --hard HEAD + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: 17 + distribution: 'zulu' + cache: maven + + - name: Cache SonarCloud packages + uses: actions/cache@v4 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + + - name: SonarCloud Scan on PR + run: mvn -B clean verify -Pci -DskipITs=true org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar -Dsonar.projectKey=kroxylicious_kroxylicious-junit5-extension -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} -Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} -Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} -Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}