1+ #! /usr/bin/env bash
2+
3+ export BROWSER=echo
4+ current_directory=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " ; pwd -P )
5+
6+ format=" $1 "
7+ repo=" $2 "
8+ branches_file=" $current_directory /../../branches.yml"
9+
10+ readarray activeBranches < <( yq e -o=j -I=0 ' .active[]' " $branches_file " )
11+
12+ get_url () {
13+ local repo=$1
14+ local branch=$2
15+ gh search prs --repo=$repo --state=open --base $branch -w | awk ' {print substr($0, 1, length($0)-12)}' 2> /dev/null
16+ }
17+
18+ terminal_bold () {
19+ local text=$1
20+ echo " \033[1m$text \033[22m"
21+ }
22+
23+ markdown_bold () {
24+ local text=$1
25+ echo " *$text *"
26+ }
27+
28+ terminal_url () {
29+ local url=$1
30+ local text=$2
31+ echo " \033]8;;$url \033\\\\ $text \033]8;;\033\\\\ "
32+ }
33+
34+ markdown_url () {
35+ local url=$1
36+ local text=$2
37+ echo " <$url |$text >"
38+ }
39+
40+ format_header () {
41+ local branch=$1
42+ local url=$2
43+ local format=$3
44+ case $format in
45+ terminal)
46+ text=$( echo " PRs open for $( terminal_url $url $branch ) :" )
47+ echo " $( terminal_bold " $text " ) "
48+ ;;
49+ * )
50+ text=$( echo " PRs open for $( markdown_url $url $branch ) :" )
51+ echo " $( markdown_bold " $text " ) "
52+ ;;
53+ esac
54+ }
55+
56+ get_and_format_prs () {
57+ local repo=$1
58+ local branch=$2
59+ local format=$3
60+ case $format in
61+ terminal)
62+ gh search prs --repo=$repo --state=open --json url,number,title,updatedAt --template ' {{range .}}{{(printf "- %s | Last Updated: %s\\n" (hyperlink .url (printf "#%v: %q" .number .title)) (timeago .updatedAt))}}{{end}}' --base $branch | cat
63+ ;;
64+ * )
65+ gh search prs --repo=$repo --state=open --json url,number,title,updatedAt --template ' {{range .}}{{(printf "• <%s|#%v>: %q | *Last Updated: %s*\\n" .url .number .title (timeago .updatedAt))}}{{end}}' --base $branch | cat
66+ ;;
67+ esac
68+ }
69+
70+ echo_terminal () {
71+ local text=$1
72+ echo -e " $text "
73+ }
74+
75+ echo_json () {
76+ local text=$1
77+ echo " $text " | jq -Rc ' {type: "mrkdwn", text: .}' | awk ' {gsub(/\\\\n/, "\\n"); print}'
78+ }
79+
80+ message=" "
81+ for activeBranch in " ${activeBranches[@]} " ; do
82+ branch=$( echo " $activeBranch " | yq -r)
83+ url=$( get_url " $repo " " $branch " )
84+ header=" $( format_header " $branch " " $url " " $format " ) "
85+ prs=" $( get_and_format_prs " $repo " " $branch " " $format " ) "
86+ if [ -n " $prs " ]; then
87+ message+=" $header \n$prs \n"
88+ fi
89+ done
90+
91+ if [ -n " $message " ]; then
92+ # chomp off the last two newlines
93+ message=" ${message::- 4} "
94+
95+ case $format in
96+ terminal|testing)
97+ echo_terminal " $message "
98+ ;;
99+ * )
100+ echo_json " $message "
101+ ;;
102+ esac
103+ fi
0 commit comments