From 958ffd15a39b074bfc8136d53a66b16018d61746 Mon Sep 17 00:00:00 2001 From: Steffen Tautenhahn Date: Mon, 5 Feb 2024 15:21:56 +0100 Subject: [PATCH] fix: silent fail if pr already exists (#6) * fix: silent fail if pr already exists Signed-off-by: Steffen Tautenhahn --- .github/workflows/macos_action_test.yml | 8 +++ .github/workflows/ubuntu_action_test.yml | 8 +++ .github/workflows/windows_action_test.yml | 8 +++ action.yml | 61 ++++++++++++++++------- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/.github/workflows/macos_action_test.yml b/.github/workflows/macos_action_test.yml index 3e4948b..4043d7f 100644 --- a/.github/workflows/macos_action_test.yml +++ b/.github/workflows/macos_action_test.yml @@ -5,11 +5,17 @@ on: branches: [main] workflow_dispatch: +permissions: + contents: read + pull-requests: write + jobs: macos-min: runs-on: macos-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -18,6 +24,8 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ with: pr_title: "Pulling ${{ github.ref }} into main" diff --git a/.github/workflows/ubuntu_action_test.yml b/.github/workflows/ubuntu_action_test.yml index 45a4ec6..e3096e3 100644 --- a/.github/workflows/ubuntu_action_test.yml +++ b/.github/workflows/ubuntu_action_test.yml @@ -5,11 +5,17 @@ on: branches: [main] workflow_dispatch: +permissions: + contents: read + pull-requests: write + jobs: ubuntu-min: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ id: pr env: @@ -20,6 +26,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ id: pr with: diff --git a/.github/workflows/windows_action_test.yml b/.github/workflows/windows_action_test.yml index 1ce8c41..70a0fd9 100644 --- a/.github/workflows/windows_action_test.yml +++ b/.github/workflows/windows_action_test.yml @@ -5,11 +5,17 @@ on: branches: [main] workflow_dispatch: +permissions: + contents: read + pull-requests: write + jobs: windows-min: runs-on: windows-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -18,6 +24,8 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: ./ with: pr_title: "Pulling ${{ github.ref }} into main" diff --git a/action.yml b/action.yml index ecc8dd7..51973f1 100644 --- a/action.yml +++ b/action.yml @@ -58,23 +58,27 @@ runs: set -e set -o pipefail - if [[ ! -z "${{ inputs.source_branch }}" ]]; then - SOURCE_BRANCH="${{ inputs.source_branch }}" + echo "::group::Gather Inputs" + if [[ ! -z "$GITHUB_HEAD_REF" ]]; then + SOURCE_BRANCH="$GITHUB_HEAD_REF" + elif [[ ! -z "$INPUT_SOURCE_BRANCH" ]]; then + SOURCE_BRANCH="$INPUT_SOURCE_BRANCH" elif [[ ! -z "$GITHUB_REF" ]]; then SOURCE_BRANCH=${GITHUB_REF/refs\/heads\//} # Remove branch prefix else - echo "Set the ${{ inputs.source_branch }} environment variable or trigger from a branch." + echo_fail "Set the INPUT_SOURCE_BRANCH environment variable or trigger from a branch." exit 1 fi + echo "SOURCE_BRANCH=$SOURCE_BRANCH" - if [[ ! -z "${{ inputs.destination_branch }}" ]]; then - DESTINATION_BRANCH="${{ inputs.destination_branch }}" - else - DESTINATION_BRANCH="main" - fi - # init pr_created bool + DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"main"}" + echo "DESTINATION_BRANCH=$DESTINATION_BRANCH" + + # init pr_created bool for output echo "pr_created=false" >> "$GITHUB_OUTPUT" + echo "::endgroup::" + echo "::group::Configure git" # 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" @@ -84,8 +88,13 @@ runs: # Print out all branches git --no-pager branch -a -vv - if [ "$(git rev-parse --revs-only "$SOURCE_BRANCH")" = "$(git rev-parse --revs-only "$DESTINATION_BRANCH")" ]; then - echo "Source and destination branches are the same." + echo "::endgroup::" + + echo "::group::Ensure pull-request contains differences" + SOURCE_REV=$(git rev-parse --revs-only "$SOURCE_BRANCH") + DESTINATION_REV=$(git rev-parse --revs-only "$DESTINATION_BRANCH") + if [ "$SOURCE_REV" = "$DESTINATION_REV" ]; then + echo "Source and destination branches are the same. Source rev: $SOURCE_REV, Destination rev: $DESTINATION_REV" exit 0 fi @@ -95,11 +104,12 @@ runs: echo "No file changes detected between source and destination branches." exit 0 fi + echo "::endgroup::" + echo "::group::Assemble gh pr parameters" # Workaround for `hub` auth error https://github.com/github/hub/issues/2149#issuecomment-513214342 export GITHUB_USER="$GITHUB_ACTOR" - # set -x declare -a COMMAND COMMAND+=(gh pr create --base $DESTINATION_BRANCH --head $SOURCE_BRANCH --no-maintainer-edit) @@ -134,6 +144,7 @@ runs: fi echo "Command that will be executed: ${COMMAND[@]}" + echo "::endgroup::" # SYNTAX: # catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] @@ -145,22 +156,38 @@ runs: } < <((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) } + echo "::group::Create pull request $SOURCE_BRANCH -> $DESTINATION_BRANCH" set +e - catch PR_URL pr_command_error "${COMMAND[@]}" + catch pr_command_output 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" + echo "Output of github cli command: " ${pr_command_output} ${pr_command_error} + echo "::endgroup::" + + echo "::group::Set outputs" + pr_already_exists=$(echo "${pr_command_error}" | grep -c "already exists:") + if [[ "$pr_command_exit_code" -eq 0 ]]; + then + pr_url="${pr_command_output}" + pr_number="${pr_command_output##*/}" + elif [[ "$pr_already_exists" -eq 1 ]]; + then + pr_url=$(echo "$pr_command_error" | grep -o 'https://github.com[^ ]*') + pr_number=${pr_url##*/} + fi + echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT" + echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" if [[ "$LINES_CHANGED" = "0" ]]; then echo "has_changed_files=false" >> "$GITHUB_OUTPUT" else echo "has_changed_files=true" >> "$GITHUB_OUTPUT" fi - if [[ "$pr_command_exit_code" != "0" ]]; then + if [[ "$pr_command_exit_code" != "0" && "$pr_already_exists" -eq 0 ]]; then exit 1 fi + # assume that if we got here, a PR was created echo "pr_created=true" >> "$GITHUB_OUTPUT" + echo "::endgroup::" shell: bash branding: