-
Notifications
You must be signed in to change notification settings - Fork 8
/
github2jira
executable file
·127 lines (106 loc) · 5.24 KB
/
github2jira
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
#!/usr/bin/env python3
# Copyright 2017 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import urllib.request, urllib.error, urllib.parse
import getpass
import json
import re
import argparse
from base64 import b64encode
parser = argparse.ArgumentParser(description='Convert GitHub issue into JIRA issue')
parser.add_argument('--pat', action='store_true', help='Use JIRA Personal Access Token')
parser.add_argument('issuenum', metavar='ISSUE', nargs='+', type=int,
help='The issue number')
parser.add_argument('--repo', metavar='REPO', type=str,
default='EnMasseProject/enmasse', help='The name of the repository (default: EnMasseProject/enmasse)')
parser.add_argument('--user', metavar='USER', type=str,
help='The name of the user in the JIRA system')
parser.add_argument('--project', metavar='PROJECT', type=str,
default='ENTMQMAAS', help='The project in the JIRA system (default: ENTMQMAAS)')
parser.add_argument('--githubbaseurl', metavar='GITHUBURL', type=str,
default='https://api.github.com',
help='The base URL for github (default: https://api.github.com)')
parser.add_argument('--jirabaseurl', metavar='JIRAURL', type=str,
default='https://issues.redhat.com/rest/api/2',
help='The base URL for jira (default: https://issues.redhat.com/rest/api/2)')
parser.add_argument('--usermapping', metavar='USERMAPPING', type=str,
default='{ "rgodfrey" : "robgodfrey", "grs" : "gordonsim" , "lulf" : "ulf.lilleengen", "tkratky" : "tkratky", "ppatierno" : "ppatiern", "kornys": "dkornel", "k-wall" : "kwall", "RH-sdavey" : "sdavey", "robshelly" : "rshelly", "junezhang" : "zhangjune99", "yih-wang" : "yihwang", "ctron" : "jreimann-1", "zschwarz" : "zschwarz", "hguerrero" : "hguerrer", "vbusch" : "vbusch", "fgiorgetti" : "fgiorget"}',
help='json map of github id to JIRA id')
parser.add_argument('--versionmapping', metavar='VERSIONMAPPING', type=str,
default='{}',
help='json map of github major.minor milestone to jira major.minor version')
parser.add_argument('--jiraadmins', metavar='JIRAADMINS', type=str,
default='[ "robgodfrey", "gordonsim" , "ulf.lilleengen" ]',
help='json list of JIRA users who can create issues on behalf of another')
args = parser.parse_args()
username = args.user
pat = args.pat
if username is None and not pat:
username = input('Username: ')
pw = getpass.getpass()
if not pat:
upwstr = username + ':' + pw
upwstrenc = "Basic " + b64encode(upwstr.encode('utf-8')).strip().decode('utf-8')
else:
upwstrenc = "Bearer " + pw
usermap = json.loads(args.usermapping)
versionmap = json.loads(args.versionmapping)
admins = json.loads(args.jiraadmins)
def milestoneToJiraVersion(milestone):
if milestone in versionmap:
return versionmap[milestone]
else:
# Some auto-conversion based on our current versioning rules
m = re.search(r"(\d+).(\d+).(\d+)", milestone)
if m:
major=int(m.group(1))
minor=int(m.group(2))
micro=int(m.group(3))
if major == 0:
minor = minor - 27
return ("%d.%d.%d") % (major + 1, minor, micro)
else:
raise ("Unable to map milestone %s to product version" % milestone)
for issue in args.issuenum:
url = '%s/repos/%s/issues/%d' % (args.githubbaseurl, args.repo, issue)
req = urllib.request.Request(url)
res = urllib.request.urlopen(req).read()
githubIssue = json.loads(res)
jiraVersion = None
if githubIssue["milestone"] != None:
jiraVersion = milestoneToJiraVersion(githubIssue["milestone"]["title"])
jiraIssueType = 'Task'
if githubIssue["labels"] != None:
for label in githubIssue["labels"]:
name = str(label["name"])
if name == "kind/bug":
jiraIssueType = 'Bug'
elif name == "kind/feature":
jiraIssueType = 'Enhancement'
fields = { 'project' : { 'key': args.project },
'issuetype' : { 'name': jiraIssueType },
'summary' : '[#%d] : %s' % ( githubIssue['number'], githubIssue['title'] ),
'description' : 'Created from upstream issue [#%d|%s]. Fix version: %s' % (githubIssue['number'], githubIssue['html_url'], jiraVersion ) }
mappeduser = usermap[ githubIssue[ 'user' ][ 'login' ] ]
if username in admins and mappeduser :
fields[ 'reporter' ] = { 'name' : mappeduser }
jira = { 'fields': fields }
posturl = '%s/issue/' % ( args.jirabaseurl )
postreq = urllib.request.Request(posturl)
postreq.add_header('Content-Type','application/json')
postreq.add_header('Authorization', upwstrenc)
postreq.data = bytes(json.dumps(jira), "utf-8")
postres = urllib.request.urlopen(postreq).read()
jiraResult = json.loads(postres)
print('Created issue: %s' % ( jiraResult['key'] ))