| 
 | 1 | +#!/usr/bin/env bash  | 
 | 2 | + | 
 | 3 | +# Name of default branch from which feature branches are created and to which PRs will be merged back to  | 
 | 4 | +DEFAULT_BRANCH="devel"  | 
 | 5 | +# Regexp to match jira number AAP-NNNNN or magic string "NO_JIRA"  | 
 | 6 | +NO_JIRA_MARKER="NO_JIRA"  | 
 | 7 | +JIRA_REGEXP="(aap-[0-9]+|${NO_JIRA_MARKER})"  | 
 | 8 | + | 
 | 9 | +# Fetch current branch name and list of commits since diverging from default branch  | 
 | 10 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)  | 
 | 11 | +CURRENT_COMMITS=$(git --no-pager log --format=%s --reverse ${DEFAULT_BRANCH}..)  | 
 | 12 | + | 
 | 13 | +# Extract Jira number or magic marker from branch and commit messages(filtered for unique values)  | 
 | 14 | +BRANCH_JIRA=$(grep -i -o -E "${JIRA_REGEXP}" <<< ${CURRENT_BRANCH})  | 
 | 15 | +COMMIT_JIRAS=$(grep -i -o -E "${JIRA_REGEXP}" <<< ${CURRENT_COMMITS} | uniq )  | 
 | 16 | +# Count all Jira numbers and those matching Jira from branch name  | 
 | 17 | +COMMIT_JIRA_COUNT=$(grep -c . <<< ${COMMIT_JIRAS})  | 
 | 18 | +MATCHING_JIRAS_COUNT=$(grep -ic -E "${BRANCH_JIRA}" <<< ${COMMIT_JIRAS})  | 
 | 19 | + | 
 | 20 | +echo "JIRA number from branch name: ${BRANCH_JIRA}"  | 
 | 21 | +echo "JIRA numbers from commits:"  | 
 | 22 | +echo "${COMMIT_JIRAS}"  | 
 | 23 | +echo "Number of JIRA numbers from commits matching JIRA number from branch name: ${MATCHING_JIRAS_COUNT}"  | 
 | 24 | + | 
 | 25 | +# if no Jira or no magic marker found in branch name, fail  | 
 | 26 | +echo "Checking branch name..."  | 
 | 27 | +if [ "${BRANCH_JIRA}" = "" ]; then  | 
 | 28 | +  echo "Fail: Branch name does not contain a JIRA number or a ${NO_JIRA_MARKER} marker."  | 
 | 29 | +  exit 1  | 
 | 30 | +# if branch does not have the magic marker, check the commits as well  | 
 | 31 | +elif [ "${BRANCH_JIRA}" != "${NO_JIRA_MARKER}" ]; then  | 
 | 32 | +    echo "Checking commit messages..."  | 
 | 33 | +    # if there is no Jira number or magic marker, fail  | 
 | 34 | +    if [ ${COMMIT_JIRA_COUNT} -eq 0 ]; then  | 
 | 35 | +      echo "Fail: No commit message contains a JIRA number or a ${NO_JIRA_MARKER} marker."  | 
 | 36 | +      exit 1  | 
 | 37 | +    # if no Jira numbers or magic marker match the Jira number from branch name, inform the user  | 
 | 38 | +    # this case might be happening when code is being back-ported under different Jira number in branch name  | 
 | 39 | +    elif [ ${MATCHING_JIRAS_COUNT} -eq 0 ]; then  | 
 | 40 | +      echo "Warning: No Jira numbers or ${NO_JIRA_MARKER} marker in commit messages match Jira number from branch name."  | 
 | 41 | +    else  | 
 | 42 | +      echo "OK. Found Jira numbers(or ${NO_JIRA_MARKER} marker) in commit messages that match Jira number in branch name."  | 
 | 43 | +    fi  | 
 | 44 | +else  | 
 | 45 | +  echo "OK. Skipping checks of commit messages, branch name includes ${NO_JIRA_MARKER}."  | 
 | 46 | +fi  | 
0 commit comments