-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbadgetizr
More file actions
executable file
·504 lines (447 loc) · 21.3 KB
/
badgetizr
File metadata and controls
executable file
·504 lines (447 loc) · 21.3 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/bin/bash
# Detect base path for all files (works in dev and Homebrew)
detect_base_path() {
if [[ -f "$(dirname "$0")/utils.sh" ]]; then
dirname "$0"
else
echo "$(dirname "$0")/../libexec"
fi
}
# Determine if a PR/MR is a hotfix based on target branch and title
# Simple logic: MR targets production (main/master) AND title contains "hotfix"
# Returns: "hotfix" if hotfix detected, "feature" otherwise
#
# Parameters:
# $1 - production_branch (default: "master")
# $2 - mr_base_branch (required: base branch of MR/PR)
# $3 - pr_title (required: PR/MR title)
is_hotfix_pr() {
local production_branch="${1:-master}"
local mr_base_branch="${2:-}"
local pr_title="${3:-}"
# Test mode: allow override via environment variable
if [[ -n "${BADGETIZR_TEST_SOURCE_BRANCH}" ]]; then
echo "${BADGETIZR_TEST_SOURCE_BRANCH}"
return 0
fi
# Simple logic: MR targets production + title contains "hotfix" = hotfix badge
if [[ "${mr_base_branch}" == "main" || "${mr_base_branch}" == "master" || "${mr_base_branch}" == "${production_branch}" ]]; then
if [[ "${pr_title}" =~ [Hh][Oo][Tt][Ff][Ii][Xx] ]]; then
echo "hotfix"
return 0
fi
fi
# Default: regular feature PR
echo "feature"
return 0
}
# load extra files
BASE_PATH=$(detect_base_path)
source "${BASE_PATH}/utils.sh"
source "${BASE_PATH}/providers/provider_utils.sh"
config_file=".badgetizr.yml"
while getopts "c:hv-:" opt; do
case ${opt} in
c)
config_file="${OPTARG}"
;;
h)
show_help
exit 0
;;
v)
# shellcheck disable=SC2154 # BADGETIZR_VERSION is defined in utils.sh
echo "${BADGETIZR_VERSION}"
exit 0
;;
-)
# LCOV_EXCL_START - Long options parsing not tested (uses --option=value format in tests)
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))
;;
provider=*)
forced_provider="${OPTARG#*=}"
;;
provider)
forced_provider="${!OPTIND}"
OPTIND=$((OPTIND + 1))
;;
ci-status=*)
ci_status_badge_status="${OPTARG#*=}"
;;
ci-status)
ci_status_badge_status="${!OPTIND}"
OPTIND=$((OPTIND + 1))
;;
ci-text=*)
ci_status_badge_text="${OPTARG#*=}"
;;
ci-text)
ci_status_badge_text="${!OPTIND}"
OPTIND=$((OPTIND + 1))
;;
# LCOV_EXCL_STOP
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
;;
*)
echo "Unexpected option: -${opt}" >&2
echo "Use --help for more information." >&2
exit 1
;;
esac
done
# Determine provider (forced or auto-detected)
if [[ -n "${forced_provider}" ]]; then
PROVIDER="${forced_provider}"
echo "🔧 Forced provider: ${PROVIDER}"
else
PROVIDER=$(detect_provider)
echo "🔍 Detected provider: ${PROVIDER}"
fi
# Check mandatory parameter first (before authentication)
if [[ -z "${ci_badge_pull_request_id}" ]]; then
echo "🔴 Error: --pr-id is mandatory." >&2
show_help
exit 1
fi
# Load the provider
if [[ "${PROVIDER}" != "github" && "${PROVIDER}" != "gitlab" ]]; then
echo "❌ Error: Unsupported provider '${PROVIDER}'. Supported: github, gitlab"
exit 1
fi
# Check if required CLI is installed
if ! check_provider_cli "${PROVIDER}"; then
exit 1
fi
load_provider "${PROVIDER}"
# Test provider authentication
if ! test_provider_auth; then
exit 1
fi
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}")
wip_badge_labelized=$(yq e '.badge_wip.settings.labelized // ""' "${config_file}")
hotfix_badge_enabled=$(yq e '.badge_hotfix.enabled // "false"' "${config_file}")
hotfix_badge_color=$(yq e '.badge_hotfix.settings.color // "red"' "${config_file}")
hotfix_badge_text_color=$(yq e '.badge_hotfix.settings.text_color // "white"' "${config_file}")
hotfix_badge_label=$(yq e '.badge_hotfix.settings.label // "HOTFIX"' "${config_file}")
hotfix_badge_labelized=$(yq e '.badge_hotfix.settings.labelized // ""' "${config_file}")
hotfix_badge_production_branch=$(yq e '.badge_hotfix.settings.production_branch // "master"' "${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=$(yq e '.badge_ci.settings.label // "CI"' "${config_file}")
ci_badge_logo=$(yq e '.badge_ci.settings.logo // "github"' "${config_file}")
ci_badge_color=$(yq e '.badge_ci.settings.color // "purple"' "${config_file}")
ci_badge_label_color=$(yq e '.badge_ci.settings.label_color // "black"' "${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}")
ready_for_approval_badge_enabled=$(yq e '.badge_ready_for_approval.enabled // "false"' "${config_file}")
ready_for_approval_badge_color=$(yq e '.badge_ready_for_approval.settings.color // "darkgreen"' "${config_file}")
ready_for_approval_badge_label=$(yq e '.badge_ready_for_approval.settings.label // "Ready"' "${config_file}")
ready_for_approval_badge_logo=$(yq e '.badge_ready_for_approval.settings.logo // "checkmark"' "${config_file}")
ready_for_approval_badge_labelized=$(yq e '.badge_ready_for_approval.settings.labelized // ""' "${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"
hotfix_badge_enabled="false"
hotfix_badge_color="red"
hotfix_badge_text_color="white"
hotfix_badge_label="HOTFIX"
hotfix_badge_production_branch="master"
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"
ready_for_approval_badge_enabled="false"
fi
# Fetch PR info in a single call for efficiency
pr_info_json=$(get_pr_info "${ci_badge_pull_request_id}")
pull_request_title=$(yq -r '.title' <<< "${pr_info_json}")
pull_request_body_raw=$(yq -r '.body // .description // ""' <<< "${pr_info_json}")
# Remove existing badgetizr badges and carriage returns from PR body
pull_request_body=$(awk '/<!--begin:badgetizr-->/ {flag=1; next} /<!--end:badgetizr-->/ {flag=0; next} !flag' <<< "${pull_request_body_raw}" | tr -d '\r')
all_badges=
#Ticket Badge
if [[ "${ticket_badge_enabled}" = "true" ]]; then
ticket_id=$(sed -n -E "s/${ticket_badge_pattern}/\1/p" <<< "${pull_request_title}")
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}"
# shellcheck disable=SC2059 # ticket_badge_url contains %s placeholder from config
ticket_badge_url_for_badge=$(printf "${ticket_badge_url}" "${ticket_id}")
ticket_id_for_badge=$(url_encode_shields "${ticket_id}")
ticket_badge="[](${ticket_badge_url_for_badge})"
all_badges=${all_badges}${ticket_badge}
fi
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=$(url_encode_shields "${branch_badge_label}")
if [[ "${ci_badge_destination_branch}" != "${branch_badge_base_branch}" ]]; then
branch_badge=""
all_badges="${all_badges} ${branch_badge}"
fi
fi
# Hotfix Badge
if [[ "${hotfix_badge_enabled}" = "true" ]]; then
# Check if PR targets production + title contains "hotfix"
hotfix_status=$(is_hotfix_pr "${hotfix_badge_production_branch}" "${ci_badge_destination_branch}" "${pull_request_title}")
if [[ "${hotfix_status}" == "hotfix" ]]; then
hotfix_badge=""
all_badges="${all_badges} ${hotfix_badge}"
# Add pr/mr label if labelized is set
if [[ -n "${hotfix_badge_labelized}" && "${hotfix_badge_labelized}" != "null" ]]; then
if ! add_pr_label "${ci_badge_pull_request_id}" "${hotfix_badge_labelized}"; then
# Create hotfix label with red color (non-customizable)
if create_pr_label "${hotfix_badge_labelized}" "d73a49" "${BADGETIZR_LABEL_DESCRIPTION}"; then
add_pr_label "${ci_badge_pull_request_id}" "${hotfix_badge_labelized}"
fi
fi
fi
else
# Branch originated from develop, not a hotfix - remove label if configured
if [[ -n "${hotfix_badge_labelized}" && "${hotfix_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${hotfix_badge_labelized}"
fi
fi
else
# Hotfix badge disabled, remove label if configured
if [[ -n "${hotfix_badge_labelized}" && "${hotfix_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${hotfix_badge_labelized}"
fi
fi
# Wip Badge
if [[ "${wip_badge_enabled}" = "true" ]]; then
if [[ "${pull_request_title}" =~ [Ww][Ii][Pp] ]]; then
wip_badge=""
all_badges="${all_badges} ${wip_badge}"
# Add pr/mr label if labelized is set
if [[ -n "${wip_badge_labelized}" && "${wip_badge_labelized}" != "null" ]]; then
if ! add_pr_label "${ci_badge_pull_request_id}" "${wip_badge_labelized}"; then
# Label doesn't exist, create it with appropriate color
# LCOV_EXCL_START - Color mapping not fully tested (tests use default yellow)
hex_color=""
case "${wip_badge_color}" in
"yellow") hex_color="fbca04" ;;
"orange") hex_color="d93f0b" ;;
"red") hex_color="d73a49" ;;
"green" | "forestgreen") hex_color="28a745" ;;
"blue") hex_color="0366d6" ;;
"purple") hex_color="6f42c1" ;;
"grey" | "gray") hex_color="586069" ;;
"black") hex_color="24292e" ;;
*) hex_color="fbca04" ;; # Default to yellow for WIP
esac
# LCOV_EXCL_STOP
if create_pr_label "${wip_badge_labelized}" "${hex_color}" "${BADGETIZR_LABEL_DESCRIPTION}"; then
add_pr_label "${ci_badge_pull_request_id}" "${wip_badge_labelized}"
fi
fi
fi
else
# PR is no longer WIP, remove label if configured
if [[ -n "${wip_badge_labelized}" && "${wip_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${wip_badge_labelized}"
fi
fi
else
# WIP badge disabled, remove label if configured
if [[ -n "${wip_badge_labelized}" && "${wip_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${wip_badge_labelized}"
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=$(sed -n -E "s/${pattern}/\1/p" <<< "${pull_request_body}")
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
label_encoded=$(url_encode_shields "${label}")
value=$(yq ".badge_dynamic.settings.patterns[${i}].value // \"default\"" "${config_file}")
value_encoded=$(url_encode_shields "${value}")
color=$(yq ".badge_dynamic.settings.patterns[${i}].color // \"orange\"" "${config_file}")
dynamic_badge=""
all_badges="${all_badges} ${dynamic_badge}"
done
fi
# Ready for Approval Badge
if [[ "${ready_for_approval_badge_enabled}" = "true" ]]; then
# Count unchecked checkboxes in PR body
unchecked_count=$(printf "%s\n" "${pull_request_body}" | grep -c "\- \[ \]" 2> /dev/null || echo "0")
unchecked_count=$(echo "${unchecked_count}" | tr -d '[:space:]')
if [[ "${unchecked_count}" -gt 0 ]]; then
echo "🔲 Found ${unchecked_count} unchecked checkbox(es), PR not ready for approval"
# Remove ready for approval label if configured
if [[ -n "${ready_for_approval_badge_labelized}" && "${ready_for_approval_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${ready_for_approval_badge_labelized}"
fi
else
echo "✅ All checkboxes are checked, PR ready for approval"
# Create ready for approval badge (forced to first position)
ready_for_approval_badge=""
all_badges="${ready_for_approval_badge} ${all_badges}"
# Add ready for approval label if configured
if [[ -n "${ready_for_approval_badge_labelized}" && "${ready_for_approval_badge_labelized}" != "null" ]]; then
if ! add_pr_label "${ci_badge_pull_request_id}" "${ready_for_approval_badge_labelized}"; then
# Label doesn't exist, create it with green color
if create_pr_label "${ready_for_approval_badge_labelized}" "28a745" "${BADGETIZR_LABEL_DESCRIPTION}"; then
add_pr_label "${ci_badge_pull_request_id}" "${ready_for_approval_badge_labelized}"
fi
fi
fi
fi
else
# Ready for approval badge disabled, remove label if configured
if [[ -n "${ready_for_approval_badge_labelized}" && "${ready_for_approval_badge_labelized}" != "null" ]]; then
remove_pr_label "${ci_badge_pull_request_id}" "${ready_for_approval_badge_labelized}"
fi
fi
# CI Badge (unified with status support)
if [[ "${ci_badge_enabled}" = "true" ]]; then
# pr-build-url is always mandatory for CI badge
if [[ -z "${ci_badge_build_url}" ]]; then
echo "🔴 Error: --pr-build-url is mandatory when CI badge is enabled." >&2
exit 1
fi
# If ci-status is provided, we're in status mode
if [[ -n "${ci_status_badge_status}" ]]; then
# Validate status value (only 4 allowed)
case "${ci_status_badge_status}" in
"started")
ci_color="yellow"
;;
"passed")
ci_color="darkgreen"
;;
"warning")
ci_color="orange"
;;
"failed")
ci_color="red"
;;
*)
echo "❌ Error: Invalid CI status '${ci_status_badge_status}'. Allowed: started, passed, warning, failed" >&2
exit 1
;;
esac
# Determine badge text based on status
if [[ "${ci_status_badge_status}" == "started" || "${ci_status_badge_status}" == "warning" ]]; then
# For intermediate statuses, use custom text if provided, otherwise use status
if [[ -n "${ci_status_badge_text}" ]]; then
ci_text="${ci_status_badge_text}"
else
ci_text="${ci_status_badge_status}"
fi
else
# For passed/failed, use build number if available, otherwise custom text, otherwise status
if [[ -n "${ci_badge_build_number}" ]]; then
ci_text="${ci_badge_build_number}"
elif [[ -n "${ci_status_badge_text}" ]]; then
ci_text="${ci_status_badge_text}"
else
ci_text="${ci_status_badge_status}"
fi
fi
echo "🔄 Updating CI badge: ${ci_status_badge_status} (${ci_text})"
else
# Legacy mode: no status provided, use build number (backward compatibility)
if [[ -z "${ci_badge_build_number}" ]]; then
echo "🔴 Error: --pr-build-number is mandatory when no --ci-status is provided." >&2
exit 1
fi
ci_color="${ci_badge_color}"
ci_text="${ci_badge_build_number}"
echo "🔄 Creating CI badge: build ${ci_text}"
fi
# Escape text and label for URL
ci_text_escaped=$(url_encode_shields "${ci_text}")
ci_label_escaped=$(url_encode_shields "${ci_badge_label}")
# Create unified CI badge (always clickable)
ci_badge="[](${ci_badge_build_url})"
all_badges="${all_badges} ${ci_badge}"
echo "✅ CI badge added to badges collection"
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}")
# Update PR description
update_pr_description "${ci_badge_pull_request_id}" "${new_pull_request_body}"