简体   繁体   English

通过 Groovy 脚本读取一个 HTML 文件

[英]Reading a HTML file through Groovy Script

I need to write a Jenkins pipeline script using Groovy where the below HTML is the input .我需要使用 Groovy 编写一个 Jenkins 管道脚本,其中以下 HTML 是输入

<table style="width:30%">
 <TR> 
 <TD>Failed A Count</TD>
 <TD>2869</TD>
 </TR>
 <TR> 
 <TD>Failed B Count</TD>
 <TD>9948</TD>
 </TR>
 <TR> 
 <TD>Failed C Count</TD>
 <TD>3456</TD>
 </TR></table>

I am getting it from a RestAPI, and if any of the value is more than 100 I need to trigger an email.我从 RestAPI 获取它,如果任何值超过 100,我需要触发 email。

def response = httpRequest 'REST_API_URI'
println("Status: "+response.status)
def responseBody =  response.content
String[] TDcollection;
String[] splitData = responseBody.split("\n");
for (String eachSplit : splitData) {
  if (eachSplit.contains("Failed")) {
    print(eachSplit);
    }
  }

I have tried this, But not able to pick up the value and validate it.我试过这个,但无法获取值并验证它。

This might seem very easy, but as I am very这可能看起来很容易,但因为我很

new to Groovy, I am kind of stuck on it. Groovy 的新手,我有点被困住了。 Thanks In Advance.提前致谢。

No-brainer groovy:毫无疑问 groovy:

String input = '''\
<table style="width:30%">
 <TR>
 <TD>Failed A Count</TD>
 <TD>2869</TD>
 </TR>
 <TR>
 <TD>Failed B Count</TD>
 <TD>9948</TD>
 </TR>
 <TR>
 <TD>Failed B Count</TD>
 <TD>10000</TD>
 </TR>
 <TR>
 <TD>Failed C Count</TD>
 <TD>3456</TD>
 </TR></table>'''

Map<String,Integer> failedValues = [:].withDefault{ 0 }
input.eachMatch( /<TD>Failed (\w+) Count<\/TD>\s*<TD>(\d+)<\/TD>/ ){ _, name, count -> failedValues[ name ] += count.toInteger() }

assert failedValues == [A:2869, B:19948, C:3456]

boolean errorOccured = failedValues.any{ 100 <= it.value }

assert errorOccured

Note also the summing up of counts for the same "name".还要注意相同“名称”的计数总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM