Skip to content

Instantly share code, notes, and snippets.

@aiKrice
Created October 6, 2024 09:18
Show Gist options
  • Select an option

  • Save aiKrice/b413aa09a7214313b61375e544f1e7b3 to your computer and use it in GitHub Desktop.

Select an option

Save aiKrice/b413aa09a7214313b61375e544f1e7b3 to your computer and use it in GitHub Desktop.
Create badges for Pull Request
#!/bin/bash
//Github gh client
//Need GH_TOKEN or GITHUB_TOKEN exported
//BITRISE_PULL_REQUEST = ID of PR
//BITRISEIO_GIT_BRANCH_DEST = Branch against the PR is opened
//BITRISE_BUILD_URL = link of the current pipeline build
// JIRA ID is extract from this pattern from the title : ...(APPS-123455) or ...(BUG-123455)...
pull_request_body=$(gh pr view $BITRISE_PULL_REQUEST --json body -q '.body')
pull_request_title=$(gh pr view $BITRISE_PULL_REQUEST --json title -q '.title')
pull_request_body=$(echo "$pull_request_body" | awk '/<!--begin-->/ {flag=1; next} /<!--end-->/ {flag=0; next} !flag')
all_badges=
#Jira Badge
jira_id=$(echo "$pull_request_title" | grep -oE '\((APPS|BUG)-[0-9]+\)' | tr -d '()')
if [[ -n "$jira_id" ]]; then
jira_id_for_badge=$(echo $jira_id | sed 's/-/--/g')
jira_badge="<a href=\"https://quotient.atlassian.net/browse/$jira_id\">![Static Badge](https://img.shields.io/badge/JIRA-$jira_id_for_badge-green?logo=jirasoftware&color=grey&labelColor=blue&link=https%3A%2F%2Fquotient.atlassian.net%2Fjira%2Fsoftware%2Fc%2Fprojects%2FAPPS%2Fboards%2F172%3FselectedIssue%3D$jira_id)</a>"
all_badges=$all_badges$jira_badge
fi
# CI Badge
ci_badge="[![Static Badge](https://img.shields.io/badge/CI-$BITRISE_BUILD_NUMBER-purple?logo=bitrise&logoColor=white&labelColor=purple&color=grey)]($BITRISE_BUILD_URL)"
all_badges=$(echo $all_badges $ci_badge)
# Target Branch Badge
if [[ $BITRISEIO_GIT_BRANCH_DEST != "develop" ]];then
branch_badge="![Static Badge](https://img.shields.io/badge/%E2%9A%A0%EF%B8%8FTarget_branch-$BITRISEIO_GIT_BRANCH_DEST-grey?labelColor=grey&color=red)"
all_badges=$(echo $all_badges $branch_badge)
fi
# Wip Badge
if [[ "$pull_request_title" =~ [Ww][Ii][Pp] ]];then
wip_badge="![Static Badge](https://img.shields.io/badge/Work_in_progress-yellow?logo=vlcmediaplayer&logoColor=white)"
all_badges=$(echo $all_badges $wip_badge)
fi
# Detect if PR tasks was not checked
if [[ "$pull_request_body" == *"- [ ] I have written the unit tests"* ]];then
unchecked_task_badge="![Static Badge](https://img.shields.io/badge/Unchecked_Task-Check_unit_tests-red?labelColor=grey&color=red)"
all_badges=$(echo $all_badges $unchecked_task_badge)
fi
if [[ "$pull_request_body" == *"- [ ] Make sure to update"* ]];then
unchecked_task_badge="![Static Badge](https://img.shields.io/badge/Unchecked_Task-Check_baseline-red?labelColor=grey&color=red)"
all_badges=$(echo $all_badges $unchecked_task_badge)
fi
# Add delimiter for next replacement
all_badges=$(printf "<!--begin--> \n%s\n<!--end-->" "$all_badges")
# Build new body
new_pull_request_body=$(printf "%s\n\n%s" "$all_badges" "$pull_request_body")
# Send the new body
gh pr edit $BITRISE_PULL_REQUEST -b "$new_pull_request_body"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment