Python: скрипт переименования спринтов в JIRA

Автор: | 28/07/2015

PythonМы пользуемся Cloud-based версией JIRA, поэтому API может отличаться.

Задача: переименовать спринты в JIRA.

Обращение к спринтам в JIRA реализовано по их ID.

Например, в URL https://account.atlassian.net/secure/RapidBoard.jspa?rapidView=23&view=reporting&chart=sprintRetrospective&sprint=144

ID спринта будет 144.

Реализовано с помощью requests и argparse.

Сам скрипт:

#!/usr/bin/env python

import sys

import requests
import json
import argparse
from urlparse import urljoin


def jira_put(user, password, url, data, headers):

        session = requests.Session()
        session.auth = (user, password)

        data_put = session.put(url, data=json.dumps(data), headers=headers)
        data_put.raise_for_status()


def getopts():

    parser = argparse.ArgumentParser()

    parser.add_argument('-i', '--sprint_id', action='store', dest='sprint_id',
                        help='Sprind ID to rename, e.g. in https://domain.atlassian.net/rest/greenhopper/1.0/sprint/138'
                             ' - ID=138 ')
    parser.add_argument('-u', '--username', action='store', dest='username',
                        help='Username for JIRA authentication')
    parser.add_argument('-p', '--password', action='store', dest='password',
                        help='Password for JIRA authentication')
    parser.add_argument('-n', '--new_sprint_name', action='store', dest='new_sprint_name',
                        help='New sprint name')
    parser.add_argument('-s', '--start_date', action='store', dest='start_date',
                        help='Sprint start date, in fromat "13/Jul/15 6:00 PM"')
    parser.add_argument('-e', '--end_date', action='store', dest='end_date',
                        help='Sprint end date, in fromat "20/Jul/15 6:00 PM"')

    if len(sys.argv) <= 1:
        parser.print_help()
        sys.exit(1)
    else:
        return parser.parse_args()


if __name__ == '__main__':

    options_set = getopts()

    if options_set.username:
        jira_username = options_set.username

    if options_set.password:
        jira_password = options_set.password

    if options_set.sprint_id:
        sprint_id = options_set.sprint_id

    if options_set.new_sprint_name:
        new_sprint_name = options_set.new_sprint_name

    if options_set.start_date:
        start_date = options_set.start_date

    if options_set.end_date:
        end_date = options_set.end_date

    base_url = 'https://domain.atlassian.net/rest/greenhopper/1.0/sprint/'
    data = {'name' : new_sprint_name, 'startDate' : start_date, 'endDate' : end_date}
    headers = {'Accept': 'application/json', 'Content-type': 'application/json'}

    url = urljoin(base_url, sprint_id)

    jira_put(jira_username, jira_password, url, data, headers)

 

И помощь:

d:RDSrdsmanager>libexternalrename_sprint.py
usage: rename_sprint.py [-h] [-i SPRINT_ID] [-u USERNAME] [-p PASSWORD]
                        [-n NEW_SPRINT_NAME] [-s START_DATE] [-e END_DATE]

optional arguments:
  -h, --help            show this help message and exit
  -i SPRINT_ID, --sprint_id SPRINT_ID
                        Sprind ID to rename, e.g. in https://domain.atla
                        ssian.net/rest/greenhopper/1.0/sprint/138 - ID=138
  -u USERNAME, --username USERNAME
                        Username for JIRA authentication
  -p PASSWORD, --password PASSWORD
                        Password for JIRA authentication
  -n NEW_SPRINT_NAME, --new_sprint_name NEW_SPRINT_NAME
                        New sprint name
  -s START_DATE, --start_date START_DATE
                        Sprint start date, in fromat "13/Jul/15 6:00 PM"
  -e END_DATE, --end_date END_DATE
                        Sprint end date, in fromat "20/Jul/15 6:00 PM"

Пример вызова:

d:RDSrdsmanager>libexternalrename_sprint.py -u someuser -p somepassword -i 138 -n "Sprint 1 Stingray (1.1)" -s "13/Jul/15 6:00 PM" -e "20/Jul/15 6:00 PM"