summaryrefslogtreecommitdiffstats
path: root/.github/workflows/edited.yml
blob: f6dcb7e8807e58308ce2f639a823af0d5a038f38 (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
# Some workflows depend on the base branch of the PR, but changing the base branch is not included in the default trigger events, which would be `opened`, `synchronize` or `reopened`.
# Instead it causes an `edited` event.
# Since `edited` is also triggered when PR title/body is changed, we use this wrapper workflow, to run the other workflows conditionally only.
# There are already feature requests for adding a `base_changed` event:
# - https://github.com/orgs/community/discussions/35058
# - https://github.com/orgs/community/discussions/64119
#
# Instead of adding this to each workflow's pull_request_target event, we trigger this in a separate workflow.
# This has the advantage, that we can actually skip running those jobs for simple edits like changing the title or description.
# The actual trigger happens by closing and re-opening the pull request, which triggers the default pull_request_target events.
# This is much simpler and reliable than other approaches.

name: "Edited base branch"

on:
  pull_request_target:
    types: [edited]

concurrency:
  group: edited-${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }}
  cancel-in-progress: true

permissions: {}

defaults:
  run:
    shell: bash

jobs:
  base:
    name: Trigger jobs
    runs-on: ubuntu-slim
    if: github.event.changes.base.ref.from && github.event.changes.base.ref.from != github.event.pull_request.base.ref
    timeout-minutes: 2
    steps:
      # Use a GitHub App to create the PR so that CI gets triggered
      # The App is scoped to Repository > Contents and Pull Requests: write for Nixpkgs
      # We only need Pull Requests: write here, but the app is also used for backports.
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        id: app-token
        with:
          client-id: ${{ vars.NIXPKGS_CI_CLIENT_ID }}
          private-key: ${{ secrets.NIXPKGS_CI_APP_PRIVATE_KEY }}
          permission-pull-requests: write

      - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        with:
          github-token: ${{ steps.app-token.outputs.token }}
          script: |
            function changeState(state) {
              return github.rest.pulls.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: context.payload.pull_request.number,
                state
              })
            }
            await changeState('closed')
            await changeState('open')