Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aiKrice committed Dec 5, 2024
2 parents 3642b12 + 7244622 commit 8b8f8d7
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 91 deletions.
29 changes: 15 additions & 14 deletions .badgetizr.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
badge_ci:
enabled: false
enabled: "true"
settings:
color: "purple"
label: "Bitrise"
logo: "bitrise"
label_color: "black"
label: "Build"
logo: "github"
color: darkgreen

badge_ticket:
enabled: true
enabled: "true"
settings:
color: "black"
label: "Github"
label: "Issue"
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: "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"
2 changes: 1 addition & 1 deletion .github/workflows/badgetizr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v3

- name: Run Badgetizr
uses: aiKrice/homebrew-badgetizr@
uses: aiKrice/homebrew-badgetizr@1.5.1
with:
pr_id: ${{ github.event.pull_request.number }}
configuration: .badgetizr.yml
Expand Down
3 changes: 2 additions & 1 deletion Formula/badgetizr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Badgetizr < Formula
depends_on "gh"

def install
bin.install "badgetizr.sh" => "badgetizr"
libexec.install "badgetizr", "utils.sh"
bin.install_symlink libexec/"badgetizr"
end

end
Expand Down
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

<h1 align="center">
Badgetizr

![Static Badge](https://img.shields.io/${VERSION}-darkgreen?logo=homebrew&logoColor=white&label=Homebrew-tap)
[![Static Badge](https://img.shields.io/${VERSION}-grey?logo=github&logoColor=white&label=Github-Action&labelColor=black)](https://github.com/marketplace/actions/badgetizr)
![Static Badge](https://img.shields.io/badge/passed-darkgreen?logo=github&logoColor=white&label=Github&labelColor=black)
![Static Badge](https://img.shields.io/badge/soon-pink?logo=gitlab&logoColor=orange&label=Gitlab&labelColor=white)

</h1>

<h2 align="center">
Expand Down Expand Up @@ -86,7 +92,7 @@ jobs:
uses: actions/checkout@v3

- name: Run Badgetizr
uses: aiKrice/homebrew-badgetizr@1.4.0
uses: aiKrice/homebrew-badgetizr@1.5.1
with:
pr_id: ${{ github.event.pull_request.number }}
configuration: .badgetizr.yml
Expand Down Expand Up @@ -116,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 @@ -144,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 @@ -159,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 @@ -198,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
5 changes: 2 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Badgetizr'
description: 'A GitHub Action to run Badgetizr for custom PR badges'
description: 'Badgetizr will automatically add visual badges and link shortcuts inside your pull requests to enhance code reviews developer experience.'
branding:
icon: 'award'
color: 'green'
Expand Down Expand Up @@ -72,6 +72,5 @@ runs:
options="$options --pr-build-url $pr_build_url"
fi
chmod +x ./badgetizr.sh
./badgetizr.sh -c $configuration $options
./badgetizr -c $configuration $options
shell: bash
74 changes: 27 additions & 47 deletions badgetizr.sh → badgetizr
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
#!/bin/bash

# Read config file

show_help() {
cat <<EOF
Usage: $0 [options]
Options :
-c <file>,
--configuration=<file>,
--configuration <file> (Optional) Specify the path to the configuration. Default is .badgetizr.yml
--pr-id=<id>,
--pr-id <id> (Mandatory) Specify the pull request id.
--pr-destination-branch=<branch>,
--pr-destination-branch <branch> (Mandatory when branch badge is enabled) Specify the pull request destination branch.
--pr-build-number=<number>,
--pr-build-number <number> (Mandatory when CI badge is enabled) Specify the pull request build number.
--pr-build-url=<url>,
--pr-build-url <url> (Mandatory when CI badge is enabled) Specify the pull request build url.
--version, -v Display the version of Badgetizr.
-h, --help Display this help.
EOF
}
# Use local utils.sh during development
if [ -f "$(dirname "$0")/../libexec/utils.sh" ]; then
UTILS_PATH="$(dirname "$0")/../libexec/utils.sh"
else
UTILS_PATH="$(dirname "$0")/utils.sh"
fi
source "$UTILS_PATH"

config_file=".badgetizr.yml"
BADGETIZR_VERSION=$(git tag --sort=-creatordate | head -n 1)

while getopts "c:-:" opt; do
while getopts "c:hv-:" opt; do
case $opt in
c)
config_file="$OPTARG"
Expand Down Expand Up @@ -106,19 +84,20 @@ 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")

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")
Expand Down Expand Up @@ -173,26 +152,23 @@ fi
# CI Badge
if [ "$ci_badge_enabled" = "true" ]; then
if [ -z "$ci_badge_build_number" ]; then
echo "🔴 Error: --pr-build-number is mandatory." >&2
show_help
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." >&2
show_help
echo "🔴 Error: --pr-build-url is mandatory. Use -h for help." >&2
exit 1
fi

ci_badge="[![Static Badge](https://img.shields.io/badge/$ci_badge_label-$ci_badge_build_number-purple?logo=$ci_badge_logo&logoColor=white&labelColor=$ci_badge_color&color=green)]($ci_badge_build_url)"
ci_badge="[![Static Badge](https://img.shields.io/badge/$ci_badge_label-$ci_badge_build_number-purple?logo=$ci_badge_logo&logoColor=white&labelColor=$ci_badge_label_color&color=$ci_badge_color)]($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." >&2
show_help
echo "🔴 Error: --pr-destination-branch is mandatory when branch badge is enabled. Use -h for help." >&2
exit 1
fi

Expand All @@ -214,19 +190,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
Loading

0 comments on commit 8b8f8d7

Please sign in to comment.