Skip to content

Commit

Permalink
Merge pull request #1058 from US-Trustee-Program/CAMS-461-cleanup-hel…
Browse files Browse the repository at this point in the history
…per-script

CAMS-461 cleanup helper script
  • Loading branch information
jamesobrooks authored Dec 10, 2024
2 parents 81d1136 + cfdb063 commit d9a1c44
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/azure-remove-branch.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name: Clean up Flexion Azure Resources

on:
pull_request:
types: [closed]
delete:
workflow_dispatch:
inputs:
hashId:
description: "Hash id of target branch deployemnt"
description: "Hash id of target branch deployment"
default: ""
type: string

Expand Down
89 changes: 89 additions & 0 deletions ops/scripts/utility/check-env-hashes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bash

# A utility script useful for identifying deployed branch environments

# Usage
# From the root directory, run the following command:
# ./ops/scripts/utility/check-env-hashes.sh [-l|h|r|e {hash}]

############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "This script prints branch names and their short hashes or checks a"
echo "known short hash against existing branches. Default lists remote"
echo "branches and their short hashes."
echo
echo "Syntax: ./ops/scripts/utility/check-env-hashes.sh [-l|h|r|e {hash}]"
echo "options:"
echo "l Check local branches."
echo "h Print this Help and exit."
echo "r Check remote branches."
echo "e Check against a specific known short hash."
echo " Example usage: -e 0a3de4"
echo
}

############################################################
############################################################
# Main program #
############################################################
############################################################
LOCAL=true
REMOTE=true
while getopts ":hlre:" option; do
case $option in
h) # display help
Help
exit;;
l) # Local branches
LOCAL=true
REMOTE=false
;;
r) # Remote branches
REMOTE=true
;;
e) # Existing environment
inputHash=${OPTARG}
;;
\?) # Invalid option
echo "Run with the '-h' option to see valid usage."
exit 1
;;
esac
done

branches=()
if [[ "$LOCAL" = "true" ]]; then
mapfile -t localBranches < <(git for-each-ref refs/heads --format='%(refname:short)')
branches+=("${localBranches[@]}")
fi
if [[ "$REMOTE" = "true" ]]; then
mapfile -t remoteBranches < <(git for-each-ref refs/remotes --format='%(refname:short)' | sed 's|^origin/||')
branches+=("${remoteBranches[@]}")
fi

found=false
for branch in "${branches[@]}"; do
# Generate the SHA256 hash
hash=$(echo -n "$branch" | openssl sha256 | awk '{print $2}')
# Extract the first 6 characters of the hash
shortHash="${hash:0:6}"
if [[ -z "$inputHash" ]]; then
echo "Branch: \"$branch\", Short Hash: \"$shortHash\""
else
if [ "$shortHash" == "$inputHash" ]; then
echo "Matching branch found: $branch"
found=true
break
fi
fi
done

# If -e was used but no branch was found
if [[ -n "$inputHash" && "$found" = "false" ]]; then
echo "No branch found with the short hash: $inputHash"
exit 0
fi

0 comments on commit d9a1c44

Please sign in to comment.