sonar.yaml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. name: Sonar Code Coverage Upload
  2. on:
  3. workflow_run:
  4. workflows: ["Build/Test"]
  5. types: [completed]
  6. jobs:
  7. sonar:
  8. name: Sonar
  9. runs-on: ubuntu-latest
  10. if: github.event.workflow_run.conclusion == 'success'
  11. steps:
  12. - uses: actions/checkout@v4
  13. with:
  14. repository: ${{ github.event.workflow_run.head_repository.full_name }}
  15. ref: ${{ github.event.workflow_run.head_branch }}
  16. fetch-depth: 0
  17. - name: 'Download code coverage'
  18. uses: actions/github-script@v7
  19. with:
  20. script: |
  21. let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
  22. owner: context.repo.owner,
  23. repo: context.repo.repo,
  24. run_id: context.payload.workflow_run.id,
  25. });
  26. let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
  27. return artifact.name == "oc-code-coverage"
  28. })[0];
  29. let download = await github.rest.actions.downloadArtifact({
  30. owner: context.repo.owner,
  31. repo: context.repo.repo,
  32. artifact_id: matchArtifact.id,
  33. archive_format: 'zip',
  34. });
  35. let fs = require('fs');
  36. fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/oc-code-coverage.zip`, Buffer.from(download.data));
  37. - name: 'Unzip code coverage'
  38. run: unzip oc-code-coverage.zip -d coverage
  39. - name: set env vars
  40. run: |
  41. echo "SONAR_PR_NUM=$(cat coverage/pr_num.txt)" >> $GITHUB_ENV
  42. echo "SONAR_BASE=$(cat coverage/base.txt)" >> $GITHUB_ENV
  43. echo "SONAR_HEAD=$(cat coverage/head.txt)" >> $GITHUB_ENV
  44. # on develop branch, only run a baseline scan
  45. - name: SonarCloud Scan (Baseline)
  46. uses: sonarsource/sonarcloud-github-action@master
  47. if: env.SONAR_HEAD == 'develop'
  48. env:
  49. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  50. SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  51. with:
  52. args: >
  53. -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
  54. -Dsonar.projectKey=opencost_opencost
  55. -Dsonar.organization=opencost
  56. -Dsonar.branch.name=develop
  57. -Dsonar.branch.target=develop
  58. - name: SonarCloud Scan (PR)
  59. uses: sonarsource/sonarcloud-github-action@master
  60. if: env.SONAR_HEAD != 'develop'
  61. env:
  62. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  63. SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  64. with:
  65. args: >
  66. -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
  67. -Dsonar.pullrequest.key=${{ env.SONAR_PR_NUM }}
  68. -Dsonar.pullrequest.branch=${{ env.SONAR_HEAD }}
  69. -Dsonar.pullrequest.base=${{ env.SONAR_BASE }}
  70. -Dsonar.projectKey=opencost_opencost
  71. -Dsonar.organization=opencost