11name : ' Check Branch and PR Status'
2- description : ' Determines source branch and checks for existing pull request'
2+ description : ' Determines source branch, update branch, and checks for existing pull request'
33author : ' FOLIO DevOps'
44
5+ branding :
6+ icon : ' git-branch'
7+ color : ' blue'
8+
59inputs :
610 repo :
7- description : ' Repository in org/repo format'
11+ description : ' Repository in org/repo format (e.g., folio-org/platform-lsp) '
812 required : true
913 release_branch :
10- description : ' Base release branch name'
14+ description : ' Base release branch name (e.g., R1-2025) '
1115 required : true
1216 update_branch :
13- description : ' Update branch name to check'
17+ description : ' Update branch name to check (e.g., R1-2025-update) '
1418 required : true
1519 github_token :
16- description : ' GitHub token for API access'
20+ description : ' GitHub token for API access with repo scope '
1721 required : false
1822 default : ${{ github.token }}
23+ fail_on_orphan_branch :
24+ description : ' Fail if update branch exists without an associated PR (default: true)'
25+ required : false
26+ default : ' true'
1927
2028outputs :
2129 source_branch :
@@ -37,53 +45,73 @@ outputs:
3745runs :
3846 using : ' composite'
3947 steps :
40- - name : ' Check branch and PR status'
41- id : check
42- shell : bash
43- env :
44- GH_TOKEN : ${{ inputs.github_token }}
45- REPO : ${{ inputs.repo }}
46- RELEASE_BRANCH : ${{ inputs.release_branch }}
47- UPDATE_BRANCH : ${{ inputs.update_branch }}
48- run : |
49- set -euo pipefail
50- IFS=$'\n\t'
48+ - name : ' Check branch and PR status'
49+ id : check
50+ shell : bash
51+ env :
52+ GH_TOKEN : ${{ inputs.github_token }}
53+ REPO : ${{ inputs.repo }}
54+ RELEASE_BRANCH : ${{ inputs.release_branch }}
55+ UPDATE_BRANCH : ${{ inputs.update_branch }}
56+ FAIL_ON_ORPHAN : ${{ inputs.fail_on_orphan_branch }}
57+ run : |
58+ set -euo pipefail
59+ IFS=$'\n\t'
5160
52- # Check if update branch exists
53- echo "::notice::Checking if update branch exists: $UPDATE_BRANCH"
54- if gh api "repos/$REPO/branches/$UPDATE_BRANCH" >/dev/null 2>&1; then
55- echo "::notice::Update branch exists, will scan: $UPDATE_BRANCH"
56- echo "source_branch=$UPDATE_BRANCH" >> "$GITHUB_OUTPUT"
57- echo "update_branch_exists=true" >> "$GITHUB_OUTPUT"
61+ # Validate inputs
62+ if [[ ! "$REPO" =~ ^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+$ ]]; then
63+ echo "::error::Invalid repo format: '$REPO'. Expected format: org/repo"
64+ exit 1
65+ fi
5866
59- # Check for existing PR
60- echo "::notice::Checking for existing PR from $UPDATE_BRANCH to $RELEASE_BRANCH"
61- pr_json=$(gh pr list \
62- --repo "$REPO" \
63- --base "$RELEASE_BRANCH" \
64- --head "$UPDATE_BRANCH" \
65- --json number,url \
66- --jq '.[0]' || echo '{}')
67+ if [ -z "$RELEASE_BRANCH" ] || [ -z "$UPDATE_BRANCH" ]; then
68+ echo "::error::Branch names cannot be empty"
69+ exit 1
70+ fi
6771
68- if [ "$pr_json" != "{}" ] && [ -n "$pr_json" ] && [ "$(echo "$pr_json" | jq -r '.url // ""')" != "" ]; then
69- pr_number=$(echo "$pr_json" | jq -r '.number // ""')
70- pr_url=$(echo "$pr_json" | jq -r '.url // ""')
71- echo "::notice::Found existing PR #$pr_number: $pr_url"
72- echo "pr_exists=true" >> "$GITHUB_OUTPUT"
73- echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
74- echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT"
75- else
76- echo "::notice::No existing PR found"
77- echo "pr_exists=false" >> "$GITHUB_OUTPUT"
78- echo "pr_number=" >> "$GITHUB_OUTPUT"
79- echo "pr_url=" >> "$GITHUB_OUTPUT"
80- fi
72+ # Check if update branch exists
73+ echo "::notice::Checking if update branch exists: $UPDATE_BRANCH"
74+ if gh api "repos/$REPO/branches/$UPDATE_BRANCH" >/dev/null 2>&1; then
75+ echo "::notice::Update branch exists, will scan: $UPDATE_BRANCH"
76+ echo "source_branch=$UPDATE_BRANCH" >> "$GITHUB_OUTPUT"
77+ echo "update_branch_exists=true" >> "$GITHUB_OUTPUT"
78+
79+ # Check for existing PR
80+ echo "::notice::Checking for existing PR from $UPDATE_BRANCH to $RELEASE_BRANCH"
81+ pr_json=$(gh pr list \
82+ --repo "$REPO" \
83+ --base "$RELEASE_BRANCH" \
84+ --head "$UPDATE_BRANCH" \
85+ --json number,url,state \
86+ --jq '.[0]' || echo '{}')
87+
88+ pr_number=$(echo "$pr_json" | jq -r '.number // empty')
89+
90+ if [ -n "$pr_number" ]; then
91+ pr_url=$(echo "$pr_json" | jq -r '.url')
92+ pr_state=$(echo "$pr_json" | jq -r '.state')
93+ echo "::notice::Found existing PR #$pr_number ($pr_state): $pr_url"
94+ echo "pr_exists=true" >> "$GITHUB_OUTPUT"
95+ echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
96+ echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT"
8197 else
82- echo "::notice::Update branch does not exist, will scan: $RELEASE_BRANCH"
83- echo "source_branch=$RELEASE_BRANCH" >> "$GITHUB_OUTPUT"
84- echo "update_branch_exists=false" >> "$GITHUB_OUTPUT"
98+ echo "::notice::No existing PR found"
8599 echo "pr_exists=false" >> "$GITHUB_OUTPUT"
86100 echo "pr_number=" >> "$GITHUB_OUTPUT"
87101 echo "pr_url=" >> "$GITHUB_OUTPUT"
102+
103+ # Check for orphan branch if validation is enabled
104+ if [[ "${FAIL_ON_ORPHAN,,}" == "true" ]]; then
105+ echo "::error::Orphan branch detected: '$UPDATE_BRANCH' exists but no associated PR found"
106+ echo "::error::Please either create a PR or delete the orphan branch: $UPDATE_BRANCH"
107+ exit 1
108+ fi
88109 fi
89-
110+ else
111+ echo "::notice::Update branch does not exist, will scan: $RELEASE_BRANCH"
112+ echo "source_branch=$RELEASE_BRANCH" >> "$GITHUB_OUTPUT"
113+ echo "update_branch_exists=false" >> "$GITHUB_OUTPUT"
114+ echo "pr_exists=false" >> "$GITHUB_OUTPUT"
115+ echo "pr_number=" >> "$GITHUB_OUTPUT"
116+ echo "pr_url=" >> "$GITHUB_OUTPUT"
117+ fi
0 commit comments