From c13a56cf093c60755e90b017f430640c26a4f6aa Mon Sep 17 00:00:00 2001 From: hbenali Date: Mon, 6 Apr 2020 14:05:14 +0200 Subject: [PATCH 1/4] ITOP-4856: Add Feature Branch Manager Script --- fb/FBManager.sh | 186 +++++++++++++++++++++++++++++++++++++++++++++++ fb/_functions.sh | 63 ++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100755 fb/FBManager.sh create mode 100644 fb/_functions.sh diff --git a/fb/FBManager.sh b/fb/FBManager.sh new file mode 100755 index 00000000..b5e9e22e --- /dev/null +++ b/fb/FBManager.sh @@ -0,0 +1,186 @@ +#!/bin/bash + +# Arguments +# 1: Configuration file JSON Format See help description +# 2: Action: create / delete Feature Branch + +# EXIT CODES +# 0 -- OK +# 1 -- Missing arguments or commands +# 2 -- Wrong provided argument +# 3 -- Missing mandatory information in the configuration file +# 4 -- Wrong provided information or insufficient privileges +# 5 -- Inability to perform action due to insufficient privileges +#### +protected_branches_list_regex="^(master|develop|stable\/[0-9.-_a-zA-Z]+)$" +#### + +scrdir=$(dirname $(realpath $0)) +source ${scrdir}/_functions.sh +if [ -z "$1" ] || [[ $1 =~ ^-(h|-help)$ ]]; then + print_help + exit 0 +fi +assert_command git +assert_command jq +if [ -z "$1" ] || [ ! -f "$1" ]; then + echo_err "Config file is missing or invalid" + exit 1 +fi +config_file=$(realpath $1) +if ! validate_json ${config_file} >/dev/null; then #Suppress only plain message + echo_err "Config file is not a valid JSON file!" + exit 2 +else + echo_ok "Config file \"$1\" is a valid JSON file" +fi +shift +if [[ ! "$1" =~ ^(create|delete)$ ]]; then + echo_err "Wrong or Missing action! only create or delete are accepted!" + exit 2 +else + echo_ok "Action \"$1\" will be performed" +fi + +action=$1 +start_time=$(date +%s); +rm -rf ${wkdir}* 2>&1 &>/dev/null #Cleanup +for el in $(cat ${config_file} | jq -c '.[]'); do + ## Args Parsing + if ! eval $(echo $el | jq -r '. | to_entries | .[] | .key + "=\"" + .value + "\""') 2>&1 &>/dev/null; then + echo_err "Could not convert the followig JSON Object to Bash variables!" + echo $el | jq + exit 3 + fi + + ## Args Common Checks + if [ -z "${git_organization}" ]; then + echo_err "Github organization is not specified!" + exit 3 + fi + + if [ -z "${git_repository}" ]; then + echo_err "Github repository is not specified!" + exit 3 + fi + + if [ -z "${name}" ]; then + echo_err "Github target branch is not specified!" + exit 3 + fi + + if [[ "${name}" =~ ${protected_branches_list_regex} ]]; then + echo_err "Github target branch \"${name}\" matches a protected branch" + exit 3 + fi + + ## Prepare repository + full_repo_name="${git_organization}/${git_repository}" + ssh_url="git@github.com:${full_repo_name}.git" + + ## Check Repository existance and access privileges + if ! valid_repo ${ssh_url}; then + echo_err "Repository ${full_repo_name} is not exist or insufficient privileges!" + exit 4 + fi + + ## Clone Repository + echo_ok "Cloning ${full_repo_name}" + local_repo_path="${wkdir}/${full_repo_name}" + [ -e ${local_repo_path} ] && rm -rf ${local_repo_path} 2>&1 &>/dev/null #Cleanup + if ! git clone ${ssh_url} ${local_repo_path} 2>&1 &>/dev/null; then + echo_err "Could not clone ${full_repo_name} !" + exit_with_cleanup 4 + fi + + ## Check if the repository is successfully cloned + if [ ! -d ${local_repo_path} ] || [ ! -d "${local_repo_path}/.git" ]; then + echo_err "Local repository ${full_repo_name} is not created !" + exit_with_cleanup 4 + else + echo_ok "Repository ${full_repo_name} is successfully cloned !" + fi + + ## Function Live Definition Make code below more redeable + r_git() { + git --git-dir=${local_repo_path}/.git $* + } + + ## Check if update is unset if yes, use false as default value + [ -z "${update}" ] && update="false" + + ## Perform Feature Branch Creation + if [[ $action == "create" ]]; then + + ## Check if base branch is unset if yes, use default branch as base one + [ -z "${git_base_branch}" ] && git_base_branch=$(r_git symbolic-ref --short HEAD) + + ## Check if target branch is already exist in the Git Repository [ Remote Check ] + if [ ! -z "$(r_git ls-remote --heads origin ${name})" ]; then + if [[ ${update} == "false" ]]; then + echo_err "Branch \"${name}\" already exist!" + fi + if [[ ${update} == "true" ]]; then + echo_warn "Update Mode is Enabled!" + else + exit_with_cleanup 5 + fi + fi + + current_branch=$(r_git rev-parse --abbrev-ref HEAD) + #Default branch protection + if [[ "${name}" == "${current_branch}" ]]; then + echo_err "Could not recreate the default branch \"${name}\"!" + exit_with_cleanup 4 + fi + ## Check if the current branch is the selected base branch if not, check out to it + if [[ "${git_base_branch}" != "${current_branch}" ]]; then + if ! r_git checkout ${git_base_branch} 2>&1 &>/dev/null; then + echo_err "Could not checkout to branch \"${git_base_branch}\" !" + exit_with_cleanup 5 + fi + echo_ok "Checked out to base branch \"${git_base_branch}\"" + else + echo_ok "Already on base branch \"${git_base_branch}\"" + fi + + # Add Force flag for the git push and remove branch as precaution + force_flag="" + [[ ${update} == "true" ]] && force_flag="-f" + r_git branch -D ${name} 2>&1 &>/dev/null + + # Create target branch locally and push to the remote + if r_git checkout -b "${name}" && r_git push origin "${name}" ${force_flag} 2>/dev/null; then + echo_ok "Branch \"${name}\" has been created!" + else + echo_err "Could not create branch \"${name}\"!" + exit_with_cleanup 5 + fi + + ## Perform Feature Branch Deletion + elif [[ $action == "delete" ]]; then + # Default Branch Protection + current_branch=$(r_git rev-parse --abbrev-ref HEAD) + if [[ "${name}" == "${current_branch}" ]]; then + echo_err "Could not delete default branch \"${name}\"!" + exit_with_cleanup 4 + fi + ## Check if target branch is already exist in the Git Repository [ Remote Check ] + if [ ! -z "$(r_git ls-remote --heads origin ${name})" ]; then + echo_ok "Branch \"${name}\" exist" + if r_git push origin :${name}; then + echo_ok "Branch \"${name}\" has been deleted!" + + else + echo_err "Could not delete branch \"${name}\"!" + exit_with_cleanup 5 + fi + else + echo_ok "Branch \"${name}\" does not exist" + fi + fi + ## Mandatory Common cleanup process for the next iteration + rm -rf $(dirname ${local_repo_path}) 2>&1 &>/dev/null #Global Cleanup : dirname = organization folder + unset name git_base_branch git_organization git_repository update force_flag +done +echo_ok "Finished in $(date -ud "@$(($(date +%s) - $start_time))" +%T)." diff --git a/fb/_functions.sh b/fb/_functions.sh new file mode 100644 index 00000000..ea53a80a --- /dev/null +++ b/fb/_functions.sh @@ -0,0 +1,63 @@ +wkdir="/tmp/fbmgnfactory" + +echo_err() { + echo "$(date +'%d/%m/%y %H:%M:%S') | Error | $*" >>/dev/stderr +} + +echo_ok() { + echo "$(date +'%d/%m/%y %H:%M:%S') | INFO | $*" +} + +echo_warn() { + echo "$(date +'%d/%m/%y %H:%M:%S') | WARN | $*" +} + +exit_with_cleanup() { + rm -rf $wkdir 2>&1 &>/dev/null + exit $1 +} + +validate_json() { + cat $1 | jq type +} + +assert_command() { + if ! hash $1 2>&1 &>/dev/null; then + echo_err "$1 is not installed !" + exit 1 + fi +} + +valid_repo() { + [ ! -z "$1" ] && [ ! -z "$(git ls-remote --heads $1 2>/dev/null)" ] +} + +print_help() { + cat < + +Actions: create, delete + +Config File Sample: + + [ + { + "name": "Feature/xxxxxxx", + "git_organization": "exo-xxxxx, + "git_repository": "eXo-Testing", + "git_base_branch": "stable/xxxxxx", // Optional, Default [create]: default branch + "update": "true or false" // Optional, Default: false, [Caution] overwrite remote branch + }, + { + "name": "Feature/xxxxxxx", + .... + .... + } + ] + +Mandatory commands: git, jq +EOF +} From f44d817ec5b9e851ea49848c5e772457172a33c1 Mon Sep 17 00:00:00 2001 From: hbenali Date: Mon, 6 Apr 2020 15:45:21 +0200 Subject: [PATCH 2/4] STD Code Refactoring --- fb/FBManager.sh | 14 +++++++------- fb/_functions.sh | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/fb/FBManager.sh b/fb/FBManager.sh index b5e9e22e..a4ff7c1f 100755 --- a/fb/FBManager.sh +++ b/fb/FBManager.sh @@ -44,10 +44,10 @@ fi action=$1 start_time=$(date +%s); -rm -rf ${wkdir}* 2>&1 &>/dev/null #Cleanup +rm -rf ${wkdir}* &>/dev/null #Cleanup for el in $(cat ${config_file} | jq -c '.[]'); do ## Args Parsing - if ! eval $(echo $el | jq -r '. | to_entries | .[] | .key + "=\"" + .value + "\""') 2>&1 &>/dev/null; then + if ! eval $(echo $el | jq -r '. | to_entries | .[] | .key + "=\"" + .value + "\""') &>/dev/null; then echo_err "Could not convert the followig JSON Object to Bash variables!" echo $el | jq exit 3 @@ -87,8 +87,8 @@ for el in $(cat ${config_file} | jq -c '.[]'); do ## Clone Repository echo_ok "Cloning ${full_repo_name}" local_repo_path="${wkdir}/${full_repo_name}" - [ -e ${local_repo_path} ] && rm -rf ${local_repo_path} 2>&1 &>/dev/null #Cleanup - if ! git clone ${ssh_url} ${local_repo_path} 2>&1 &>/dev/null; then + [ -e ${local_repo_path} ] && rm -rf ${local_repo_path} &>/dev/null #Cleanup + if ! git clone ${ssh_url} ${local_repo_path} &>/dev/null; then echo_err "Could not clone ${full_repo_name} !" exit_with_cleanup 4 fi @@ -135,7 +135,7 @@ for el in $(cat ${config_file} | jq -c '.[]'); do fi ## Check if the current branch is the selected base branch if not, check out to it if [[ "${git_base_branch}" != "${current_branch}" ]]; then - if ! r_git checkout ${git_base_branch} 2>&1 &>/dev/null; then + if ! r_git checkout ${git_base_branch} &>/dev/null; then echo_err "Could not checkout to branch \"${git_base_branch}\" !" exit_with_cleanup 5 fi @@ -147,7 +147,7 @@ for el in $(cat ${config_file} | jq -c '.[]'); do # Add Force flag for the git push and remove branch as precaution force_flag="" [[ ${update} == "true" ]] && force_flag="-f" - r_git branch -D ${name} 2>&1 &>/dev/null + r_git branch -D ${name} &>/dev/null # Create target branch locally and push to the remote if r_git checkout -b "${name}" && r_git push origin "${name}" ${force_flag} 2>/dev/null; then @@ -180,7 +180,7 @@ for el in $(cat ${config_file} | jq -c '.[]'); do fi fi ## Mandatory Common cleanup process for the next iteration - rm -rf $(dirname ${local_repo_path}) 2>&1 &>/dev/null #Global Cleanup : dirname = organization folder + rm -rf $(dirname ${local_repo_path}) &>/dev/null #Global Cleanup : dirname = organization folder unset name git_base_branch git_organization git_repository update force_flag done echo_ok "Finished in $(date -ud "@$(($(date +%s) - $start_time))" +%T)." diff --git a/fb/_functions.sh b/fb/_functions.sh index ea53a80a..6331346f 100644 --- a/fb/_functions.sh +++ b/fb/_functions.sh @@ -13,7 +13,7 @@ echo_warn() { } exit_with_cleanup() { - rm -rf $wkdir 2>&1 &>/dev/null + rm -rf $wkdir &>/dev/null exit $1 } @@ -22,7 +22,7 @@ validate_json() { } assert_command() { - if ! hash $1 2>&1 &>/dev/null; then + if ! hash $1 &>/dev/null; then echo_err "$1 is not installed !" exit 1 fi From 38baaf1f07e8c0d57741d8a17c706d47852e8d10 Mon Sep 17 00:00:00 2001 From: hbenali Date: Sun, 12 Apr 2020 12:22:46 +0200 Subject: [PATCH 3/4] Add Maven FB --- fb/FBManager.sh | 108 ++++++++++++++++++++++++++++++++--------------- fb/_functions.sh | 15 +++++-- 2 files changed, 85 insertions(+), 38 deletions(-) diff --git a/fb/FBManager.sh b/fb/FBManager.sh index a4ff7c1f..f1f59b95 100755 --- a/fb/FBManager.sh +++ b/fb/FBManager.sh @@ -3,6 +3,7 @@ # Arguments # 1: Configuration file JSON Format See help description # 2: Action: create / delete Feature Branch +# 3: Feature Branch Name: word "feature/" will be added automatically in case of unset # EXIT CODES # 0 -- OK @@ -16,13 +17,14 @@ protected_branches_list_regex="^(master|develop|stable\/[0-9.-_a-zA-Z]+)$" #### scrdir=$(dirname $(realpath $0)) -source ${scrdir}/_functions.sh +. ${scrdir}/_functions.sh if [ -z "$1" ] || [[ $1 =~ ^-(h|-help)$ ]]; then print_help exit 0 fi assert_command git assert_command jq +assert_command mvn if [ -z "$1" ] || [ ! -f "$1" ]; then echo_err "Config file is missing or invalid" exit 1 @@ -41,9 +43,25 @@ if [[ ! "$1" =~ ^(create|delete)$ ]]; then else echo_ok "Action \"$1\" will be performed" fi - action=$1 -start_time=$(date +%s); +shift +if [ -z "$1" ]; then + echo_err "Missing Feature Branch name!!" + exit 2 +fi +featurebranch=$1 +[[ "${featurebranch}" =~ ^Feature ]] || featurebranch="feature/${featurebranch}" + +maventag=$(echo ${featurebranch} | sed -E 's|^(f\|F)eature/||g') +#Check for illegal characteres like / etc +if [[ ! "${maventag}" =~ ^[A-Za-z0-9_-]+$ ]]; then + echo_err "Feature branch \"${featurebranch}\" contains illegal characters!" + exit 3 +fi + +shift +echo_ok "Feature Branch is \"${featurebranch}\"" +start_time=$(date +%s) rm -rf ${wkdir}* &>/dev/null #Cleanup for el in $(cat ${config_file} | jq -c '.[]'); do ## Args Parsing @@ -64,16 +82,6 @@ for el in $(cat ${config_file} | jq -c '.[]'); do exit 3 fi - if [ -z "${name}" ]; then - echo_err "Github target branch is not specified!" - exit 3 - fi - - if [[ "${name}" =~ ${protected_branches_list_regex} ]]; then - echo_err "Github target branch \"${name}\" matches a protected branch" - exit 3 - fi - ## Prepare repository full_repo_name="${git_organization}/${git_repository}" ssh_url="git@github.com:${full_repo_name}.git" @@ -103,7 +111,7 @@ for el in $(cat ${config_file} | jq -c '.[]'); do ## Function Live Definition Make code below more redeable r_git() { - git --git-dir=${local_repo_path}/.git $* + git --git-dir=${local_repo_path}/.git --work-tree=${local_repo_path} $* } ## Check if update is unset if yes, use false as default value @@ -115,10 +123,10 @@ for el in $(cat ${config_file} | jq -c '.[]'); do ## Check if base branch is unset if yes, use default branch as base one [ -z "${git_base_branch}" ] && git_base_branch=$(r_git symbolic-ref --short HEAD) - ## Check if target branch is already exist in the Git Repository [ Remote Check ] - if [ ! -z "$(r_git ls-remote --heads origin ${name})" ]; then + ## Check if feature branch is already exist in the Git Repository [ Remote Check ] + if [ ! -z "$(r_git ls-remote --heads origin ${featurebranch})" ]; then if [[ ${update} == "false" ]]; then - echo_err "Branch \"${name}\" already exist!" + echo_err "Branch \"${featurebranch}\" already exist!" fi if [[ ${update} == "true" ]]; then echo_warn "Update Mode is Enabled!" @@ -129,8 +137,8 @@ for el in $(cat ${config_file} | jq -c '.[]'); do current_branch=$(r_git rev-parse --abbrev-ref HEAD) #Default branch protection - if [[ "${name}" == "${current_branch}" ]]; then - echo_err "Could not recreate the default branch \"${name}\"!" + if [[ "${featurebranch}" == "${current_branch}" ]]; then + echo_err "Could not recreate the default branch \"${featurebranch}\"!" exit_with_cleanup 4 fi ## Check if the current branch is the selected base branch if not, check out to it @@ -147,36 +155,68 @@ for el in $(cat ${config_file} | jq -c '.[]'); do # Add Force flag for the git push and remove branch as precaution force_flag="" [[ ${update} == "true" ]] && force_flag="-f" - r_git branch -D ${name} &>/dev/null + r_git branch -D ${featurebranch} &>/dev/null - # Create target branch locally and push to the remote - if r_git checkout -b "${name}" && r_git push origin "${name}" ${force_flag} 2>/dev/null; then - echo_ok "Branch \"${name}\" has been created!" + # Create feature branch locally and push to the remote + if r_git checkout -b "${featurebranch}" 2>/dev/null; then + echo_ok "Branch \"${featurebranch}\" has been created locally" else - echo_err "Could not create branch \"${name}\"!" + echo_err "Could not create branch \"${featurebranch}\"!" exit_with_cleanup 5 fi + # Apply changes to Maven Project + if [ -f "${local_repo_path}/pom.xml" ]; then + project_artifactId=$(get_maven_property "${local_repo_path}" "project.artifactId") + project_groupId=$(get_maven_property "${local_repo_path}" "project.groupId") + project_version=$(get_maven_property "${local_repo_path}" "project.version") + new_project_version=$(echo ${project_version} | sed "s|-SNAPSHOT|-${maventag}-SNAPSHOT|g") + echo_ok "Maven Project Informations:" + echo "***" + echo " ArtifactId: ${project_artifactId}" + echo " GroupId: ${project_groupId}" + echo " Version: ${project_version}" + echo " New version: ${new_project_version}" + echo "***" + if change_maven_project_version "${local_repo_path}" ${project_groupId} ${project_artifactId} ${new_project_version}; then + echo_ok "Maven Project version changed to ${new_project_version}" + else + echo_err "Could not change project version to ${new_project_version}" + exit_with_cleanup 4 + fi + r_git add $(find ${local_repo_path} -name pom.xml | xargs) + if ! echo "Create Feature Branch ${featurebranch}" | r_git commit -F -; then + echo_err "Could not commit on the ${featurebranch} branch!" + exit_with_cleanup 4 + fi + fi + + if ! r_git push origin "${featurebranch}" ${force_flag} 2>/dev/null; then + echo_err "Could not push on the ${featurebranch} branch!" + exit_with_cleanup 4 + else + echo_ok "Branch ${featurebranch} has been created" + fi + ## Perform Feature Branch Deletion elif [[ $action == "delete" ]]; then # Default Branch Protection current_branch=$(r_git rev-parse --abbrev-ref HEAD) - if [[ "${name}" == "${current_branch}" ]]; then - echo_err "Could not delete default branch \"${name}\"!" + if [[ "${featurebranch}" == "${current_branch}" ]]; then + echo_err "Could not delete default branch \"${featurebranch}\"!" exit_with_cleanup 4 fi - ## Check if target branch is already exist in the Git Repository [ Remote Check ] - if [ ! -z "$(r_git ls-remote --heads origin ${name})" ]; then - echo_ok "Branch \"${name}\" exist" - if r_git push origin :${name}; then - echo_ok "Branch \"${name}\" has been deleted!" - + ## Check if feature branch is already exist in the Git Repository [ Remote Check ] + if [ ! -z "$(r_git ls-remote --heads origin ${featurebranch})" ]; then + echo_ok "Branch \"${featurebranch}\" exist" + if r_git push origin :${featurebranch}; then + echo_ok "Branch \"${featurebranch}\" has been deleted!" else - echo_err "Could not delete branch \"${name}\"!" + echo_err "Could not delete branch \"${featurebranch}\"!" exit_with_cleanup 5 fi else - echo_ok "Branch \"${name}\" does not exist" + echo_ok "Branch \"${featurebranch}\" does not exist" fi fi ## Mandatory Common cleanup process for the next iteration diff --git a/fb/_functions.sh b/fb/_functions.sh index 6331346f..bc14339c 100644 --- a/fb/_functions.sh +++ b/fb/_functions.sh @@ -32,12 +32,20 @@ valid_repo() { [ ! -z "$1" ] && [ ! -z "$(git ls-remote --heads $1 2>/dev/null)" ] } +get_maven_property() { + mvn -f "$1/pom.xml" -o org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=$2 | grep -v '\[' 2>/dev/null +} + +change_maven_project_version() { + mvn -f "$1/pom.xml" -q versions:set -DgenerateBackupPoms=false -DgroupId=$2 -DartifactId=$3 -DnewVersion=$4 &>/dev/null +} + print_help() { cat < +Usage: $0 Actions: create, delete @@ -45,19 +53,18 @@ Config File Sample: [ { - "name": "Feature/xxxxxxx", "git_organization": "exo-xxxxx, "git_repository": "eXo-Testing", "git_base_branch": "stable/xxxxxx", // Optional, Default [create]: default branch "update": "true or false" // Optional, Default: false, [Caution] overwrite remote branch }, { - "name": "Feature/xxxxxxx", + "git_organization": "exo-yyyyy, .... .... } ] -Mandatory commands: git, jq +Mandatory commands: git, jq, maven (mvn) EOF } From 64281fe49e2bdd0c7f9e2404f038f5b7b7701923 Mon Sep 17 00:00:00 2001 From: hbenali Date: Sun, 12 Apr 2020 13:42:59 +0200 Subject: [PATCH 4/4] Input parsing refactoring --- fb/FBManager.sh | 70 ++++++++++++++++++++++++++++++++++++------------ fb/_functions.sh | 4 +-- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/fb/FBManager.sh b/fb/FBManager.sh index f1f59b95..e5597586 100755 --- a/fb/FBManager.sh +++ b/fb/FBManager.sh @@ -18,48 +18,84 @@ protected_branches_list_regex="^(master|develop|stable\/[0-9.-_a-zA-Z]+)$" scrdir=$(dirname $(realpath $0)) . ${scrdir}/_functions.sh -if [ -z "$1" ] || [[ $1 =~ ^-(h|-help)$ ]]; then +SHORT=abchj +LONG=jira,config,action,featurebranch,help +PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@") +if [[ $? -ne 0 ]]; then print_help exit 0 fi +while true; do + case "$1" in + -h | --help) + print_help + exit 0 + ;; + -j | --jira) + jira_id="$2" + shift 2 + ;; + -c | --config) + config_file="$(realpath $2)" + shift 2 + ;; + -a | --action) + action="$2" + shift 2 + ;; + -b | --featurebranch) + featurebranch="$2" + shift 2 + ;; + "") + break + ;; + *) + print_help + exit 0 + ;; + esac +done + +#Check commands assert_command git assert_command jq assert_command mvn -if [ -z "$1" ] || [ ! -f "$1" ]; then + +if [ -z "${jira_id}" ] || [[ ! "${jira_id}" =~ ^ITOP-[0-9]+$ ]]; then + echo_err "Jira ID is missing or invalid" + exit 1 +fi + +if [ -z "${config_file}" ] || [ ! -f "${config_file}" ]; then echo_err "Config file is missing or invalid" exit 1 fi -config_file=$(realpath $1) if ! validate_json ${config_file} >/dev/null; then #Suppress only plain message echo_err "Config file is not a valid JSON file!" exit 2 else - echo_ok "Config file \"$1\" is a valid JSON file" + echo_ok "Config file \"${config_file}\" is a valid JSON file" fi -shift -if [[ ! "$1" =~ ^(create|delete)$ ]]; then +if [[ ! "${action}" =~ ^(create|delete)$ ]]; then echo_err "Wrong or Missing action! only create or delete are accepted!" exit 2 else - echo_ok "Action \"$1\" will be performed" + echo_ok "Action \"${action}\" will be performed" fi -action=$1 -shift -if [ -z "$1" ]; then - echo_err "Missing Feature Branch name!!" +if [ -z "${featurebranch}" ]; then + echo_err "Missing Feature Branch name!" exit 2 fi -featurebranch=$1 -[[ "${featurebranch}" =~ ^Feature ]] || featurebranch="feature/${featurebranch}" +[[ "${featurebranch}" =~ ^(F|f)eature/ ]] || featurebranch="feature/${featurebranch}" maventag=$(echo ${featurebranch} | sed -E 's|^(f\|F)eature/||g') -#Check for illegal characteres like / etc + +#Check for illegal characteres like / etc if [[ ! "${maventag}" =~ ^[A-Za-z0-9_-]+$ ]]; then echo_err "Feature branch \"${featurebranch}\" contains illegal characters!" exit 3 fi - -shift echo_ok "Feature Branch is \"${featurebranch}\"" start_time=$(date +%s) rm -rf ${wkdir}* &>/dev/null #Cleanup @@ -185,7 +221,7 @@ for el in $(cat ${config_file} | jq -c '.[]'); do exit_with_cleanup 4 fi r_git add $(find ${local_repo_path} -name pom.xml | xargs) - if ! echo "Create Feature Branch ${featurebranch}" | r_git commit -F -; then + if ! echo "${jira_id}: Create Feature Branch ${featurebranch}" | r_git commit -F -; then echo_err "Could not commit on the ${featurebranch} branch!" exit_with_cleanup 4 fi diff --git a/fb/_functions.sh b/fb/_functions.sh index bc14339c..ac8d5ca1 100644 --- a/fb/_functions.sh +++ b/fb/_functions.sh @@ -45,9 +45,7 @@ print_help() { **** Feature Branch Manager ****** ** Created by eXo SWF / ITOP Team -Usage: $0 - -Actions: create, delete +Usage: $0 -j|--jira ITOP_XXXX -c|--config -a|--action -b|--featurebranch <[Feature/]xxxxxxx> Config File Sample: