summaryrefslogtreecommitdiffstats
path: root/.github/workflows/bot.yml
blob: 3e404d89c8c28ad22ae690f3c4328f7e1772ef4a (plain)
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
# WARNING:
# When extending this action, be aware that $GITHUB_TOKEN allows some write
# access to the GitHub API. This means that it should not evaluate user input in
# a way that allows code injection.

name: Bot

on:
  schedule:
    # Run every 10m
    # i.e., at each of the listed minutes, every hour
    - cron: '05,15,25,35,45,55 * * * *'
  workflow_call:
    inputs:
      headBranch:
        required: true
        type: string
    secrets:
      NIXPKGS_CI_APP_PRIVATE_KEY:
        required: true
  workflow_dispatch:

concurrency:
  # This explicitly avoids using `run_id` for the concurrency key to make sure that only
  # *one* scheduled run can run at a time.
  group: bot-${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number }}
  # PR-triggered runs will be cancelled, but scheduled runs will be queued.
  cancel-in-progress: ${{ github.event_name != 'schedule' }}

# This is used as fallback without app only.
# This happens when testing in forks without setting up that app.
permissions:
  issues: write # managing issue labels and comments
  pull-requests: write # managing pull request labels and comments

defaults:
  run:
    shell: bash

jobs:
  run:
    runs-on: ubuntu-slim
    if: github.event_name != 'schedule' || github.repository_owner == 'NixOS'
    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          persist-credentials: false
          sparse-checkout: |
            ci/github-script

      - name: Install dependencies
        run: npm ci --package-lock-only=false --no-audit @actions/artifact bottleneck
        working-directory: ci/github-script

      # Use a GitHub App, because it has much higher rate limits: 12,500 instead of 5,000 req / hour.
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        if: github.event_name != 'pull_request' && vars.NIXPKGS_CI_CLIENT_ID
        id: app-token
        with:
          client-id: ${{ vars.NIXPKGS_CI_CLIENT_ID }}
          private-key: ${{ secrets.NIXPKGS_CI_APP_PRIVATE_KEY }}
          permission-administration: read
          permission-contents: write
          permission-issues: write
          permission-members: read
          permission-pull-requests: write

      - name: Log current API rate limits
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
        run: gh api /rate_limit | jq

      - name: Run bot
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        with:
          github-token: ${{ steps.app-token.outputs.token || github.token }}
          retries: 3
          script: |
            const { default: bot } = await import('${{ github.workspace }}/ci/github-script/bot.js')
            await bot({
              github,
              context,
              core,
              dry: context.eventName == 'pull_request'
            })

      - name: Log current API rate limits
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
        run: gh api /rate_limit | jq

      - uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13 # v7.0.0
        name: Labels from touched files
        if: |
          github.event_name == 'pull_request_target' &&
          !contains(fromJSON(inputs.headBranch).type, 'development')
        with:
          repo-token: ${{ steps.app-token.outputs.token || github.token }}
          configuration-path: .github/labeler.yml # default
          sync-labels: true

      - uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13 # v7.0.0
        name: Labels from touched files (no sync)
        if: |
          github.event_name == 'pull_request_target' &&
          !contains(fromJSON(inputs.headBranch).type, 'development')
        with:
          repo-token: ${{ steps.app-token.outputs.token || github.token }}
          configuration-path: .github/labeler-no-sync.yml
          sync-labels: false

      - uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13 # v7.0.0
        name: Labels from touched files (development branches)
        # Development branches like staging-next, haskell-updates and python-updates get special labels.
        # This is to avoid the mass of labels there, which is mostly useless - and really annoying for
        # the backport labels.
        if: |
          github.event_name == 'pull_request_target' &&
          contains(fromJSON(inputs.headBranch).type, 'development')
        with:
          repo-token: ${{ steps.app-token.outputs.token || github.token }}
          configuration-path: .github/labeler-development-branches.yml
          sync-labels: true

      - name: Log current API rate limits
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
        run: gh api /rate_limit | jq