From 98e1172e3bec18d8c5d4d8afeff1a9531227ed97 Mon Sep 17 00:00:00 2001 From: Steffen Tautenhahn Date: Mon, 5 Feb 2024 10:34:48 +0100 Subject: [PATCH 1/2] feat: upgrade actions - use worklows to test PRs - new output `pr_created` added Signed-off-by: Steffen Tautenhahn --- .github/workflows/macos_action_test.yml | 17 +++++-------- .github/workflows/ubuntu_action_test.yml | 21 ++++++---------- .github/workflows/windows_action_test.yml | 19 +++++---------- README.md | 18 +++++++------- action.yml | 29 +++++++++++++++-------- 5 files changed, 48 insertions(+), 56 deletions(-) diff --git a/.github/workflows/macos_action_test.yml b/.github/workflows/macos_action_test.yml index 844bdfc..5a4a3c3 100644 --- a/.github/workflows/macos_action_test.yml +++ b/.github/workflows/macos_action_test.yml @@ -1,9 +1,7 @@ name: Action test on MacOS on: - schedule: - - cron: "0 4 * * MON-FRI" # Runs at 04:00 UTC - push: + pull_request: branches: [main] workflow_dispatch: @@ -11,19 +9,16 @@ jobs: macos-min: runs-on: macos-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main - with: - source_branch: "main" - destination_branch: "test-macos-min" + - uses: actions/checkout@v4 + - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} macos-max: runs-on: macos-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main + - uses: actions/checkout@v4 + - uses: ./ with: source_branch: "main" destination_branch: "test-macos-max" @@ -36,4 +31,4 @@ jobs: pr_draft: true pr_allow_empty: true env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/ubuntu_action_test.yml b/.github/workflows/ubuntu_action_test.yml index 64e2172..638f114 100644 --- a/.github/workflows/ubuntu_action_test.yml +++ b/.github/workflows/ubuntu_action_test.yml @@ -1,9 +1,7 @@ name: Action test on Ubuntu on: - schedule: - - cron: "0 2 * * MON-FRI" # Runs at 02:00 UTC - push: + pull_request: branches: [main] workflow_dispatch: @@ -11,23 +9,18 @@ jobs: ubuntu-min: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main - with: - source_branch: "main" - destination_branch: "test-ubuntu-min" + - uses: actions/checkout@v4 + - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ubuntu-max: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main + - uses: actions/checkout@v4 + - uses: ./ with: - source_branch: "main" - destination_branch: "test-ubuntu-max" - pr_title: "Pulling ${{ github.ref }} into main" + pr_title: "Pulling ${{ github.ref_name }} into main" pr_body: "An automated PR" pr_reviewer: "GuillaumeFalourd" pr_assignee: "GuillaumeFalourd" @@ -36,4 +29,4 @@ jobs: pr_draft: true pr_allow_empty: true env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/windows_action_test.yml b/.github/workflows/windows_action_test.yml index eaf35ba..1ce8c41 100644 --- a/.github/workflows/windows_action_test.yml +++ b/.github/workflows/windows_action_test.yml @@ -1,9 +1,7 @@ name: Action test on Windows on: - schedule: - - cron: "0 6 * * MON-FRI" # Runs at 06:00 UTC - push: + pull_request: branches: [main] workflow_dispatch: @@ -11,22 +9,17 @@ jobs: windows-min: runs-on: windows-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main - with: - source_branch: "main" - destination_branch: "test-windows-min" + - uses: actions/checkout@v4 + - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} windows-max: runs-on: windows-latest steps: - - uses: actions/checkout@v2 - - uses: GuillaumeFalourd/pull-request-action@main + - uses: actions/checkout@v4 + - uses: ./ with: - source_branch: "main" - destination_branch: "test-windows-max" pr_title: "Pulling ${{ github.ref }} into main" pr_body: "An automated PR" pr_reviewer: "GuillaumeFalourd" @@ -36,4 +29,4 @@ jobs: pr_draft: true pr_allow_empty: true env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 8d25dd9..55074d9 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ # Pull Request Action -[![Action test on Ubuntu](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/ubuntu_action_test.yml/badge.svg)](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/ubuntu_action_test.yml) [![Action test on MacOS](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/macos_action_test.yml/badge.svg)](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/macos_action_test.yml) [![Action test on Windows](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/windows_action_test.yml/badge.svg)](https://github.com/GuillaumeFalourd/pull-request-action/actions/workflows/windows_action_test.yml) +[![Action test on Ubuntu](https://github.com/datadrivers/pull-request-action/actions/workflows/ubuntu_action_test.yml/badge.svg)](https://github.com/datadrivers/pull-request-action/actions/workflows/ubuntu_action_test.yml) [![Action test on MacOS](https://github.com/datadrivers/pull-request-action/actions/workflows/macos_action_test.yml/badge.svg)](https://github.com/datadrivers/pull-request-action/actions/workflows/macos_action_test.yml) [![Action test on Windows](https://github.com/datadrivers/pull-request-action/actions/workflows/windows_action_test.yml/badge.svg)](https://github.com/datadrivers/pull-request-action/actions/workflows/windows_action_test.yml) ![](https://user-images.githubusercontent.com/22433243/157692326-2e75f43d-e563-4fa9-8947-67c06e4e469f.png) -☞ Github Actions to create pull request using Github CLI ⤵️ +☞ Github Actions to create pull request using Github CLI ⤵️ _**Note**: This action is supported on **all runners** operating systems (`ubuntu`, `macos`, `windows`)_ _Inspired from [https://github.com/repo-sync/pull-request](https://github.com/repo-sync/pull-request)_ +_Original Code from [github.com/GuillaumeFalourd/pull-request-action](https://github.com/GuillaumeFalourd/pull-request-action)_ + ## 📝 Features - Create pull requests @@ -31,7 +33,7 @@ _Inspired from [https://github.com/repo-sync/pull-request](https://github.com/re ### Minimum configurations ```yaml - - uses: GuillaumeFalourd/pull-request-action@v1 + - uses: datadrivers/pull-request-action@v2 with: destination_branch: "main" env: @@ -41,7 +43,7 @@ _Inspired from [https://github.com/repo-sync/pull-request](https://github.com/re ### Full configurations ```yaml - - uses: GuillaumeFalourd/pull-request-action@v1 + - uses: datadrivers/pull-request-action@v2 with: source_branch: "main" # If blank, default: triggered branch destination_branch: "feature" # If blank, default: main @@ -59,16 +61,16 @@ _Inspired from [https://github.com/repo-sync/pull-request](https://github.com/re ## 🤝 Contributing -☞ If you're interested in contributing to this repository, please follow the [guidelines](https://github.com/GuillaumeFalourd/pull-request-action/blob/main/CONTRIBUTING.md) +☞ If you're interested in contributing to this repository, please follow the [guidelines](https://github.com/datadrivers/pull-request-action/blob/main/CONTRIBUTING.md) ## 🏅 Licensed -☞ This repository uses the [Apache License 2.0](https://github.com/GuillaumeFalourd/pull-request-action/blob/main/LICENSE) +☞ This repository uses the [Apache License 2.0](https://github.com/datadrivers/pull-request-action/blob/main/LICENSE) diff --git a/action.yml b/action.yml index 4225641..63754e3 100644 --- a/action.yml +++ b/action.yml @@ -1,15 +1,15 @@ name: Pull Request Action (Github CLI) -description: Github Actions to create pull request (all os supported) using Github CLI ⤵️ +description: Github Actions to create pull request (all os supported) using Github CLI ⤵️ inputs: source_branch: description: Branch name to pull from, default is triggered branch required: false destination_branch: - description: Branch name to sync to in this repo, default is master + description: Branch name to sync to in this repo, default is main required: false - default: master + default: main pr_title: description: Pull request title required: false @@ -38,15 +38,22 @@ inputs: outputs: pr_url: description: 'Pull request URL' + value: ${{ steps.pr-creation.outputs.pr_url }} pr_number: description: 'Pull request number' + value: ${{ steps.pr-creation.outputs.pr_number }} has_changed_files: description: 'Boolean string indicating whether any file has been changed' + value: ${{ steps.pr-creation.outputs.has_changed_files }} + pr_created: + description: 'Boolean string indicating whether a PR was created' + value: ${{ steps.pr-creation.outputs.pr_created }} runs: using: "composite" steps: - - name: Create Pull Request ⤵️ + - name: Create Pull Request ⤵️ + id: pr-creation run: | set -e set -o pipefail @@ -65,6 +72,8 @@ runs: else DESTINATION_BRANCH="main" fi + # init pr_created bool + echo "pr_created=false" >> "$GITHUB_OUTPUT" # Github actions no longer auto set the username and GITHUB_TOKEN git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@${GITHUB_SERVER_URL#https://}/$GITHUB_REPOSITORY" @@ -131,17 +140,17 @@ runs: if [[ "$?" != "0" ]]; then exit 1 fi - + echo "pr_created=true" >> "$GITHUB_OUTPUT" echo ${PR_URL} - echo "::set-output name=pr_url::${PR_URL}" - echo "::set-output name=pr_number::${PR_URL##*/}" + echo "pr_url=${PR_URL}" >> "$GITHUB_OUTPUT" + echo "pr_number=${PR_URL##*/}" >> "$GITHUB_OUTPUT" if [[ "$LINES_CHANGED" = "0" ]]; then - echo "::set-output name=has_changed_files::false" + echo "has_changed_files=false" >> "$GITHUB_OUTPUT" else - echo "::set-output name=has_changed_files::true" + echo "has_changed_files=true" >> "$GITHUB_OUTPUT" fi shell: bash branding: icon: 'git-pull-request' - color: 'black' \ No newline at end of file + color: 'blue' From 659fad81aa612c6ae4f9ea68d37aede7d2d4819a Mon Sep 17 00:00:00 2001 From: Steffen Tautenhahn Date: Mon, 5 Feb 2024 13:53:50 +0100 Subject: [PATCH 2/2] feat: fail on error and parse output Signed-off-by: Steffen Tautenhahn --- .github/workflows/macos_action_test.yml | 2 - .github/workflows/ubuntu_action_test.yml | 4 ++ action.yml | 52 +++++++++++++++--------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.github/workflows/macos_action_test.yml b/.github/workflows/macos_action_test.yml index 5a4a3c3..3e4948b 100644 --- a/.github/workflows/macos_action_test.yml +++ b/.github/workflows/macos_action_test.yml @@ -20,8 +20,6 @@ jobs: - uses: actions/checkout@v4 - uses: ./ with: - source_branch: "main" - destination_branch: "test-macos-max" pr_title: "Pulling ${{ github.ref }} into main" pr_body: "An automated PR" pr_reviewer: "GuillaumeFalourd" diff --git a/.github/workflows/ubuntu_action_test.yml b/.github/workflows/ubuntu_action_test.yml index 638f114..45a4ec6 100644 --- a/.github/workflows/ubuntu_action_test.yml +++ b/.github/workflows/ubuntu_action_test.yml @@ -11,14 +11,17 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./ + id: pr env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - run: echo -e "Output of pr job is:\n${{ tojson(steps.pr.outputs) }}" ubuntu-max: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ./ + id: pr with: pr_title: "Pulling ${{ github.ref_name }} into main" pr_body: "An automated PR" @@ -30,3 +33,4 @@ jobs: pr_allow_empty: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - run: echo -e "Output of pr job is:\n${{ tojson(steps.pr.outputs) }}" diff --git a/action.yml b/action.yml index 63754e3..ecc8dd7 100644 --- a/action.yml +++ b/action.yml @@ -99,49 +99,57 @@ runs: # Workaround for `hub` auth error https://github.com/github/hub/issues/2149#issuecomment-513214342 export GITHUB_USER="$GITHUB_ACTOR" - COMMAND="gh pr create --base $DESTINATION_BRANCH --head $SOURCE_BRANCH --no-maintainer-edit" + # set -x + declare -a COMMAND + COMMAND+=(gh pr create --base $DESTINATION_BRANCH --head $SOURCE_BRANCH --no-maintainer-edit) if [[ ! -z "${{ inputs.pr_title }}" ]]; then - COMMAND="$COMMAND --title \"${{ inputs.pr_title }}\"" + COMMAND+=(--title "${{ inputs.pr_title }}") else - COMMAND="$COMMAND --fill" + COMMAND+=(--fill) fi if [[ ! -z "${{ inputs.pr_body }}" ]]; then - COMMAND="$COMMAND --body \"${{ inputs.pr_body }}\"" + COMMAND+=(--body "${{ inputs.pr_body }}") fi if [[ ! -z "${{ inputs.pr_reviewer }}" ]]; then - COMMAND="$COMMAND --reviewer \"${{ inputs.pr_reviewer }}\"" + COMMAND+=(--reviewer "${{ inputs.pr_reviewer }}") fi if [[ ! -z "${{ inputs.pr_assignee }}" ]]; then - COMMAND="$COMMAND --assignee \"${{ inputs.pr_assignee }}\"" + COMMAND+=(--assignee "${{ inputs.pr_assignee }}") fi if [[ ! -z "${{ inputs.pr_label }}" ]]; then - COMMAND="$COMMAND --label \"${{ inputs.pr_label }}\"" + COMMAND+=(--label "${{ inputs.pr_label }}") fi if [[ ! -z "${{ inputs.pr_milestone }}" ]]; then - COMMAND="$COMMAND --milestone \"${{ inputs.pr_milestone }}\"" + COMMAND+=(--milestone "${{ inputs.pr_milestone }}") fi if [[ "${{ inputs.pr_draft }}" == "true" ]]; then - COMMAND="$COMMAND --draft" + COMMAND+=(--draft) fi - COMMAND="$COMMAND || true" - - echo "Command that will be executed:" - echo "$COMMAND" - - PR_URL=$(sh -c "$COMMAND") - if [[ "$?" != "0" ]]; then - exit 1 - fi - echo "pr_created=true" >> "$GITHUB_OUTPUT" - echo ${PR_URL} + echo "Command that will be executed: ${COMMAND[@]}" + + # SYNTAX: + # catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] + catch() { + { + IFS=$'\n' read -r -d '' "${1}"; + IFS=$'\n' read -r -d '' "${2}"; + (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); + } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) + } + + set +e + catch PR_URL pr_command_error "${COMMAND[@]}" + pr_command_exit_code=$? + set -e + echo "Output of github cli command: " ${PR_URL} ${pr_command_error} echo "pr_url=${PR_URL}" >> "$GITHUB_OUTPUT" echo "pr_number=${PR_URL##*/}" >> "$GITHUB_OUTPUT" if [[ "$LINES_CHANGED" = "0" ]]; then @@ -149,6 +157,10 @@ runs: else echo "has_changed_files=true" >> "$GITHUB_OUTPUT" fi + if [[ "$pr_command_exit_code" != "0" ]]; then + exit 1 + fi + echo "pr_created=true" >> "$GITHUB_OUTPUT" shell: bash branding: