aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/pr_check.yml
blob: fbdf4c6d8806f85ec58aa7593f2f4d5a5713b41f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
name: PR-check
on:
  pull_request_target:
    branches:
      - 'main'
      - 'stable-*'
      - 'stream-nb-*'
      - '*-stable-*'
      - 'dev-*'
    types:
      - 'opened'
      - 'synchronize'
      - 'reopened'
      - 'labeled'
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true
jobs:
  check-running-allowed:
    if: ${{vars.CHECKS_SWITCH != '' && fromJSON(vars.CHECKS_SWITCH).pr_check == true}}
    runs-on: ubuntu-latest
    outputs:
      result: ${{ steps.check-ownership-membership.outputs.result == 'true' && steps.check-is-mergeable.outputs.result == 'true' }}
      commit_sha: ${{ steps.check-is-mergeable.outputs.commit_sha }}
    steps:
      - name: Reset integrated status
        run: |
          curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{github.repository}}/statuses/${{github.event.pull_request.head.sha}} \
            -d '{"state":"pending","description":"Waiting for relevant checks to complete","context":"checks_integrated"}'

      - name: Check if running tests is allowed
        id: check-ownership-membership
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
          script: |
            const labels = context.payload.pull_request.labels;
            const okToTestLabel = labels.find(
              label => label.name == 'ok-to-test'
            );
            
            console.log("okToTestLabel=%o", okToTestLabel !== undefined);
            
            if (okToTestLabel !== undefined) {
              return true;
            }
            
            // This is used primarily in forks. Repository owner
            // should be allowed to run anything.
            const userLogin = context.payload.pull_request.user.login;
            
            // How to interpret membership status code:
            // https://docs.github.com/rest/collaborators/collaborators#check-if-a-user-is-a-repository-collaborator
            const isRepoCollaborator = async function () {
              try {
                const response = await github.rest.repos.checkCollaborator({
                  owner: context.payload.repository.owner.login,
                  repo: context.payload.repository.name,
                  username: userLogin,
                });
                return response.status == 204;
              } catch (error) {
                if (error.status && error.status == 404) {
                  return false;
                }
                throw error;
              }
            }
            
            if (context.payload.repository.owner.login == userLogin) {
              console.log("You are the repository owner!");
              return true;
            }
            
            if (await isRepoCollaborator()) {
              console.log("You are a collaborator!");
              return true;
            }
            
            return false;
      - name: comment-if-waiting-on-ok
        if: steps.check-ownership-membership.outputs.result == 'false' &&
            github.event.action == 'opened'
        uses: actions/github-script@v7
        with:
          script: |
            let externalContributorLabel = 'external';
            
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              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!'
            });

            github.rest.issues.addLabels({
                ...context.repo,
                issue_number: context.issue.number,
                labels: [externalContributorLabel]
            });

      - name: cleanup-test-label
        uses: actions/github-script@v7
        with:
          script: |
            let labelsToRemove = ['ok-to-test', 'rebase-and-check'];
            const prNumber = context.payload.pull_request.number;
            const prLabels = new Set(context.payload.pull_request.labels.map(l => l.name));
            for await (const label of labelsToRemove.filter(l => prLabels.has(l))) {
              core.info(`remove label=${label} for pr=${prNumber}`);
              try {
                const result = await github.rest.issues.removeLabel({
                  ...context.repo,
                  issue_number: prNumber,
                  name: label
                });
              } catch(error) {
                // ignore the 404 error that arises
                // when the label did not exist for the
                // organization member
                if (error.status && error.status != 404) {
                  throw error;
                }
              }
            }
      - name: check is mergeable
        id: check-is-mergeable
        if: steps.check-ownership-membership.outputs.result == 'true'
        uses: actions/github-script@v7
        with:
          result-encoding: string
          script: |
            let pr = context.payload.pull_request;
            const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
            const header = `<!-- merge pr=${pr.number} -->\n`;
            
            const fail_msg = header + ':red_circle: Unable to merge your PR into the base branch. '
                + 'Please rebase or merge it with the base branch.'
            
            let i = 0;
            
            while (pr.mergeable == null && i < 60) {
                console.log("get pull-request status");
    
                let result = await github.rest.pulls.get({
                    ...context.repo,
                    pull_number: pr.number
                })
            
                pr = result.data;
              
                if (pr.mergeable == null) {
                  await delay(5000);
                }
            
                i += 1;
            }
            
            console.log("pr.mergeable=%o", pr.mergeable);
            
            if (pr.mergeable === null) {
                core.setFailed("Unable to check if the PR is mergeable, please re-run the check.");
                return false;
            }
            
            const { data: comments } = await github.rest.issues.listComments({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo
            });
            
            const commentToUpdate = comments.find(comment => comment.body.startsWith(header));
            
            if (!pr.mergeable) {
                let commentParams = {
                    ...context.repo,
                    issue_number: context.issue.number,
                    body: fail_msg
                };
            
                if (commentToUpdate) {
                    await github.rest.issues.updateComment({
                        ...commentParams,
                        comment_id: commentToUpdate.id,
                    });
                } else {
                    await github.rest.issues.createComment({...commentParams});
                }
                core.setFailed("Merge conflict detected");
                return false;
            } else if (commentToUpdate) {
                await github.rest.issues.deleteComment({
                    ...context.repo,
                    issue_number: context.issue.number,
                    comment_id: commentToUpdate.id,
                });
            }
            core.info(`commit_sha=${pr.commit_sha}`);
            core.setOutput('commit_sha', pr.merge_commit_sha);
            return true;
  build_and_test:
    needs:
      - check-running-allowed
    if: needs.check-running-allowed.outputs.result == 'true' && needs.check-running-allowed.outputs.commit_sha != ''
    strategy:
      fail-fast: false
      matrix:
        build_preset: ["relwithdebinfo", "release-asan"]
    runs-on: [ self-hosted, auto-provisioned, "${{ format('build-preset-{0}', matrix.build_preset) }}" ]
    name: Build and test ${{ matrix.build_preset }}
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        ref: ${{ needs.check-running-allowed.outputs.commit_sha }}
        fetch-depth: 2
    - name: Setup ydb access
      uses: ./.github/actions/setup_ci_ydb_service_account_key_file_credentials
      with:
        ci_ydb_service_account_key_file_credentials: ${{ secrets.CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS }}
    - name: Build and test
      uses: ./.github/actions/build_and_test_ya
      with:
        build_preset: ${{ matrix.build_preset }}
        build_target: "ydb/"
        increment: true
        run_tests: ${{ contains(fromJSON('["relwithdebinfo", "release-asan"]'), matrix.build_preset) }}
        test_size: "small,medium"
        test_threads: 52
        put_build_results_to_cache: true
        additional_ya_make_args: -DDEBUGINFO_LINES_ONLY  # we don't need full symbols in CI checks
        secs: ${{ format('{{"TESTMO_TOKEN2":"{0}","AWS_KEY_ID":"{1}","AWS_KEY_VALUE":"{2}","REMOTE_CACHE_USERNAME":"{3}","REMOTE_CACHE_PASSWORD":"{4}"}}',
          secrets.TESTMO_TOKEN2, secrets.AWS_KEY_ID, secrets.AWS_KEY_VALUE, secrets.REMOTE_CACHE_USERNAME, secrets.REMOTE_CACHE_PASSWORD ) }}
        vars: ${{ format('{{"AWS_BUCKET":"{0}","AWS_ENDPOINT":"{1}","REMOTE_CACHE_URL":"{2}","TESTMO_URL":"{3}","TESTMO_PROJECT_ID":"{4}"}}',
          vars.AWS_BUCKET, vars.AWS_ENDPOINT, vars.REMOTE_CACHE_URL_YA, vars.TESTMO_URL, vars.TESTMO_PROJECT_ID ) }}

  update_integrated_status:
    runs-on: ubuntu-latest
    needs: build_and_test
    if: always()
    steps:
      - name: Gather required checks results
        shell: bash
        run: |
          successbuilds=$(curl -L -X GET -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{github.repository}}/commits/${{github.event.pull_request.head.sha}}/status | \
            jq -cr '.statuses | .[] | select(.state=="success") | select(.context | (startswith("build_relwithdebinfo") or startswith("build_release-asan") or startswith("test_relwithdebinfo")) ) | .context' | \
            wc -l )
          if [[ $successbuilds == "3" ]];then
            integrated_status="success"
          else
            integrated_status="failure"
          fi
          curl -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{github.token}}" -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{github.repository}}/statuses/${{github.event.pull_request.head.sha}} \
            -d '{"state":"'$integrated_status'","description":"All checks completed","context":"checks_integrated"}'