OCram85/DroneHelper
OCram85
/
DroneHelper
Archived
1
0
Fork 0
This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
DroneHelper/src/PRComment/Send-PRComment.ps1

181 lines
6.9 KiB
PowerShell

function Send-PRComment {
<#
.SYNOPSIS
Sends build report as Gitea PR comment.
.DESCRIPTION
Send-PRComment is used to report the build details from drone.io pipeline.
.PARAMETER Mode
Sets the report mode. Default is 'Renew'. This mode deletes the old pr comments and creates a new onw.
Also available:
- 'Add' -> simply adds new pr comments.
- 'Edit' -> Edits the last known pr comment. Doesn't clean old ones.
.INPUTS
[None].
.OUTPUTS
[None]
.EXAMPLE
Send-PRComment
Depends on Drone.IO injected environment vars. Doesn't work locally on dev systems.
.NOTES
#>
[CmdletBinding()]
#[OutputType([string])]
param (
[Parameter(Mandatory = $false, HelpMessage = 'HelpMessage')]
[ValidateSet('Add', 'Edit', 'Renew')]
[string]$Mode = 'Renew',
[Parameter(Mandatory = $false, HelpMessage = 'Gitea user for drone bot')]
[ValidateNotNullOrEmpty()]
[string]$GiteaUser = 'drone-bot'
)
begin {
# workaround for false positive PSReviewUnusedParameter
$null = $GiteaUser
}
process {
$Repo = Get-RepoPath
$Workspace = $Repo.Root
Write-Debug -Message ('Workspace: {0}' -f $Workspace)
$PRCommentFile = Join-Path -Path $Workspace -ChildPath 'pr_comment.log'
Write-Debug -Message ('PRCommentFile: {0}' -f $PRCommentFile)
$PipelineStateFile = Join-Path -Path $Workspace -ChildPath 'failure.log'
Write-Debug -Message ('PipelineStateFile: {0}' -f $PipelineStateFile)
Write-Debug -Message ('CUSTOM_PIPELINE_STATE: {0}' -f $Env:CUSTOM_PIPELINE_STATE)
if ($Env:CUSTOM_PIPELINE_STATE -eq $true) {
if (Test-Path $PipelineStateFile) {
Write-Debug -Message ('Setting custom pipeline status to failed')
$PipelineState = 'failed'
}
else {
Write-Debug -Message ('Setting custom pipeline status to success')
$PipelineState = 'success'
}
}
else {
Write-Debug -Message ('Setting global drone status {0}' -f $Env:DRONE_BUILD_STATUS)
$PipelineState = $Env:DRONE_BUILD_STATUS
}
if ($Env:GITEA_BASE) {
$GiteaBase = $Env:GITEA_BASE
}
else {
$GiteaBase = 'https://gitea.ocram85.com'
}
$APIHeaders = @{
accept = 'application/json'
'Content-Type' = 'application/json'
}
# Can be used with POST method to add new comment. Used with GET method returns all comments.
$CommentAPICall = ('{0}/api/v1/repos/{1}/{2}/issues/{3}/comments?access_token={4}' -f
$GiteaBase,
$Env:DRONE_REPO_OWNER,
$Env:DRONE_REPO_NAME,
$Env:DRONE_PULL_REQUEST,
$Env:GITEA_TOKEN
)
# Update Comment API endpoint: 0 - GiteaBase, 1 - Owner, 2- Repo, 3 - PR, 4 - Token
# Method Delete - removes the given comment. Patch - updates the given comment.
$UpdateAPICall = '{0}/api/v1/repos/{1}/{2}/issues/comments/{3}?access_token={4}'
if ($Mode -eq 'Renew') {
$Comments = Invoke-RestMethod -Method 'Get' -Headers $APIHeaders -Uri $CommentAPICall
$DroneComments = $Comments | Where-Object {
$_.user.login -eq $GiteaUser
} | Select-Object -ExpandProperty 'id'
Write-Debug -Message ('Found Drone comments: {0}.' -f ($DroneComments -join ', '))
foreach ($id in $DroneComments) {
$ExtAPI = $UpdateAPICall -f @(
$GiteaBase,
$Env:DRONE_REPO_OWNER,
$Env:DRONE_REPO_NAME,
$id,
$Env:GITEA_TOKEN
)
Write-Debug -Message ('Exec API Call: {0}' -f $ExtAPI)
Invoke-RestMethod -Method 'Delete' -Headers $APIHeaders -Uri $ExtAPI
}
}
if ($Mode -eq 'Edit') {
$Comments = Invoke-RestMethod -Method 'Get' -Headers $APIHeaders -Uri $CommentAPICall
$DroneComments = $Comments | Where-Object {
$_.user.login -eq 'drone'
} | Select-Object -ExpandProperty 'id'
Write-Debug -Message ('Found Drone comments: {0}.' -f ($DroneComments -join ', '))
$EditId = $DroneComments | Sort-Object | Select-Object -Last 1
Write-Debug -Message ('Edit Comment with id {0}' -f $EditId)
}
$PRCommentHeader = ('> Drone.io PR Build No. [#{0}]({1}://{2}/{3}/{4}): ``{5}``' -f
$Env:DRONE_BUILD_NUMBER,
$Env:DRONE_SYSTEM_PROTO,
$Env:DRONE_SYSTEM_HOST,
$Env:DRONE_REPO,
$Env:DRONE_BUILD_NUMBER,
$PipelineState
)
$PRCommentHeader | Out-File -FilePath $PRCommentFile -Encoding 'utf8'
$LogFiles = (Get-ChildItem -Path $Env:LOG_FILES -File).FullName
foreach ($file in $LogFiles) {
if (Test-Path -Path $file) {
('#### ``{0}``' -f $file) | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8'
$fileContent = Get-Content -Path $file -Raw -Encoding utf8
$fileContent | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8'
[Environment]::NewLine | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8' -NoNewline
}
else {
Write-Warning -Message ('Given file {0} not found!' -f $file)
('##### ``{0}`` not found!' -f $file) | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8'
}
}
if ($Mode -eq 'Edit') {
'Last mod: {0}' -f (Get-Date -Format 'u') | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8'
}
'end.' | Out-File -FilePath $PRCommentFile -Append -Encoding 'utf8'
$PRCommentJSON = ConvertTo-Json -InputObject @{
Body = Get-Content -Path $PRCommentFile -Encoding utf8 -Raw
}
Write-Debug -Message ('PR JSON body has a size of {0} chars' -f $PRCommentJSON.length)
if ($Mode -ne 'Edit') {
Write-Debug -Message 'Adding new Comment.'
Invoke-RestMethod -Method 'Post' -Headers $APIHeaders -Uri $CommentAPICall -Body $PRCommentJSON
}
else {
$ExtAPI = $UpdateAPICall -f @(
$GiteaBase,
$Env:DRONE_REPO_OWNER,
$Env:DRONE_REPO_NAME,
$EditId,
$Env:GITEA_TOKEN
)
Write-Debug -Message 'Edit last comment.'
Invoke-RestMethod -Method 'Patch' -Headers $APIHeaders -Uri $ExtAPI -Body $PRCommentJSON
}
}
end {
}
}