From 29589a63b503b30820a13a442de533239dec06f4 Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Thu, 27 Jul 2023 17:38:45 +0200 Subject: [PATCH] fix: gha skip whitespace trim on body (#73) this also added a github workflow example --- dist/gha/index.js | 4 +- examples/on-pr-merge/automated-workflow.yaml | 53 +++++++++++++ examples/on-pr-merge/pr-merge-event.json | 79 ++++++++++++++++++++ src/service/args/gha/gha-args-parser.ts | 4 +- 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 examples/on-pr-merge/automated-workflow.yaml create mode 100644 examples/on-pr-merge/pr-merge-event.json diff --git a/dist/gha/index.js b/dist/gha/index.js index c7464fa..0261515 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -190,8 +190,8 @@ class GHAArgsParser extends args_parser_1.default { gitUser: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("git-user")), gitEmail: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("git-email")), title: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("title")), - body: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("body")), - bodyPrefix: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("body-prefix")), + body: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("body", { trimWhitespace: false })), + bodyPrefix: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("body-prefix", { trimWhitespace: false })), bpBranchName: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("bp-branch-name")), reviewers: (0, args_utils_1.getAsCleanedCommaSeparatedList)((0, core_1.getInput)("reviewers")), assignees: (0, args_utils_1.getAsCleanedCommaSeparatedList)((0, core_1.getInput)("assignees")), diff --git a/examples/on-pr-merge/automated-workflow.yaml b/examples/on-pr-merge/automated-workflow.yaml new file mode 100644 index 0000000..191a21f --- /dev/null +++ b/examples/on-pr-merge/automated-workflow.yaml @@ -0,0 +1,53 @@ +name: Automated Backporting on PR merge using Git Backporting + +on: + pull_request_target: + types: [closed, labeled] + branches: + - main + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + compute-targets: + if: ${{ github.event.pull_request.state == 'closed' && github.event.pull_request.merged }} + runs-on: ubuntu-latest + outputs: + target-branches: ${{ steps.set-targets.outputs.targets }} + env: + LABELS: ${{ toJSON(github.event.pull_request.labels) }} + steps: + - name: Set target branches + id: set-targets + uses: kiegroup/kie-ci/.ci/actions/parse-labels@main + with: + labels: ${LABELS} + + backporting: + if: ${{ github.event.pull_request.state == 'closed' && github.event.pull_request.merged && needs.compute-targets.outputs.target-branches != '[]' }} + name: "[${{ matrix.target-branch }}] - Backporting" + runs-on: ubuntu-latest + needs: compute-targets + strategy: + matrix: + target-branch: ${{ fromJSON(needs.compute-targets.outputs.target-branches) }} + fail-fast: true + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Backporting + uses: ./ + with: + dry-run: true + pull-request: ${{ github.event.pull_request.html_url }} + target-branch: ${{ matrix.target-branch }} + auth: "${{ env.GITHUB_TOKEN }}" + title: "[${{ matrix.target-branch }}] ${{ github.event.pull_request.title }}" + body-prefix: "**Backport:** ${{ github.event.pull_request.html_url }}\r\n\r\n**Note**: comment 'ok to test' to properly launch Jenkins jobs\r\n\r\n" + body: "${{ github.event.pull_request.body }}" + labels: "cherry-pick :cherries:" + inherit-labels: false + bp-branch-name: "${{ matrix.target-branch }}_${{ github.event.pull_request.head.ref }}" \ No newline at end of file diff --git a/examples/on-pr-merge/pr-merge-event.json b/examples/on-pr-merge/pr-merge-event.json new file mode 100644 index 0000000..a30b31f --- /dev/null +++ b/examples/on-pr-merge/pr-merge-event.json @@ -0,0 +1,79 @@ +{ + "pull_request": { + "url": "https://api.github.com/repos/lampajr/backporting-example/pulls/66", + "html_url": "https://github.com/lampajr/backporting-example/pull/66", + "diff_url": "https://github.com/lampajr/backporting-example/pull/66.diff", + "patch_url": "https://github.com/lampajr/backporting-example/pull/66.patch", + "issue_url": "https://api.github.com/repos/lampajr/backporting-example/issues/66", + "number": 66, + "state": "closed", + "title": "Feature1: multiple changes", + "user": { + "login": "lampajr" + }, + "body": "This is the body of multiple change", + "merge_commit_sha": "0bcaa01cdd509ca434e123d2e2b9ce7f66234bd7", + "assignee": null, + "assignees": [ + + ], + "requested_reviewers": [ + + ], + "requested_teams": [ + + ], + "labels": [ + { + "name": "backport-develop", + "color": "AB975B", + "default": false, + "description": "" + } + ], + "head": { + "label": "lampajr:feature1", + "ref": "feature1", + "sha": "69e49388ea2ca9be272b188a9271806d487bf01e", + "user": { + "login": "lampajr" + }, + "repo": { + "name": "backporting-example", + "full_name": "lampajr/backporting-example", + "owner": { + "login": "lampajr" + }, + "html_url": "https://github.com/lampajr/backporting-example", + "clone_url": "https://github.com/lampajr/backporting-example.git" + } + }, + "base": { + "label": "lampajr:main", + "ref": "main", + "sha": "c85b8fcdb741814b3e90e6e5729455cf46ff26ea", + "user": { + "login": "lampajr" + }, + "repo": { + "name": "backporting-example", + "full_name": "lampajr/backporting-example", + "owner": { + "login": "lampajr" + }, + "html_url": "https://github.com/lampajr/backporting-example", + "description": "Playground repository for automated backporting testing", + "url": "https://api.github.com/repos/lampajr/backporting-example", + "issues_url": "https://api.github.com/repos/lampajr/backporting-example/issues{/number}", + "pulls_url": "https://api.github.com/repos/lampajr/backporting-example/pulls{/number}", + "clone_url": "https://github.com/lampajr/backporting-example.git" + } + }, + "merged": true, + "merged_by": { + "login": "lampajr" + }, + "comments": 0, + "commits": 2 + } +} \ No newline at end of file diff --git a/src/service/args/gha/gha-args-parser.ts b/src/service/args/gha/gha-args-parser.ts index a6c2534..29580ab 100644 --- a/src/service/args/gha/gha-args-parser.ts +++ b/src/service/args/gha/gha-args-parser.ts @@ -21,8 +21,8 @@ export default class GHAArgsParser extends ArgsParser { gitUser: getOrUndefined(getInput("git-user")), gitEmail: getOrUndefined(getInput("git-email")), title: getOrUndefined(getInput("title")), - body: getOrUndefined(getInput("body")), - bodyPrefix: getOrUndefined(getInput("body-prefix")), + body: getOrUndefined(getInput("body", { trimWhitespace: false })), + bodyPrefix: getOrUndefined(getInput("body-prefix", { trimWhitespace: false })), bpBranchName: getOrUndefined(getInput("bp-branch-name")), reviewers: getAsCleanedCommaSeparatedList(getInput("reviewers")), assignees: getAsCleanedCommaSeparatedList(getInput("assignees")),