Skip to content

Commit

Permalink
fix: silent fail if pr already exists (#6)
Browse files Browse the repository at this point in the history
* fix: silent fail if pr already exists

Signed-off-by: Steffen Tautenhahn <[email protected]>
  • Loading branch information
stevie- authored Feb 5, 2024
1 parent 16eed67 commit 958ffd1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/macos_action_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/ubuntu_action_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -20,6 +26,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: ./
id: pr
with:
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/windows_action_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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"
Expand Down
61 changes: 44 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]]]]
Expand All @@ -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:
Expand Down

0 comments on commit 958ffd1

Please sign in to comment.