88from six .moves import input
99
1010master_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" , "_" )
1213repo = os .environ .get ("GITFEATURES_REPO" , "github" )
1314merge_strategy = os .environ .get ("GITFEATURES_STRATEGY" , "merge" )
1415fork_pr_strategy = os .environ .get ("GITFEATURES_FORK_PR_STRATEGY" , "" )
16+ require_ticket_id = os .environ .get ("GITFEATURES_REQUIRE_TICKETID" , "false" )
1517
1618
1719def _call (args ):
1820 try :
1921 return check_output (args ).decode ("utf-8" )
2022 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
2424
2525
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
2931
3032
31- def new_feature (name , prefix ):
33+ def new_feature (name , prefix , ticket_id ):
3234 name = re .sub (r"\W" , "_" , name )
3335 original_branch = _current_branch ()
3436 if original_branch != master_branch :
@@ -42,12 +44,10 @@ def new_feature(name, prefix):
4244 sys .exit ("Ok, Exiting" ) # noqa
4345
4446 _call (["git" , "remote" , "update" , "origin" ])
45- new_branch = _get_branch_name (prefix , name )
47+ new_branch = _get_branch_name (prefix , name , ticket_id )
4648
4749 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
5151
5252 _call (["git" , "checkout" , "-b" , new_branch ])
5353 _call (["git" , "push" , "-u" , "origin" , new_branch + ":" + new_branch ])
@@ -64,15 +64,11 @@ def finish_feature(name, prefix):
6464 branch = cur_branch
6565 _call (["git" , "checkout" , master_branch ])
6666 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 ))
7068
7169 _call (["git" , "remote" , "update" , "origin" ])
7270
73- commits = _call (
74- ["git" , "log" , "--oneline" , branch , "^origin/{}" .format (master_branch )]
75- )
71+ commits = _call (["git" , "log" , "--oneline" , branch , "^origin/{}" .format (master_branch )])
7672 if commits :
7773 sys .exit (
7874 __name__
@@ -145,14 +141,10 @@ def pullrequest(args):
145141
146142 # check its up to date with remote master if not pull
147143 _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 )])
151145 if commits :
152146 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 )
156148 ) # noqa
157149 print (commits )
158150 print (
@@ -166,15 +158,9 @@ def pullrequest(args):
166158 _call (["git" , "checkout" , branch ])
167159 try :
168160 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" )
172162 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 ))
178164 except CalledProcessError as e :
179165 if b"CONFLICT" in e .output :
180166 err = (
@@ -200,9 +186,7 @@ def pullrequest(args):
200186 print ("name" , name )
201187 print ("branch" , branch )
202188 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
206190 print (url )
207191 else :
208192 webbrowser .open_new_tab (url )
@@ -216,9 +200,7 @@ def _get_pullrequest_url(name, branch):
216200 else :
217201 url = "https://github.com/" + name + "/pull/new/" + branch
218202 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
222204 return url
223205
224206
@@ -241,7 +223,7 @@ def _get_branches(branch_type):
241223 try :
242224 branch_list = (
243225 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
245227 shell = True ,
246228 )
247229 .decode ("utf-8" )
@@ -256,6 +238,7 @@ def _get_branches(branch_type):
256238
257239
258240def run (prefix , args ):
241+ print (prefix , args , require_ticket_id )
259242 if len (args ) and args [0 ].lower () == "new" :
260243 allowed_branch_types = ["releasecandidate" , "stable" , "release" , "hotfix" ]
261244 if prefix in allowed_branch_types :
@@ -264,8 +247,17 @@ def run(prefix, args):
264247 else :
265248 name = str (datetime .date .today ())
266249 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 )
269261 else :
270262 sys .exit ("Usage: git %s new <%s_name>" % (prefix , prefix ))
271263 elif len (args ) and args [0 ].lower () == "finish" :
0 commit comments