Repository navigation
Expand file tree
/
Copy pathgit-simplifiers.sh
More file actions
109 lines (94 loc) · 3.52 KB
/
Copy pathgit-simplifiers.sh
File metadata and controls
109 lines (94 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
script_dir=$(dirname $0)
root_dir=$(echo $script_dir | perl -pe 's/^(.+)\/bin.*$/$1/g')
source "$root_dir/shared/alias.sh"
# Git Cherry Pick Branch Environment
function gcpbe() {
function gcpbeHelp() {
echo -e "gcpbe [OPTIONS...]\n
Options:
-w, --workitem-id Set Azure workitem id (mandatory)
-c, --commit-sha Set commitSHA to cherry pick (mandatory)
-e, --env [develop|staging|main] Set ENABLE_ENVIRONMENTS of branch (mandatory). One or two ENABLE_ENVIRONMENTS, separated by comma, where create branch (develop | staging | main)
-pr, --pull-request Create pull request for branch on specified environment
-t, --tech Set tech flag for branch name and commit name"
}
if [[ $# == 0 || "$@" =~ "-h" ]]; then
gcpbeHelp
return
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--commit-sha)
if [[ $(echo "${#2}") != 40 ]]; then
writeErrorLog "$2 is not a valid branch name !"
return
fi
local commitSHA=$2
shift 2 # past argument and value
;;
-e|--env)
local envs=( $(echo "$2" | tr ',' ' ') )
for env in ${envs[@]}; do
if [[ ! " ${ENABLE_ENVIRONMENTS[@]} " =~ " $env " ]]; then
writeErrorLog "$2 is not a valid environment name ! (${ENABLE_ENVIRONMENTS[@]})"
return
fi
done
shift 2 # past argument and value
;;
-t|--tech)
local techArg="--tech"
shift # past argument
;;
-w|--workitem-id)
if [[ $(echo "$2" | grep -Ec '^[0-9]+$') == 0 ]]; then
writeErrorLog "$2 is not workitem id !"
return
fi
local workitemId=$2
shift 2 # past argument and value
;;
-pr|--pull-resquest)
local buildPullRequest=true
shift # past argument
;;
*|-*|--*)
writeErrorLog "Unknown option $1 !"
return
;;
esac
done
if [[ -z "$envs" ]]; then writeErrorLog "Environnments is mandatory !"; return; fi
if [[ -z "$workitemId" ]]; then writeErrorLog "Workitem id is mandatory !"; return; fi
if [[ -z "$commitSHA" ]]; then writeErrorLog "Commit SHA is mandatory !"; return; fi
for env in ${envs[@]}; do
echo -e "${CYAN_COLOR}## Env: $env${RESET_COLOR}"
local workitem=$(azureWorkItem.sh $workitemId -s)
local branchName=$(branchName.sh -w "$workitem" -e $env ${techArg:-})
git brd $env &> /dev/null
echo -e "${CYAN_COLOR}Going to branch $env...${RESET_COLOR}"
git checkout -q $env && git pull -q && git fetch -q
echo -e "${CYAN_COLOR}Creating branch $branchName...${RESET_COLOR}"
git checkout -q -b $branchName
echo -e "${CYAN_COLOR}Cherry picking...${RESET_COLOR}"
git cherry-pick $commitSHA > /dev/null 2>&1
# check if cp has conflit
if git status | grep -q "Unmerged paths"; then
writeErrorLog "There are merge conflicts. Please resolve them, then gcpc and gp"
return
fi
local remoteBranchExists=$(git ls-remote --heads origin $branchName)
echo -e "${CYAN_COLOR}Pushing branch $branchName...${RESET_COLOR}"
if [[ -z "$remoteBranchExists" ]]; then
git push -q --set-upstream origin "$branchName"
else
git push -q --force origin "$branchName"
fi
if [[ "${buildPullRequest:-false}" == true ]]; then
echo -e "${CYAN_COLOR}Building pull request on $env...${RESET_COLOR}"
azureCreatePullRequest.sh -e "$env" -w $workitem
fi
echo ""
done
}