pr_check.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. name: PR-check
  2. on:
  3. pull_request_target:
  4. branches:
  5. - 'main'
  6. - 'stable-*'
  7. - 'stream-nb-*'
  8. - '*-stable-*'
  9. - 'dev-*'
  10. types:
  11. - 'opened'
  12. - 'synchronize'
  13. - 'reopened'
  14. - 'labeled'
  15. concurrency:
  16. group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  17. cancel-in-progress: true
  18. jobs:
  19. check-running-allowed:
  20. if: ${{vars.CHECKS_SWITCH != '' && fromJSON(vars.CHECKS_SWITCH).pr_check == true}}
  21. runs-on: ubuntu-latest
  22. outputs:
  23. result: ${{ steps.check-ownership-membership.outputs.result == 'true' && steps.check-is-mergeable.outputs.result == 'true' }}
  24. commit_sha: ${{ steps.check-is-mergeable.outputs.commit_sha }}
  25. steps:
  26. - name: Reset integrated status
  27. run: |
  28. curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
  29. https://api.github.com/repos/${{github.repository}}/statuses/${{github.event.pull_request.head.sha}} \
  30. -d '{"state":"pending","description":"Waiting for relevant checks to complete","context":"checks_integrated"}'
  31. - name: Check if running tests is allowed
  32. id: check-ownership-membership
  33. uses: actions/github-script@v7
  34. with:
  35. github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
  36. script: |
  37. const labels = context.payload.pull_request.labels;
  38. const okToTestLabel = labels.find(
  39. label => label.name == 'ok-to-test'
  40. );
  41. console.log("okToTestLabel=%o", okToTestLabel !== undefined);
  42. if (okToTestLabel !== undefined) {
  43. return true;
  44. }
  45. // This is used primarily in forks. Repository owner
  46. // should be allowed to run anything.
  47. const userLogin = context.payload.pull_request.user.login;
  48. // How to interpret membership status code:
  49. // https://docs.github.com/rest/collaborators/collaborators#check-if-a-user-is-a-repository-collaborator
  50. const isRepoCollaborator = async function () {
  51. try {
  52. const response = await github.rest.repos.checkCollaborator({
  53. owner: context.payload.repository.owner.login,
  54. repo: context.payload.repository.name,
  55. username: userLogin,
  56. });
  57. return response.status == 204;
  58. } catch (error) {
  59. if (error.status && error.status == 404) {
  60. return false;
  61. }
  62. throw error;
  63. }
  64. }
  65. if (context.payload.repository.owner.login == userLogin) {
  66. console.log("You are the repository owner!");
  67. return true;
  68. }
  69. if (await isRepoCollaborator()) {
  70. console.log("You are a collaborator!");
  71. return true;
  72. }
  73. return false;
  74. - name: comment-if-waiting-on-ok
  75. if: steps.check-ownership-membership.outputs.result == 'false' &&
  76. github.event.action == 'opened'
  77. uses: actions/github-script@v7
  78. with:
  79. script: |
  80. let externalContributorLabel = 'external';
  81. github.rest.issues.createComment({
  82. issue_number: context.issue.number,
  83. owner: context.repo.owner,
  84. repo: context.repo.repo,
  85. body: 'Hi! Thank you for contributing!\nThe tests on this PR will run after a maintainer adds an `ok-to-test` label to this PR manually. Thank you for your patience!'
  86. });
  87. github.rest.issues.addLabels({
  88. ...context.repo,
  89. issue_number: context.issue.number,
  90. labels: [externalContributorLabel]
  91. });
  92. - name: cleanup-test-label
  93. uses: actions/github-script@v7
  94. with:
  95. script: |
  96. let labelsToRemove = ['ok-to-test', 'rebase-and-check'];
  97. const prNumber = context.payload.pull_request.number;
  98. const prLabels = new Set(context.payload.pull_request.labels.map(l => l.name));
  99. for await (const label of labelsToRemove.filter(l => prLabels.has(l))) {
  100. core.info(`remove label=${label} for pr=${prNumber}`);
  101. try {
  102. const result = await github.rest.issues.removeLabel({
  103. ...context.repo,
  104. issue_number: prNumber,
  105. name: label
  106. });
  107. } catch(error) {
  108. // ignore the 404 error that arises
  109. // when the label did not exist for the
  110. // organization member
  111. if (error.status && error.status != 404) {
  112. throw error;
  113. }
  114. }
  115. }
  116. - name: check is mergeable
  117. id: check-is-mergeable
  118. if: steps.check-ownership-membership.outputs.result == 'true'
  119. uses: actions/github-script@v7
  120. with:
  121. result-encoding: string
  122. script: |
  123. let pr = context.payload.pull_request;
  124. const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
  125. const header = `<!-- merge pr=${pr.number} -->\n`;
  126. const fail_msg = header + ':red_circle: Unable to merge your PR into the base branch. '
  127. + 'Please rebase or merge it with the base branch.'
  128. let i = 0;
  129. while (pr.mergeable == null && i < 60) {
  130. console.log("get pull-request status");
  131. let result = await github.rest.pulls.get({
  132. ...context.repo,
  133. pull_number: pr.number
  134. })
  135. pr = result.data;
  136. if (pr.mergeable == null) {
  137. await delay(5000);
  138. }
  139. i += 1;
  140. }
  141. console.log("pr.mergeable=%o", pr.mergeable);
  142. if (pr.mergeable === null) {
  143. core.setFailed("Unable to check if the PR is mergeable, please re-run the check.");
  144. return false;
  145. }
  146. const { data: comments } = await github.rest.issues.listComments({
  147. issue_number: context.issue.number,
  148. owner: context.repo.owner,
  149. repo: context.repo.repo
  150. });
  151. const commentToUpdate = comments.find(comment => comment.body.startsWith(header));
  152. if (!pr.mergeable) {
  153. let commentParams = {
  154. ...context.repo,
  155. issue_number: context.issue.number,
  156. body: fail_msg
  157. };
  158. if (commentToUpdate) {
  159. await github.rest.issues.updateComment({
  160. ...commentParams,
  161. comment_id: commentToUpdate.id,
  162. });
  163. } else {
  164. await github.rest.issues.createComment({...commentParams});
  165. }
  166. core.setFailed("Merge conflict detected");
  167. return false;
  168. } else if (commentToUpdate) {
  169. await github.rest.issues.deleteComment({
  170. ...context.repo,
  171. issue_number: context.issue.number,
  172. comment_id: commentToUpdate.id,
  173. });
  174. }
  175. core.info(`commit_sha=${pr.commit_sha}`);
  176. core.setOutput('commit_sha', pr.merge_commit_sha);
  177. return true;
  178. build_and_test:
  179. needs:
  180. - check-running-allowed
  181. if: needs.check-running-allowed.outputs.result == 'true' && needs.check-running-allowed.outputs.commit_sha != ''
  182. strategy:
  183. fail-fast: false
  184. matrix:
  185. build_preset: ["relwithdebinfo", "release-asan"]
  186. runs-on: [ self-hosted, auto-provisioned, "${{ format('build-preset-{0}', matrix.build_preset) }}" ]
  187. name: Build and test ${{ matrix.build_preset }}
  188. steps:
  189. - name: Checkout
  190. uses: actions/checkout@v4
  191. with:
  192. ref: ${{ needs.check-running-allowed.outputs.commit_sha }}
  193. fetch-depth: 2
  194. - name: Setup ydb access
  195. uses: ./.github/actions/setup_ci_ydb_service_account_key_file_credentials
  196. with:
  197. ci_ydb_service_account_key_file_credentials: ${{ secrets.CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS }}
  198. - name: Build and test
  199. uses: ./.github/actions/build_and_test_ya
  200. with:
  201. build_preset: ${{ matrix.build_preset }}
  202. build_target: "ydb/"
  203. increment: true
  204. run_tests: ${{ contains(fromJSON('["relwithdebinfo", "release-asan"]'), matrix.build_preset) }}
  205. test_size: "small,medium"
  206. test_threads: 52
  207. put_build_results_to_cache: true
  208. additional_ya_make_args: -DDEBUGINFO_LINES_ONLY # we don't need full symbols in CI checks
  209. secs: ${{ format('{{"TESTMO_TOKEN2":"{0}","AWS_KEY_ID":"{1}","AWS_KEY_VALUE":"{2}","REMOTE_CACHE_USERNAME":"{3}","REMOTE_CACHE_PASSWORD":"{4}"}}',
  210. secrets.TESTMO_TOKEN2, secrets.AWS_KEY_ID, secrets.AWS_KEY_VALUE, secrets.REMOTE_CACHE_USERNAME, secrets.REMOTE_CACHE_PASSWORD ) }}
  211. vars: ${{ format('{{"AWS_BUCKET":"{0}","AWS_ENDPOINT":"{1}","REMOTE_CACHE_URL":"{2}","TESTMO_URL":"{3}","TESTMO_PROJECT_ID":"{4}"}}',
  212. vars.AWS_BUCKET, vars.AWS_ENDPOINT, vars.REMOTE_CACHE_URL_YA, vars.TESTMO_URL, vars.TESTMO_PROJECT_ID ) }}
  213. update_integrated_status:
  214. runs-on: ubuntu-latest
  215. needs: build_and_test
  216. if: always()
  217. steps:
  218. - name: Gather required checks results
  219. shell: bash
  220. run: |
  221. successbuilds=$(curl -L -X GET -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
  222. https://api.github.com/repos/${{github.repository}}/commits/${{github.event.pull_request.head.sha}}/status | \
  223. jq -cr '.statuses | .[] | select(.state=="success") | select(.context | (startswith("build_relwithdebinfo") or startswith("build_release-asan") or startswith("test_relwithdebinfo")) ) | .context' | \
  224. wc -l )
  225. if [[ $successbuilds == "3" ]];then
  226. integrated_status="success"
  227. else
  228. integrated_status="failure"
  229. fi
  230. curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
  231. https://api.github.com/repos/${{github.repository}}/statuses/${{github.event.pull_request.head.sha}} \
  232. -d '{"state":"'$integrated_status'","description":"All checks completed","context":"checks_integrated"}'