Commit ae29527d by 左磊磊

add:新增部分Jenkins构建后执行的代码

parent a49d8147
......@@ -20,6 +20,8 @@
<!-- aspectj Maven坐标-->
<aspectj.version>1.9.8</aspectj.version>
<jackson.version>2.6.5</jackson.version>
</properties>
......@@ -218,6 +220,18 @@
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.7</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
......
......@@ -25,7 +25,7 @@ public class PayCase extends BaseCase {
// 线上环境-RAYS获取商户号支付信息
@Test(dataProvider = "datas0")
@Description("创建支付订单")
@Description("获取商户号支付信息")
public void testPay(int id, String casename, String url, String expectValue) throws Exception {
String cookie = "checkUserInfo=userId=84; userInfo=officialAccountsId=997&channelId=1362&wechatUserId=182833248&hasSnapsis=0&userType=REGULAR&bookId=12567110&adviserId=1362&sceneId=3930381&sourceType=QRCODE&spreadType=0";
// 创建get请求
......@@ -68,7 +68,7 @@ public class PayCase extends BaseCase {
// 线上环境-小睿伴学获取支付商户号信息
@Test(dataProvider = "datas1")
@Description("创建支付订单")
@Description("获取商户号支付信息")
public void testRaysPay(int id, String casename, String url, String expectValue) throws Exception {
String cooki = "userInfo=wechatUserId=183639793&officialAccountsId=50687";
// 创建get请求
......
package com.lemon.utils;
import com.alibaba.fastjson.JSONObject;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Job;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
......@@ -12,6 +16,11 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.impl.client.BasicResponseHandler;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
/**
* @program: Lemon
* @description:
......@@ -20,9 +29,91 @@ import org.apache.http.impl.client.BasicResponseHandler;
**/
public class Robot {
public static String WechatWebHook() throws Exception {
String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fc6f4a8e-b208-47ab-9ceb-e9c7e47564c7";
String json = "{\"msgtype\":\"text\",\"text\":{\"content\":\"msg\",\"mentioned_mobile_list\":[\"17611377226\"]}}";
public static String sendRobotMessage() throws Exception {
// Jenkins的url, http://192.168.92.13:8080/job/Lemon/lastBuild/buildNumber
URI uri = new URI("http://192.168.92.13:8080");
//Jenkins请求的账号和密码
JenkinsHttpClient client = new JenkinsHttpClient(uri, "admin", "782fc581e22146388643bb2a252dcd85");
//Jenkins的服务
JenkinsServer jenkinsServer = new JenkinsServer(client);
// 获取所有job
Map<String, Job> jobs = jenkinsServer.getJobs();
System.out.println("获取所有jobs:" + jobs);
// 获取最后一次构建结果,结果为BuiledResult枚举类型
Integer LastBuild = (jenkinsServer.getJob("Lemon").getLastBuild()).getNumber();
System.out.println("最后一次构建次数为:" + LastBuild);
String BuildStatus = String.valueOf(jenkinsServer.getJob("Lemon").getLastBuild().details().getResult());
System.out.println("最后一次构建状态为:" + BuildStatus);
// 获取最后一次构建的Allure报告
String LastBuildAllureUrl = String.format("http://192.168.92.13:8080/job/Lemon/%d/allure/", LastBuild);
System.out.println(LastBuildAllureUrl);
// 通过最后一次的状态做判断,成功 SUCCESS 、失败 FAILURE
if (BuildStatus == "FAILURE") {
// 将失败的结果推送到企微群聊
String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fc6f4a8e-b208-47ab-9ceb-e9c7e47564c7";
JSONObject object = new JSONObject();
JSONObject markdown = new JSONObject();
String content = String.format("【Lemon自动化测试项目】运行有失败的用例,<font color=\"warning\">第 %d 次构建失败</font>,请相关同事注意。\n" +
" >项目名称:<font color=\\\"comment\\\">Lemon自动化测试</font>\n" +
" >所属模块:<font color=\\\"comment\\\">RAYS支付</font>\n" +
" >报告结果:<font color=\\\"comment\\\"> [报告结果](%s) </font>", LastBuild, LastBuildAllureUrl);
markdown.put("content", content);
object.put("msgtype", "markdown");
object.put("markdown", markdown);
String body = WechatWebHook(url, object.toJSONString());
return body;
} else {
System.out.println("运行正确,无需告警");
}
return BuildStatus;
}
/**
* 获取Jenkins最后一次构建的状态和次数
*
* @return
* @throws IOException
* @throws URISyntaxException
*/
public String getLastBuildInfo() throws IOException, URISyntaxException {
// Jenkins的url
URI uri = new URI("http://192.168.92.13:8080");
//Jenkins请求的账号和密码
JenkinsHttpClient client = new JenkinsHttpClient(uri, "admin", "782fc581e22146388643bb2a252dcd85");
//Jenkins的服务
JenkinsServer jenkinsServer = new JenkinsServer(client);
// 获取所有job
Map<String, Job> jobs = jenkinsServer.getJobs();
// 脚本触发Jenkins构建
// jenkinsServer.getJob("Lemon").build();
// 获取最后一次构建次数
Integer LastBuild = (jenkinsServer.getJob("Lemon").getLastBuild()).getNumber();
System.out.println("最后一次构建次数为:" + LastBuild);
// 最后一次构建状态,结果,结果为BuiledResult枚举类型
String BuildStatus = String.valueOf(jenkinsServer.getJob("Lemon").getLastBuild().details().getResult());
System.out.println("最后一次构建状态为:" + BuildStatus);
// 获取最后一次构建的Allure报告
String LastBuildAllureUrl = String.format("http://192.168.92.13:8080/job/Lemon/%d/allure/", LastBuild);
System.out.println("最后一次的Allure报告" + LastBuildAllureUrl);
return LastBuildAllureUrl;
// // 获取最后一次构建的结果
// String LastBuildStatus = String.format("http://192.168.92.13:8080/job/Lemon/%d/api/xml", LastBuild);
// System.out.println(LastBuildStatus);
// // 获取Jenkins控制台输出
// String LastBuildConsole = String.format("http://192.168.92.13:8080/job/Lemon/%d/console", LastBuild);
// System.out.println(LastBuildConsole);
}
/**
* 使用企业微信群聊机器人
*
* @param url
* @param json
* @return
* @throws Exception
*/
public static String WechatWebHook(String url, String json) throws Exception {
// 请求url
HttpPost httpPost = new HttpPost(url);
// 请求header
......@@ -35,41 +126,11 @@ public class Robot {
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
Header[] headers = response.getAllHeaders();
String body = EntityUtils.toString(entity);
return body;
}
public static String httpClientPost(String url, String jsonData) {
String result = "";
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
BasicResponseHandler handler = new BasicResponseHandler();
//设置请求格式
StringEntity entity = new StringEntity(jsonData, "utf-8"); //解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
//执行POST请求
result = httpClient.execute(httpPost, handler);
return result;
} catch (Exception e) {
} finally {
//释放连接
try {
httpClient.close();
} catch (Exception e) {
}
}
return result;
}
public static String httpClientGet(String url, String jsonData) {
String result = "";
//创建post方式请求对象
......@@ -98,20 +159,8 @@ public class Robot {
}
public static void main(String[] args) throws Exception {
// 获取最后一次构建的编号
String last_build_url = "http://192.168.92.13:9988/job/ryasdata_api_test/lastBuild/buildNumber";
//
String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fc6f4a8e-b208-47ab-9ceb-e9c7e47564c7";
String json = "{\"msgtype\":\"text\",\"text\":{\"content\":\"msg\",\"mentioned_mobile_list\":[\"17611377226\"]}}";
String body=WechatWebHook();
System.out.println(body);
sendRobotMessage();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment