Created
October 6, 2024 09:18
-
-
Save aiKrice/b413aa09a7214313b61375e544f1e7b3 to your computer and use it in GitHub Desktop.
Create badges for Pull Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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\"></a>" | |
| all_badges=$all_badges$jira_badge | |
| fi | |
| # CI Badge | |
| ci_badge="[]($BITRISE_BUILD_URL)" | |
| all_badges=$(echo $all_badges $ci_badge) | |
| # Target Branch Badge | |
| if [[ $BITRISEIO_GIT_BRANCH_DEST != "develop" ]];then | |
| branch_badge="" | |
| all_badges=$(echo $all_badges $branch_badge) | |
| fi | |
| # Wip Badge | |
| if [[ "$pull_request_title" =~ [Ww][Ii][Pp] ]];then | |
| wip_badge="" | |
| 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="" | |
| all_badges=$(echo $all_badges $unchecked_task_badge) | |
| fi | |
| if [[ "$pull_request_body" == *"- [ ] Make sure to update"* ]];then | |
| unchecked_task_badge="" | |
| 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