Skip to content

Commit 66fc75e

Browse files
Merge pull request #145 from microsoft/users/denis-tikhomirov/issue2123-loc
2 parents 46c515c + 6d0b1f6 commit 66fc75e

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

Localize/localize-pipeline.yml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,18 @@ stages:
9292
displayName: Open a PR
9393
condition: and(succeeded(), or(and(eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule')), eq(variables['build.reason'], 'Manual')))
9494

95-
- powershell: |
96-
$message="Created tool-lib localization update PR. Someone please approve/merge it. :please-puss-in-boots: $env:PR_LINK"
97-
$body = [PSCustomObject]@{
98-
text = $message
99-
} | ConvertTo-Json
100-
101-
Invoke-RestMethod -Uri $(slackUri) -Method Post -Body $body -ContentType 'application/json'
102-
displayName: 'Send Slack notification about PR opened'
95+
# Two next tasks are used to notify about Localization update PRs
96+
# Notifications are set by POST method to MS Teams webhook
97+
# Body of message is compiled as Office 365 connector card
98+
# More details about cards - https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-reference#office-365-connector-card
99+
- powershell: .\send-notifications.ps1 -IsPRCreated $true -RepoName "Tool-lib"
100+
env:
101+
TEAMS_WEBHOOK: $(MSTeamsUri)
102+
displayName: 'Send MS Teams notification about PR opened'
103103
condition: and(succeeded(), eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule'))
104104

105-
- powershell: |
106-
$buildUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)&_a=summary"
107-
$message="Something went wrong while creating tool-lib localization update PR. Build: $buildUrl"
108-
$body = [PSCustomObject]@{
109-
text = $message
110-
} | ConvertTo-Json
111-
112-
Invoke-RestMethod -Uri $(slackUri) -Method Post -Body $body -ContentType 'application/json'
113-
displayName: 'Send Slack notification about error'
105+
- powershell: .\send-notifications.ps1 -IsPRCreated $false -RepoName "Tool-lib"
106+
env:
107+
TEAMS_WEBHOOK: $(MSTeamsUri)
108+
displayName: 'Send MS Teams notification about error'
114109
condition: and(failed(), eq(variables['SHOULDCREATEPR'], 'True'), eq(variables['build.reason'], 'Schedule'))

open-pullrequest.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ param(
44
$SourceBranch
55
)
66

7+
# Getting a created PR. Result object has interface in accordance with article https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
78
function Get-PullRequest() {
8-
$prInfo = (gh api -X GET repos/:owner/:repo/pulls -F head=":owner:$SourceBranch" -f state=open -f base=master | ConvertFrom-Json)
9-
return $prInfo.html_url
9+
return (gh api -X GET repos/:owner/:repo/pulls -F head=":owner:$SourceBranch" -f state=open -f base=master | ConvertFrom-Json)
1010
}
1111

1212
$openedPR = Get-PullRequest
1313

14-
if ($openedPR.length -ne 0) {
14+
if ($openedPR.html_url.length -ne 0) {
1515
throw "A PR from $SourceBranch to master already exists."
1616
}
1717

@@ -20,6 +20,10 @@ $body = "This PR was auto-generated with [the localization pipeline build]($buil
2020

2121
gh pr create --head $SourceBranch --title 'Localization update' --body $body
2222

23+
# Getting a number to the opened PR
24+
$PR_NUMBER = (Get-PullRequest).number
25+
Write-Host "##vso[task.setvariable variable=PR_NUMBER]$PR_NUMBER"
26+
2327
# Getting a link to the opened PR
24-
$PR_LINK = Get-PullRequest
28+
$PR_LINK = (Get-PullRequest).html_url
2529
Write-Host "##vso[task.setvariable variable=PR_LINK]$PR_LINK"

send-notifications.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
param(
2+
[Parameter(Mandatory = $true)]
3+
[bool]$IsPRCreated,
4+
[Parameter(Mandatory = $true)]
5+
[string]$RepoName
6+
)
7+
8+
# Function sends Office 365 connector card to webhook.
9+
# It requires title and message text displyed in card and theme color used to hignlight card.
10+
function Send-Notification {
11+
param (
12+
[Parameter(Mandatory = $true)]
13+
[string]$titleText,
14+
[Parameter(Mandatory = $true)]
15+
[string]$messageText,
16+
[Parameter(Mandatory = $true)]
17+
[string]$themeColor
18+
)
19+
20+
$body = [PSCustomObject]@{
21+
title = $titleText
22+
text = $messageText
23+
themeColor = $themeColor
24+
} | ConvertTo-Json
25+
26+
Invoke-RestMethod -Uri $($env:TEAMS_WEBHOOK) -Method Post -Body $body -ContentType 'application/json'
27+
}
28+
29+
$wikiLink = "[Wiki](https://mseng.visualstudio.com/AzureDevOps/_wiki/wikis/AzureDevOps.wiki/16150/Localization-update)"
30+
31+
if ($IsPRCreated) {
32+
$pullRequestLink = "[PR $($env:PR_NUMBER)]($($env:PR_LINK))"
33+
$titleText = "Azure Pipelines $RepoName Localization update PR created - ID $($env:PR_NUMBER)"
34+
$messageText = "Created $RepoName Localization update PR. Please review and approve/merge $pullRequestLink. Related article in $wikiLink."
35+
$themeColor = "#FFFF00"
36+
}
37+
else {
38+
$buildUrl = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$env:SYSTEM_TEAMPROJECT/_build/results?buildId=$($env:BUILD_BUILDID)&_a=summary"
39+
$buildLink = "[ID $($env:BUILD_BUILDID)]($($buildUrl))"
40+
$titleText = "Azure Pipelines $RepoName Localization build failed - ID $($env:BUILD_BUILDID)"
41+
$messageText = "Failed to create $RepoName Localization update PR. Please review the results of failed build $buildLink. Related article in $wikiLink."
42+
$themeColor = "#FF0000"
43+
}
44+
45+
Send-Notification -titleText $titleText -messageText $messageText -themeColor $themeColor

0 commit comments

Comments
 (0)