Skip to content

Instantly share code, notes, and snippets.

@vkostyanetsky
Created July 31, 2022 08:15
Show Gist options
  • Save vkostyanetsky/b0807f2df2501bbeeb97c95a977f3e23 to your computer and use it in GitHub Desktop.
Save vkostyanetsky/b0807f2df2501bbeeb97c95a977f3e23 to your computer and use it in GitHub Desktop.
Send Allure Report Summary to Slack
Import-Module PSSlack
function SendSummaryToSlack() {
<#
.SYNOPSIS
Creates a summary for a pipeline execution and sends this in a specified Slack channel.
.DESCRIPTION
Parses git branch, git commit, client type, backend type, number of passed scenarios, and number of failed scenarios.
Sends all the collected data as a message to a specified Slack channel.
.PARAMETER AllureSourceFolder
This folder must contain source data for Allure.
.PARAMETER AllureReportFolder
This folder must contain report data for Allure.
.PARAMETER SlackWebhookURL
The Slack webhook URL to dispatch messages.
.PARAMETER PipelineURL
The Gitlab URL of a running pipeline.
.PARAMETER PipelineID
The Gitlab ID of a running pipeline.
.EXAMPLE
SendSummaryToSlack -AllureSourceFolder "allure" `
-AllureReportFolder "allure-ready" `
-SlackWebhookURL "https://hooks.slack.com/services/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"
-PipelineURL "https://git.company.com/cia/erp-test/-/pipelines/10115"
-PipelineID "10115"
.LINK
https://api.slack.com/apps/
#>
[OutputType([System.Void])]
param(
[string]$AllureSourceFolder,
[string]$AllureReportFolder,
[string]$SlackWebhookURL,
[string]$PipelineURL,
[string]$PipelineID
)
function Get-Properties() {
<#
.SYNOPSIS
Parses specific environment parameters.
.DESCRIPTION
The function reads the environment.properties file in the folder provided, then parses several parameters, mentioned in the PropertyKeys variable.
.PARAMETER Data
The hashtable with data collected to build a summary.
.PARAMETER DataPath
The source folder which contains the "allure" folder.
#>
[OutputType([System.Collections.Hashtable])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Collections.Hashtable]
$Data,
[string]$DataPath
)
$FilePath = "$DataPath/environment.properties"
$PropertyKeys = ("Vanessa_Test_Branch", "Onece_Backend_Type", "App_Source_GIT_Commit", "Onece_Test_Client")
foreach($Line in Get-Content $FilePath) {
$SplittedLine = $line.Split("=")
$LinePropertyKey = $SplittedLine[0].Trim()
$LinePropertyValue = $SplittedLine[1].Trim()
foreach($PropertyKey in $PropertyKeys) {
if ($PropertyKey -eq $LinePropertyKey) {
$Data.Add($LinePropertyKey, $LinePropertyValue)
}
}
}
return $Data
}
function Get-Counters() {
<#
.SYNOPSIS
Returns the numbers of passed & failed scenarios.
.DESCRIPTION
The function reads the behaviors.csv file in the folder provided, then counts how many scenarios have passed and how many scenarios have failed.
.PARAMETER Data
The hashtable with data collected to build a summary.
.PARAMETER DataPath
The source folder which contains the "allure-ready" folder.
#>
[OutputType([System.Collections.Hashtable])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Collections.Hashtable]
$Data,
[string]$DataPath
)
$FilePath = "$DataPath/data/behaviors.csv"
$Data.Add("ScenariosPassed", 0)
$Data.Add("ScenariosFailed", 0)
Import-CSV -Path $FilePath | ForEach-Object {
$Data["ScenariosPassed"] += $_.PASSED
$Data["ScenariosFailed"] += $_.FAILED
}
return $Data
}
function Get-Summary() {
<#
.SYNOPSIS
Returns a summary.
.DESCRIPTION
The function makes a hashtable with values which are stores in the $Properties variable and the $Counters variable.
.PARAMETER Data
The hashtable with data collected to build a summary.
#>
[OutputType([System.Collections.Hashtable])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Collections.Hashtable]
$Data
)
$Object = [pscustomobject]@{
"Git Branch" = $Data['Vanessa_Test_Branch'].ToString()
"Git Commit" = "<" + $Data['App_Source_GIT_Commit'].ToString() + "|Link>"
"Client Type" = $Data['Onece_Test_Client'].ToString()
"Backend Type" = $Data['Onece_Backend_Type'].ToString()
"Scenarios Passed" = $Data["ScenariosPassed"].ToString()
"Scenarios Failed" = $Data["ScenariosFailed"].ToString()
}
$Fields = @()
foreach($Prop in $Object.psobject.Properties.Name)
{
$Fields += @{
title = $Prop
value = $Object.$Prop
short = $true
}
}
return $Fields
}
$Summary = @{} | Get-Properties -DataPath $AllureSourceFolder | Get-Counters -DataPath $AllureReportFolder | Get-Summary
$Result = New-SlackMessageAttachment -AuthorName "Gitlab" `
-Title "Pipeline #$PipelineID has generated an Allure report" `
-TitleLink $PipelineURL `
-Fields $Summary `
-Fallback "Allure report for an ERP testing iteration has been generated!" |
New-SlackMessage |
Send-SlackMessage -Uri $SlackWebhookURL
if ($Result -ne "ok") {
throw "Unexpected Slack answer while sending a summary to Slack!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment