from jira import JIRA
import pylightxl as xl
from datetime import date, datetime, time
import time
from openpyxl import load_workbook
jira = JIRA( server='jira ip地址', basic_auth=('xiaohu', 'xiaohu1') )
"""
检查需求及子任务字段是否完整
"""
def check():
issues_list = readdata()
for row in issues_list :
if row[1]== '子任务-程序开发' or row[1] == '子任务-程序测试':
if row[1]=='' or row[2]==''or row[3]=='' or row[6]==''or row[7]=='' or row[8]=='' or row[10]=='':
return("子任务必填字段为空%s"%row)
if row[1]== '需求':
if row[1]=='' or row[2]==''or row[3]=='' or row[4]=='' or row[5]==''or row[6]==''or row[8]=='' or row[9]=='' or row[10]=='':
return("需求必填字段为空%s" %row)
"""
读取数据
"""
def readdata() :
db = xl.readxl( fn='jiraIssues_bdc.xlsx' )
listdata = [ ]
for sheet in db.ws_names :
first_row = True
for row in db.ws( sheet ).rows :
if not first_row :
if len( row ) == 0 : # 空行跳过
continue
row = row [ :13 ] # 取前13列
listdata.append( row )
else :
first_row = False
return listdata
"""
日期处理
"""
def jsondate(obj) :
if type( obj ) is int :
# 对日期是整数型特殊处理
t0 = datetime.datetime( year=1900, month=1, day=1 )
delta = datetime.timedelta( days=obj )
obj = (t0 + delta).strftime( '%Y-%m-%d' )
return obj
"""
需求字段组装
"""
def storyAssembly(row) :
row [ 8 ] = jsondate( row [ 8 ] ) # 处理日期
dict_story = {
'project' : row [ 0 ],
'issuetype' : { 'name' : '产品需求&用户故事&新功能' }, # 类型
'summary' : row [ 2 ], # 概要
'description' : row [ 3 ], # 描述
'customfield_10604' : { 'value' : row [ 4 ] }, # 关联项目
'components' : [ { 'name' : row [ 5 ] } ], # 模块
'fixVersions' : [ { 'name' : row [ 6 ] } ], # 修复的版本
'duedate' : row [ 8 ], # 到期日
'reporter' : { 'name' : row [ 9 ] }, # 报告人
}
return dict_story
"""
子任务(开发/测试)字段组装
"""
# 父任务工作项
parentId = ' '
def subTaskAssembly(row, parentId) :
row [ 8 ] = jsondate( row [ 8 ] ) # 处理日期
row [ 7 ] = row [ 7 ]*8 #传入天改为小时
subTask_dict = {
'project' : { 'key' : row [ 0 ] }, # 项目
'issuetype' : { 'name' : row [ 1 ] }, # 类型
'summary' : row [ 2 ], # 概要
'description' : row [ 3 ], # 描述
"timetracking" : { "remainingEstimate" : row [ 7 ] }, # 初始预
'duedate' : row [ 8 ], # 到期日
'parent' : { 'key' : parentId }, # 父任务
}
return subTask_dict
"""
创建任务或者需求
"""
def creatsubTask() :
#检查需求及子任务必填字段
check()
#创建需求及子任务
i, j, z = 0, 0, 1
issue_dict = { }
issues_list = readdata()
wb = load_workbook( "jiraIssues_bdc.xlsx" ) #建立任务模板,为excle格式
wa = wb.active
for row in issues_list :
z=z+1
if row [ 1 ] == '需求' :
i += 1
issue_dict = storyAssembly( row )
parentIds = jira.create_issue( issue_dict )
parentId=str(parentIds)
jira.assign_issue(parentId, row [ 10 ] )
testLink="http://atu-project.crv.com.cn:8080/browse/" + parentId
wa.cell(z,13,testLink)
print( '创建第', i, '个需求%s' % parentId )
if row [ 1 ] == '子任务-程序开发' or row [ 1 ] == '子任务-程序测试' :
j += 1
issue_dict = subTaskAssembly( row, parentId )
subTaskIds = jira.create_issue(issue_dict )
subTaskId=str(subTaskIds)
jira.assign_issue(subTaskId,row[10]) #分配任务
print( '创建第', j, '个子任务%s'%subTaskId )
wa.cell(z,12,parentId) #取子任务对应的父任务写入对应的表格
testLink="http://atu-project.crv.com.cn:8080/browse/" + subTaskId #拼接新建任务连接
wa.cell( z, 13,testLink) #连接写入表格
#已写入创建的jira工作项
nameTime = str( time.strftime( '%Y-%m-%d_%H-%M-%S' ) )
excelName = 'jiraIssues' + nameTime + '.xlsx'
wb.save(excelName) #回写任务编号,将建立的任务编号写入模板
if __name__ == '__main__' :
creatsubTask()