From: Aric Gardner Date: Fri, 26 Apr 2019 22:19:19 +0000 (-0400) Subject: Preliminary jira api tools X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F15430%2F9;p=releng%2Flftools.git Preliminary jira api tools This code does not work with AUTH-zero Each AUTH0 jira will need to have an application link created. docs for oauth/creating an application link: https://developer.atlassian.com/server/jira/platform/oauth/ working code for oauth: https://gist.github.com/Aricg/1e65cc3e4dde3ce23f30ee414a11c469 Given that we are moving everything to oauth I will probably rewrite this with the python jira library. https://github.com/pycontribs/jira Commands: create-project set-owner (ldap group that is owner for a project) get lftools jira get is needed to find the values for create-project and set-owner For example: to set owner for a project in opnfv the Administrators group is 1002 this is probably the default, but I have to check all the jiras first. at least if your project adds ldap groups as jira-devlopers you can adapt this to 1001 or whatever This is implemented with basic auth, base64 encoded in the lftools config file [jira] section echo -n "user:pass" | base64 example: [jira] base64 = dXNlcjpwYXNzd29yZGZvbw== ISSUE: RELENG-1525 Change-Id: I6efbf15e3be0d9389a995d7674ea4b18ee2f0f0f Signed-off-by: Aric Gardner --- diff --git a/docs/commands/index.rst b/docs/commands/index.rst index a0032c77..cd9cb81f 100644 --- a/docs/commands/index.rst +++ b/docs/commands/index.rst @@ -16,6 +16,7 @@ It supports the following commands: gerrit github infofile + jira lfidapi license nexus diff --git a/docs/commands/jira.rst b/docs/commands/jira.rst new file mode 100644 index 00000000..b0762b2d --- /dev/null +++ b/docs/commands/jira.rst @@ -0,0 +1,25 @@ +**** +Jira +**** + +.. program-output:: lftools jira --help + +Commands +======== + +create +-------- + +.. program-output:: lftools jira create-project --help + + +get +--- + +.. program-output:: lftools jira get --help + + +set-owner +--------- + +.. program-output:: lftools jira set-owner --help diff --git a/lftools/cli/__init__.py b/lftools/cli/__init__.py index b1fd9343..2e0a18aa 100644 --- a/lftools/cli/__init__.py +++ b/lftools/cli/__init__.py @@ -26,6 +26,7 @@ from lftools.cli.gerrit import gerrit_cli from lftools.cli.github_cli import github_cli from lftools.cli.infofile import infofile from lftools.cli.jenkins import jenkins_cli +from lftools.cli.jira import jira from lftools.cli.lfidapi import lfidapi from lftools.cli.license import license from lftools.cli.nexus import nexus @@ -84,6 +85,7 @@ cli.add_command(gerrit_cli, name='gerrit') cli.add_command(github_cli, name='github') cli.add_command(infofile) cli.add_command(jenkins_cli, name='jenkins') +cli.add_command(jira) cli.add_command(license) cli.add_command(nexus) cli.add_command(schema) diff --git a/lftools/cli/jira.py b/lftools/cli/jira.py new file mode 100644 index 00000000..502834ed --- /dev/null +++ b/lftools/cli/jira.py @@ -0,0 +1,168 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Jira api tools.""" + +from __future__ import print_function + +import json +import logging + +import click +import requests + +from lftools import config + +log = logging.getLogger(__name__) + + +@click.group() +@click.pass_context +def jira(ctx): + """Jira api tools.""" + pass + + +@click.command(name='get') +@click.argument('url') +@click.argument('endpoint') +@click.pass_context +def get_scheme(ctx, url, endpoint): + """Get request a jira end-point. + + \b + url = jira.example.com + Example Endpoints: + role + avatar/project/system + issuesecurityschemes + notificationscheme + permissionscheme + projectCategory + """ + userandpass = config.get_setting("jira", "base64") + url = ("https://{}/rest/api/2/{}").format(url, endpoint) + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": "Basic %s" % userandpass + } + response = requests.request( + "GET", + url, + headers=headers + ) + log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))) + + +@click.command(name='create-project') +@click.argument('notificationscheme') +@click.argument('description') +@click.argument('lead') +@click.argument('url') +@click.argument('projecttemplatekey') +@click.argument('longname') +@click.argument('shortkey') +@click.argument('projecturl') +@click.pass_context +def create_project(ctx, notificationscheme, description, lead, url, projecttemplatekey, longname, shortkey, projecturl): + """Create a JIRA project. + + \b + notificationscheme: 10000 is the global default + lead: lfid of the project lead. (lead must have previously signed in to jira) + url: jira.example.com + projectTemplateKey: Valid values are one of the following + com.pyxis.greenhopper.jira:gh-scrum-template + com.pyxis.greenhopper.jira:gh-kanban-template + com.pyxis.greenhopper.jira:basic-software-development-template + longname: Long project name, please double quote. + shortkey: Ten character SHORTKEY must be capitalized + projecturl: Projects URL, probaly a link to their wiki. + + TODO: do we need to add these as options? + issueSecurityScheme: 10001, + categoryId: 10120 + + Example: + 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 + """ + userandpass = config.get_setting("jira", "base64") + url = ("https://{}/rest/api/2/project/").format(url) + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": "Basic %s" % userandpass + } + + payload = json.dumps({ + "notificationScheme": "{}".format(notificationscheme), # 10000 make a default + "description": "{}".format(description), + "lead": "{}".format(lead), + "url": "{}".format(projecturl), + "projectTemplateKey": "{}".format(projecttemplatekey), + "name": "{}".format(longname), # 80 chars + "permissionScheme": 0, # this is default + "assigneeType": "PROJECT_LEAD", + "projectTypeKey": "software", + "key": "{}".format(shortkey), # 10 chars + }) + + log.info(payload) + response = requests.request( + "POST", + url, + data=payload, + headers=headers + ) + + log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))) + + +@click.command(name='set-owner') +@click.argument('jiraurl') +@click.argument('currentgroup') +@click.argument('projectkey') +@click.argument('role') +@click.pass_context +def set_owner(ctx, jiraurl, currentgroup, projectkey, role): + """Set an ldap group as owner on a jira project. 1002 is the role for owner, 1001 is devloper. + + \b + Example: jira.example.com ldap-group-committers PROJECTSHORTKEY 1002 + Rolls may be diffrent across jiras. + find admin role with lftools jira get-scheme jira.example.com role + """ + userandpass = config.get_setting("jira", "base64") + url = ("https://{}/rest/api/2/project/{}/role/{}").format(jiraurl, projectkey, role) + + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": "Basic %s" % userandpass + } + + payload = json.dumps({ + "group": [ + "{}".format(currentgroup) + ]}) + + response = requests.request( + "POST", + url, + data=payload, + headers=headers + ) + + log.info(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))) + + +jira.add_command(get_scheme) +jira.add_command(create_project) +jira.add_command(set_owner) diff --git a/releasenotes/notes/jira-b4fcd20dc6f9e724.yaml b/releasenotes/notes/jira-b4fcd20dc6f9e724.yaml new file mode 100644 index 00000000..70040faf --- /dev/null +++ b/releasenotes/notes/jira-b4fcd20dc6f9e724.yaml @@ -0,0 +1,20 @@ +--- +features: + - | + Jira project creation, role and api tools. + + Usage: lftools jira [OPTIONS] COMMAND [ARGS]... + + .. code-block:: none + + Commands: + create-project Create a JIRA project. + get Get api end points. + set-owner Set an ldap group as owner on a jira project. + + + .. code-block:: none + + Options: + --help Show this message and exit. +