Skip to content

Commit 3fede79

Browse files
committed
feat: Add configuration file for GitHub to Slack mapping
1 parent d2829cd commit 3fede79

1 file changed

Lines changed: 41 additions & 23 deletions

File tree

.functions

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,40 +189,51 @@ function aws.print-all-instances() {
189189
done
190190
}
191191

192+
# Configuration file path
193+
CONFIG_FILE="reviewers.txt"
192194

193195
function ghpra() {
194196
echo "Enter a prefix for the PR title (e.g. fix)"
195197
read -r PREFIX
196198
echo "Creating PR with prefix: $PREFIX"
197199

198-
# GitHub to Slack username mapping
199-
declare -A gh_to_slack=(
200-
["<GH_USERNAME>"]="<SLACK_DISPLAYNAME>"
201-
["<GH_USERNAME>"]="<SLACK_DISPLAYNAME>"
202-
)
203-
204-
# Randomly select code owners
205-
frontend=$(shuf -e GH_USERNAME GH_USERNAME -n 1)
206-
backend=$(shuf -e GH_USERNAME GH_USERNAME -n 1)
207-
infra=$(shuf -e GH_USERNAME GH_USERNAME -n 1)
208-
migration="<GH_USERNAME>"
209-
other="<GH_USERNAME>"
200+
# Associative arrays for mapping
201+
declare -A gh_to_slack
202+
declare -A regex_to_gh
203+
204+
# Read configuration file
205+
while IFS=, read -r github_user slack_user regex_path; do
206+
# Trim whitespace
207+
github_user=$(echo "$github_user" | xargs)
208+
slack_user=$(echo "$slack_user" | xargs)
209+
regex_path=$(echo "$regex_path" | xargs)
210+
211+
# Populate the associative arrays
212+
gh_to_slack["$github_user"]="$slack_user"
213+
regex_to_gh["$regex_path"]="$github_user"
214+
done < "$CONFIG_FILE"
210215

211216
declare -a reviewers
212217

213-
# Determine modified directories and assign reviewers
218+
# Determine modified directories and assign reviewers based on regex
214219
git diff --name-only HEAD HEAD~1 | while read -r file; do
215-
case "$file" in
216-
frontend/*) reviewers+=("$frontend") ;;
217-
api/*) reviewers+=("$backend") ;;
218-
migration/*) reviewers+=("$migration") ;;
219-
.github/*) reviewers+=("$infra") ;;
220-
esac
220+
for regex in "${!regex_to_gh[@]}"; do
221+
if [[ "$file" =~ $regex ]]; then
222+
reviewers+=("${regex_to_gh[$regex]}")
223+
break
224+
fi
225+
done
221226
done
222227

223228
# Remove duplicate reviewers
224229
reviewers=($(printf "%s\n" "${reviewers[@]}" | sort -u))
225230

231+
# If no reviewers were found, assign a default
232+
if [[ ${#reviewers[@]} -eq 0 ]]; then
233+
echo "No reviewers found for the modified files. Please specify a reviewer."
234+
exit 1
235+
fi
236+
226237
assignee=$(IFS=,; echo "${reviewers[*]}")
227238
title="$PREFIX: $(git symbolic-ref --short HEAD)"
228239

@@ -236,14 +247,21 @@ function ghpra() {
236247

237248
if [[ -n "$existing_pr" ]]; then
238249
echo "PR already exists: $existing_pr"
239-
echo "Copy and paste the following in Slack:"
240-
echo "[$title]($existing_pr)"
250+
pr_url=$existing_pr
241251
else
242252
output=$(gh pr create --fill --title="$title" --reviewer "$assignee" --assignee "$assignee")
243253
echo "PR created: $output"
244-
echo "Copy and paste the following in Slack:"
245-
echo "[$title]($output)"
254+
pr_url=$(echo "$output" | grep -o 'https://github\.com/[^[:space:]]*')
246255
fi
256+
257+
# Check for a valid URL before proceeding
258+
if [[ -z "$pr_url" ]]; then
259+
echo "Could not find a valid PR URL. Please check the 'gh pr create' output."
260+
exit 1
261+
fi
262+
263+
echo "Copy and paste the following in Slack:"
264+
echo "<$pr_url|$title>"
247265

248266
# Notify reviewers on Slack
249267
for reviewer in "${reviewers[@]}"; do

0 commit comments

Comments
 (0)