Preliminary jira api tools
[releng/lftools.git] / lftools / cli / jira.py
1 # SPDX-License-Identifier: EPL-1.0
2 ##############################################################################
3 # Copyright (c) 2019 The Linux Foundation and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Eclipse Public License v1.0
7 # which accompanies this distribution, and is available at
8 # http://www.eclipse.org/legal/epl-v10.html
9 ##############################################################################
10 """Jira api tools."""
11
12 from __future__ import print_function
13
14 import json
15 import logging
16
17 import click
18 import requests
19
20 from lftools import config
21
22 log = logging.getLogger(__name__)
23
24
25 @click.group()
26 @click.pass_context
27 def jira(ctx):
28     """Jira api tools."""
29     pass
30
31
32 @click.command(name='get')
33 @click.argument('url')
34 @click.argument('endpoint')
35 @click.pass_context
36 def get_scheme(ctx, url, endpoint):
37     """Get request a jira end-point.
38
39     \b
40     url = jira.example.com
41     Example Endpoints:
42     role
43     avatar/project/system
44     issuesecurityschemes
45     notificationscheme
46     permissionscheme
47     projectCategory
48     """
49     userandpass = config.get_setting("jira", "base64")
50     url = ("https://{}/rest/api/2/{}").format(url, endpoint)
51     headers = {
52         "Accept": "application/json",
53         "Content-Type": "application/json",
54         "Authorization": "Basic %s" % userandpass
55     }
56     response = requests.request(
57         "GET",
58         url,
59         headers=headers
60     )
61     log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
62
63
64 @click.command(name='create-project')
65 @click.argument('notificationscheme')
66 @click.argument('description')
67 @click.argument('lead')
68 @click.argument('url')
69 @click.argument('projecttemplatekey')
70 @click.argument('longname')
71 @click.argument('shortkey')
72 @click.argument('projecturl')
73 @click.pass_context
74 def create_project(ctx, notificationscheme, description, lead, url, projecttemplatekey, longname, shortkey, projecturl):
75     """Create a JIRA project.
76
77     \b
78     notificationscheme: 10000 is the global default
79     lead: lfid of the project lead. (lead must have previously signed in to jira)
80     url: jira.example.com
81     projectTemplateKey: Valid values are one of the following
82     com.pyxis.greenhopper.jira:gh-scrum-template
83     com.pyxis.greenhopper.jira:gh-kanban-template
84     com.pyxis.greenhopper.jira:basic-software-development-template
85     longname: Long project name, please double quote.
86     shortkey: Ten character SHORTKEY must be capitalized
87     projecturl: Projects URL, probaly a link to their wiki.
88
89     TODO: do we need to add these as options?
90     issueSecurityScheme: 10001,
91     categoryId: 10120
92
93     Example:
94     lftools jira create-project 10000 "example description" adminlfid jira.example.com com.pyxis.greenhopper.jira:gh-scrum-template "Example test project" EXAMPLE https://wiki.example.com/exampleproject
95     """
96     userandpass = config.get_setting("jira", "base64")
97     url = ("https://{}/rest/api/2/project/").format(url)
98     headers = {
99         "Accept": "application/json",
100         "Content-Type": "application/json",
101         "Authorization": "Basic %s" % userandpass
102     }
103
104     payload = json.dumps({
105         "notificationScheme": "{}".format(notificationscheme),  # 10000 make a default
106         "description": "{}".format(description),
107         "lead": "{}".format(lead),
108         "url": "{}".format(projecturl),
109         "projectTemplateKey": "{}".format(projecttemplatekey),
110         "name": "{}".format(longname),  # 80 chars
111         "permissionScheme": 0,  # this is default
112         "assigneeType": "PROJECT_LEAD",
113         "projectTypeKey": "software",
114         "key": "{}".format(shortkey),  # 10 chars
115     })
116
117     log.info(payload)
118     response = requests.request(
119         "POST",
120         url,
121         data=payload,
122         headers=headers
123     )
124
125     log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
126
127
128 @click.command(name='set-owner')
129 @click.argument('jiraurl')
130 @click.argument('currentgroup')
131 @click.argument('projectkey')
132 @click.argument('role')
133 @click.pass_context
134 def set_owner(ctx, jiraurl, currentgroup, projectkey, role):
135     """Set an ldap group as owner on a jira project. 1002 is the role for owner, 1001 is devloper.
136
137     \b
138     Example: jira.example.com ldap-group-committers PROJECTSHORTKEY 1002
139     Rolls may be diffrent across jiras.
140     find admin role with lftools jira get-scheme jira.example.com role
141     """
142     userandpass = config.get_setting("jira", "base64")
143     url = ("https://{}/rest/api/2/project/{}/role/{}").format(jiraurl, projectkey, role)
144
145     headers = {
146         "Accept": "application/json",
147         "Content-Type": "application/json",
148         "Authorization": "Basic %s" % userandpass
149     }
150
151     payload = json.dumps({
152         "group": [
153             "{}".format(currentgroup)
154         ]})
155
156     response = requests.request(
157         "POST",
158         url,
159         data=payload,
160         headers=headers
161         )
162
163     log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
164
165
166 jira.add_command(get_scheme)
167 jira.add_command(create_project)
168 jira.add_command(set_owner)