-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk-import-components.py
executable file
·90 lines (76 loc) · 2.91 KB
/
bulk-import-components.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
#!/usr/bin/python
'''
SYNOPSIS
bulk-import-components.py [-u, --username] [-k, --key] [csv file]
DESCRIPTION
Reads a csv list of components and imports them to a JIRA Project
based on the passed project key.
Uses JIRA REST API (tested using API v6.3.4)
'''
import sys, argparse, getpass, json, requests, csv
from requests.auth import HTTPDigestAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter
from requests import Session, exceptions
# DEFINE JIRA URL
jiraurl = ''
# main module - define what the script needs to do
def main(args, password):
# get the JIRA project data
try:
jiraResponse = s.get(jiraurl + '/rest/api/2/project/' + args.key, auth = (args.username, password), verify=False)
jiraResponse.raise_for_status()
except requests.exceptions.HTTPError as err:
# display error and exit
print err
sys.exit(1)
headers = {'Content-Type': 'application/json'}
# open the csv file
with open(args.csv, 'rb') as f:
reader = csv.reader(f)
try:
# iterate thru each record
for row in reader:
print "Adding " + row[0]
try:
data = {'name': row[0], 'project': args.key}
r = s.post(jiraurl + '/rest/api/2/component', data = json.dumps(data), headers = headers, auth = (args.username, password), verify = False)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
# display error and exit
print err
sys.exit(1)
except csv.Error as e:
sys.exit('File %s, line %d: %s' % (args.csv, reader.line_num, e))
finally:
# close the file
f.close()
# Define script entry-point and parameters
if __name__ == "__main__":
# check if JIRA instance URL was defined
if not jiraurl:
print "JIRA URL not defined. Edit this script and enter the details"
sys.exit(1)
# define required paramaters
parser = argparse.ArgumentParser(
description = "Bulk deletion of components in a JIRA project"
)
parser.add_argument(
"-u", "--username", help = "JIRA username", required = True
)
parser.add_argument(
"-k", "--key", help = "JIRA Project Key", required = True
)
parser.add_argument(
"csv", help = "CSV file"
)
args = parser.parse_args()
# Password
password = getpass.getpass()
# disable SSL verication warnings
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# define HTTP session retries
s = Session()
s.mount('https://', HTTPAdapter(max_retries=Retry(total=5)))
main(args, password)