Skip to content

Commit

Permalink
ci(backport): fetch original PR description
Browse files Browse the repository at this point in the history
Backport PR will fetch the original PR description and other content and
amend the backport PR description with it.
  • Loading branch information
Piotr1215 committed Jan 17, 2025
1 parent 646c6b0 commit 0ff481a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions .backportrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"repoOwner": "loft-sh",
"repoName": "vcluster",
"targetBranchChoices": ["v0.19", "v0.20"],
"prDescription": "Backport from `{{sourceBranch}}` to `{{targetBranch}}`\n\nOriginal PR Nr.: #{{sourcePullRequest.number}}\n\n### Backported Commits:\n{{#each commits}}\n- {{shortSha this.sourceCommit.sha}} {{this.sourceCommit.message}}\n{{/each}}\n\n## Original PR Description:\n{{sourcePullRequest}}",
"branchLabelMapping": {
"^backport-to-(.+)$": "$1"
}
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/backport.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ jobs:
if: github.event.pull_request.merged == true && !(contains(github.event.pull_request.labels.*.name, 'backport'))
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install GH CLI
uses: dev-hanz-ops/[email protected]

- name: Fetch PR description and update backportrc
env:
GH_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
# Grab raw original PR body, gh CLI uses the default token.
BODY_CONTENT="$(gh api "/repos/$REPO/pulls/$PR_NUMBER" | jq -r .body)"
# Escape the markdown content so that sed can produce valid JSON.
# Remove starting and ending quites and replace new lines with literals.
ESCAPED_CONTENT=$(echo "$BODY_CONTENT" | jq -aRs .)
ESCAPED_CONTENT="${ESCAPED_CONTENT%\"}"
ESCAPED_CONTENT="${ESCAPED_CONTENT#\"}"
ESCAPED_CONTENT=$(echo "$ESCAPED_CONTENT" | sed 's/[\/&]/\\&/g')
# sourcePullRequest is an actual variable, but the PR description is not provided byt the action or the CLI.
# Instead we are using it as a substitution target and replacing it with the markdown content.
sed -i "s/{{sourcePullRequest}}/$ESCAPED_CONTENT/g" .backportrc.json
- name: Backport Action
uses: sorenlouv/[email protected]
with:
Expand Down

0 comments on commit 0ff481a

Please sign in to comment.