From 4f77a4517876bc48d739a66c03f8c58d0e88eac3 Mon Sep 17 00:00:00 2001 From: Denis Tikhomirov <90906678+denis-tikhomirov@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:15:12 +0300 Subject: [PATCH] Move notifications about Tasks Localization PR from Slack to MS Teams - Part 1 (#15917) --- Localize/localize-pipeline.yml | 27 +++++------ .../send-notifications.ps1 | 45 +++++++++++++++++++ ci/open-pullrequest.ps1 | 12 +++-- 3 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 ci/localization-updates/send-notifications.ps1 diff --git a/Localize/localize-pipeline.yml b/Localize/localize-pipeline.yml index 14c7862cb67a..7e7ce04d7175 100644 --- a/Localize/localize-pipeline.yml +++ b/Localize/localize-pipeline.yml @@ -109,21 +109,18 @@ stages: displayName: Open a PR condition: and(succeeded(), or(and(eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule')), eq(variables['build.reason'], 'Manual'))) - - powershell: | - $message="Created tasks localization update PR. Someone please approve/merge it. :please-puss-in-boots: $env:PR_LINK" - $body = [PSCustomObject]@{ - text = $message - } | ConvertTo-Json - Invoke-RestMethod -Uri $(slackUri) -Method Post -Body $body -ContentType 'application/json' - displayName: 'Send Slack notification about PR opened' + # Two next tasks are used to notify about Localization update PRs + # Notifications are set by POST method to MS Teams webhook + # Body of message is compiled as Office 365 connector card + # More details about cards - https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-reference#office-365-connector-card + - powershell: .\ci\localization-updates\send-notifications.ps1 -IsPRCreated $true -RepoName "Tasks" + displayName: 'Send MS Teams notification about PR opened' + env: + TEAMS_WEBHOOK: $(MSTeamsUri) condition: and(succeeded(), eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule')) - - powershell: | - $buildUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)&_a=summary" - $message="Something went wrong while creating tasks localization update PR. Build: $buildUrl" - $body = [PSCustomObject]@{ - text = $message - } | ConvertTo-Json - Invoke-RestMethod -Uri $(slackUri) -Method Post -Body $body -ContentType 'application/json' - displayName: 'Send Slack notification about error' + - powershell: .\ci\localization-updates\send-notifications.ps1 -IsPRCreated $false -RepoName "Tasks" + displayName: 'Send MS Teams notification about error' + env: + TEAMS_WEBHOOK: $(MSTeamsUri) condition: and(failed(), eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule')) diff --git a/ci/localization-updates/send-notifications.ps1 b/ci/localization-updates/send-notifications.ps1 new file mode 100644 index 000000000000..4de65d33be01 --- /dev/null +++ b/ci/localization-updates/send-notifications.ps1 @@ -0,0 +1,45 @@ +param( + [Parameter(Mandatory = $true)] + [bool]$IsPRCreated, + [Parameter(Mandatory = $true)] + [string]$RepoName +) + +# Function sends Office 365 connector card to webhook. +# It requires title and message text displyed in card and theme color used to hignlight card. +function Send-Notification { + param ( + [Parameter(Mandatory = $true)] + [string]$titleText, + [Parameter(Mandatory = $true)] + [string]$messageText, + [Parameter(Mandatory = $true)] + [string]$themeColor + ) + + $body = [PSCustomObject]@{ + title = $titleText + text = $messageText + themeColor = $themeColor + } | ConvertTo-Json + + Invoke-RestMethod -Uri $($env:TEAMS_WEBHOOK) -Method Post -Body $body -ContentType 'application/json' +} + +$wikiLink = "[Wiki](https://mseng.visualstudio.com/AzureDevOps/_wiki/wikis/AzureDevOps.wiki/16150/Localization-update)" + +if ($IsPRCreated) { + $pullRequestLink = "[PR $($env:PR_NUMBER)]($($env:PR_LINK))" + $titleText = "Azure Pipelines $RepoName Localization update PR created - ID $($env:PR_NUMBER)" + $messageText = "Created $RepoName Localization update PR. Please review and approve/merge $pullRequestLink. Related article in $wikiLink." + $themeColor = "#FFFF00" +} +else { + $buildUrl = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$env:SYSTEM_TEAMPROJECT/_build/results?buildId=$($env:BUILD_BUILDID)&_a=summary" + $buildLink = "[ID $($env:BUILD_BUILDID)]($($buildUrl))" + $titleText = "Azure Pipelines $RepoName Localization build failed - ID $($env:BUILD_BUILDID)" + $messageText = "Failed to create $RepoName Localization update PR. Please review the results of failed build $buildLink. Related article in $wikiLink." + $themeColor = "#FF0000" +} + +Send-Notification -titleText $titleText -messageText $messageText -themeColor $themeColor diff --git a/ci/open-pullrequest.ps1 b/ci/open-pullrequest.ps1 index 26a6560e8ad9..870dd7dbf641 100644 --- a/ci/open-pullrequest.ps1 +++ b/ci/open-pullrequest.ps1 @@ -4,14 +4,14 @@ param( $SourceBranch ) +# Getting a created PR. Result object has interface in accordance with article https://docs.github.com/en/rest/reference/pulls#get-a-pull-request function Get-PullRequest() { - $prInfo = (gh api -X GET repos/:owner/:repo/pulls -F head=":owner:$SourceBranch" -f state=open -f base=master | ConvertFrom-Json) - return $prInfo.html_url + return (gh api -X GET repos/:owner/:repo/pulls -F head=":owner:$SourceBranch" -f state=open -f base=master | ConvertFrom-Json) } $openedPR=Get-PullRequest -if ($openedPR.length -ne 0) { +if ($openedPR.html_url.length -ne 0) { throw "A PR from $SourceBranch to master already exists." } @@ -20,6 +20,10 @@ $body = "This PR was auto-generated with [the localization pipeline build]($buil gh pr create --head $SourceBranch --title 'Localization update' --body $body +# Getting a number to the opened PR +$PR_NUMBER = (Get-PullRequest).number +Write-Host "##vso[task.setvariable variable=PR_NUMBER]$PR_NUMBER" + # Getting a link to the opened PR -$PR_LINK = Get-PullRequest +$PR_LINK = (Get-PullRequest).html_url Write-Host "##vso[task.setvariable variable=PR_LINK]$PR_LINK"