JIRA API:自动关闭当前迭代并把未完成的工作移动到下个迭代

  1. 登录, POST: http://localhost:8080/rest/auth/1/session
  2. 获取rapidviewid(第四步和第五步要用)GET: http://localhost:8080/rest/greenhopper/latest/rapidviews/list
  3. 创建新迭代 POST /rest/agile/1.0/sprint
  4. 获取当前迭代和下一个迭代id, GET: http://localhost:8080/rest/greenhopper/latest/sprintquery/10?includeHistoricSprints=true&includeFutureSprints=true
  5. 关闭当前迭代并把未完成的工作移动到下一个迭代(当前迭代是35, 下一个迭代id是36):
    JIRA API:自动关闭当前迭代并把未完成的工作移动到下个迭代_第1张图片
    image.png

Python示例代码

import requests
import json
import datetime

loginData={
    "username": "bob",
    "password": "111111"
}
kanbanName = 'PETS'


#Login
urlLogin="http://localhost:8080/rest/auth/1/session"
data_json = json.dumps(loginData)
headers = {'Content-type': 'application/json'}
loginResponse = requests.post(urlLogin, data=data_json, headers=headers)
print(loginResponse.text)
jsessionid = json.loads(loginResponse.text)['session']['value']
print(jsessionid)


# Get rapidViewId
cookies = {'JSESSIONID': jsessionid}
urlGetRapidViewID = "http://localhost:8080/rest/greenhopper/latest/rapidviews/list"
getRapidViewIDResponse = requests.get(urlGetRapidViewID,cookies=cookies, headers=headers)
rapidView = json.loads(getRapidViewIDResponse.text)
rapidViewId = None
for rapidview2 in rapidView['views']:
    print(rapidview2['name'])
    if rapidview2['name'] == kanbanName:
        rapidViewId = rapidview2['id']
print(rapidViewId)
print(getRapidViewIDResponse.text)


# Get active sprint Id
activeSprintId = None
activeSprintName = None
nextSprintId = None
urlGetSprintList = "http://localhost:8080/rest/greenhopper/latest/sprintquery/" + str(rapidViewId) + "?includeFutureSprints=true"
getSprintListResponse = requests.get(urlGetSprintList,cookies=cookies, headers=headers)
sprintList = json.loads(getSprintListResponse.text)
for sprint in sprintList['sprints']:
    if sprint['state'] == 'ACTIVE':
        activeSprintId = sprint['id']
        activeSprintName = sprint['name']
    if sprint['state'] == 'FUTURE':
        if nextSprintId is None:
            nextSprintId = sprint['id']
        elif nextSprintId is None:
            if nextSprintId < sprint['id']:
                nextSprintId = sprint['id']
print("active sprint id: " + str(activeSprintId))
print("next sprint id: " + str(nextSprintId))


# create new sprint, 2 weeks from tomorrow
urlCreateNewSprint = "http://localhost:8080/rest/agile/1.0/sprint"
if nextSprintId is None:
    today = datetime.datetime.today()
    tomorrow = (today + datetime.timedelta(1)).strftime('%Y-%m-%d')
    twoWeeks = datetime.timedelta(days=14)
    twoWeeksLater = (today + twoWeeks).strftime('%Y-%m-%d')

    createNewSprintRequestBody={
        "name": "PETS Sprint " + str(int(activeSprintName.split(' ')[-1]) + 1),
        "startDate": "" + tomorrow + "T09:00:00.000+10:00",
        "endDate": "" + twoWeeksLater + "T18:00:00.000+10:00",
        "originBoardId": rapidViewId,
        "goal": ""
    }
    createNewSprintResponse = requests.post(urlCreateNewSprint, data=json.dumps(createNewSprintRequestBody), cookies=cookies, headers=headers)
    if createNewSprintResponse.status_code == 201:
        print("created new sprint successfully")
        nextSprintId = json.loads(createNewSprintResponse.text)['id']
    else:
        print("failed to create new sprint")
else:
    print("No need to create new sprint")


# close active sprint and move incomplete tasks to next sprint
urlCompleteSprint = "http://localhost:8080/rest/greenhopper/1.0/sprint/" + str(activeSprintId) + "/complete"
completeSprintRequestBody={
    "rapidViewId": rapidViewId,
    "sprintId": activeSprintId,
    "incompleteIssuesDestination": "" + str(nextSprintId)
}
if activeSprintId is None:
    print("there no active sprint")
else:
    completeSprintResponse = requests.put(urlCompleteSprint, data=json.dumps(completeSprintRequestBody), cookies=cookies, headers=headers)
    print(completeSprintResponse.text)

你可能感兴趣的:(JIRA API:自动关闭当前迭代并把未完成的工作移动到下个迭代)