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

feat: Workflow that runs each day and creates new issues for failing AVM pipelines #783

Merged
merged 23 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6a1cdfc
add pipeline for testing
rahalan Dec 7, 2023
bbf37b1
Merge branch 'Azure:main' into main
rahalan Dec 12, 2023
12c335c
Merge branch 'Azure:main' into main
rahalan Dec 18, 2023
31bed87
Merge branch 'Azure:main' into main
rahalan Dec 20, 2023
54700f3
Merge branch 'Azure:main' into main
rahalan Dec 22, 2023
fd8cec4
Merge branch 'Azure:main' into main
rahalan Jan 2, 2024
bf0238b
Merge branch 'Azure:main' into main
rahalan Jan 4, 2024
28d95ee
Merge branch 'Azure:main' into main
rahalan Jan 6, 2024
6c3e630
Merge branch 'Azure:main' into main
rahalan Jan 8, 2024
70d2d5d
Merge branch 'main' of https://github.com/rahalan/bicep-registry-modules
rahalan Jan 10, 2024
3eee400
add code
rahalan Jan 10, 2024
2cd9881
Update .github/workflows/avm.platform.manage-issue-for-failing-pipeli…
rahalan Jan 11, 2024
cd883ff
Update .github/workflows/avm.platform.manage-issue-for-failing-pipeli…
rahalan Jan 11, 2024
96bd2d9
update labels
rahalan Jan 11, 2024
dde5caa
rename pipeline
rahalan Jan 11, 2024
c842dda
rename file & function
rahalan Jan 11, 2024
1f8df93
Merge branch 'main' into users/rahalan/issueCreator
eriqua Jan 13, 2024
d26ff61
Update avm/utilities/pipelines/platform/Set-AvmGithubIssueForWorkflow…
rahalan Jan 13, 2024
48509b3
remove impact
rahalan Jan 14, 2024
3874946
Update avm/utilities/pipelines/platform/Set-AvmGithubIssueForWorkflow…
rahalan Jan 14, 2024
6223864
Update avm/utilities/pipelines/platform/Set-AvmGithubIssueForWorkflow…
rahalan Jan 14, 2024
5618f5f
Merge branch 'main' into users/rahalan/issueCreator
AlexanderSehr Jan 15, 2024
96b4a00
Merge branch 'main' into users/rahalan/issueCreator
eriqua Jan 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/avm.platform.manage-workflow-issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "avm.platform.manage-workflow-issue"

on:
schedule:
- cron: "30 5 * * *" # Every day at 5:30 am

jobs:
manage-issues:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: "Checkout"
uses: actions/checkout@v4
with:
fetch-depth: 0
- env:
GH_TOKEN: ${{ github.token }}
name: Manage issues
shell: pwsh
run: |
# Load used functions
. (Join-Path $env:GITHUB_WORKSPACE 'avm' 'utilities' 'pipelines' 'platform' 'Set-AvmGithubIssueForWorkflow.ps1')

$functionInput = @{
Repo = "${{ github.repository_owner }}/${{ github.event.repository.name }}"
LimitNumberOfRuns = 500
LimitInDays = 2
IgnoreWorkflows = @()
eriqua marked this conversation as resolved.
Show resolved Hide resolved
}

Write-Verbose "Invoke task with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose

Set-AvmGithubIssueForWorkflow @functionInput -Verbose # -WhatIf
128 changes: 128 additions & 0 deletions avm/utilities/pipelines/platform/Set-AvmGithubIssueForWorkflow.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<#
.SYNOPSIS
Check for failing pipelines and create issues for those, that are failing.

.DESCRIPTION
If a pipeline fails, a new issue will be created, with a link to the failed pipeline. If the issue is already existing, a comment will be added, if a new run failed (with the link for the new failed run). If a pipeline run succeeds and an issue is open for the failed run, it will be closed (and a link to the successful run is added to the issue).

.PARAMETER Repo
Mandatory. The name of the respository to scan. Needs to have the structure "<owner>/<repositioryName>"

.PARAMETER LimitNumberOfRuns
Optional. Number of recent runs to scan for failed runs. Default is 100.

.PARAMETER LimitInDays
Optional. Only runs in the past selected days will be analyzed. Default is 2.

.PARAMETER IgnoreWorkflows
Optional. List of workflow names that should be ignored (even if they fail, no ticket will be created). Default is an empty array.

.EXAMPLE
Set-AvmGithubIssueForWorkflow -Repo 'owner/repo01' -LimitNumberOfRuns 100 -LimitInDays 2 -IgnoreWorkflows @('Pipeline 01')

Check the last 100 workflow runs in the repository 'owner/repo01' that happened in the last 2 days. If the workflow name is 'Pipeline 01', then ignore the workflow run.

.NOTES
The function requires GitHub CLI to be installed.
#>
function Set-AvmGithubIssueForWorkflow {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
AlexanderSehr marked this conversation as resolved.
Show resolved Hide resolved
param (
[Parameter(Mandatory = $true)]
[string] $Repo,

[Parameter(Mandatory = $false)]
[int] $LimitNumberOfRuns = 100,

[Parameter(Mandatory = $false)]
[int] $LimitInDays = 2,

[Parameter(Mandatory = $false)]
[String[]] $IgnoreWorkflows = @()
)

$issues = gh issue list --state open --label 'Type: AVM :a: :v: :m:,Type: Bug :bug:' --json 'title,url,body,comments' --repo $Repo | ConvertFrom-Json -Depth 100
$runs = gh run list --json 'url,workflowName,headBranch,startedAt' --limit $LimitNumberOfRuns --repo $Repo | ConvertFrom-Json -Depth 100
$workflowRuns = @{}
$issuesCreated = 0
$issuesCommented = 0
$issuesClosed = 0

foreach ($run in $runs) {
$runStartTime = [Datetime]::ParseExact($run.startedAt, 'MM/dd/yyyy HH:mm:ss', $null)

if (($run.headBranch -eq 'main') -and ($runStartTime -gt (Get-Date).AddDays(-$LimitInDays)) -and ($IgnoreWorkflows -notcontains $run.workflowName)) {
$runId = $run.url.Split('/') | Select-Object -Last 1
$runDetails = gh run view $runId --json 'conclusion,number' --repo $Repo | ConvertFrom-Json -Depth 100

if ($run.workflowName.StartsWith("avm.")) {
eriqua marked this conversation as resolved.
Show resolved Hide resolved
if (-not $workflowRuns.ContainsKey($run.workflowName) -or $runDetails.number -gt $workflowRuns[$run.workflowName].number) {
$workflowRun = New-Object PSObject -Property @{
workflowName = $run.workflowName
number = $runDetails.number
url = $run.url
conclusion = $runDetails.conclusion
}

if (-not $workflowRuns.ContainsKey($run.workflowName)) {
$workflowRuns.Add($run.workflowName, $workflowRun)
}
else {
$workflowRuns[$run.workflowName] = $workflowRun
}
}
}
}
}

foreach ($workflowRun in $workflowRuns.values) {
if ($workflowRun.conclusion -eq 'failure') {
$issueName = "[Failed pipeline] $($workflowRun.workflowName)"
$failedrun = "Failed run: $($workflowRun.url)"

if ($issues.title -notcontains $issueName) {
if ($PSCmdlet.ShouldProcess("Issue [$issueName]", 'Create')) {
gh issue create --title "$issueName" --body "$failedrun" --label 'Type: AVM :a: :v: :m:,Type: Bug :bug:' --repo $Repo
}

$issuesCreated++
}
else {
$issue = ($issues | Where-Object { $_.title -eq $issueName })[0]

if (!$issue.body.Contains($failedrun)) {
rahalan marked this conversation as resolved.
Show resolved Hide resolved
if ($issue.comments.length -eq 0) {
if ($PSCmdlet.ShouldProcess("Issue [$issueName]", 'Add comment')) {
gh issue comment $issue.url --body $failedrun --repo $Repo
}

$issuesCommented++
}
else {
if (!$issue.comments.body.Contains($failedrun)) {
rahalan marked this conversation as resolved.
Show resolved Hide resolved
if ($PSCmdlet.ShouldProcess("Issue [$issueName]", 'Close')) {
gh issue comment $issue.url --body $failedrun --repo $Repo
}

$issuesCommented++
}
}
}
}
}
else {
$issueName = "[Failed pipeline] $($workflowRun.workflowName)"

if ($issues.title -contains $issueName) {
$issue = ($issues | Where-Object { $_.title -eq $issueName })[0]
$successfulrun = "successful run: $($workflowRun.url)"
gh issue close $issue.url --comment $successfulrun --repo $Repo
$issuesClosed++
}
}
}

Write-Verbose ('[{0}] issue(s){1} created' -f $issuesCreated, $($WhatIfPreference ? ' would have been' : ''))
Write-Verbose ('[{0}] issue(s){1} commented' -f $issuesCommented, $($WhatIfPreference ? ' would have been' : ''))
Write-Verbose ('[{0}] issue(s){1} closed' -f $issuesClosed, $($WhatIfPreference ? ' would have been' : ''))
}