diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml index f311fed4..8f304fa6 100644 --- a/.github/workflows/sonar.yaml +++ b/.github/workflows/sonar.yaml @@ -26,9 +26,9 @@ jobs: sonarcloud: name: Upload to SonarCloud runs-on: ubuntu-22.04 - if: github.event.workflow_run.conclusion == 'success' + if: github.event.workflow_run.conclusion == 'success' && github.repository_owner == 'apache' steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: repository: ${{ github.event.workflow_run.head_repository.full_name }} ref: ${{ github.event.workflow_run.head_sha }} @@ -36,7 +36,7 @@ jobs: - name: Install sonar-scanner and build-wrapper uses: SonarSource/sonarcloud-github-c-cpp@v2 - name: 'Download code coverage' - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ @@ -59,43 +59,40 @@ jobs: run: | unzip sonarcloud-data.zip -d sonarcloud-data ls -a sonarcloud-data - - - uses: actions/setup-python@v4 + + - name: Check upload + id : check + # We only run the sonar-scanner during a commit event on the unstable branch. + run: | + ref=$(jq -r '.ref' github-event.json) + if [[ -n "$ref" ]] && [[ "$ref" != "refs/heads/unstable" ]]; then + echo "run_flag=false" >> $GITHUB_OUTPUT + else + echo "run_flag=true" >> $GITHUB_OUTPUT + fi + + - uses: actions/setup-python@v5 + if: ${{steps.check.outputs.run_flag == 'true'}} with: python-version: 3.x - name: Configure Kvrocks + if: ${{steps.check.outputs.run_flag == 'true'}} run: | ./x.py build -j$(nproc) --compiler gcc --skip-build - name: Run sonar-scanner + if: ${{steps.check.outputs.run_flag == 'true'}} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }} run: | PR_NUMBER=$(jq -r '.number | select (.!=null)' sonarcloud-data/github-event.json) - HEAD_REF=$(jq -r '.pull_request.head.ref | select (.!=null)' sonarcloud-data/github-event.json) - BASE_REF=$(jq -r '.pull_request.base.ref | select (.!=null)' sonarcloud-data/github-event.json) - REF=$(jq -r '.ref | select (.!=null)' sonarcloud-data/github-event.json) - - echo "The PR number is $PR_NUMBER, ref is $REF, head ref is $HEAD_REF, base ref is $BASE_REF" - - if [ -n "$PR_NUMBER" ]; then - git remote add upstream https://github.com/jihuayu/kvrocks-test.git - git fetch upstream - sonar-scanner \ - --define sonar.cfamily.build-wrapper-output="sonarcloud-data" \ - --define sonar.coverageReportPaths=sonarcloud-data/coverage.xml \ - --define sonar.projectKey=jihuayu_kvrocks-test \ - --define sonar.organization=jihuayu \ - --define sonar.scm.revision="${{ github.event.workflow_run.head_sha }}" \ - --define sonar.pullrequest.key="$PR_NUMBER" \ - --define sonar.pullrequest.branch="$HEAD_REF" \ - --define sonar.pullrequest.base=upstream/unstable - else - sonar-scanner \ - --define sonar.cfamily.build-wrapper-output="sonarcloud-data" \ - --define sonar.coverageReportPaths=sonarcloud-data/coverage.xml \ - --define sonar.projectKey=jihuayu_kvrocks-test \ - --define sonar.organization=jihuayu \ - --define sonar.scm.revision="${{ github.event.workflow_run.head_sha }}" - fi + echo "The PR number is $PR_NUMBER" + + sonar-scanner \ + --define sonar.cfamily.build-wrapper-output="sonarcloud-data" \ + --define sonar.coverageReportPaths=sonarcloud-data/coverage.xml \ + --define sonar.projectKey=apache_kvrocks \ + --define sonar.organization=apache \ + --define sonar.scm.revision=${{ github.event.workflow_run.head_sha }} \ + --define sonar.pullrequest.key=$PR_NUMBER