From 9b4b0d22d8b4297a936047d7b540f8ea8d721f09 Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <v-aleche@microsoft.com> Date: Thu, 2 Jul 2020 17:05:38 +0300 Subject: [PATCH 1/2] slack notification --- .../send-slack-notification.ps1 | 63 +++++++++++++++++++ slack-notification/slack.helpers.psm1 | 38 +++++++++++ 2 files changed, 101 insertions(+) create mode 100644 slack-notification/send-slack-notification.ps1 create mode 100644 slack-notification/slack.helpers.psm1 diff --git a/slack-notification/send-slack-notification.ps1 b/slack-notification/send-slack-notification.ps1 new file mode 100644 index 0000000..a465d2d --- /dev/null +++ b/slack-notification/send-slack-notification.ps1 @@ -0,0 +1,63 @@ +<# +.SYNOPSIS +Sending messages using Incoming Webhooks + +.PARAMETER Url +Required parameter. Incoming Webhook URL to post a message +.PARAMETER ToolName +Required parameter. The name of tool +.PARAMETER ToolVersion +Required parameter. Specifies the version of tool +.PARAMETER PipelineUrl +Required parameter. The pipeline URL +.PARAMETER ImageUrl +Optional parameter. The image URL +#> + +param( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.Uri]$Url, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.String]$ToolName, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.String]$ToolVersion, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.String]$PipelineUrl, + + [System.String]$ImageUrl = 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png' +) + +# Import helpers module +Import-Module $PSScriptRoot/slack.helpers.psm1 -DisableNameChecking + +# Create JSON body +$text = "The following versions of '$toolName' are available to upload: $toolVersion\nLink to the pipeline: $pipelineUrl" +$jsonBodyMessage = @" +{ + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "$text" + }, + "accessory": { + "type": "image", + "image_url": "$imageUrl", + "alt_text": "$toolName" + } + } + ] +} +"@ + +# Send Slack message +$null = Send-SlackPostMessageIncomingWebHook -Uri $url -Body $jsonBodyMessage +Write-Host "Message template: `n $jsonBodyMessage" diff --git a/slack-notification/slack.helpers.psm1 b/slack-notification/slack.helpers.psm1 new file mode 100644 index 0000000..208f1c1 --- /dev/null +++ b/slack-notification/slack.helpers.psm1 @@ -0,0 +1,38 @@ +<# +.SYNOPSIS +Sending messages using Incoming Webhooks +https://api.slack.com/messaging/webhooks +#> + +function Send-SlackPostMessageIncomingWebHook +{ + param( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.Uri]$Uri, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.Object]$Body + ) + + try + { + $response = Invoke-RestMethod -Uri $uri -Method POST -Body $body -ErrorAction Stop + if ($response -eq 'ok') + { + return $response + } + else + { + Write-Host "##vso[task.LogIssue type=error;] Something went wrong. Response is '$response'" + } + } + catch + { + Write-Host "##vso[task.LogIssue type=error;] Slack send post message failed: '$_'" + } + + Write-Host "##vso[task.complete result=Failed;]" + exit 1 +} \ No newline at end of file From 9a31e4815a426c003fce1458d68b362142efdc5c Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <v-aleche@microsoft.com> Date: Fri, 3 Jul 2020 13:53:05 +0300 Subject: [PATCH 2/2] move to the get-new-tool-versions folder --- get-new-tool-versions/helpers.psm1 | 39 +++++++++++++++++++ .../send-slack-notification.ps1 | 2 +- slack-notification/slack.helpers.psm1 | 38 ------------------ 3 files changed, 40 insertions(+), 39 deletions(-) rename {slack-notification => get-new-tool-versions}/send-slack-notification.ps1 (95%) delete mode 100644 slack-notification/slack.helpers.psm1 diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index 2f9d582..fb63f61 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -70,4 +70,43 @@ function Skip-ExistingVersions { ) return $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } +} + +<# +.SYNOPSIS +Sending messages using Incoming Webhooks +https://api.slack.com/messaging/webhooks +#> + +function Send-SlackPostMessageIncomingWebHook +{ + param( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.Uri]$Uri, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [System.Object]$Body + ) + + try + { + $response = Invoke-RestMethod -Uri $uri -Method POST -Body $body -ErrorAction Stop + if ($response -eq 'ok') + { + return $response + } + else + { + Write-Host "##vso[task.LogIssue type=error;] Something went wrong. Response is '$response'" + } + } + catch + { + Write-Host "##vso[task.LogIssue type=error;] Slack send post message failed: '$_'" + } + + Write-Host "##vso[task.complete result=Failed;]" + exit 1 } \ No newline at end of file diff --git a/slack-notification/send-slack-notification.ps1 b/get-new-tool-versions/send-slack-notification.ps1 similarity index 95% rename from slack-notification/send-slack-notification.ps1 rename to get-new-tool-versions/send-slack-notification.ps1 index a465d2d..0e6fbc5 100644 --- a/slack-notification/send-slack-notification.ps1 +++ b/get-new-tool-versions/send-slack-notification.ps1 @@ -35,7 +35,7 @@ param( ) # Import helpers module -Import-Module $PSScriptRoot/slack.helpers.psm1 -DisableNameChecking +Import-Module $PSScriptRoot/helpers.psm1 -DisableNameChecking # Create JSON body $text = "The following versions of '$toolName' are available to upload: $toolVersion\nLink to the pipeline: $pipelineUrl" diff --git a/slack-notification/slack.helpers.psm1 b/slack-notification/slack.helpers.psm1 deleted file mode 100644 index 208f1c1..0000000 --- a/slack-notification/slack.helpers.psm1 +++ /dev/null @@ -1,38 +0,0 @@ -<# -.SYNOPSIS -Sending messages using Incoming Webhooks -https://api.slack.com/messaging/webhooks -#> - -function Send-SlackPostMessageIncomingWebHook -{ - param( - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [System.Uri]$Uri, - - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [System.Object]$Body - ) - - try - { - $response = Invoke-RestMethod -Uri $uri -Method POST -Body $body -ErrorAction Stop - if ($response -eq 'ok') - { - return $response - } - else - { - Write-Host "##vso[task.LogIssue type=error;] Something went wrong. Response is '$response'" - } - } - catch - { - Write-Host "##vso[task.LogIssue type=error;] Slack send post message failed: '$_'" - } - - Write-Host "##vso[task.complete result=Failed;]" - exit 1 -} \ No newline at end of file