Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[devops] Rework how we hide GitHub comments. #21320

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions tools/devops/automation/scripts/GitHub.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ class GitHubComments {
}

[void] WriteCommentFooter(
[object] $stringBuilder
[object] $stringBuilder,
[string] $commentId
) {
$targetUrl = Get-TargetUrl
$stringBuilder.AppendLine("[Pipeline]($targetUrl) on Agent $Env:TESTS_BOT") # Env:TESTS_BOT is added by the pipeline as a variable coming from the execute tests job
Expand All @@ -308,12 +309,20 @@ class GitHubComments {
$hashUrl= "https://github.com/$($this.Org)/$($this.Repo)/commit/$($this.Hash)"
$hashSource = " [CI build]"
}
$ciComment = "[comment]: <> (This is a comment added by Azure DevOps)"
$ciComment = $this.GetCommentIdentifier($commentId)
$stringBuilder.AppendLine("Hash: [$($this.Hash)]($hashUrl) $hashSource")
$stringBuilder.AppendLine("")
$stringBuilder.AppendLine($ciComment)
}

[string] GetCommentIdentifier([string] $commentId)
{
$sanitizedId = $commentId
$sanitizedId = $sanitizedId.Replace("(","_").Replace(")","_").Replace("[","_").Replace("]","_")
$sanitizedId = $sanitizedId.Replace("{","_").Replace("}","_").Replace("<","_").Replace(">","_")
return "[comment]: <> (This is a comment added by Azure DevOps, id: $sanitizedId)"
}

[string] GetCommentUrl() {
# if the build was due to PR, we want to write the comment in the PR rather than in the commit
if ([GitHubComments]::IsPR()) {
Expand Down Expand Up @@ -360,11 +369,46 @@ class GitHubComments {
return $request
}

[void] HideComments(
[string] $commentId
) {
if (!$commentId) {
Write-Host "Not hiding comments, because no comment id provided"
return
}

if (![GitHubComments]::IsPR()) {
Write-Host "Not hiding comments, because we're not in a pull request"
return
}

$prId = "$Env:BUILD_SOURCEBRANCH".Replace("refs/pull/", "").Replace("/merge", "")
$prComments = $this.GetCommentsForPR($prId)

$botComments = [System.Collections.ArrayList]@()
$commentToHide = $this.GetCommentIdentifier($commentId)

foreach ($c in $prComments) {
if ($c.Author -eq "vs-mobiletools-engineering-service2") {
if ($c.Body.Contains($CommentToHide)) {
$botComments.Add($c)
}
}
}

Write-Host "Hiding $($botComments.Count) comments for PR #$prId with comment id '$commentId'"

$this.MinimizeComments($botComments)
}

[object] NewCommentFromObject(
[string] $commentTitle,
[string] $commentEmoji,
[object] $commentObject
[object] $commentObject,
[string] $commentId
) {
$this.HideComments($commentId)

# build the message, which will be sent to github, users can use markdown
$msg = [System.Text.StringBuilder]::new()

Expand All @@ -376,16 +420,19 @@ class GitHubComments {
$msg.AppendLine()

# footer
$this.WriteCommentFooter($msg)
$this.WriteCommentFooter($msg, $commentId)

return $this.NewComment($msg)
}

[object] NewCommentFromFile(
[string] $commentTitle,
[string] $commentEmoji,
[string] $filePath
[string] $filePath,
[string] $commentId
) {
$this.HideComments($commentId)

# build the message, which will be sent to github, users can use markdown
$msg = [System.Text.StringBuilder]::new()

Expand All @@ -404,16 +451,19 @@ class GitHubComments {
$msg.AppendLine()

# footer
$this.WriteCommentFooter($msg)
$this.WriteCommentFooter($msg, $commentId)

return $this.NewComment($msg)
}

[object] NewCommentFromMessage(
[string] $commentTitle,
[string] $commentEmoji,
[string] $content
[string] $content,
[string] $commentId
) {
$this.HideComments($commentId)

$msg = [System.Text.StringBuilder]::new()

# header
Expand All @@ -424,7 +474,7 @@ class GitHubComments {
$msg.AppendLine()

# footer
$this.WriteCommentFooter($msg)
$this.WriteCommentFooter($msg, $commentId)

return $this.NewComment($msg)
}
Expand Down
36 changes: 0 additions & 36 deletions tools/devops/automation/scripts/clean_past_comments.ps1

This file was deleted.

12 changes: 0 additions & 12 deletions tools/devops/automation/templates/api-diff-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ parameters:

stages:

- ${{ if parameters.isPR }}:
- stage: clean
displayName: 'Clean up'
dependsOn: []
jobs:
- job:
displayName: 'Clean comments'
pool:
vmImage: windows-latest
steps:
- template: ./common/clean.yml

- stage: configure_build
displayName: 'Configure'
jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ steps:
$converted = $inputContents + "`n`nUnable to convert markdown: $_`n`n"
}
$githubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $Env:GITHUB_TOKEN -Hash $Env:COMMENT_HASH
$result = $githubComments.NewCommentFromMessage("", "", $converted)
$result = $githubComments.NewCommentFromMessage("", "", $converted, "api diff")
displayName: 'Publish GitHub comment for change detection'
timeoutInMinutes: 10
continueOnError: true # don't let any failures here stop us
Expand Down
2 changes: 1 addition & 1 deletion tools/devops/automation/templates/build/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ steps:
- pwsh: |
Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\xamarin-macios\tools\devops\automation\scripts\MaciosCI.psd1
$githubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $(GitHub.Token) -Hash $Env:COMMENT_HASH
$githubComments.NewCommentFromMessage("Build failed", ":fire:", "Build failed for the job '$(System.JobDisplayName)'")
$githubComments.NewCommentFromMessage("Build failed", ":fire:", "Build failed for the job '$(System.JobDisplayName)'", "build")
condition: failed()
displayName: 'Report build failure'
env:
Expand Down
36 changes: 0 additions & 36 deletions tools/devops/automation/templates/common/clean.yml

This file was deleted.

4 changes: 2 additions & 2 deletions tools/devops/automation/templates/mac/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ steps:
$githubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $(GitHub.Token) -Hash $Env:COMMENT_HASH

if (Test-Path -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" -PathType Leaf) {
$githubComments.NewCommentFromFile("Tests on macOS ${{ parameters.statusContext }} failed", ":x:", "$Env:GITHUB_FAILURE_COMMENT_FILE")
$githubComments.NewCommentFromFile("Tests on macOS ${{ parameters.statusContext }} failed", ":x:", "$Env:GITHUB_FAILURE_COMMENT_FILE", "macOS ${{ parameters.statusContext }}")
} else {
$message = ":white_check_mark: **All** tests on macOS ${{ parameters.statusContext }} passed."
$githubComments.NewCommentFromMessage("Tests on macOS ${{ parameters.statusContext }} passed", ":computer:", $message)
$githubComments.NewCommentFromMessage("Tests on macOS ${{ parameters.statusContext }} passed", ":computer:", $message, "macOS ${{ parameters.statusContext }}")
}
displayName: 'Report results to GitHub'
timeoutInMinutes: 5
Expand Down
15 changes: 0 additions & 15 deletions tools/devops/automation/templates/main-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,6 @@ stages:
commit: ${{ parameters.commit }}
stageDisplayNamePrefix: ${{ parameters.stageDisplayNamePrefix }}

- ${{ if parameters.isPR }}:
- stage: clean
displayName: '${{ parameters.stageDisplayNamePrefix }}Clean up'
dependsOn: []
jobs:
- job:
displayName: 'Clean comments'
pool:
name: AzurePipelines-EO
demands:
- ImageOverride -equals 1ESPT-Windows2022

steps:
- template: ./common/clean.yml

- stage: configure_build
displayName: '${{ parameters.stageDisplayNamePrefix }}Configure'
dependsOn: ${{ parameters.dependsOn }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ steps:
Import-Module $Env:SYSTEM_DEFAULTWORKINGDIRECTORY\xamarin-macios\tools\devops\automation\scripts\MaciosCI.psd1
$artifact = New-ArtifactsFromJsonFile -Path "$Env:ARTIFACTS_JSON_PATH"
$gihubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $Env:GITHUB_TOKEN -Hash $Env:COMMENT_HASH
$result = $gihubComments.NewCommentFromObject("Artifacts", ":books:", $artifact)
$result = $gihubComments.NewCommentFromObject("Artifacts", ":books:", $artifact, "artifacts")
Write-Host $result
env:
GITHUB_TOKEN: $(GitHub.Token)
Expand Down
12 changes: 0 additions & 12 deletions tools/devops/automation/templates/tests-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,6 @@ parameters:

stages:

- ${{ if parameters.isPR }}:
- stage: clean
displayName: '${{ parameters.stageDisplayNamePrefix }}Clean up'
dependsOn: []
jobs:
- job:
displayName: 'Clean comments'
pool:
vmImage: windows-latest
steps:
- template: ./common/clean.yml

- stage: configure_build
displayName: '${{ parameters.stageDisplayNamePrefix }}Configure'
dependsOn: ${{ parameters.dependsOn }}
Expand Down
8 changes: 1 addition & 7 deletions tools/devops/automation/templates/tests/publish-html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ steps:

- template: download-artifacts.yml

- ${{ if parameters.isPR }}:
- template: ../common/clean.yml
parameters:
commentToHide: '[comment]: <> (This is a test result report added by Azure DevOps)'
checkoutCode: false # we already execute the 'clean.yml' template elsewhere in this job, and running 'checkout' again is useless (it also leads to duplicate step names which Azure DevOps doesn't like).

- pwsh: $(System.DefaultWorkingDirectory)/xamarin-macios/tools/devops/automation/scripts/show_env.ps1
displayName: 'Show Environment'

Expand Down Expand Up @@ -83,7 +77,7 @@ steps:
}

$gihubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $Env:GITHUB_TOKEN -Hash $Env:COMMENT_HASH -Debug
$result = $gihubComments.NewCommentFromObject("Test results", $emoji, $parallelResults)
$result = $gihubComments.NewCommentFromObject("Test results", $emoji, $parallelResults, "test results")
} catch {
Write-Host "##vso[task.complete result=Failed;]Failed to compute test summaries: $_"
New-GitHubComment -Header "Failed to compute test summaries on $Env:CONTEXT" -Emoji ":fire:" -Description "Failed to compute test summaries: $_."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ stages:
$githubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $Env:GITHUB_TOKEN -Hash $Env:COMMENT_HASH -Debug
$sb = [System.Text.StringBuilder]::new()
$sb.AppendLine("All tests have been skipped because the label 'skip-all-tests' was set.")
$result = $githubComments.NewCommentFromMessage("Test results", ":seedling:", $sb.ToString())
$result = $githubComments.NewCommentFromMessage("Test results", ":seedling:", $sb.ToString(), "test results")
displayName: 'Set comment'
env:
CONTEXT: ${{ parameters.statusContext }}
Expand Down
6 changes: 3 additions & 3 deletions tools/devops/automation/templates/windows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ steps:
$githubComments = New-GitHubCommentsObjectFromUrl -Url "$(Build.Repository.Uri)" -Token $(GitHub.Token) -Hash $Env:COMMENT_HASH

if (Test-Path -Path "$Env:GITHUB_FAILURE_COMMENT_FILE" -PathType Leaf) {
$githubComments.NewCommentFromFile("${{ parameters.statusContext }} failed", ":x:", "$Env:GITHUB_FAILURE_COMMENT_FILE")
$githubComments.NewCommentFromFile("${{ parameters.statusContext }} failed", ":x:", "$Env:GITHUB_FAILURE_COMMENT_FILE", "windows tests")
} elseif ("$($Env:AGENT_JOBSTATUS)" -ne "Succeeded") {
$message = ":x: $($Env:AGENT_JOBSTATUS) :x:"
$githubComments.NewCommentFromMessage("${{ parameters.statusContext }} failed", ":x:", $message)
$githubComments.NewCommentFromMessage("${{ parameters.statusContext }} failed", ":x:", $message, "windows tests")
} else {
$message = ":white_check_mark: **All** ${{ parameters.statusContext }} passed."
$githubComments.NewCommentFromMessage("${{ parameters.statusContext }} passed", ":computer:", $message)
$githubComments.NewCommentFromMessage("${{ parameters.statusContext }} passed", ":computer:", $message, "windows tests")
}
displayName: 'Report results to GitHub'
timeoutInMinutes: 5
Expand Down