8
8
from six .moves import input
9
9
10
10
master_branch = os .environ .get ("GITFEATURES_MASTER_BRANCH" , "master" )
11
- seperator = os .environ .get ("GITFEATURES_BRANCH_SEPERATOR" , "_" )
11
+ branch_seperator = os .environ .get ("GITFEATURES_BRANCH_SEPERATOR" , "_" )
12
+ ticket_seperator = os .environ .get ("GITFEATURES_TICKET_SEPERATOR" , "_" )
12
13
repo = os .environ .get ("GITFEATURES_REPO" , "github" )
13
14
merge_strategy = os .environ .get ("GITFEATURES_STRATEGY" , "merge" )
14
15
fork_pr_strategy = os .environ .get ("GITFEATURES_FORK_PR_STRATEGY" , "" )
16
+ require_ticket_id = os .environ .get ("GITFEATURES_REQUIRE_TICKETID" , "false" )
15
17
16
18
17
19
def _call (args ):
18
20
try :
19
21
return check_output (args ).decode ("utf-8" )
20
22
except CalledProcessError :
21
- sys .exit (
22
- __name__ + ": none zero exit status executing: " + " " .join (args )
23
- ) # noqa
23
+ sys .exit (__name__ + ": none zero exit status executing: " + " " .join (args )) # noqa
24
24
25
25
26
- def _get_branch_name (prefix , name ):
27
- name = f"{ prefix } { seperator } { name } "
28
- return name
26
+ def _get_branch_name (prefix , name , ticket_id ):
27
+ branch_name = f"{ prefix } { branch_seperator } { name } "
28
+ if ticket_id :
29
+ branch_name = f"{ prefix } { branch_seperator } { ticket_id } { ticket_seperator } { name } "
30
+ return branch_name
29
31
30
32
31
- def new_feature (name , prefix ):
33
+ def new_feature (name , prefix , ticket_id ):
32
34
name = re .sub (r"\W" , "_" , name )
33
35
original_branch = _current_branch ()
34
36
if original_branch != master_branch :
@@ -42,12 +44,10 @@ def new_feature(name, prefix):
42
44
sys .exit ("Ok, Exiting" ) # noqa
43
45
44
46
_call (["git" , "remote" , "update" , "origin" ])
45
- new_branch = _get_branch_name (prefix , name )
47
+ new_branch = _get_branch_name (prefix , name , ticket_id )
46
48
47
49
if _branch_exists (new_branch ):
48
- sys .exit (
49
- __name__ + ": local or remote branch already exists: " + new_branch
50
- ) # noqa
50
+ sys .exit (__name__ + ": local or remote branch already exists: " + new_branch ) # noqa
51
51
52
52
_call (["git" , "checkout" , "-b" , new_branch ])
53
53
_call (["git" , "push" , "-u" , "origin" , new_branch + ":" + new_branch ])
@@ -64,15 +64,11 @@ def finish_feature(name, prefix):
64
64
branch = cur_branch
65
65
_call (["git" , "checkout" , master_branch ])
66
66
else :
67
- sys .exit (
68
- __name__ + ": please provide a branch name if on {}" .format (master_branch )
69
- )
67
+ sys .exit (__name__ + ": please provide a branch name if on {}" .format (master_branch ))
70
68
71
69
_call (["git" , "remote" , "update" , "origin" ])
72
70
73
- commits = _call (
74
- ["git" , "log" , "--oneline" , branch , "^origin/{}" .format (master_branch )]
75
- )
71
+ commits = _call (["git" , "log" , "--oneline" , branch , "^origin/{}" .format (master_branch )])
76
72
if commits :
77
73
sys .exit (
78
74
__name__
@@ -145,14 +141,10 @@ def pullrequest(args):
145
141
146
142
# check its up to date with remote master if not pull
147
143
_call (["git" , "remote" , "update" , "origin" ])
148
- commits = _call (
149
- ["git" , "log" , "--oneline" , "^" + branch , "origin/{}" .format (master_branch )]
150
- )
144
+ commits = _call (["git" , "log" , "--oneline" , "^" + branch , "origin/{}" .format (master_branch )])
151
145
if commits :
152
146
print (
153
- "Your branch is behind origin/{} so cannot be automatically {}d." .format (
154
- master_branch , merge_strategy
155
- )
147
+ "Your branch is behind origin/{} so cannot be automatically {}d." .format (master_branch , merge_strategy )
156
148
) # noqa
157
149
print (commits )
158
150
print (
@@ -166,15 +158,9 @@ def pullrequest(args):
166
158
_call (["git" , "checkout" , branch ])
167
159
try :
168
160
print ("git {} {}" .format (merge_strategy , master_branch ))
169
- output = check_output (["git" , merge_strategy , master_branch ]).decode (
170
- "utf-8"
171
- )
161
+ output = check_output (["git" , merge_strategy , master_branch ]).decode ("utf-8" )
172
162
print (output )
173
- print (
174
- "Congratulations, successfully {}d {}" .format (
175
- merge_strategy , master_branch
176
- )
177
- )
163
+ print ("Congratulations, successfully {}d {}" .format (merge_strategy , master_branch ))
178
164
except CalledProcessError as e :
179
165
if b"CONFLICT" in e .output :
180
166
err = (
@@ -200,9 +186,7 @@ def pullrequest(args):
200
186
print ("name" , name )
201
187
print ("branch" , branch )
202
188
url = _get_pullrequest_url (name , branch )
203
- if (len (args ) > 0 and args [0 ] == "--dry-run" ) or os .environ .get (
204
- "CONSOLEONLY" , False
205
- ): # noqa
189
+ if (len (args ) > 0 and args [0 ] == "--dry-run" ) or os .environ .get ("CONSOLEONLY" , False ): # noqa
206
190
print (url )
207
191
else :
208
192
webbrowser .open_new_tab (url )
@@ -216,9 +200,7 @@ def _get_pullrequest_url(name, branch):
216
200
else :
217
201
url = "https://github.com/" + name + "/pull/new/" + branch
218
202
elif repo == "bitbucket" :
219
- url = (
220
- "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch
221
- ) # noqa
203
+ url = "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch # noqa
222
204
return url
223
205
224
206
@@ -241,7 +223,7 @@ def _get_branches(branch_type):
241
223
try :
242
224
branch_list = (
243
225
check_output (
244
- f"git branch -r | grep -e '\/{ branch_type } { seperator } \d\d\d\d\d\d\d\d'" , # noqa
226
+ f"git branch -r | grep -e '\/{ branch_type } { branch_seperator } \d\d\d\d\d\d\d\d'" , # noqa
245
227
shell = True ,
246
228
)
247
229
.decode ("utf-8" )
@@ -256,6 +238,7 @@ def _get_branches(branch_type):
256
238
257
239
258
240
def run (prefix , args ):
241
+ print (prefix , args , require_ticket_id )
259
242
if len (args ) and args [0 ].lower () == "new" :
260
243
allowed_branch_types = ["releasecandidate" , "stable" , "release" , "hotfix" ]
261
244
if prefix in allowed_branch_types :
@@ -264,8 +247,17 @@ def run(prefix, args):
264
247
else :
265
248
name = str (datetime .date .today ())
266
249
new_feature (name , prefix )
267
- elif len (args ) == 2 :
268
- new_feature (args [1 ], prefix )
250
+ elif len (args ) >= 2 :
251
+ if len (args ) > 2 :
252
+ ticket_id = args [2 ]
253
+ else :
254
+ ticket_id = None
255
+
256
+ if require_ticket_id == "true" :
257
+ if len (args ) < 3 :
258
+ sys .exit ("Usage: git %s new <%s_name> <ticket_id>" % (prefix , prefix ))
259
+
260
+ new_feature (args [1 ], prefix , ticket_id )
269
261
else :
270
262
sys .exit ("Usage: git %s new <%s_name>" % (prefix , prefix ))
271
263
elif len (args ) and args [0 ].lower () == "finish" :
0 commit comments