# coding:utf-8
import urllib2
import json
import sys
import time
import os
class dingHook(object):
def __init__(self, pro):
self.user = "tech"
self.pro = pro
self.url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"
def request(self, url, method, data=None, head={}):
request = urllib2.Request(url=url, headers=head)
request.get_method = lambda: method
httpRes = urllib2.urlopen(request, data)
content = httpRes.read()
httpRes.close()
return content
def post_start(self):
data = {}
data["msgtype"] = "text"
data["text"] = {}
data["text"]["content"] = "FROM 67 jenkins:开始执行【%s】自动化......" % self.pro
data = json.dumps(data)
head = {"Content-Type": "application/json"}
content = self.request(self.url, "POST", data, head)
return content
def post_link(self, text, title, picurl="", messageurl=""):
data = {}
data["msgtype"] = "link"
data["link"] = {}
data["link"]["text"] = text
data["link"]["title"] = title
data["link"]["picUrl"] = picurl
data["link"]["messageUrl"] = messageurl
data = json.dumps(data)
head = {"Content-Type": "application/json"}
content = self.request(self.url, "POST", data, head)
print content
return content
def post_result(self):
i = 0
result = "FAILURE"
# 获取当前jenkins构建版本号
get_buildnum = "cat /home/%s/.jenkins/jobs/%s/nextBuildNumber" % (
self.user, self.pro)
output = os.popen(get_buildnum)
buildNumber = str(int(output.read().strip()) - 1)
# 获取jenkins自动化执行结果,判断是否执行结束
get_buildresult = "grep Finished /home/%s/.jenkins/jobs/%s/builds/%s/log" % (
self.user, self.pro, buildNumber)
cat_version = "cat /home/%s/local/testcase/%s_report/version" % (self.user,self.pro)
# 获取结果报告中的返回结果
get_RF_result = "grep 'All Tests' /home/%s/.jenkins/jobs/%s/builds/%s/robot-plugin/output.xml" % (
self.user, self.pro, buildNumber)
# jenkins自动化结果报告地址
messageurl = "http://10.16.29.167:18080/jenkins/job/%s/%s/robot/report/report.html" % (
self.pro, buildNumber)
# 钉钉结果报告消息图片
picurl = "xxxxx.jpg"
# 钉钉结果报告标题
title = "【%s】自动化执行结果" % self.pro
while i <= 1000:
i = i + 1
try:
output_result = os.popen(get_buildresult)
build_result = output_result.read()
print build_result
if "No such file or directory" in build_result:
time.sleep(10)
continue
if "Finished" in build_result:
output_rf_result = os.popen(get_RF_result)
rf_result = output_rf_result.read().strip()
case_count_list = rf_result.split('"')
fail_case = int(case_count_list[1])
suc_case = int(case_count_list[3])
if fail_case == 0:
result = "SUCCESS-成功:%d-失败:%d" % (suc_case,fail_case)
else:
result = "FAILURE-成功:%d-失败:%d" % (suc_case,fail_case)
output_version = os.popen(cat_version)
version = output_version.read()
if "zip" in version:
result = "【"+result+"】【"+version+"】"
content = self.post_link(
text=result, title=title, messageurl=messageurl,picurl=picurl)
return content
time.sleep(10)
except Exception, e:
print e
time.sleep(10)
continue
def ding_hook(self, itype="start"):
print itype
if itype == "start":
self.post_start()
elif itype == "end":
self.post_result()
if __name__ == '__main__':
dh = dingHook(sys.argv[1])
print dh.ding_hook(sys.argv[2])