Skip to content

Commit

Permalink
feat(badge): Support sed for pattern and fix boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
aiKrice committed Dec 5, 2024
1 parent a5779d5 commit 9a4bac4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
20 changes: 10 additions & 10 deletions .badgetizr.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
badge_ci:
enabled: false
enabled: "false"
settings:
color: "purple"
label: "Bitrise"
logo: "bitrise"

badge_ticket:
enabled: true
enabled: "true"
settings:
color: "black"
label: "Github"
Expand All @@ -15,40 +15,40 @@ badge_ticket:
logo: "github"

badge_base_branch:
enabled: true
enabled: "false"
settings:
color: "orange"
label: "Base Branch"
base_branch: "develop"

badge_wip:
enabled: true
enabled: "true"
settings:
color: "yellow"
label: "WIP"
logo: "vlcmediaplayer"

badge_dynamic:
enabled: true
enabled: "true"
settings:
patterns:
- badge_1:
pattern: "- [ ] Task 1"
sed_pattern: '(- \[ \] Task 1)'
label: "Task 1"
value: "Missing"
color: "orange"
- badge_2:
pattern: "- [x] Task 1"
sed_pattern: '(- \[x\] Task 1)'
label: "Task 1"
value: "Completed"
color: "forestgreen"
- badge_3:
pattern: "- [ ] Task 2"
sed_pattern: '(- \[x\] Task 2)'
label: "Task 2"
value: "Missing"
color: "orange"
- badge_3:
pattern: "- [x] Task 2"
- badge_4:
sed_pattern: '(- [ ] Task 2)'
label: "Task 2"
value: "Completed"
color: "forestgreen"
40 changes: 25 additions & 15 deletions .badgetizr.yml.example
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
badge_ci:
enabled: true
enabled: "false"
settings:
color: "purple"
label: "Bitrise"
logo: "bitrise"

badge_ticket:
enabled: true
enabled: "true"
settings:
color: "blue"
label: "JIRA"
sed_pattern: '.*\(([^)]+)\).*'
url: "https://yourproject.atlassian.net/browse/%s"
logo: "jirasoftware"
color: "black"
label: "Github"
sed_pattern: '.*\[GH-([0-9]+)\].*'
url: "https://github.com/aiKrice/badgetizr/issues/%s"
logo: "github"

badge_base_branch:
enabled: true
enabled: "false"
settings:
color: "orange"
label: "Base Branch"
base_branch: "develop"

badge_wip:
enabled: true
enabled: "true"
settings:
color: "yellow"
label: "WIP"
logo: "vlcmediaplayer"

badge_dynamic:
enabled: true
enabled: "true"
settings:
patterns:
- badge_1:
pattern: "- [ ] Task 1"
sed_pattern: '(- \[ \] Task 1)'
label: "Task 1"
value: "Not done"
value: "Missing"
color: "orange"
- badge_2:
pattern: "- [x] Task 2"
sed_pattern: '(- \[x\] Task 1)'
label: "Task 1"
value: "Completed"
color: "forestgreen"
- badge_3:
sed_pattern: '(- \[x\] Task 2)'
label: "Task 2"
value: "Missing"
color: "orange"
- badge_4:
sed_pattern: '(- [ ] Task 2)'
label: "Task 2"
value: "Done"
color: "darkgreen"
value: "Completed"
color: "forestgreen"
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ If you want to use an icon for your badge, you can use the `icon` option and spe
#### Description

The badge ticket is a badge that will be displayed on your pull request if you have a Jira ticket or a Youtrack ticket in your pull request title or overall, something you would like to extract from the pull request title.
To do so, you have to define a pattern that will be used to extract the data (ie: ticket id). This pattern will be used with the `sed` command with the `-n -E` options and the `p` flag and will extract the first occurrence of the pattern found in the string. Badgetizr will process the pattern like this: `sed -n -E "s/${ticket_badge_pattern}/\1/p"'`.
To do so, you have to define a pattern that will be used to extract the data (ie: ticket id). This pattern will be used with the `sed` command with the `-n -E` options and the `p` flag and will extract the first occurrence of the pattern found in the string. Badgetizr will process the pattern like this: `sed -n -E "s/${ticket_badge_pattern}/\1/p"'`. You have to provide a regex group with ()

#### Configuration:
- `color`: The color of the badge (default: `blue`).
Expand Down Expand Up @@ -150,12 +150,11 @@ The badge dynamic is a badge that will be displayed on your pull request if the
- `color`: The color of the badge (default: `grey`).
- `label`: The label of the badge (default: `Task 2`).
- `value`: The value of the badge (default: `Done`).
- `labelColor`: The color of the label (default: `grey`).
- `color`: The color of the badge (default: `darkgreen`).
- `sed_pattern`: The pattern which will be applied against the pull request body (default: `no pattern`). A regex group `()` must be present or it will trigger a sed error. See .badgetizr.yml.example file.

example:
```yaml
- pattern: "- [ ] Task 1"
- sed_pattern: "(- [ ] Task 1)"
label: "Task 1"
value: "Not done"
color: "orange"
Expand All @@ -165,7 +164,7 @@ will display: ![Static Badge](https://img.shields.io/badge/Task_1-Not_done-orang
In this case you can also use this to display a badge if the checkbox is checked.
example:
```yaml
- pattern: "- [x] Task 2"
- sed_pattern: "(- [x] Task 2)"
label: "Task 2"
value: "Done"
color: "green"
Expand Down Expand Up @@ -204,6 +203,19 @@ The `badge ci` is a badge that will be displayed on your pull request to indicat
- `label`: The label of the badge (default: `Bitrise`).
- `logo`: The logo of the badge (default: `bitrise`).

#### Example
```yaml
badge_ticket:
enabled: "true"
settings:
color: "black"
label: "Github"
sed_pattern: '.*\[GH-([0-9]+)\].*'
url: "https://github.com/aiKrice/badgetizr/issues/%s"
logo: "github"
```
will match feat(scope): implement stuff [GH-1234] #will extract 1234

## Contributing
You are welcome to contribute to this project. The current rules to follow is that each time you are opening a pull request, you have to generate yourself the badge inside the pull request by running the script locally on your machine when your forked the repository.

Expand Down
24 changes: 14 additions & 10 deletions badgetizr
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ if [ -z "$ci_badge_pull_request_id" ]; then
show_help
exit 1
fi

if [ -f "$config_file" ]; then
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 // "true"' "$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")
Expand Down Expand Up @@ -192,19 +192,23 @@ 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
pattern=$(yq ".badge_dynamic.settings.patterns[$i].pattern // \"no_pattern\"" "$config_file")
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")

if [[ "$pull_request_body" == *"$pattern"* ]];then
dynamic_badge="![Static Badge](https://img.shields.io/badge/$default_label-$value-grey?label=$override_label&labelColor=grey&color=${color})"
all_badges=$(echo $all_badges $dynamic_badge)
fi

dynamic_badge="![Static Badge](https://img.shields.io/badge/$default_label-$value-grey?label=$override_label&labelColor=grey&color=${color})"
all_badges=$(echo $all_badges $dynamic_badge)
done
fi

Expand Down

0 comments on commit 9a4bac4

Please sign in to comment.