ciNotify.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import sys
  2. import paho.mqtt.client as mqtt
  3. import paho.mqtt.publish as publish
  4. from git import Repo
  5. import urllib
  6. import requests
  7. import http.cookiejar
  8. import json
  9. #$MQTTADDR $MQTTPORT $MQTTTOPIC "https://xxxx" "air101" DD_APPKEY DD_APPSECRET DD_NOTIFY_LIST DD_API_TOKEN DD_API_SEND
  10. # 1 2 3 4 5 6 7 8 9 10
  11. repo = Repo("../")
  12. #暂时停用
  13. ############# MQTT ###############
  14. # client = mqtt.Client()
  15. # try:
  16. # #服务器请自行修改,需要传入参数
  17. # client.connect(sys.argv[1], int(sys.argv[2]), 60)
  18. # #topic请根据需要自行修改,需要传入参数
  19. # info = sys.argv[4]+"\r\n"+str(repo.head.commit.author)+"-"+str(repo.head.commit.message)
  20. # if len(sys.argv) >= 6:
  21. # repo = Repo("../../"+sys.argv[5])
  22. # info = info+"\r\n子仓库"+sys.argv[5]+"最后提交:\r\n"+str(repo.head.commit.author)+"-"+str(repo.head.commit.message)
  23. # pub = client.publish(sys.argv[3],info)
  24. # pub.wait_for_publish()
  25. # client.disconnect()
  26. # print("sent")
  27. # except Exception as e:
  28. # print(e)
  29. ###############钉钉提醒######################
  30. dd_appkey = sys.argv[6]
  31. dd_appsecret = sys.argv[7]
  32. dd_list = sys.argv[8].split(",")
  33. dd_api_token = sys.argv[9]
  34. dd_api_send = sys.argv[10]
  35. try:
  36. headers = {'user-agent': '114514'}
  37. token = requests.post(dd_api_token,json={"appKey":dd_appkey,"appSecret":dd_appsecret},headers=headers).json()["accessToken"]
  38. #发消息
  39. headers = {'user-agent': '114514', 'x-acs-dingtalk-access-token': token}
  40. r = requests.post(dd_api_send,json={
  41. "robotCode":dd_appkey,
  42. "userIds":dd_list,
  43. "msgKey" : "sampleLink",
  44. "msgParam" : json.dumps({
  45. "title": sys.argv[5]+"的编译炸了",
  46. "text": "最后提交:"+str(repo.head.commit.author)+"\r\n"+str(repo.head.commit.message),
  47. "messageUrl": sys.argv[4],
  48. "picUrl": "https://www.luatos.com/img/footer-logo.png",
  49. })
  50. },headers=headers)
  51. print(r.json())
  52. except Exception as e:
  53. print(e)