-
Notifications
You must be signed in to change notification settings - Fork 0
/
izigit.sh
189 lines (159 loc) · 6.15 KB
/
izigit.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
#Install github cli instruction if not exit on your machine
command -v gh >/dev/null 2>&1 || {
echo >&2 "please install github cli and run command gh auth login: https://cli.github.com/manual/installation"
exit 1
}
# Global variable
# Retrieving the current date
date=$(date +"%d/%m/%Y %T")
# Get Your github Username
github_name=$(gh api user -q .name)
# Help command
# Fonction for display help
Help() {
echo "#####################################################################################################"
echo "#"
echo "# IZIGIT.SH"
echo "#"
echo "# This script allows izidor developers to automate and manage git workflow easily."
echo "#"
echo "#####################################################################################################"
echo ""
echo
echo "Syntax: izigit [-s|c|i|p|h]"
echo "options:"
echo ""
echo "create [ticket_number] Create, fetch and checkout branch for ticket ( example: izigit create 654 )"
echo "reset Reset test branch, so that it is strictly identical to the preprod branch ( example: izigit reset )"
echo "test [ticket_number] Merge issue branch into test branch ( example: izigit test 654 )"
echo "pr [ticket_number] Creating a pull request from the github issue branch to the preprod branch ( example: izigit pr 654 )"
echo "h Print this Help."
}
# Creating a pull request from the github issue branch to the preprod branch
# Arguments: $2 = ticket number
PrIssueToPreprod() {
if [ $2 ]; then
preprod_branch=preprod
# Get branch name with ticket number
issue_branch_name=$(git branch -r | grep $2 | sed 's/origin\///')
if [ -z $issue_branch_name ]; then
echo "No git branch found for ticket $2"
exit 0
fi
# Updates the issue branch, relative to the preprod branch
git checkout $issue_branch_name
git pull origin $issue_branch_name
git merge $preprod_branch
git push origin $issue_branch_name
# Check if there are any conflicts
#disable lf and crlf warnings
git config core.autocrlf false
if git diff --name-only --diff-filter=U | grep -q "^"; then
echo "There are merge conflicts to resolve :"
git diff --name-only --diff-filter=U
exit 0
fi
# Get issue title
pr_title=$(gh issue view $2 --json title --jq .title)
# remove special characters from pr_title
pr_title=${pr_title//[^a-zA-Z0-9 ]/}
# Create a pull request from the issue branch to the target branch (preprod)
gh pr create --base preprod --head $issue_branch_name -t "$2 $pr_title" -b ""
# Add comment to issue
comment="$date - create pull request branch $issue_branch_name to $preprod_branch - $github_name"
gh issue comment $2 -b "$comment"
echo $comment
exit 0
fi
echo "Please specify allows ticket number, izigit pr [ticket_number] ( example: izigit pr 654 )"
}
# Fonction for merge issue branch into test branch
# Arguments: $2 = ticket number
MergeIssueInTest() {
if [ $2 ]; then
test_branch=test
# Get branch name with ticket number
branch_name=$(git branch -r | grep $2 | sed 's/origin\///')
if [ -z $branch_name ]; then
echo "No git branch found for ticket $2"
exit 0
fi
git checkout $test_branch
git pull origin $test_branch
git merge $branch_name
git push origin $test_branch
# Add comment to issue
comment="$date - merge branch $branch_name into $test_branch - $github_name"
gh issue comment $2 -b "$comment"
echo $comment
exit 0
fi
echo "Please specify allows ticket number, izigit test [ticket_number] ( example: izigit test 654 )"
}
# Fonction for reset test branch, so that it is strictly identical to the preprod branch
ResetTestToPreprod() {
test_branch="test"
reference_branch="preprod"
# Fetch and checkout the reference branch (preprod)
git fetch
git checkout $reference_branch
git pull $reference_branch
# Delete the local working branch
git branch -D $test_branch
# Create a new branch from the reference branch (preprod)
git checkout -b $test_branch
# Push the new branch to the remote repository
git push -u origin $test_branch --force
comment="$date - $test_branch reset to $reference_branch - $github_name"
echo $comment
}
# Fonction for create branch for ticket and fetch, checkout this branch
# Arguments: $2 = ticket number
CreateBranch() {
if [ "$2" ]; then
# Get Your github Username
github_username=$(gh api user -q .login)
# Assign user to issue
gh issue edit "$2" --add-assignee "$github_username"
# Get the title of the issue using the gh command
issue_title=$(gh issue view $2 --json title --jq .title)
# remove special characters from issue_title
issue_title=${issue_title//[^a-zA-Z0-9 ]/}
# replace multiple spaces with single space and trim title
issue_title="$(echo -e "${issue_title}" | tr -s ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Replace spaces with dashes and concatenate the issue number to the branch name to avoid filename issues
branch_name=$2-${issue_title// /-}
# Create the issue branch and link the branch to the GitHub issue
gh issue develop $2 --name $branch_name --base $branch_name
git fetch origin
# Checkout to the created branch
git checkout $branch_name
comment="$date - creation of the $branch_name branch and branch link to issue $2 - $github_name"
# Add comment to issue
gh issue comment $2 -b "$comment"
echo $comment
exit 0
fi
echo "Please specify allows ticket number, izigit create [ticket_number] ( example: izigit create 654 )"
}
# Enable options arguments
while getopts "h" option; do
case $option in
h)
Help
exit
;;
?)
printf "Command not found, please use command -h for display help"
exit 1
;;
esac
done
# Display message if no argument or option supplied
if [ $OPTIND -eq 1 ] && [ $# -eq 0 ]; then echo "An argument is required, please use command -h for display help"; fi
# Enable options
if [ "$1" == "create" ]; then CreateBranch $1 $2; fi
if [ "$1" == "reset" ]; then ResetTestToPreprod; fi
if [ "$1" == "test" ]; then MergeIssueInTest $1 $2; fi
if [ "$1" == "pr" ]; then PrIssueToPreprod $1 $2; fi