飞书机器人通知项目变更信息

190 阅读2分钟

前言

每次我们改了bug,发了版之后 测试同学不知道本次更新修改了哪些bug,导致配合起来有点费劲,这里我们将大家的commit信息都展示给测试同学看 自然就明白了。前提是每次修改代码后,我们的commit内容要符合规范。 这里我使用pythongitLab进行配合开发

获取commit信息

先装个包 pip install isodate

import isodate

通过调用gitLabapi获取对应信息 参照接口文档链接Commits API | GitLab

def get_commit_list(url, header):
    n = datetime.now()
    data = {
        'since': to_iso_time(datetime(n.year, n.month, n.day - 1)),#获取的数据是在这个时间之后的
        "ref_name": "需要获取commit信息的分支名称"
    }
    print(data)
    res = requests.get(url, headers=header, params=data)
    return res.json()

如果你请求成功可以看到如下返回格式

{
        "id": "",
        "short_id": "",
        "created_at": "2022-03-17T18:04:41.000+08:00",
        "parent_ids": [
            ""
        ],
        "title": "feat: 修改角色bug",
        "message": "feat: 修改角色bug\n",
        "author_name": "",
        "author_email": "",
        "authored_date": "2022-03-17T18:04:41.000+08:00",
        "committer_name": "",
        "committer_email": "",
        "committed_date": "2022-03-17T18:04:41.000+08:00",
        "web_url": ""
    },

接下来根据自己的需求按照飞书机器人开发文档 处理响应的数据 (富文本-开发文档 - 飞书开放平台 (feishu.cn))

我的处理方式

#将iso时间转为普通时间
def to_common_time(val):
    return datetime.strptime(val, "%Y-%m-%dT%H:%M:%S.%f%z").strftime('%Y-%m-%d')

def handle_commit_data(data):
    res = []
    for item in data:
        if 'Merge' in item['title']:
            continue
        res.append({
            'title': item['title'],
            'description': item['message'],
            "time": to_common_time(item['committed_date']),
            "username": item['author_name']
        })
    return res

创建机器人信息

def count_day_label(val: int):
    c = datetime.now().day - val
    if c == 0:
        return '今日'
    elif c == 1:
        return '昨日'
    else:
        return '更久以前'

def create_commit_msg(merge_list):
    res = []

    for item in merge_list:
        day_label = count_day_label(datetime.strptime(item['time'], '%Y-%m-%d').day)
        title = {
            "tag": "markdown",
            "content": f'''
    **{item['title']}** <text_tag color='indigo'>{item['username']}</text_tag>  
    <text_tag color='lime'>{item['time']}</text_tag>
    <text_tag color='lime'>{day_label}</text_tag>
'''
        }
        res.append(title)
        if item['description'] and len(item['description']) > 0:
            t = {
                "tag": "markdown",
                "content": f"{item['description']}"
            }
            res.append(t)
    return res

然后就可以将上面返回的数据放入以下函数中

def send_group(msg):
    url = '机器人调用的url'
    res = requests.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({
        "msg_type": "interactive",
        "card": {
            "schema": "2.0",
            "body": {
                "elements": msg
            }
        }
    }))

看看效果吧

image.png

整体调用流程

commit_list = get_commit_list(f'{host}/{commit_path}', header_data)
commit_data = handle_commit_data(commit_list)
commit_msg = create_commit_msg(commit_data)
print(commit_msg)
send_group(commit_msg)