diff --git a/README.md b/README.md index 58c8f4c..aebd78a 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,16 @@ python3.7 -m pip install --upgrade pip pip install -r requirements.txt ``` -#### 如果引入其他新的依赖,可以执行冻结第三方库,就是将所有第三方库及版本号保存到requirements.txt文本文件中 +* 如果引入其他新的依赖,可以执行冻结第三方库,就是将所有第三方库及版本号保存到requirements.txt文本文件中 ```shell pip freeze > requirements.txt ``` +* 如果pip不起作用,可以从pypi上下载最新的源码包(https://pypi.python.org/pypi/)进行安装: +```shell +python setup.py install +``` + + # 项目使用说明 ## 1.修改配置 @@ -66,6 +72,11 @@ pip freeze > requirements.txt "feishu": { "webhook": "https://open.feishu.cn/open-apis/bot/v2/hook/xxxx", 飞书机器人webhook "secret":"xxxx" 飞书机器人秘钥 + }, + "timing_task":{ + "week":"1-5", 每周一至五 + "hour":9, 上午9点30分执行一次 + "minute":30 } } ``` @@ -93,10 +104,14 @@ pip freeze > requirements.txt ## 单元测试 ```shell -cd src/test pytest ``` +## 定时任务 +``` +python main.py +``` + # 飞书机器人 diff --git a/config/config.json b/config/config.json index 5eae75e..0108928 100644 --- a/config/config.json +++ b/config/config.json @@ -1,12 +1,17 @@ { - "jira": { - "url": "https://jira.xxxx.com", - "url_browse": "https://jira.xxxx.com/browse/", - "username": "", - "password": "" - }, - "feishu": { - "webhook": "https://open.feishu.cn/open-apis/bot/v2/hook/xxxx", - "secret":"xxxx" - } -} \ No newline at end of file + "jira": { + "url": "https://jira.xxxx.com", + "url_browse": "https://jira.xxxx.com/browse/", + "username": "你的jira账号", + "password": "你的jira密码" + }, + "feishu": { + "webhook": "https://open.feishu.cn/open-apis/bot/v2/hook/xxxx", + "secret": "xxxx" + }, + "timing_task": { + "week": "1-5", + "hour": 9, + "minute": 30 + } +} diff --git a/log/log2022-2209 23:20:23.txt b/log/log2022-2209 23:20:23.txt deleted file mode 100644 index b034a33..0000000 --- a/log/log2022-2209 23:20:23.txt +++ /dev/null @@ -1,5 +0,0 @@ -2022-03-09 23:20:32,778 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:32,779 - src.api.log - INFO - 姓名:李银池 -2022-03-09 23:20:32,780 - src.api.log - INFO - open bug:10 -2022-03-09 23:20:32,781 - src.api.log - INFO - resolved bug:0 -2022-03-09 23:20:33,094 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} diff --git a/log/log2022-2209 23:20:23.txt.1 b/log/log2022-2209 23:20:23.txt.1 deleted file mode 100644 index f300d7d..0000000 --- a/log/log2022-2209 23:20:23.txt.1 +++ /dev/null @@ -1,13 +0,0 @@ -2022-03-09 23:20:30,906 - src.api.log - INFO - resolved bug:13 -2022-03-09 23:20:31,386 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:31,387 - src.api.log - INFO - 姓名:王俊奇 -2022-03-09 23:20:31,387 - src.api.log - INFO - open bug:2 -2022-03-09 23:20:31,387 - src.api.log - INFO - resolved bug:0 -2022-03-09 23:20:31,832 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:31,832 - src.api.log - INFO - 姓名:许冰艳 -2022-03-09 23:20:31,832 - src.api.log - INFO - open bug:11 -2022-03-09 23:20:31,832 - src.api.log - INFO - resolved bug:1 -2022-03-09 23:20:32,441 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:32,462 - src.api.log - INFO - 姓名:康丽婷 -2022-03-09 23:20:32,464 - src.api.log - INFO - open bug:6 -2022-03-09 23:20:32,464 - src.api.log - INFO - resolved bug:4 diff --git a/log/log2022-2209 23:20:23.txt.2 b/log/log2022-2209 23:20:23.txt.2 deleted file mode 100644 index a504fbc..0000000 --- a/log/log2022-2209 23:20:23.txt.2 +++ /dev/null @@ -1,14 +0,0 @@ -2022-03-09 23:20:29,549 - src.api.log - INFO - 姓名:王美玲 -2022-03-09 23:20:29,551 - src.api.log - INFO - open bug:22 -2022-03-09 23:20:29,551 - src.api.log - INFO - resolved bug:12 -2022-03-09 23:20:29,880 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:29,881 - src.api.log - INFO - 姓名:陈耀杰 -2022-03-09 23:20:29,881 - src.api.log - INFO - open bug:24 -2022-03-09 23:20:29,881 - src.api.log - INFO - resolved bug:-1 -2022-03-09 23:20:30,497 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:30,497 - src.api.log - INFO - 姓名:练春木 -2022-03-09 23:20:30,497 - src.api.log - INFO - open bug:21 -2022-03-09 23:20:30,497 - src.api.log - INFO - resolved bug:7 -2022-03-09 23:20:30,905 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} -2022-03-09 23:20:30,906 - src.api.log - INFO - 姓名:林志祥 -2022-03-09 23:20:30,906 - src.api.log - INFO - open bug:33 diff --git a/log/log2022-2211 16:11:19.txt b/log/log2022-2211 16:11:19.txt new file mode 100644 index 0000000..9fd26b3 --- /dev/null +++ b/log/log2022-2211 16:11:19.txt @@ -0,0 +1,5 @@ +2022-03-11 16:12:08,755 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:08,756 - src.api.log - INFO - 姓名:李银池 +2022-03-11 16:12:08,756 - src.api.log - INFO - open bug:10 +2022-03-11 16:12:08,756 - src.api.log - INFO - resolved bug:0 +2022-03-11 16:12:09,140 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} diff --git a/log/log2022-2211 16:11:19.txt.1 b/log/log2022-2211 16:11:19.txt.1 new file mode 100644 index 0000000..03ac3bd --- /dev/null +++ b/log/log2022-2211 16:11:19.txt.1 @@ -0,0 +1,13 @@ +2022-03-11 16:12:07,015 - src.api.log - INFO - resolved bug:11 +2022-03-11 16:12:07,417 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:07,418 - src.api.log - INFO - 姓名:王俊奇 +2022-03-11 16:12:07,418 - src.api.log - INFO - open bug:0 +2022-03-11 16:12:07,418 - src.api.log - INFO - resolved bug:0 +2022-03-11 16:12:07,832 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:07,832 - src.api.log - INFO - 姓名:许冰艳 +2022-03-11 16:12:07,832 - src.api.log - INFO - open bug:14 +2022-03-11 16:12:07,832 - src.api.log - INFO - resolved bug:2 +2022-03-11 16:12:08,218 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:08,218 - src.api.log - INFO - 姓名:康丽婷 +2022-03-11 16:12:08,219 - src.api.log - INFO - open bug:4 +2022-03-11 16:12:08,219 - src.api.log - INFO - resolved bug:2 diff --git a/log/log2022-2211 16:11:19.txt.2 b/log/log2022-2211 16:11:19.txt.2 new file mode 100644 index 0000000..e2d3aae --- /dev/null +++ b/log/log2022-2211 16:11:19.txt.2 @@ -0,0 +1,14 @@ +2022-03-11 16:12:05,655 - src.api.log - INFO - 姓名:王美玲 +2022-03-11 16:12:05,656 - src.api.log - INFO - open bug:23 +2022-03-11 16:12:05,656 - src.api.log - INFO - resolved bug:16 +2022-03-11 16:12:06,055 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:06,056 - src.api.log - INFO - 姓名:陈耀杰 +2022-03-11 16:12:06,056 - src.api.log - INFO - open bug:19 +2022-03-11 16:12:06,056 - src.api.log - INFO - resolved bug:0 +2022-03-11 16:12:06,498 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:06,499 - src.api.log - INFO - 姓名:练春木 +2022-03-11 16:12:06,499 - src.api.log - INFO - open bug:21 +2022-03-11 16:12:06,499 - src.api.log - INFO - resolved bug:7 +2022-03-11 16:12:07,014 - src.api.log - INFO - {'Extra': None, 'StatusCode': 0, 'StatusMessage': 'success'} +2022-03-11 16:12:07,014 - src.api.log - INFO - 姓名:林志祥 +2022-03-11 16:12:07,015 - src.api.log - INFO - open bug:33 diff --git a/main.py b/main.py new file mode 100644 index 0000000..36c919b --- /dev/null +++ b/main.py @@ -0,0 +1,174 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- +from datetime import datetime +from apscheduler.schedulers.blocking import BlockingScheduler +from src.api.feishu_sign import sign +import sys +import os +from util.get_json import config +from asyncio.log import logger +import datetime +from jira import JIRA +from datetime import datetime as dt +import time +from src.api.log import log # 封装日志引入 +from src.api.feishu_robot_push import feishu_robot # 飞书机器人引入 +from src.api.feishu_sign import sign # 飞书签名 + +# 日志实例化 +logger = log().logger() +# 读取配置文件 +Config = config("{}/config/config.json".format(os.getcwd()) + ).get_config_from_json() +UserInfo = config("{}/config/userInfo.json".format(os.getcwd()) + ).get_config_from_json() +# jira初始化 +jira = JIRA(auth=(Config["jira"]["username"], Config["jira"] + ["password"]), options={'server': Config["jira"]["url"]}) +# 飞书签名 +timestamp = str(round(time.time())) +secret = Config["feishu"]["secret"] +sign = sign(timestamp, secret).gen_sign() + + +""" + @Time :2022/02/11 + @Author :liyinchi + @File :main.py + @Ddescription: 定时任务 +""" +def job(): + # 存放jira bug list + result = [] + # 读取人员名单 + workers = UserInfo["userInfo"] + # 初始化迭代 + i = 0 + k = 0 + # 遍历人员名单 + for worker in workers: + # 注意:如果不加maxResults=-1参数,则实际总数大于50时只能查出50条数据。 + open_issues = jira.search_issues( + 'status = 激活 AND creator in ("'+worker["username"]+'") ORDER BY priority DESC, updated DESC', maxResults=-1) + # 日志输出 + logger.info('【{}】{}'.format(worker["username"], open_issues)) + # 放入第一层数据 + result.append([ + {"username": worker["username"]}, + {"name": worker["name"]}, + {"open": []}, # 激活状态bug list + {"resolved": []} # 已解决状态bug list + ]) + """ + result的数据格式: + [[{"username":"zhanggsan"},{"name":"张三"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}], + [{"username":"lisi"},{"name":"李四"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}], + [{"username":"wangwu"},{"name":"王五"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}]] + """ + # 放入第一层数据open节点中 + for index in range(len(open_issues)): + print("result[{}]][2]".format(i), result[i][2]) + (result[i][2])['open'].append({ + "key": open_issues[index].key, + "id": open_issues[index].id, + "url": Config["jira"]["url_browse"]+open_issues[index].key, + }) + resolved_issues = jira.search_issues( + 'status = 已解决 AND creator in ("'+worker["username"]+'") ORDER BY priority DESC, updated DESC', maxResults=-1) + # 日志输出 + logger.info('【{}】{}'.format(worker["username"], resolved_issues)) + # 放入第一层数据resolved节点中 + for index in range(len(resolved_issues)): + print("result[{}][3]".format(i), result[i][3]) + (result[i][3])['resolved'].append({ + "key": resolved_issues[index].key, + "id": resolved_issues[index].id, + "url": Config["jira"]["url_browse"]+resolved_issues[index].key, + }) + i += 1 + logger.info('result' + str(result)) + + """ + result的数据格式: + [[{"username":"zhanggsan"},{"name":"张三"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}], + [{"username":"lisi"},{"name":"李四"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}], + [{"username":"王五"},{"name":"王五"},{"open":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}],"resolved":[{"key":xxx,"id":xxx},{"key":xxx,"id":xxx}]}]] + """ + # 遍历result 依次调用飞书机器人发送结果 + i = 0 + for j in result: + # + # logger.info("j数据类型:"+ str(type(j))) # + # 姓名 + logger.info("姓名:" + j[1]['name']) + logger.info("open bug:" + str(len(j[2]["open"])-1)) + logger.info("resolved bug:" + str(len(j[3]["resolved"])-1)) + + message_body = { + "timestamp": timestamp, + "sign": sign, + "msg_type": "post", # 消息类型 包括:text、post、image、file、audio、media、sticker、interactive、share_chat、share_user等,类型定义请参考发送消息content说明:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/create_json + "content": + { + "post": { + "zh_cn": { + "title": "JIRA bug 日常提醒【{}】【{}】".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), j[1]['name']), + "content": [ + [{ + "tag": "text", + "text": "未处理:{}".format(len(j[2]["open"])-1), + }, + { + "tag": "a", + "href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("激活", j[0]['username']), + "text": "点击查看" + }], + [{ + "tag": "text", + "text": "已解决:{}".format(len(j[3]["resolved"])-1), + }, + { + "tag": "a", + "href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("已解决", j[0]['username']), + "text": "点击查看" + }], + # [{ + # "tag": "text", + # "text": "已关闭:{}".format(len(j)-1), + # }, + # { + # "tag": "a", + # "href": "https://jira.homeking365.com/browse/S5A-1924?filter=10818&jql=status = {} AND assignee in ({}) ORDER BY priority DESC, updated DESC".format("已关闭", j[0]['username']), + # "text": "点击查看" + # }], + # [ + # { + # "tag": "at", + # "user_id": "all", + # "user_name": " 所有人 " + # } + # ], + # [{ + # "tag": "img", + # "image_key": "img_7ea74629-9191-4176-998c-2e603c9c5e8g", + # "width": 100, + # "height": 100 + # }] + ] + }, + }} + } + i += 1 + # 发送飞书机器人 + response_message = feishu_robot( + Config["feishu"]["webhook"], message_body).push() + logger.info(response_message) + + +# BlockingScheduler +scheduler = BlockingScheduler() +# 创建定时器 +scheduler.add_job(job, 'cron', day_of_week=Config["timing_task"]["week"], hour=Config["timing_task"] + ["hour"], minute=Config["timing_task"]["minute"]) # 每周1-5 上午9点30分执行 +# 启动 +scheduler.start() diff --git a/requirements.txt b/requirements.txt index 6869d52..da5e195 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,4 +19,5 @@ websockets==10.1 XlsxWriter==3.0.2 xlwt==1.3.0 jira==3.1.1 -bunch==1.0.1 \ No newline at end of file +bunch==1.0.1 +APScheduler==3.9.1 \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py index e69de29..f66216e 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- + + +if __name__=="__main__": + print("主程序入口") +else : + print("初始化/非主程/被引入或单元测试") + \ No newline at end of file diff --git a/src/api/__init__.py b/src/api/__init__.py index e69de29..f66216e 100644 --- a/src/api/__init__.py +++ b/src/api/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- + + +if __name__=="__main__": + print("主程序入口") +else : + print("初始化/非主程/被引入或单元测试") + \ No newline at end of file diff --git a/src/api/feishu_robot_push.py b/src/api/feishu_robot_push.py index cd892c2..a893999 100644 --- a/src/api/feishu_robot_push.py +++ b/src/api/feishu_robot_push.py @@ -8,8 +8,8 @@ import logging #日志引入 ''' 飞书机器人发送消息 - :param 飞书webhook地址 - :param 发送消息内容 + :param web_hook 飞书webhook地址 + :param message_body 发送消息内容 ''' class feishu_robot(): def __init__(self,web_hook,message_body,payload_message=''): diff --git a/src/api/feishu_sign.py b/src/api/feishu_sign.py index 25a63f2..fbf1be3 100644 --- a/src/api/feishu_sign.py +++ b/src/api/feishu_sign.py @@ -4,6 +4,12 @@ import base64 import hmac +""" + @Time :2022/02/11 + @Author :liyinchi + @File :main.py + @Ddescription: 飞书机器人签名 +""" class sign: def __init__(self,timestamp, secret): self.timestamp=timestamp diff --git a/src/api/jira_client_api.py b/src/api/jira_client_api.py new file mode 100644 index 0000000..0cb5ca9 --- /dev/null +++ b/src/api/jira_client_api.py @@ -0,0 +1,191 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- +from jira import JIRA +from util.get_json import config +# 读取配置文件 +Config = config("../../config/config.json").get_config_from_json() +UserInfo = config("../../config/userInfo.json").get_config_from_json() +# jira初始化 +url = Config["jira"]["url"] +auth = (Config["jira"]["username"], Config["jira"]["password"]) # jira登录账号和密码 +jira = JIRA(auth=auth, options={'server': url}) + + +""" + @Time :2022/02/11 + @Author :liyinchi + @File :jira_get.py + @Version:1.0 +""" + + +class jira_get(): + def __init__(self): + pass + """ + [jira所有项目名称] + :param none + :return: list + """ + def get_projects(seft): + projects = jira.projects() + print(projects) + return projects + """ + [获取issue信息] + :param 'JRA-1330' + :return: list + """ + def get_issue(issue_id): + issue = jira.issue(issue_id) + return issue + """ + [issue添加关注者] + :param issue 例如:'JRA-1330' + :param username 例如:'liyc' + :return: list + """ + def add_watcher_issue(issue, username): + jira.add_watcher(issue, username) + + """ + [获取issue所有关注者] + :param issue 例如:'JRA-1330' + :return: list + """ + def get_watcher_issue(issue): + print(jira.watchers(issue)) + return jira.watchers(issue) + """ + [获取issue所有评论] + :param issue 例如:'JRA-1330' + :return: list + """ + def get_comment_all_issue(issue): + print(jira.comments(issue)) + return jira.comments(issue) + """ + [issue获取某条评论] + :param issue 例如:'JRA-1330' + :param comment_id 例如:'10234' + :return: list + """ + def get_comment_issue(issue, comment_id): + return jira.comment(issue, comment_id) + """ + [issue新增评论] + :param issue 例如:'JRA-1330' + :param comment_id 例如:'10234' + :return: list + """ + def add_comment_issue(issue, text): + jira.add_comment(issue, 'new comment') + """ + [issu更新评论] + :param issue 例如:'JRA-1330' + :param comment_id 例如:'10234' + :return: list + """ + def update_comment_issue(issue, comment_id, text): + comment = jira.comment(issue, comment_id) + return comment.update(body=text) # 更新评论 + """ + [issue删除评论] + :param issue 例如:'JRA-1330' + :param comment_id 例如:'10234' + :return: list + """ + def delete_comment_issue(issue, comment_id): # comment_id:'10234' + comment = jira.comment(issue, comment_id) # 某条评论 + comment.delete() # 删除该评论 + """ + [获取issue附件] + :param issue 例如:'JRA-1330' + :return: list + """ + def get_attachment_issue(issue): + print(issue.fields.attachment) # 问题附件 + return issue.fields.attachment + + """ + [issue添加附件] + :param issue 例如:'JRA-1330' + :param attachment 例如:'/some/path/attachment.txt' + :return: list + """ + def add_attachment_issue(issue, attachment): + jira.add_attachment( + issue, attachment=attachment) # 添加附件 + + """ + 创建issue + :param issue_dict 例如:issue_dict = { + 'project': {'id': 123}, + 'summary': 'New issue from jira-python', + 'description': 'Look into this one', + 'issuetype': {'name': 'Bug'}, + } + :return: none + """ + def add_issue(issue_dict): + jira.create_issue(fields=issue_dict) + """ + [批量创建issue] + :param issue_dict 例如:issue_list = [ + { + 'project': {'id': 123}, + 'summary': 'First issue of many', + 'description': 'Look into this one', + 'issuetype': {'name': 'Bug'}, + }, + { + 'project': {'key': 'FOO'}, + 'summary': 'Second issue', + 'description': 'Another one', + 'issuetype': {'name': 'Bug'}, + }, + { + 'project': {'name': 'Bar'}, + 'summary': 'Last issue', + 'description': 'Final issue of batch.', + 'issuetype': {'name': 'Bug'}, + }] + :return: none + """ + def add_issues(issue_list): + jira.create_issues(field_list=issue_list) + # 分配问题 + def add_issues(issue,newassignee): + jira.assign_issue(issue, newassignee) + + + # 转换问题 + # jira.transition_issue(issue, '5', assignee={'name': 'pm_user'}, resolution={'id': '3'}) + """ + [搜索issue] + Jira的搜索非常强大,并配有一套专门的搜索语言,称为JQL(Jira Query Language),Jira的Python库便是基于JQL语法进行搜索的,返回的是搜索到的问题列表。 + 默认最大结果数未1000,可以通过maxResults参数配置,该参数为-1时不限制数量,返回所有搜索结果。 + :param jql jirajql语句 + :return list + """ + def search_issue(jql): + return jira.search_issues(jql, maxResults=-1) + +''' + [创建/分配/转换问题] + jira.create_issue(): 创建问题 + jira.create_issues(): 批量创建问题 + jira.assign_issue(): 分配问题 + jira.transitions(): 获取问题的工作流 + jira.transition_issue(): 转换问题 + [关注者/评论/附件] + jira.watchers(): 问题的关注者 + jira.add_watcher(): 添加关注者 + jira.remove_watcher(): 移除关注者 + jira.comments(): 问题的所有评论 + jira.comment(): 某条评论 + jira.add_comment():添加评论 + comment.update()/delete(): 更新/删除评论 + jira.add_attachment(): 添加附件 + +''' \ No newline at end of file diff --git a/src/api/jira_create_issue.py b/src/api/jira_create_issue.py deleted file mode 100644 index b5a4388..0000000 --- a/src/api/jira_create_issue.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: UTF-8 -*- -from jira import JIRA - -url = 'http://jira.***.com/' - -auth = ("username", "password")# jira登录账号和密码 - -jira = JIRA(auth=auth, options={'server': url}) - -''' -创建/分配/转换问题# -jira.create_issue(): 创建问题 -jira.create_issues(): 批量创建问题 -jira.assign_issue(): 分配问题 -jira.transitions(): 获取问题的工作流 -jira.transition_issue(): 转换问题 -''' - -# 创建问题 -issue_dict = { - 'project': {'id': 123}, - 'summary': 'New issue from jira-python', - 'description': 'Look into this one', - 'issuetype': {'name': 'Bug'}, -} -new_issue = jira.create_issue(fields=issue_dict) - -# 批量创建问题 -issue_list = [ -{ - 'project': {'id': 123}, - 'summary': 'First issue of many', - 'description': 'Look into this one', - 'issuetype': {'name': 'Bug'}, -}, -{ - 'project': {'key': 'FOO'}, - 'summary': 'Second issue', - 'description': 'Another one', - 'issuetype': {'name': 'Bug'}, -}, -{ - 'project': {'name': 'Bar'}, - 'summary': 'Last issue', - 'description': 'Final issue of batch.', - 'issuetype': {'name': 'Bug'}, -}] -issues = jira.create_issues(field_list=issue_list) - -# 分配问题 -# jira.assign_issue(issue, 'newassignee') - -# 转换问题 -# jira.transition_issue(issue, '5', assignee={'name': 'pm_user'}, resolution={'id': '3'}) - -# 搜索issue -# Jira的搜索非常强大,并配有一套专门的搜索语言,称为JQL(Jira Query Language),Jira的Python库便是基于JQL语法进行搜索的,返回的是搜索到的问题列表。 -# 默认最大结果数未1000,可以通过maxResults参数配置,该参数为-1时不限制数量,返回所有搜索结果。 -jira.search_issues('project=PROJ and assignee = currentUser()', maxResults=-1) \ No newline at end of file diff --git a/src/api/jira_get_issue.py b/src/api/jira_get_issue.py deleted file mode 100644 index 10896da..0000000 --- a/src/api/jira_get_issue.py +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: UTF-8 -*- -from jira import JIRA - -url = 'http://jira.***.com/' - -auth = ("username", "password")# jira登录账号和密码 - -jira = JIRA(auth=auth, options={'server': url}) -issue = jira.issue('JRA-1330') -''' -关注者/评论/附件# -jira.watchers(): 问题的关注者 -jira.add_watcher(): 添加关注者 -jira.remove_watcher(): 移除关注者 -jira.comments(): 问题的所有评论 -jira.comment(): 某条评论 -jira.add_comment():添加评论 -comment.update()/delete(): 更新/删除评论 -jira.add_attachment(): 添加附件 -''' - -print(jira.watchers(issue)) # 所有关注者 -jira.add_watcher(issue, 'username') # 添加关注者 - -print(jira.comments(issue)) # 所有评论 -comment = jira.comment(issue, '10234') # 某条评论 -jira.add_comment(issue, 'new comment') # 新增评论 -comment.update(body='update comment') # 更新评论 -comment.delete() # 删除该评论 - -print(issue.fields.attachment) # 问题附件 -jira.add_attachment(issue=issue, attachment='/some/path/attachment.txt') # 添加附件 diff --git a/src/api/jira_get_projects.py b/src/api/jira_get_projects.py deleted file mode 100644 index 75f2174..0000000 --- a/src/api/jira_get_projects.py +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: UTF-8 -*- -from jira import JIRA - -url = 'http://jira.***.com/' - -auth = ("username", "password")# jira登录账号和密码 - -jira = JIRA(auth=auth, options={'server': url}) -projects = jira.projects() -print(projects) - diff --git a/src/api/jira_jql.py b/src/api/jira_jql.py index f1eb03f..49bbdcd 100644 --- a/src/api/jira_jql.py +++ b/src/api/jira_jql.py @@ -2,9 +2,9 @@ # -*- coding: UTF-8 -*- from jira import JIRA -url = 'https://jira.homeking365.com' +url = 'https://jira.xxx.com' -auth = ("liyc", "")# jira登录账号和密码 +auth = ("账号", "密码")# jira登录账号和密码 jira = JIRA(auth=auth, options={'server': url}) diff --git a/src/api/log.py b/src/api/log.py index 406bfc3..e66c8fa 100644 --- a/src/api/log.py +++ b/src/api/log.py @@ -1,28 +1,48 @@ #!/usr/bin/python3 # -*- coding: UTF-8 -*- +import os +import sys import logging -from logging.handlers import RotatingFileHandler # 日志回滚 +from logging.handlers import RotatingFileHandler # 日志回滚 import time + +""" +@Time :2022/02/11 +@Author :liyinchi +@File :log.py +@Version:1.0 +""" + + class log: def __init__(self): pass + + """ + 控制台信息保存日志 + :param none + :return: logger + """ + def logger(self): - logger = logging.getLogger(__name__) - logger.setLevel(level = logging.INFO) - #定义一个RotatingFileHandler,最多备份3个日志文件,每个日志文件最大1K - rHandler = RotatingFileHandler("../../log/log{}.txt".format(time.strftime("%Y-%y%d %X")),maxBytes = 1*1024,backupCount = 3) - rHandler.setLevel(logging.INFO) - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - rHandler.setFormatter(formatter) - - console = logging.StreamHandler() - console.setLevel(logging.INFO) - console.setFormatter(formatter) - - logger.addHandler(rHandler) - logger.addHandler(console) - return logger - logger.info("Start print log") - logger.debug("Do something") - logger.warning("Something maybe fail.") \ No newline at end of file + logger = logging.getLogger(__name__) + logger.setLevel(level=logging.INFO) + # 定义一个RotatingFileHandler,最多备份3个日志文件,每个日志文件最大1K + rHandler = RotatingFileHandler( + os.getcwd()+"/log/log{}.txt".format(time.strftime("%Y-%y%d %X")), maxBytes=1*1024, backupCount=3) + rHandler.setLevel(logging.INFO) + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s') + rHandler.setFormatter(formatter) + + console = logging.StreamHandler() + console.setLevel(logging.INFO) + console.setFormatter(formatter) + + logger.addHandler(rHandler) + logger.addHandler(console) + return logger + logger.info("Start print log") + logger.debug("Do something") + logger.warning("Something maybe fail.") diff --git a/src/test/_test.py b/src/test/_test.py index 1643f7e..7702961 100644 --- a/src/test/_test.py +++ b/src/test/_test.py @@ -1,20 +1,18 @@ #!/usr/bin/python3 # -*- coding: UTF-8 -*- -from src.api.get_json import config +from util.get_json import config from asyncio.log import logger -from urllib import response -import pytest +import pytest,time,sys,os from jira import JIRA from datetime import datetime as dt -import time from src.api.log import log # 封装日志引入 from src.api.feishu_robot_push import feishu_robot # 飞书机器人引入 from src.api.feishu_sign import sign# 飞书签名 # 日志实例化 logger = log().logger() # 读取配置文件 -Config = config("../../config/config.json").get_config_from_json() -UserInfo = config("../../config/userInfo.json").get_config_from_json() +Config = config(os.getcwd()+"/config/config.json").get_config_from_json() +UserInfo = config(os.getcwd()+"/config/userInfo.json").get_config_from_json() # jira初始化 jira = JIRA(auth=(Config["jira"]["username"], Config["jira"] ["password"]), options={'server': Config["jira"]["url"]}) diff --git a/util/__init__.py b/util/__init__.py new file mode 100644 index 0000000..f66216e --- /dev/null +++ b/util/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- + + +if __name__=="__main__": + print("主程序入口") +else : + print("初始化/非主程/被引入或单元测试") + \ No newline at end of file diff --git a/src/api/get_json.py b/util/get_json.py similarity index 71% rename from src/api/get_json.py rename to util/get_json.py index 516f827..6e5ccce 100644 --- a/src/api/get_json.py +++ b/util/get_json.py @@ -6,13 +6,14 @@ import datetime import json from bunch import Bunch -''' - [配置读取] - 配置读取函数提供的功能和扩展,如下: - 配置文件是JSON格式; - 配置文件转换为配置类和配置字典 -''' +""" + @Time :2022/02/11 + @Author :liyinchi + @File :jira_get.py + @Version:1.0 + @Ddescription: 配置读取函数提供的功能和扩展,如下:配置文件是JSON格式;配置文件转换为配置类和配置字典 +""" class config(): def __init__(self,json_file): self.json_file=json_file