Python 开发一个飞书GitLab提交机器人

程序效果展示

每一个review 按钮 对应一个提交详情 文件差异 点击即可查看
一个push中的多次提交会合并到一个panel里
在这里插入图片描述

EXE配置:

编辑 bin 目录下 conf.json文件
在这里插入图片描述

config.json 预览

在这里插入图片描述

{
    
    
    
    "WEB_HOOK":"https://open.feishu.cn/open-apis/bot/v2/hook/2c26ff64-6a70-4b09-96f2-aa036cfe8832",
    "SECRET":"G9rWIBOKysswilBqKREjoc",

    "GITLAB_HOME":"http://192.168.2.50:8090/",
    "GITLAB_TOKEN":"WBUrYRUySkWV2qj418U5",

    "LISTEN_PROJ":"Makeover-Client"

}

第一步 在飞书群组添加机器人

在这里插入图片描述
在这里插入图片描述

第二步 Web_Hook 和 签名

在这里插入图片描述
在这里插入图片描述

第三步 仓库Home地址配置

进入到你的项目仓库里 将网页地址中 ip+端口号 的copy下来

例如 http://192.168.2.50:8090/31/helloworld-client

那么 Home地址就是 http://192.168.2.50:8090/
在这里插入图片描述
在这里插入图片描述

仓库权限Token配置

在这里插入图片描述
点击生成后 上方后出现Token码 Copy到机器人 conf.json配置中
在这里插入图片描述
在这里插入图片描述

项目名称配置

在这里插入图片描述
在这里插入图片描述

运行exe 大功告成

https://download.csdn.net/download/qq_39162566/87447917
在这里插入图片描述

Exe下载

https://download.csdn.net/download/qq_39162566/87447917
运行环境 python 3.x

发送飞书消息源码

import base64
import hmac
import json
import os
import time
from hashlib import sha256
import requests

envDict = json.load(open("./bin/conf.json"))


def SendMsg( text,user="",proj="",branch="",web_urls = [] ):
    timestamp = str(round(time.time()))
    secret = envDict["SECRET"]
    url = envDict["WEB_HOOK"]

    key = f'{
      
      timestamp}\n{
      
      secret}'
    key_enc = key.encode('utf-8')
    msg = ""
    msg_enc = msg.encode('utf-8')
    hmac_code = hmac.new(key_enc, msg_enc, digestmod=sha256).digest()
    sign = base64.b64encode(hmac_code).decode('utf-8')
    
    # text content
    # payload_message = {
    
    
    #     "timestamp": timestamp,
    #     "sign": sign,
    #     "msg_type": "text",
    #     "content": {
    
    
    #         "text": text
    #     }
    # }

    # review window
    patchset_msg = '**Subject :** ' + text + '\n' +\
               '**Owner  :** ' + user + '\n' +\
               '**Project :** ' + proj + '\n' +\
               '**Branch :** ' + branch

    payload_message = {
    
    
    "msg_type": "interactive",
    "timestamp": timestamp,
    "sign": sign,
    "card": {
    
    
        "config": {
    
    
            "wide_screen_mode": True
        },
        "elements": [
            {
    
    
                "tag": "div",
                "text": {
    
    
                    "content": patchset_msg,
                    "tag": "lark_md"
                }
            },
            {
    
    
                "tag": "hr"
            },
            {
    
    
                "actions": [
                    {
    
    
                        "tag": "button",
                        "text": {
    
    
                            "content": "Review " + web_urls[0][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[0][0]
                    }
                ],
                "tag": "action"
            }
        ],
        "header": {
    
    
            "template": "blue",
            "title": {
    
    
                "content": "GitLab Commit",
                "tag": "plain_text"
                }
            }
        }
    }

    #补充额外的提交review按钮
    if len( web_urls ) > 1:
        actions = payload_message['card']['elements'][2]['actions']
        for i in range(1,len(web_urls),1):
            action = {
    
    
                        "tag": "button",
                        "text": {
    
    
                            "content":  "Review " + web_urls[i][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[i][0]
                    }
            actions.append(action)
    headers = {
    
    
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
    print(response.text)


飞书消息 面板设计

在这里插入图片描述
每一个review 按钮 对应一个提交详情 文件差异 点击即可查看

 patchset_msg = '**Subject :** ' + text + '\n' +\
               '**Owner  :** ' + user + '\n' +\
               '**Project :** ' + proj + '\n' +\
               '**Branch :** ' + branch

    payload_message = {
    
    
    "msg_type": "interactive",
    "timestamp": timestamp,
    "sign": sign,
    "card": {
    
    
        "config": {
    
    
            "wide_screen_mode": True
        },
        "elements": [
            {
    
    
                "tag": "div",
                "text": {
    
    
                    "content": patchset_msg,
                    "tag": "lark_md"
                }
            },
            {
    
    
                "tag": "hr"
            },
            {
    
    
                "actions": [
                    {
    
    
                        "tag": "button",
                        "text": {
    
    
                            "content": "Review " + web_urls[0][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[0][0]
                    }
                ],
                "tag": "action"
            }
        ],
        "header": {
    
    
            "template": "blue",
            "title": {
    
    
                "content": "GitLab Commit",
                "tag": "plain_text"
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_39162566/article/details/129009429