pr_check.yml 10 KB

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