1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
__Author__ = "Actpi"
import sys import argparse from dingtalkchatbot.chatbot import DingtalkChatbot, ActionCard, CardItem
def send_notify(webhook,secret,message): bot = DingtalkChatbot(webhook,secret,pc_slide=False) bot.send_link(title=message[0],text=message[1],message_url=message[2],pic_url=message[3])
def set_actioncard(args): if args.CI_JOB_STATUS == "failed": V_IMG = "https://s2.loli.net/2022/09/28/WpklwGHgMZuoCXU.png" V_STATUS = "执行失败 " elif args.CI_JOB_STATUS == "success" and args.CI_JOB_NAME == "Merge_Branch": V_IMG = "https://s2.loli.net/2022/09/28/R8QKZqrXHUedmB5.png" V_STATUS = "执行成功 " else: sys.exit(10) V_TITLE = "任务 %s %s" %(args.CI_JOB_NAME,V_STATUS) V_TEXT = "项目: %s \n作者: %s" %(args.CI_PROJECT_TITLE,args.GITLAB_USER_NAME) message_url = args.CI_JOB_URL pic_url = V_IMG
return [V_TITLE,V_TEXT,message_url,pic_url]
def get_args(): parser = argparse.ArgumentParser(description='钉钉发送job结果消息、需要传入参数:') parser.add_argument('TOKEN', help='钉钉机器人 TOKEN') parser.add_argument('SECRET', help='钉钉机器人 SECRET') parser.add_argument('CI_JOB_STATUS', help='变量 CI_JOB_STATUS') parser.add_argument('CI_JOB_URL', help='变量 CI_JOB_URL') parser.add_argument('CI_JOB_NAME', help='变量 CI_JOB_NAME') parser.add_argument('CI_PROJECT_TITLE', help='变量 CI_PROJECT_TITLE') parser.add_argument('GITLAB_USER_NAME', help='变量 GITLAB_USER_NAME') return parser.parse_args() def main(): args = get_args() url = "https://oapi.dingtalk.com/robot/send?access_token=" webhook = url + args.TOKEN secret = args.SECRET message = set_actioncard(args) send_notify(webhook,secret,message) if __name__ == '__main__': main()
|