-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbadgetizr
executable file
·223 lines (195 loc) · 9.07 KB
/
badgetizr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
if [ -f "$(dirname "$0")/../libexec/utils.sh" ]; then
UTILS_PATH="$(dirname "$0")/../libexec/utils.sh"
elif [ -f "$(dirname "$0")/utils.sh" ]; then
UTILS_PATH="$(dirname "$0")/utils.sh"
else
echo "Error: utils.sh not found!"
exit 1
fi
# Charge utils.sh
source "$UTILS_PATH"
config_file=".badgetizr.yml"
while getopts "c:hv-:" opt; do
case $opt in
c)
config_file="$OPTARG"
;;
h)
show_help
exit 0
;;
v)
echo "$BADGETIZR_VERSION"
exit 0
;;
-)
case "${OPTARG}" in
configuration=*)
config_file="${OPTARG#*=}"
;;
configuration)
config_file="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
;;
pr-build-number=*)
ci_badge_build_number="${OPTARG#*=}"
;;
pr-build-number)
ci_badge_build_number="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
pr-build-url=*)
ci_badge_build_url="${OPTARG#*=}"
;;
pr-build-url)
ci_badge_build_url="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
pr-destination-branch=*)
ci_badge_destination_branch="${OPTARG#*=}"
;;
pr-destination-branch)
ci_badge_destination_branch="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
pr-id=*)
ci_badge_pull_request_id="${OPTARG#*=}"
;;
pr-id)
ci_badge_pull_request_id="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
;;
version)
echo "$BADGETIZR_VERSION"
exit 0
;;
help)
show_help
exit 0
;;
*)
echo "Invalid option --${OPTARG}" >&2
echo "Use --help for more information." >&2
exit 1
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo "Use --help for more information." >&2
exit 1
;;
esac
done
if [ -z "$ci_badge_pull_request_id" ]; then
echo "🔴 Error: --pr-id is mandatory." >&2
show_help
exit 1
fi
echo "$config_file"
if [[ -f "$config_file" && -s "$config_file" ]]; then
wip_badge_enabled=$(yq e '.badge_wip.enabled // "true"' "$config_file")
wip_badge_label=$(yq e '.badge_wip.settings.label // "Work in Progress"' "$config_file")
wip_badge_color=$(yq e '.badge_wip.settings.color // "yellow"' "$config_file")
wip_badge_logo=$(yq e '.badge_wip.settings.logo // "vlcmediaplayer"' "$config_file")
branch_badge_enabled=$(yq e '.badge_base_branch.enabled // "false"' "$config_file")
branch_badge_base_branch=$(yq e '.badge_base_branch.settings.base_branch // "develop"' "$config_file")
branch_badge_color=$(yq e '.badge_base_branch.settings.color // "orange"' "$config_file")
branch_badge_label=$(yq e '.badge_base_branch.settings.label // "Target branch"' "$config_file")
ci_badge_enabled=$(yq e '.badge_ci.enabled // "false"' "$config_file")
ci_badge_label_color=$(yq e '.badge_ci.settings.label_color // "purple"' "$config_file")
ci_badge_color=$(yq e '.badge_ci.settings.color // "purple"' "$config_file")
ci_badge_label=$(yq e '.badge_ci.settings.label // "CI"' "$config_file")
ci_badge_logo=$(yq e '.badge_ci.settings.logo // "🤖"' "$config_file")
ticket_badge_enabled=$(yq e '.badge_ticket.enabled // "false"' "$config_file")
ticket_badge_pattern=$(yq e '.badge_ticket.settings.sed_pattern // ".*\\(([^)]+)\\).*"' "$config_file")
ticket_badge_url=$(yq e '.badge_ticket.settings.url // "https://yourproject.atlassian.net/browse/"' "$config_file")
ticket_badge_color=$(yq e '.badge_ticket.settings.color // "blue"' "$config_file")
ticket_badge_label=$(yq e '.badge_ticket.settings.label // "Jira"' "$config_file")
ticket_badge_logo=$(yq e '.badge_ticket.settings.logo // "jirasoftware"' "$config_file")
dynamic_badge_enabled=$(yq e '.badge_dynamic.enabled // "false"' "$config_file")
else
echo "🔵 $config_file not found. Using default values."
wip_badge_enabled="true"
wip_badge_label="Work in Progress"
wip_badge_color="yellow"
wip_badge_logo="vlcmediaplayer"
branch_badge_enabled="true"
branch_badge_base_branch="develop"
branch_badge_color="orange"
branch_badge_label="Target branch"
ci_badge_enabled="false"
ticket_badge_enabled="false"
dynamic_badge_enabled="false"
fi
pull_request_body=$(gh pr view $ci_badge_pull_request_id --json body -q '.body')
pull_request_title=$(gh pr view $ci_badge_pull_request_id --json title -q '.title')
# Extract current badges from the pull request body
pull_request_body=$(echo "$pull_request_body" | awk '/<!--begin:badgetizr-->/ {flag=1; next} /<!--end:badgetizr-->/ {flag=0; next} !flag' | tr -d '\r')
all_badges=
#Ticket Badge
if [ "$ticket_badge_enabled" = "true" ]; then
ticket_id=$(echo "$pull_request_title" | sed -n -E "s/${ticket_badge_pattern}/\1/p")
if [[ -z "$ticket_id" ]]; then
echo "🟠 No ticket id identified in the PR title. Maybe your pattern is not correct."
else
echo "🟡 Ticket id identified is -> $ticket_id"
ticket_badge_url_for_badge=$(printf "$ticket_badge_url" "$ticket_id")
ticket_id_for_badge=$(echo $ticket_id | sed -E 's/ /_/g; s/-/--/g')
ticket_badge="[]($ticket_badge_url_for_badge)"
all_badges=$all_badges$ticket_badge
fi
fi
# CI Badge
if [ "$ci_badge_enabled" = "true" ]; then
if [ -z "$ci_badge_build_number" ]; then
echo "🔴 Error: --pr-build-number is mandatory. Use -h for help." >&2
exit 1
fi
if [ -z "$ci_badge_build_url" ]; then
echo "🔴 Error: --pr-build-url is mandatory. Use -h for help." >&2
exit 1
fi
ci_badge="[]($ci_badge_build_url)"
all_badges=$(echo $all_badges $ci_badge)
fi
# Target Branch Badge
if [ "$branch_badge_enabled" = "true" ]; then
if [ -z "$ci_badge_destination_branch" ]; then
echo "🔴 Error: --pr-destination-branch is mandatory when branch badge is enabled. Use -h for help." >&2
exit 1
fi
branch_badge_label_for_badge=$(echo $branch_badge_label | sed -E 's/ /_/g; s/-/--/g')
if [[ "$ci_badge_destination_branch" != "$branch_badge_base_branch" ]];then
branch_badge=""
all_badges=$(echo $all_badges $branch_badge)
fi
fi
# Wip Badge
if [ "$wip_badge_enabled" = "true" ]; then
if [[ "$pull_request_title" =~ [Ww][Ii][Pp] ]]; then
wip_badge=""
all_badges=$(echo $all_badges $wip_badge)
fi
fi
# Dynamic Badge
if [ "$dynamic_badge_enabled" = "true" ]; then
badge_count=$(yq '.badge_dynamic.settings.patterns | length' "$config_file")
for ((i=0; i<$badge_count; i++)); do
label=$(yq ".badge_dynamic.settings.patterns[$i].label // \"Badge_$1\"" "$config_file")
pattern=$(yq ".badge_dynamic.settings.patterns[$i].sed_pattern // \"no_pattern\"" "$config_file")
match=$(echo $pull_request_body | sed -n -E "s/${pattern}/\1/p")
if [[ -z "$match" ]]; then
echo "🔵 Match=${match} is empty for badge label: ${label} at position[$i]. Check your regex: ${pattern} against your PR. The regexp must contain a group (*.)"
continue
fi
default_label=$(echo $label | sed -E 's/ /_/g; s/-/--/g')
override_label=$(jq -rn --arg s "$label" '$s | @uri')
value=$(yq ".badge_dynamic.settings.patterns[$i].value // \"default\"" "$config_file" | sed -E 's/ /_/g; s/-/--/g')
color=$(yq ".badge_dynamic.settings.patterns[$i].color // \"orange\"" "$config_file")
dynamic_badge=""
all_badges=$(echo $all_badges $dynamic_badge)
done
fi
# Add delimiter for next replacement
all_badges=$(printf "<!--begin:badgetizr--> \n%s\n<!--end:badgetizr-->" "$all_badges")
# Build new body
new_pull_request_body=$(printf "%s\n%s" "$all_badges" "$pull_request_body")
# Send the new body
gh pr edit $ci_badge_pull_request_id -b "$new_pull_request_body"