利用python同步jira数据到mysql-初始化

编写python脚本,通过jira库获取各个项目的jira数据,处理解析好相应字段,通过pymysql库将解析好的jira数据写入mysql。

  • 引入库
from pymysql import *
from jira import JIRA
  • 声明格式化后需要的字段
class Question:
    def __init__(self):
        self.key = None
        self.title = None
        self.creator = None
        self.assignee = None
        self.created = None
        self.status = None
        self.updated = None
        self.issuetype = None
        self.priority = None
        self.description = None
        self.project = None
        self.type = None
        self.pubproject = None
        self.pubstatus = None
        self.commit = None
        self.resolutiondate = None
        self.resolution = None
  • 获取jira项目
    
jira = JIRA("https://jira.***.net", auth=('username', 'password'))
projects = jira.projects()
keys = sorted([project.key for project in projects])
  • 获取每个项目2022年之后的issues
for key_val in keys:
        issues = jira.search_issues('project=' + key_val + ' and created >= 2022-01-01', maxResults=-1)
        format_data(issues)
  • 格式化issues数据
    
questions = []

# 格式化jira数据
def format_data(issues):
    for issue in issues:
        question = Question()
        field = issue.fields
        question.key = issue.key
        if field.summary is not None:
            question.title = field.summary[:50]
        question.creator = field.creator.displayName
        # 报告人
        if field.assignee is not None:
            question.assignee = field.assignee.displayName
        question.created = field.created
        question.status = field.status.name
        question.updated = field.updated
        question.issuetype = field.issuetype.name
        if hasattr(field.priority, 'name'):
            question.priority = field.priority.name
        else:
            question.priority = None
        if field.description is not None:
            question.description = field.description[:500]
        question.project = field.project.name
        if field.issuetype.name == '测试Bug' or field.issuetype.name == '线上bug':
            if hasattr(field, 'customfield_12702'):
                if field.customfield_12702 is not None:
                    value = field.customfield_12702.value
                    if value:
                        question.type = value
                    else:
                        question.type = None
        if hasattr(field, 'customfield_11901'):
            if hasattr(field.customfield_11901, 'child'):
                question.pubproject = field.customfield_11901.child.value
        elif hasattr(field, 'customfield_11902'):
            if hasattr(field.customfield_11902, 'child'):
                question.pubproject = field.customfield_11902.child.value
        elif hasattr(field, 'customfield_11903'):
            if hasattr(field.customfield_11903, 'child'):
                question.pubproject = field.customfield_11903.child.value
        if hasattr(field, 'customfield_11900'):
            question.pubstatus = field.customfield_11900
        if hasattr(field, 'customfield_11301'):
            question.commit = field.customfield_11301
        question.resolutiondate = field.resolutiondate
        if hasattr(field.resolution, 'name'):
            question.resolution = field.resolution.name
        else:
            question.resolution = None
        questions.append((question.key, question.title, question.creator, question.assignee, question.created,
                          question.status, question.updated,
                          question.issuetype, question.priority, question.description, question.project, question.type,
                          question.pubproject, question.pubstatus, question.commit, question.resolutiondate, question.resolution
                          ))
  • 将格式化后的数据写入mysql
def add_jira_data(questions):
    conn = connect(host='**.**.**.**', port=****, user='******', password='******', database='******', charset='utf8')
    cs = conn.cursor()  # 获取光标
    sql = "delete from dwd_jira_data"
    cs.execute(sql)
    cs.executemany('insert into dwd_jira_data values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', questions)
    conn.commit()
    cs.close()
    conn.close()

你可能感兴趣的:(jira,数据库,python,mysql)