codecov_ats.yml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. name: IGNORE ME codecov-ats
  2. on:
  3. pull_request:
  4. env:
  5. CLI_VERSION: v0.1.5
  6. # Cancel in progress workflows on pull_requests.
  7. # https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value
  8. concurrency:
  9. group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  10. cancel-in-progress: true
  11. defaults:
  12. run:
  13. # the default default is:
  14. # bash --noprofile --norc -eo pipefail {0}
  15. shell: bash --noprofile --norc -eo pipefail -ux {0}
  16. jobs:
  17. files-changed:
  18. name: detect what files changed
  19. runs-on: ubuntu-24.04
  20. timeout-minutes: 3
  21. # Map a step output to a job output
  22. outputs:
  23. api_docs: ${{ steps.changes.outputs.api_docs }}
  24. backend: ${{ steps.changes.outputs.backend_all }}
  25. backend_dependencies: ${{ steps.changes.outputs.backend_dependencies }}
  26. backend_any_type: ${{ steps.changes.outputs.backend_any_type }}
  27. migration_lockfile: ${{ steps.changes.outputs.migration_lockfile }}
  28. steps:
  29. - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
  30. - name: Check for backend file changes
  31. uses: dorny/paths-filter@0bc4621a3135347011ad047f9ecf449bf72ce2bd # v3.0.0
  32. id: changes
  33. with:
  34. token: ${{ github.token }}
  35. filters: .github/file-filters.yml
  36. coverage-ats:
  37. if: needs.files-changed.outputs.backend == 'true'
  38. needs: files-changed
  39. runs-on: ubuntu-latest
  40. steps:
  41. - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
  42. with:
  43. # fetch-depth: 0 - Use if the BASE_COMMIT on codecov_automated_test_selection is at unknown depth
  44. # (i.e. git merge-base ${{ github.sha }}^ origin/main)
  45. # fetch-depth: 2 - Use if the BASE_COMMIT on codecov_automated_test_selection is at known depth
  46. # (i.e. git rev-parse ${{ github.sha }}^)
  47. fetch-depth: 0
  48. - name: Set up Python 3.10.10
  49. uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4
  50. with:
  51. python-version: '3.10.10'
  52. # We need the setup to collect the list of tests properly
  53. - name: Setup sentry env
  54. uses: ./.github/actions/setup-sentry
  55. id: setup
  56. with:
  57. mode: backend-ci
  58. - name: Download Codecov CLI
  59. run: |
  60. pip install --extra-index-url https://pypi.org/simple --no-cache-dir codecov-cli>=0.4.1
  61. # Creates the commit and report objects in codecov
  62. - name: Codecov startup
  63. run: |
  64. codecovcli create-commit
  65. codecovcli create-report
  66. env:
  67. CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
  68. # Sends static analysis information to codecov
  69. - name: Static Analysis
  70. run: |
  71. codecovcli static-analysis --token=${CODECOV_STATIC_TOKEN} \
  72. --folders-to-exclude .artifacts \
  73. --folders-to-exclude .github \
  74. --folders-to-exclude .venv \
  75. --folders-to-exclude static \
  76. --folders-to-exclude bin
  77. env:
  78. CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
  79. CODECOV_STATIC_TOKEN: ${{ secrets.CODECOV_STATIC_TOKEN }}
  80. # Run Automated Test Selection in dry mode to get the list of tests to run
  81. # The base commit will be the parent commit (apparently commits on master don't exist in codecov)
  82. - name: Codecov Automated Test Selection
  83. id: codecov_automated_test_selection
  84. run: |
  85. # Directory for the artifacts from this step
  86. mkdir .artifacts/codecov_ats
  87. # This is the base for the git diff BASE..HEAD
  88. BASE_COMMIT=$(git merge-base ${{ github.sha }}^ origin/master)
  89. # Get list of tests to run from Codecov
  90. output=$(codecovcli --codecov-yml-path=codecov.yml label-analysis --dry-run --token=${CODECOV_STATIC_TOKEN} --base-sha=${BASE_COMMIT}) || true
  91. # Post processing and validation
  92. if [ -n "${output}" ];
  93. then
  94. jq <<< $output '.runner_options + .ats_tests_to_run | @json' --raw-output > .artifacts/codecov_ats/tests_to_run.json
  95. jq <<< $output '.runner_options + .ats_tests_to_skip | @json' --raw-output > .artifacts/codecov_ats/tests_to_skip.json
  96. testcount() { jq <<< $output ".$1 | length"; }
  97. run_count=$(testcount ats_tests_to_run)
  98. skip_count=$(testcount ats_tests_to_skip)
  99. # Parse any potential errors that made ATS fallback to running all tests
  100. # And surface them
  101. ats_fallback_reason=$(jq <<< "$output" '.ats_fallback_reason')
  102. if [ "$ats_fallback_reason" == "null" ]; then
  103. ats_success=true
  104. else
  105. ats_success=false
  106. fi
  107. tee <<< \
  108. "{\"ats_success\": $ats_success, \"error\": $ats_fallback_reason, \"tests_to_run\": $run_count, \"tests_analyzed\": $((run_count+skip_count))}" \
  109. "$GITHUB_STEP_SUMMARY" \
  110. ".artifacts/codecov_ats/result.json"
  111. else
  112. # We need not forget to add the search options in the fallback command, otherwise pytest might run more tests than expected
  113. # These search options match what's defined in codecov.yml:105
  114. jq '@json' --raw-output <<< '[
  115. "--cov-context=test",
  116. "tests/sentry",
  117. "tests/integration",
  118. "--ignore=tests/sentry/eventstream/kafka",
  119. "--ignore=tests/sentry/post_process_forwarder",
  120. "--ignore=tests/sentry/snuba",
  121. "--ignore=tests/sentry/search/events",
  122. "--ignore=tests/sentry/ingest/ingest_consumer/test_ingest_consumer_kafka.py"
  123. ]' > .artifacts/codecov_ats/tests_to_skip.json
  124. echo '[]' > .artifacts/codecov_ats/tests_to_run.json
  125. # If we reached this point it means that ATS failed with some error
  126. tee <<< '{"ats_success": false, "error": "exception_raised"}' "$GITHUB_STEP_SUMMARY" ".artifacts/codecov_ats/result.json"
  127. fi
  128. env:
  129. CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
  130. CODECOV_STATIC_TOKEN: ${{ secrets.CODECOV_STATIC_TOKEN }}
  131. - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3
  132. with:
  133. name: codecov_ats
  134. path: .artifacts/codecov_ats
  135. if-no-files-found: error
  136. # The actual running of tests would come here, after the labels are available
  137. # Something like pytest <options> $ATS_TESTS_TO_RUN
  138. debug:
  139. runs-on: ubuntu-latest
  140. needs:
  141. - coverage-ats
  142. - files-changed
  143. # Avoids running this job if it's a frontend change
  144. # It would fail if the coverage-ats step didn't run
  145. if: needs.files-changed.outputs.backend == 'true'
  146. steps:
  147. - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3
  148. with:
  149. name: codecov_ats
  150. path: .artifacts
  151. - name: Debug ATS_TESTS_TO_RUN
  152. run: |
  153. length_of_tests=$(cat .artifacts/tests_to_run.json | jq 'length')
  154. # The 1st value doesn't count, it's '--cov-context=test' (hence -gt 1)
  155. if [ $length_of_tests -gt 1 ]; then
  156. echo "Running $length_of_tests tests"
  157. # --raw-output0 doesn't work.
  158. cat .artifacts/tests_to_run.json | jq 'join("\u0000")' --raw-output | tr -d '\n' | xargs -r0 echo 'pytest'
  159. else
  160. echo "No tests to run"
  161. fi
  162. - name: Debug ATS_TESTS_TO_SKIP
  163. run: |
  164. length_of_tests=$(cat .artifacts/tests_to_skip.json | jq 'length')
  165. # The 1st value doesn't count, it's '--cov-context=test'
  166. if [ $length_of_tests -gt 1 ]; then
  167. echo "Running $length_of_tests tests"
  168. # --raw-output0 doesn't work.
  169. cat .artifacts/tests_to_skip.json | jq 'join("\u0000")' --raw-output | tr -d '\n' | xargs -r0 echo 'pytest'
  170. else
  171. echo "No tests to run"
  172. fi