about summary refs log tree commit diff
path: root/nixpkgs/.github/workflows/rebase-staging.yml
blob: 41b06f95668a45955f42b31c0bfc3316860b1844 (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
on:
  issue_comment:
    types:
      - created

# This action allows people with write access to the repo to rebase a PRs base branch from
# master to staging by commenting `/rebase-staging` on the PR while avoiding CODEOWNER notifications.

jobs:
  rebase:
    runs-on: ubuntu-latest
    if: github.repository_owner == 'NixOS' && github.event.issue.pull_request != '' && contains(github.event.comment.body, '/rebase-staging')
    steps:
      - uses: peter-evans/create-or-update-comment@v1
        with:
          comment-id: ${{ github.event.comment.id }}
          reactions: eyes
      - uses: scherermichael-oss/action-has-permission@1.0.6
        id: check-write-access
        with:
          required-permission: write
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: check base branch is master
        if: steps.check-write-access.outputs.has-permission
        run: |
          if [ "$(curl https://api.github.com/repos/NixOS/nixpkgs/pulls/${{ github.event.issue.number }} | jq -r '.base.ref')" != "master" ]; then
            echo "This action only works when the current base branch is master."
            exit 1
          fi
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
        if: steps.check-write-access.outputs.has-permission
      - name: rebase pull request
        if: steps.check-write-access.outputs.has-permission
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST: ${{ github.event.issue.number }}
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
          git fetch origin
          gh pr checkout "$PULL_REQUEST"
          git rebase \
            --onto="$(git merge-base origin/master origin/staging)" \
            "HEAD~$(git rev-list --count HEAD ^master)"
          git push --force
          curl \
            -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            -H "Authorization: token $GITHUB_TOKEN" \
            -d '{ "base": "staging" }' \
            "https://api.github.com/repos/NixOS/nixpkgs/pulls/$PULL_REQUEST"
      - uses: peter-evans/create-or-update-comment@v1
        if: ${{ failure() }}
        with:
          issue-number: ${{ github.event.issue.number }}
          body: |
            [Failed to rebase on `staging`](https://github.com/NixOS/nixpkgs/actions/runs/${{ github.run_id }})