forked from jamescdavis/jbranch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbranch.py
142 lines (114 loc) · 3.59 KB
/
jbranch.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import requests
import subprocess
import sys
from colorama import Fore, Style
from getpass import getpass
from jira import JIRA, JIRAError
from prompt_toolkit.shortcuts import prompt
def bold(text):
if os.environ.get('JBRANCH_NO_COLOR'):
return text
return Style.BRIGHT + text + Style.NORMAL
def action(message):
sys.stdout.write(message + ' ... ')
sys.stdout.flush()
def success(message='✓'):
if os.environ.get('JBRANCH_NO_COLOR'):
print message
else:
print Fore.GREEN + message + Style.RESET_ALL
def error(message):
if os.environ.get('JBRANCH_NO_COLOR'):
print 'Error: ' + message
else:
print Fore.RED + 'Error: ' + message + Style.RESET_ALL
sys.exit()
if len(sys.argv) < 2:
error('must provide a JIRA issue key')
key = sys.argv[1]
if os.environ.get('JBRANCH_JIRA_URL'):
url = os.environ.get('JBRANCH_JIRA_URL')
else:
url = raw_input('JIRA URL: ')
if os.environ.get('JBRANCH_JIRA_USERNAME'):
username = os.environ.get('JBRANCH_JIRA_USERNAME')
else:
username = raw_input('JIRA Username: ')
if os.environ.get('JBRANCH_JIRA_PASSWORD'):
password = os.environ.get('JBRANCH_JIRA_PASSWORD')
else:
password = getpass('JIRA Password: ')
action('Authenticating to {0} as {1}'.format(bold(url), bold(username)))
try:
jira = JIRA(server=url, basic_auth=(username, password), max_retries=0)
except requests.exceptions.MissingSchema:
error('JIRA URL ' + bold(url) + ' is invalid')
except requests.exceptions.ConnectionError:
error('could not connect to JIRA Server at ' + bold(url))
except JIRAError as e:
error(requests.status_codes._codes[e.status_code][0])
else:
success()
action('Getting info for ' + bold(key))
try:
issue = jira.issue(key)
except JIRAError as e:
error(e.text)
else:
success()
if not issue.fields.issuetype.name:
error('could not get issue type')
if not issue.fields.summary:
error('could not get summary')
branch_type = issue.fields.issuetype.name.lower()
if os.environ.get('JBRANCH_LC_SUMMARY'):
summary = issue.fields.summary.lower()
elif os.environ.get('JBRANCH_LC_FIRST'):
summary = issue.fields.summary[:1].lower() + issue.fields.summary[1:]
else:
summary = issue.fields.summary
branch = '{0}_{1}'.format(key, summary)
if not os.environ.get('JBRANCH_NO_PREFIX'):
branch = '{0}/{1}'.format(branch_type, branch)
branch = re.sub(r'\s+', '_', branch)
branch = re.sub(r'[^\w\-\/]', '', branch)
if os.environ.get('JBRANCH_ALLOW_EDIT'):
branch = prompt(u'Branch name: ', default=branch.decode('utf-8'))
action('Creating branch ' + bold(branch))
try:
if os.environ.get('JBRANCH_NO_SWITCH'):
subprocess.check_output(
['git', 'branch', branch],
stderr=subprocess.STDOUT
)
else:
subprocess.check_output(
['git', 'checkout', '-b', branch],
stderr=subprocess.STDOUT
)
except OSError as e:
error(str(e))
except subprocess.CalledProcessError as e:
error(e.output.rstrip())
else:
success()
transition = os.environ.get('JBRANCH_TRANSITION')
if transition:
action('Triggering transition: ' + bold(transition))
transition_id = jira.find_transitionid_by_name(issue, transition)
if transition_id is None:
error(
'the transition {0} is not available for this issue'.format(
bold(transition)
)
)
try:
jira.transition_issue(issue, transition_id)
except JIRAError as jira_error:
error(jira_error.text)
else:
success()