-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathcreate_orc_jira.py
More file actions
executable file
·148 lines (117 loc) · 4.66 KB
/
create_orc_jira.py
File metadata and controls
executable file
·148 lines (117 loc) · 4.66 KB
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
#!/usr/bin/env python3
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 os
import re
import subprocess
import sys
import traceback
try:
import jira.client
JIRA_IMPORTED = True
except ImportError:
JIRA_IMPORTED = False
# ASF JIRA access token
JIRA_ACCESS_TOKEN = os.environ.get("JIRA_ACCESS_TOKEN")
JIRA_API_BASE = "https://issues.apache.org/jira"
def fail(msg):
print(msg)
sys.exit(-1)
def run_cmd(cmd):
print(cmd)
if isinstance(cmd, list):
return subprocess.check_output(cmd).decode("utf-8")
else:
return subprocess.check_output(cmd.split(" ")).decode("utf-8")
import argparse
def create_jira_issue(title, parent_jira_id=None, issue_type=None, version=None, component=None):
asf_jira = jira.client.JIRA(
{"server": JIRA_API_BASE},
token_auth=JIRA_ACCESS_TOKEN,
timeout=(3.05, 30)
)
if version:
affected_version = version
else:
versions = asf_jira.project_versions("ORC")
# Consider only x.y.z, unreleased, unarchived versions
versions = [
x for x in versions
if not x.raw["released"] and not x.raw["archived"] and re.match(r"\d+\.\d+\.\d+", x.name)
]
versions = sorted(versions, key=lambda x: x.name, reverse=True)
affected_version = versions[0].name
issue_dict = {
'project': {'key': 'ORC'},
'summary': title,
'description': '',
'versions': [{'name': affected_version}],
}
if component:
issue_dict['components'] = [{'name': component}]
if parent_jira_id:
issue_dict['issuetype'] = {'name': 'Sub-task'}
issue_dict['parent'] = {'key': parent_jira_id}
else:
issue_dict['issuetype'] = {'name': issue_type if issue_type else 'Improvement'}
try:
new_issue = asf_jira.create_issue(fields=issue_dict)
return new_issue.key
except Exception as e:
fail("Failed to create JIRA issue: %s" % e)
def create_and_checkout_branch(jira_id):
try:
run_cmd("git checkout -b %s" % jira_id)
print("Created and checked out branch: %s" % jira_id)
except subprocess.CalledProcessError as e:
fail("Failed to create branch %s: %s" % (jira_id, e))
def create_commit(jira_id, title):
try:
run_cmd(['git', 'commit', '-a', '-m', '%s: %s' % (jira_id, title)])
print("Created a commit with message: %s: %s" % (jira_id, title))
except subprocess.CalledProcessError as e:
fail("Failed to create commit: %s" % e)
def main():
if not JIRA_IMPORTED:
fail("Could not find jira-python library. Run 'sudo pip3 install jira' to install.")
if not JIRA_ACCESS_TOKEN:
fail("The env-var JIRA_ACCESS_TOKEN is not set.")
parser = argparse.ArgumentParser(description="Create an ORC JIRA issue.")
parser.add_argument("title", help="Title of the JIRA issue")
parser.add_argument("-p", "--parent", help="Parent JIRA ID for subtasks")
parser.add_argument("-t", "--type", help="Issue type to create when no parent is specified (e.g. Bug). Defaults to Improvement.")
parser.add_argument("-v", "--version", help="Version to use for the issue")
parser.add_argument("-c", "--component", help="Component for the issue")
parser.add_argument("-n", "--no-commit", action="store_true", help="Skip creating a commit")
args = parser.parse_args()
if args.parent:
print("Creating a subtask of %s with title: %s" % (args.parent, args.title))
else:
print("Creating JIRA issue with title: %s" % args.title)
jira_id = create_jira_issue(args.title, args.parent, args.type, args.version, args.component)
print("Created JIRA issue: %s" % jira_id)
create_and_checkout_branch(jira_id)
if not args.no_commit:
create_commit(jira_id, args.title)
else:
print("Skipped commit creation")
if __name__ == "__main__":
try:
main()
except BaseException:
traceback.print_exc()
sys.exit(-1)