jira小工具与邮件发送

代码示例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# coding:utf8

import requests
import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header


class JiraTest(object):

    # def __init__(self):
    #     pass


    # 获取需求列表
    def query_wf_project(param):
        payload={
        'jql': 'issuetype in (业务需求, 技术需求) AND 需求类型 = 项目 AND status not in (发布上线, Done, 不解决, 废弃) ORDER BY createdDate ASC',
        'startAt': 0, 'expand': 'changelog'}
        apiUrl = 'http://wf.vdian.net/rest/api/2/search'
        r = requests.get(apiUrl, params=payload)
        issues = r.json()["issues"]
        total = r.json()["total"]
        print(r.url)
        return issues

    def publish_delay(publistTime, testTime, created):
        desc = ""
        if testTime:
            planTestTime = datetime.datetime.strptime(testTime, '%Y-%m-%d')
            nowTimeStamp = datetime.datetime.now()
            # 提测延期部分需要优化,跟状态流转记录比较
            if nowTimeStamp > planTestTime:
                desc = "提测延期" + str((nowTimeStamp - planTestTime).days) + "天;"
            else:
                desc = ""
        else:
            desc = "计划提测时间未填写;"
        if publistTime:
            planTimeStamp = datetime.datetime.strptime(publistTime, '%Y-%m-%d')   #由字符串转为日期型的函数为:datetime.datetime.strptime()
            nowTimeStamp = datetime.datetime.now()
            # print(planTimeStamp)
            # print(nowTimeStamp)
            if nowTimeStamp > planTimeStamp:
                desc = desc + "发布延期" + str((nowTimeStamp - planTimeStamp).days) + "天;"
            else:
                desc = desc + ""
        else:
            desc = desc + "计划发布时间未填写;"
        ##项目时间超过2个月
        nowTimeStamp = datetime.datetime.now()
        createdStamp = datetime.datetime.strptime(created, '%Y-%m-%d')
        if (nowTimeStamp - createdStamp).days > 60:
            desc = desc + "项目创建时间超过2个月;"
        if desc.strip() == "":
            return "项目进度正常"
        return desc

    # 发送邮件
    def send_notify(email, key, title, res, subject):
        # 第三方 SMTP 服务
        mail_host = 'smtp.126.com'  # 设置服务器
        mail_user = '[email protected]'  # 用户名
        mail_pass ='126password'  # 126邮箱对应的口令
        sender = '[email protected]'
        receivers = '[email protected]'  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
        mail_msg=''
        mail_msg = mail_msg.replace("TEST-412", key)
        mail_msg = mail_msg.replace("标题", title)
        mail_msg = mail_msg.replace("项目描述", res)
        message = MIMEText(mail_msg, 'html', 'utf-8')

        message['From'] = '[email protected]'
        message['to'] = '[email protected]'
        message['Subject'] = Header(subject, 'utf-8')

        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host)  # 25 为 SMTP 端口号
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        smtpObj.quit()
        print('邮件发送成功')

    issues=query_wf_project(0)
    for issue in issues:

        #url
        url = "http://wf.vdian.net/browse/"+(issue["key"])
        #标题
        title = (issue["fields"]["summary"])
        #报告人
        reporter = (issue["fields"]["reporter"]["displayName"])
        #创建日期
        created = issue["fields"]["created"][0:10]
        #产品
        project = (issue["fields"]["project"]["name"])
        #产品线
        projectCategory = (issue["fields"]["project"]["projectCategory"]["name"])
        #计划提测时间
        testPlanTime = (issue["fields"]["customfield_10200"])
        #计划上线时间
        publistPlanTime = (issue["fields"]["customfield_10109"])
        #进度
        status = (issue["fields"]["status"]["name"])
        #经办人
        assignee = (issue["fields"]["assignee"]["displayName"])
        #测试人员
        if issue["fields"]["customfield_10108"]:
            tester = (issue["fields"]["customfield_10108"][0]["displayName"])
        else:
            tester = ("无测试人员")
        delay = publish_delay(publistPlanTime, testPlanTime, created)
        print('延期信息打印',delay)
    send_notify('[email protected]', 'WDAPP-8503', 'title', 'desc', 'subject')

猜你喜欢

转载自www.cnblogs.com/niuniumother/p/11287767.html