Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bin/orphaned-pr-environments
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# -----------------------------------------------------------------------------
set -euo pipefail

GITHUB_STEP_SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/null}

app_name="$1"

echo "::group::Initialize Terraform"
Expand All @@ -17,7 +19,9 @@ echo "::endgroup::"
echo "::group::List PRs with PR environments"
echo terraform -chdir="infra/${app_name}/service" workspace list
workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)"
pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' | sed 's/p-//')"
# grep will exit with code `1` if there's no match, so ignore that for when
# there are no PR workspaces for the application
pr_nums="$(echo "${workspaces}" | { grep -o 'p-[0-9]\+' || test $? = 1; } | sed 's/p-//')"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment like

# Search for workspaces that start with "p-" and extract the PR numbers
# The "test $? = 1" is used to avoid errors if there are no PR environments

also, can you help me understand the advantage of test $? = 1 over || true? The latter seems simpler to grok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| true ignores all errors, which while unlikely seems worse than just ignoring the specific expected case. grep will exit with 2 if something else is wrong.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok that makes sense, thanks

echo "PRs"
echo "${pr_nums}"
echo "::endgroup::"
Expand All @@ -28,7 +32,7 @@ for pr_num in $pr_nums; do
pr_status="$(gh pr view "$pr_num" --json state --jq ".state")"
echo "PR ${pr_num}: ${pr_status}"

if [ "$pr_status" == "CLOSED" ]; then
if [ "$pr_status" == "CLOSED" ] || [ "$pr_status" == "MERGED" ]; then
closed_prs+=("$pr_num")
fi
done
Expand Down
Loading