#对一接口执行post请求,拿到ID后再执行get请求,拿到返回的Url,该Url将在pipeline中使用
# -*- coding: utf-8 -*-
#@auth:WuLei
#@date:20211108
import requests
import json
import re
import sys
import subprocess
import os
import time
def func():
postUrl = "#httpPostUrl#" #jenkins-pipeline中替换该变量为postUrl
jsonData = '#jsonParams#'
res = requests.post(url=postUrl,data=jsonData)
time.sleep(5)
getUrl = '#httpGetUrl#/'+res.text[1:-1]
#time.sleep(10)
num=1
while num == 1:
get_resource = requests.get(getUrl).text
resuGet=json.loads(get_resource)
var=str(resuGet["PlanStatus"])
if var == "False":
time.sleep(5)
else:
time.sleep(1)
pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
Result = re.findall(pattern,get_resource)[0]
ResultUrl = Result.strip()
with open("jsonResult.txt", 'w') as file1:
file1.write(json.dumps(resuGet))
file1.close()
with open("PlanTotalCase.txt", 'w') as file2:
TotalCase=str(resuGet["PlanTotalCase"])
file2.write(TotalCase)
file2.close()
return ResultUrl
break
url=func()
print url.strip()
##pipeline中调用该python脚本
stage("replacea-python"){
steps{
script{
def getPath="${params.getPath}";
def postUrl="${params.postUrl}";
def jsonParams="${params.jsonParams}";
def getUrl="${params.getUrl}";
def pyFile="${WORKSPACE}/get_file.py"
def data = readFile(file: getPath)
data=data.replaceAll("#httpPostUrl#",postUrl)
data=data.replaceAll("#jsonParams#",jsonParams)
data=data.replaceAll("#httpGetUrl#",getUrl)
println(data)
writeFile(file: pyFile, text: data)
def str=sh(script:"python " +pyFile, returnStdout:true) #pipeline中执行py脚本并拿到返回值
sh "echo --------------------------------------------------------"
def productInfos=[];
def productInfo = [:];
productInfo.put("name","指标1");
productInfo.put("key","点击跳转");
productInfo.put("value","测试结果URL");
productInfo.put("url",str);
productInfos.add(productInfo);
saveTaskProduct("测试名","测试入口","标题","测试结果URL","typexxx",productInfos); #saveTaskProduct的groovy脚本进行页面展示,点击跳转
}
}
}
####saveTaskProduct脚本
!groovy
import com.devops.util.HttpUtil
import groovy.json.JsonOutput
@NonCPS
def call(def name,def status,def title,def url,def infoType,def productInfos) {
//pipeline服务URL
def targeturl = "${env.DEVOPS_URL}/product/saveTaskProduct/${env.JOB_NAME}/${env.BUILD_NUMBER}" #jenkins接口
def taskProduct = [:];
//名称
taskProduct.put("name",name);
//状态
taskProduct.put("status",status);
//链接标题
taskProduct.put("title",title);
//链接地址
taskProduct.put("url", url);
//自定义类型
taskProduct.put("infoType", infoType);
//指标集合
taskProduct.put("productInfos", productInfos);
println JsonOutput.toJson(taskProduct)
//发送请求
println "-------------http-----------"
println targeturl;
HttpUtil http = new HttpUtil();
http.requestUtil("POST", JsonOutput.toJson(taskProduct), targeturl);
println "-----------httpend-------------"
}