Skip to content

Commit 9aa2afb

Browse files
authored
bin/orphaned-pr-environments: Handle no PR envs and merged PRs better (#889)
When there are no PR environments for an application, `grep 'pr-'` against the workspace names will not match anything, in which case grep returns a code of `1`. Since `-o pipefail` is set, `grep` returning `1` will fail the pipeline, and since `-e` is set, the pipeline will fail the script. So catch a return of `1` from `grep` and continue on. Also handle merged PR statuses in addition to closed, as that will sometimes be the case. Also set a default for `GITHUB_STEP_SUMMARY` for when running the script locally.
1 parent c77cf60 commit 9aa2afb

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

bin/orphaned-pr-environments

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# -----------------------------------------------------------------------------
88
set -euo pipefail
99

10+
GITHUB_STEP_SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/null}
11+
1012
app_name="$1"
1113

1214
echo "::group::Initialize Terraform"
@@ -17,7 +19,9 @@ echo "::endgroup::"
1719
echo "::group::List PRs with PR environments"
1820
echo terraform -chdir="infra/${app_name}/service" workspace list
1921
workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)"
20-
pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' | sed 's/p-//')"
22+
# grep will exit with code `1` if there's no match, so ignore that for when
23+
# there are no PR workspaces for the application
24+
pr_nums="$(echo "${workspaces}" | { grep -o 'p-[0-9]\+' || test $? = 1; } | sed 's/p-//')"
2125
echo "PRs"
2226
echo "${pr_nums}"
2327
echo "::endgroup::"
@@ -28,7 +32,7 @@ for pr_num in $pr_nums; do
2832
pr_status="$(gh pr view "$pr_num" --json state --jq ".state")"
2933
echo "PR ${pr_num}: ${pr_status}"
3034

31-
if [ "$pr_status" == "CLOSED" ]; then
35+
if [ "$pr_status" == "CLOSED" ] || [ "$pr_status" == "MERGED" ]; then
3236
closed_prs+=("$pr_num")
3337
fi
3438
done

0 commit comments

Comments
 (0)