Skip to content

backport-command

backport-command #1527

name: backport-command
on:
repository_dispatch:
types: [backport-command]
env:
SCRIPT_DIR: "${{ github.workspace }}/.github/workflows/scripts/backport-command"
PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
ORIG_ISSUE_URL: ${{ github.event.client_payload.github.payload.issue.html_url }}
TARGET_ORG: ${{ github.event.client_payload.slash_command.args.named.org }}
TARGET_REPO: ${{ github.event.client_payload.slash_command.args.named.repo }}
ARG1: ${{ github.event.client_payload.slash_command.args.unnamed.arg1 }}
MILESTONE_ARG: ${{ github.event.client_payload.slash_command.args.named.milestone }}
TARGET_FULL_REPO: ${{ github.event.client_payload.slash_command.args.named.org }}/${{ github.event.client_payload.slash_command.args.named.repo }}
jobs:
# assumptions:
# label "kind/backport" exists
# the TARGET_REPO has been already forked into the bots account
# outputs the source of the comment (PR or issue)
backport-type:
outputs:
commented_on: ${{ steps.get_backport_type.outputs.commented_on }}
backport_branch: ${{ steps.get_backport_type.outputs.backport_branch }}
target_milestone: ${{ steps.get_backport_type.outputs.target_milestone }}
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: Get type of backport (issue or PR)
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
CLIENT_PAYLOAD: ${{ toJson(github.event.client_payload) }}
id: get_backport_type
run: $SCRIPT_DIR/get_backport_type.sh
shell: bash
- name: Failed reaction
uses: peter-evans/create-or-update-comment@v1
if: failure()
with:
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reaction-type: "-1"
- name: Post Error
if: failure()
env:
COMMENTED_ON: ${{ steps.get_backport_type.outputs.commented_on }}
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
run: $SCRIPT_DIR/post_error.sh
shell: bash
# creates backport issue if commented on issue, or
# creates backport PR if commented on PR
# eg /backport v21.11.x
type-branch:
needs: backport-type
runs-on: ubuntu-latest
env:
BACKPORT_BRANCH: ${{ needs.backport-type.outputs.backport_branch }}
steps:
- name: checkout
uses: actions/checkout@v3
- name: Get user
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
id: user
run: |
username=$(gh api user --jq .login)
echo ::set-output name=username::"$username"
echo ::set-output name=repo::"$TARGET_REPO"
echo ::set-output name=email::"[email protected]"
- name: Get assignees
env:
ASSIGNEES: ${{ toJson(github.event.client_payload.github.payload.issue.assignees) }}
id: assignees
run: echo ::set-output name=assignees::$(echo "$ASSIGNEES" | jq -r '.[].login' | paste -s -d ',' -)
- name: Discover and create milestone
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ needs.backport-type.outputs.target_milestone }}
id: create_milestone
run: $SCRIPT_DIR/create_milestone.sh
shell: bash
- name: Create issue
if: needs.backport-type.outputs.commented_on == 'issue'
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
ORIG_LABELS: ${{ toJson(github.event.client_payload.github.payload.issue.labels) }}
ORIG_ASSIGNEES: ${{ steps.assignees.outputs.assignees }}
id: create_issue
run: $SCRIPT_DIR/create_issue.sh
shell: bash
- name: Get reviewers of PR
if: needs.backport-type.outputs.commented_on == 'pr'
env:
REVIEWERS: ${{ toJson(github.event.client_payload.pull_request.requested_reviewers) }}
id: reviewers
run: echo ::set-output name=reviewers::$(echo "$REVIEWERS" | jq -r '.[].login' | paste -s -d ',' -)
- name: Get commits of PR
if: needs.backport-type.outputs.commented_on == 'pr'
env:
BACKPORT_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
id: backport_commits
run: |
backport_commits=$(gh api "repos/$TARGET_FULL_REPO/pulls/$BACKPORT_PR_NUMBER/commits" --jq .[].sha | paste -s -d ' ' -)
echo ::set-output name=backport_commits::$backport_commits
- uses: actions/checkout@v3
if: needs.backport-type.outputs.commented_on == 'pr'
with:
repository: ${{ steps.user.outputs.username }}/${{ steps.user.outputs.repo }}
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
path: ./fork
- name: Backport commits and get details
if: needs.backport-type.outputs.commented_on == 'pr'
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
BACKPORT_COMMITS: ${{ steps.backport_commits.outputs.backport_commits }}
IS_MERGED: ${{ github.event.client_payload.pull_request.merged }}
PR_BASE_BRANCH: ${{ github.event.client_payload.pull_request.base.ref }}
REPO_DEFAULT_BRANCH: ${{ github.event.client_payload.pull_request.base.repo.default_branch }}
GIT_USER: ${{ steps.user.outputs.username }}
GIT_EMAIL: ${{ steps.user.outputs.email }}
id: pr_details
run: $SCRIPT_DIR/pr_details.sh
shell: bash
- name: Create pull request
if: needs.backport-type.outputs.commented_on == 'pr'
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
ORIG_REVIEWERS: ${{ steps.reviewers.outputs.reviewers }}
HEAD_BRANCH: ${{ steps.pr_details.outputs.head_branch }}
FIXING_ISSUE_URLS: ${{ steps.pr_details.outputs.fixing_issue_urls }}
ORIG_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
ORIG_PR_URL: ${{ github.event.client_payload.pull_request.html_url }}
GIT_USER: ${{ steps.user.outputs.username }}
id: create_pr
run: $SCRIPT_DIR/create_pr.sh
shell: bash
- name: Add reaction
uses: peter-evans/create-or-update-comment@v1
with:
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reaction-type: hooray
- name: Failed reaction
uses: peter-evans/create-or-update-comment@v1
if: failure()
with:
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reaction-type: "-1"
- name: Post Error
if: failure()
env:
COMMENTED_ON: ${{ needs.backport-type.outputs.commented_on }}
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
run: $SCRIPT_DIR/post_error.sh
shell: bash
- name: Create Issue On Error
if: failure()
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
TARGET_MILESTONE: ${{ steps.create_milestone.outputs.milestone }}
ORIG_TITLE: ${{ github.event.client_payload.github.payload.issue.title }}
ORIG_LABELS: ${{ toJson(github.event.client_payload.github.payload.issue.labels) }}
ORIG_ASSIGNEES: ${{ steps.assignees.outputs.assignees }}
CREATE_ISSUE_ON_ERROR: "true"
ORIG_REVIEWERS: ${{ steps.reviewers.outputs.reviewers }}
HEAD_BRANCH: ${{ steps.pr_details.outputs.head_branch }}
GIT_USER: ${{ steps.user.outputs.username }}
BACKPORT_COMMITS: ${{ steps.backport_commits.outputs.backport_commits }}
id: create_issue_on_backport_error
run: $SCRIPT_DIR/create_issue.sh
shell: bash