From 196e70fdee33397ef34919a783ea882b3a846d33 Mon Sep 17 00:00:00 2001 From: Denis Tikhomirov Date: Mon, 14 Feb 2022 14:45:40 +0300 Subject: [PATCH 1/2] Changes to use updates in notifications from Localization CI --- open-pullrequest.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/open-pullrequest.ps1 b/open-pullrequest.ps1 index 0ddbb4440..6e12b8dde 100644 --- a/open-pullrequest.ps1 +++ b/open-pullrequest.ps1 @@ -5,13 +5,12 @@ param( ) 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 +19,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" From 881ace006e2a78ef986814264a55ddc18447e13d Mon Sep 17 00:00:00 2001 From: Denis Tikhomirov Date: Thu, 10 Mar 2022 15:35:35 +0300 Subject: [PATCH 2/2] Moved inline script for sending of notifications to separate file --- open-pullrequest.ps1 | 1 + send-notifications.ps1 | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 send-notifications.ps1 diff --git a/open-pullrequest.ps1 b/open-pullrequest.ps1 index 6e12b8dde..d00c05170 100644 --- a/open-pullrequest.ps1 +++ b/open-pullrequest.ps1 @@ -4,6 +4,7 @@ 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() { return (gh api -X GET repos/:owner/:repo/pulls -F head=":owner:$SourceBranch" -f state=open -f base=master | ConvertFrom-Json) } diff --git a/send-notifications.ps1 b/send-notifications.ps1 new file mode 100644 index 000000000..4de65d33b --- /dev/null +++ b/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