Skip to content

Instantly share code, notes, and snippets.

@mateuszwrobel
Last active April 8, 2022 12:16
Show Gist options
  • Save mateuszwrobel/831958e35c2e4a91dddd16417f80d455 to your computer and use it in GitHub Desktop.
Save mateuszwrobel/831958e35c2e4a91dddd16417f80d455 to your computer and use it in GitHub Desktop.
Branch naming policy guard with Azure Pipeline handling funtionality
- task: Bash@3
displayName: Branch name check
inputs:
filePath: '$(System.DefaultWorkingDirectory)/git-hooks/hooks/prepare-commit-msg'
#!/bin/bash
# hook arguments
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
IS_PIPELINE=0
BRANCH_NAME="${SYSTEM_PULLREQUEST_SOURCEBRANCH#refs/heads/}"
if [ -n "$BRANCH_NAME" ]; then
IS_PIPELINE=1
else
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
fi
#
# Automatically adds branch name and branch description to every commit message.
# Modified from the gist here https://gist.github.com/bartoszmajsak/1396344
#
# This way you can customize which branches should be skipped when
# prepending commit message.
RED="\033[1;31m"
GREEN="\033[1;32m"
ORANGE="\033[0;33m"
NOCOLOR="\033[0m"
if [ -z "$MAIN_BRANCHES" ]; then
MAIN_BRANCHES=(release RC master develop hotfix bugfix)
fi
MAIN_BRANCHES_UNION="release|RC|master|develop|hotfix|bugfix"
# Regex to check the valid branch name
NOT_ALLOWED_BRANCH="^(${MAIN_BRANCHES_UNION})$"
VALID_BRANCH_REGEX="^(${MAIN_BRANCHES_UNION})\/([a-z0-9._-]+)$"
TASK_VALID_BRANCH_REGEX="^(feature|bugfix)\/(s{1}\d+\-{1}(u|b){1}\d+)\-([a-z0-9._-]+)$"
# Get branch name and description
TASK_BRANCH_NAME=$(printf $BRANCH_NAME | LC_CTYPE=en_US.utf8 grep -Po '(s{1}\d+\-{1}(u|b){1}\d+)')
IS_TASK_BRANCH=$(printf $BRANCH_NAME | LC_CTYPE=en_US.utf8 grep -Poc "${TASK_VALID_BRANCH_REGEX}")
IS_VALID_BRANCH=$(printf $BRANCH_NAME | LC_CTYPE=en_US.utf8 grep -Poc "${VALID_BRANCH_REGEX}")
IS_NOT_ALLOWED_BRANCH=$(printf $BRANCH_NAME | LC_CTYPE=en_US.utf8 grep -Poc "${NOT_ALLOWED_BRANCH}")
echo -e "IS_TASK_BRANCH ${IS_TASK_BRANCH} IS_VALID_BRANCH ${IS_VALID_BRANCH} IS_NOT_ALLOWED_BRANCH ${IS_NOT_ALLOWED_BRANCH}"
IS_VALID=0
if [ -n "$BRANCH_NAME" ] && ([[ $IS_TASK_BRANCH -ne 0 ]] || [[ IS_VALID_BRANCH -ne 0 ]]) && [[ IS_NOT_ALLOWED_BRANCH -lt 1 ]]; then
IS_VALID=1
fi
# check the branch name is valid or not
# if face any error in mac then run chmod u+x .git/hooks/prepare-commit-msg and restart your terminal
if [[ IS_VALID -le 0 ]]; then
echo -e "\n${RED}Please correct the branch name${NOCOLOR}"
printf "\nBranch Name not as per the defined rules like: "
echo -e "${GREEN}feature/s43-u21455-some-name, bugfix/s43-b33255-some-name\n"
echo -e "${ORANGE}You cannot push in these branches directly: ${MAIN_BRANCHES[@]}\n"
echo -e "${ORANGE}Current BRANCH_NAME: ${BRANCH_NAME}"
echo -e "\n${RED}Oh, please stop. I cannot allow you to commit with your current branch: ${BRANCH_NAME}${NOCOLOR}"
exit 1;
fi
if [[ $IS_PIPELINE -ge 1 ]]; then
echo -e "\n${GREEN}Pipeline branch: ${BRANCH_NAME}${NOCOLOR}"
exit 0;
fi
IS_BRANCH_IN_COMMIT=$(grep -c "$TASK_BRANCH_NAME" $1)
echo -e "BRANCH_NAME ${BRANCH_NAME} IS_TASK_BRANCH ${IS_TASK_BRANCH} IS_BRANCH_IN_COMMIT ${IS_BRANCH_IN_COMMIT}"
if [[ $IS_TASK_BRANCH -ge 1 ]] && [[ $IS_BRANCH_IN_COMMIT -le 0 ]]; then
sed -i.bak -e "1s/^/$TASK_BRANCH_NAME /" $COMMIT_MSG_FILE
fi
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment