aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/gate_postcommits.yml
blob: 69533a0685f564d89c8eb89ec31179a808809bd0 (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
name: Postcommits gate
on:
  schedule:
    - cron: "*/5 * * * *"  # Every 5 minutes
  workflow_dispatch:

jobs:
  switch_gate:
    runs-on: ubuntu-latest
    name: SwitchGate
    steps:
    - name: main
      shell: bash
      run: |
        queue_size=$(curl -Ls -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}}/actions/runs?per_page=1000\&page=1\&status=queued\&event=pull_request_target | \
          jq '[.workflow_runs[] | {name,status,id,updated_at,"waiting_seconds":(now - (.updated_at | fromdate)), html_url}]' | \
          jq '[.[] | select(.waiting_seconds > 60)] | length')
        echo "Queue size: $queue_size"
        if (( queue_size > 0 ));then
          echo "Close gate for postcommits"
          query='.runners[] | select(.labels[].name=="ghrun") | select( any([.labels[].name][]; .=="postcommit")) | .id'
        else
          echo "Open gate for postcommits"
          query='.runners[] | select(.labels[].name=="ghrun") | select( all([.labels[].name][]; .!="postcommit")) | .id'
        fi
        
        echo "Requesting a list of runners to update labels..."
        j1=$(curl -Ls -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
          -H "X-GitHub-Api-Version: 2022-11-28" -w "%{http_code}\n" \
          https://api.github.com/repos/${{github.repository}}/actions/runners?per_page=100\&page=1)
        http_code=$(echo "$j1" | tail -n 1)
        echo "Result code: $http_code"
        if [ "$http_code" != "200" ];then
          echo "HTTP result code is not 200, exiting"
          echo "$j1"
          exit 1
        fi
        # echo "$j1"
        
        echo "$j1" | sed '$d' | jq "$query" | while read x;do
          echo "Runner: $x"
          if (( queue_size > 0 ));then
            curl -Ls -X DELETE -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
              -H "X-GitHub-Api-Version: 2022-11-28" \
              https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels/postcommit
          else
            curl -Ls -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{secrets.GH_PERSONAL_ACCESS_TOKEN}}" \
              -H "X-GitHub-Api-Version: 2022-11-28" \
              https://api.github.com/repos/${{github.repository}}/actions/runners/$x/labels \
              -d '{"labels":["postcommit"]}'
          fi
        done
        echo "Runner labels synchronized"