diff --git a/.github/actions/backport/backport.sh b/.github/actions/backport/backport.sh new file mode 100755 index 000000000..7a9cbc18e --- /dev/null +++ b/.github/actions/backport/backport.sh @@ -0,0 +1,39 @@ +#!/bin/bash -e + +echo "::notice::Fetching PR information" +curl -L -H "Accept: application/vnd.github+json" "https://api.github.com/repos/nextcloud/cookbook/pulls/$number" > /tmp/pr-data + +baseSha=$(cat /tmp/pr-data | jq -r '.base.sha') +echo "::debug::Base SHA is $baseSha" +baseRef=$(cat /tmp/pr-data | jq -r '.base.ref') +echo "::debug::Base reference is $baseRef" +headSha=$(cat /tmp/pr-data | jq -r '.base.sha') +echo "::debug::Head SHA of rebase is $headSha" + +IFS=' ' read tmp branchName < /tmp/comment +echo "::debug::Branch name of destination branch is $branchName" + +git config user.name "Cookbook bot" +git config user.email "bot@noreply.github.com" + +backportBranch() { + backportBranchName="backport-$branchName/$headSha" + echo "::debug::Backport branch name to create is $backportBranchName" + echo "branchName=$backportBranchName" >> "$GITHUB_OUTPUT" + echo "targetBranch=$baseRef" >> "$GITHUB_OUTPUT" + + git branch "$backportBranchName" "$headSha" + + echo "::debug::Switching to backport branch" + git checkout "$backportBranchName" + + echo "Branch was created, ready for porting to other branch" + + git rebase --onto "$branchName" "$baseSha" "$backportBranchName" + + echo "Rebasing was successful" + + git push origin "$backportBranchName" +} + +backportBranch diff --git a/.github/workflows/approve-on-comment.yaml b/.github/workflows/approve-on-comment.yaml deleted file mode 100644 index 43a7a248c..000000000 --- a/.github/workflows/approve-on-comment.yaml +++ /dev/null @@ -1,32 +0,0 @@ -name: Slack-like comments -on: - issue_comment: - types: - - "created" - - "edited" - -defaults: - run: - shell: bash - -jobs: - approve-pr: - name: Approve PR - runs-on: ubuntu-latest - if: github.event.issue.pull_request - env: - admins: '["seyfeb", "christianlupus"]' - steps: - - run: |- - echo "Admins: $admins" - echo "User is permitted: ${{ contains(fromJSON(env.admins), github.event.comment.user.login) }}" - echo "Comment body matches /approve: ${{github.event.comment.body == '/approve'}}" - - uses: hmarr/auto-approve-action@v3 - if: >- - github.event.comment.body == '/approve' && - contains(fromJSON(env.admins), github.event.comment.user.login) - with: - github-token: ${{ secrets.COOKBOOK_BOT_TOKEN }} - pull-request-number: ${{ github.event.issue.number }} - review-message: "Automatically approved by GitHub action on behalf of `/approve`" - diff --git a/.github/workflows/slack-commands.yaml b/.github/workflows/slack-commands.yaml new file mode 100644 index 000000000..07dada610 --- /dev/null +++ b/.github/workflows/slack-commands.yaml @@ -0,0 +1,75 @@ +name: Slack-like comments +on: + issue_comment: + types: + - "created" + - "edited" + +defaults: + run: + shell: bash + +jobs: + approve-pr: + name: Approve PR + runs-on: ubuntu-latest + if: github.event.issue.pull_request + env: + admins: '["seyfeb", "christianlupus"]' + steps: + - run: |- + echo "Admins: $admins" + echo "User is permitted: ${{ contains(fromJSON(env.admins), github.event.comment.user.login) }}" + echo "Comment body matches /approve: ${{github.event.comment.body == '/approve'}}" + - uses: hmarr/auto-approve-action@v3 + if: >- + github.event.comment.body == '/approve' && + contains(fromJSON(env.admins), github.event.comment.user.login) + with: + github-token: ${{ secrets.COOKBOOK_BOT_TOKEN }} + pull-request-number: ${{ github.event.issue.number }} + review-message: "Automatically approved by GitHub action on behalf of `/approve`" + + backport: + name: Backport PR + runs-on: ubuntu-latest + if: github.event.issue.pull_request + env: + admins: '["seyfeb", "christianlupus"]' + steps: + - name: Collect data + run: |- + echo "Admins: $admins" + echo "User is permitted: ${{ contains(fromJSON(env.admins), github.event.comment.user.login) }}" + echo "permitted_user=${{ contains(fromJSON(env.admins), github.event.comment.user.login) }}" >> "$GITHUB_OUTPUT" + echo "Comment body matches /approve: ${{ startsWith(github.event.comment.body, '/backport')}}" + echo "use_backport=${{ startsWith(github.event.comment.body, '/backport')}}" >> "$GITHUB_OUTPUT" + echo "number=${{ github.event.issue.number }}" >> "$GITHUB_ENV" + echo "${{ github.event.comment.body }}" > /tmp/comment + id: config + - name: Add reaction unon start + if: steps.config.outputs.permitted_user && steps.config.outputs.use_backport + uses: peter-evans/create-or-update-comment@v3.0.1 + with: + token: ${{ secrets.COOKBOOK_BOT_TOKEN }} + repository: ${{ github.event.repository.full_name }} + comment-id: ${{ github.event.comment.id }} + reaction-type: "+1" + - name: Carry out the backport + if: steps.config.outputs.permitted_user && steps.config.outputs.use_backport + run: .github/actions/backport/backport.sh + id: backport + - name: Create a pull request + if: steps.config.outputs.permitted_user && steps.config.outputs.use_backport + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ secrets.COOKBOOK_BOT_TOKEN }} + branch: ${{ steps.backport.outputs.branchName }} + base: ${{ steps.backport.outputs.targetBranch }} + title: Backport of ${{ github.event.pull_request.title }} + body: |- + Backport of #${{ github.event.issue.number }} + + ${{ github.event.body }} + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 60857645f..6d639388d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### Maintenance - Add PHP lint checker to ensure valid (legacy) PHP syntax [#1931](https://github.com/nextcloud/cookbook/pull/1931) @christianlupus +- Add backport script to simplify development + [#1935](https://github.com/nextcloud/cookbook/pull/1935) @christianlupus ## 0.10.3 - 2023-12-04