Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
askbooks
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
齐文博
askbooks
Commits
ae29527d
Commit
ae29527d
authored
Mar 22, 2023
by
左磊磊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add:新增部分Jenkins构建后执行的代码
parent
a49d8147
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
111 additions
and
48 deletions
+111
-48
pom.xml
pom.xml
+14
-0
PayCase.java
src/test/java/com/lemon/cases/PayCase.java
+2
-2
Robot.java
src/test/java/com/lemon/utils/Robot.java
+95
-46
PayCase.class
target/test-classes/com/lemon/cases/PayCase.class
+0
-0
睿思选题.xls
target/test-classes/睿思选题.xls
+0
-0
No files found.
pom.xml
View file @
ae29527d
...
...
@@ -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>
...
...
src/test/java/com/lemon/cases/PayCase.java
View file @
ae29527d
...
...
@@ -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请求
...
...
src/test/java/com/lemon/utils/Robot.java
View file @
ae29527d
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
();
}
}
target/test-classes/com/lemon/cases/PayCase.class
View file @
ae29527d
No preview for this file type
target/test-classes/睿思选题.xls
View file @
ae29527d
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment