简体   繁体   中英

Reading a HTML file through Groovy Script

I need to write a Jenkins pipeline script using Groovy where the below HTML is the 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 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.

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. Thanks In Advance.

No-brainer 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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