-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSend-Report.ps1
118 lines (107 loc) · 5.28 KB
/
Send-Report.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
param($tc_user,$tc_psw,$smtpServer,$smtpPort,$username,$password,$from,$receivers,$serverAddress,$labManager,$acceptanceTests,$buildType,$product)
function Execute-HTTPPostCommand() {
param(
[string] $target = $null
)
$request = [System.Net.WebRequest]::Create($target)
Write-Host $request.RequestUri
$request.PreAuthenticate = $true
$request.Method = "GET"
$request.Credentials = new-object system.net.networkcredential($tc_user, $tc_psw)
$response = $request.GetResponse()
$sr = [Io.StreamReader]($response.GetResponseStream())
$xmlout = $sr.ReadToEnd()
return $xmlout;
}
function sendEmail($subject, $content)
{
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$receivers | % {$msg.To.add($_)}
$msg.Subject = $subject
$msg.IsBodyHTML = $true
$msg.Body = $content
$smtp.Send($msg)
}
$result = $false
$buildList = $labManager + $acceptanceTests
foreach ($build in $buildList)
{
if($build -eq $buildType) {continue;}
$command = $($serverAddress + "/httpAuth/app/rest/buildTypes/id:$build/builds/running:true/")
$xml = [xml]$(Execute-HTTPPostCommand $command)
if($xml -ne $null)
{
$result = $true
break;
}
}
function get_result($xml,$v)
{
if($xml -ne $null)
{
$command = $serverAddress + $xml.build.statistics.href
$statisticsXml = [xml]$(Execute-HTTPPostCommand $command)
$buildDuration = ([int](($statisticsXml.properties.property | ?{"BuildDuration" -eq $_.name}).value)*0.00001667).ToString("f1")
$startDate = $xml.build.startDate.Substring(0,13)
$startDate = $startDate.Substring(0,4) + "/" + $startDate.Substring(4,2)+ "/" + $startDate.Substring(6,2) + " " + $startDate.Substring(9,2) + ":" + $startDate.Substring(11,2)
$formattedStartDate = (Get-Date $startDate).ToUniversalTime().AddHours(8).ToString('yyyy/MM/dd HH:mm')
$totalTestCount = $xml.build.testOccurrences.count
$passedTestCount = $xml.build.testOccurrences.passed
$failedTestCount = $xml.build.testOccurrences.failed
$newFailedTestCount = $xml.build.testOccurrences.newFailed
if($newFailedTestCount -ne $null){$failedTestCount +="($newFailedTestCount new)"}
if($xml.build.status -eq "FAILURE" -and $failedTestCount -eq $null) {$failedTestCount = $xml.build.statusText}
$urls = $xml.build.webUrl.Split('/')
$logUrl = $($serverAddress + "/" + $urls[$urls.length-1])
if($xml.build.status -eq "FAILURE"){$tdstyle="tg-mzz2"} else {$tdstyle="tg-uhi5"}
if($v -eq $version)
{
return "<tr><td class=""tg-xdyu"">$($xml.build.buildType.name)</td><td class=""$tdstyle"">$($xml.build.status)</td><td class=""tg-031e""><a href=""$logUrl"">$($xml.build.number)</a></td><td class=""tg-031e"">$formattedStartDate</td><td class=""tg-031e"">$buildDuration</td><td class=""tg-031e"">$totalTestCount</td><td class=""tg-031e"">$passedTestCount</td><td class=""tg-mzz2"">$failedTestCount</td></tr>"
}
else
{
return "<tr><td class=""tg-xdyu"">$($xml.build.buildType.name)</td><td class=""tg-vnts"">$($xml.build.status)</td><td class=""tg-vnts""><a href=""$logUrl"">$($xml.build.number)</a></td><td class=""tg-vnts"">$formattedStartDate</td><td class=""tg-vnts"">$buildDuration</td><td class=""tg-vnts"">$totalTestCount</td><td class=""tg-vnts"">$passedTestCount</td><td class=""tg-vnts"">$failedTestCount</td></tr>"
}
}
}
echo "Check current build whether is last build"
if($result -eq $false)
{
$result = @"
<head>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-wv6k{font-weight:bold;background-color:#329a9d;text-align:center}
.tg .tg-xdyu{font-weight:bold;font-style:italic}
.tg .tg-mzz2{font-weight:bold;color:#fe0000}
.tg .tg-uhi5{font-weight:bold;color:#32cb00}
.tg .tg-vnts{font-weight:bold;color:#808080}
</style>
</head><body>
<table class="tg"><tr><th class="tg-wv6k">Acceptance Tests</th><th class="tg-wv6k">Status</th><th class="tg-wv6k">Build</th><th class="tg-wv6k">Start Date</th><th class="tg-wv6k">Build Duration(m)</th><th class="tg-wv6k">Total Tests</th><th class="tg-wv6k">Tests Passed</th><th class="tg-wv6k">Tests Failed</th></tr>
"@
Sleep 10
$command = $($serverAddress + "/httpAuth/app/rest/buildTypes/id:$buildType/builds/running:true/")
$xml = [xml]$(Execute-HTTPPostCommand $command)
$version = $xml.build.number
$result += get_result $xml $xml.build.number
echo "Get all the test result"
foreach ($build in $acceptanceTests)
{
if($buildType -ne $build)
{
$command = $($serverAddress + "/httpAuth/app/rest/buildTypes/id:$build/builds/running:false/")
$xml = [xml]$(Execute-HTTPPostCommand $command)
$result += get_result $xml $xml.build.number
}
}
$result += "</table></body></html>"
echo "Send emails"
sendEmail "$product Test Result($version)" $result
}