Skip to content

Commit

Permalink
(#3) Adds Discord Notification Plugin
Browse files Browse the repository at this point in the history
Adds a Discord notification plugin, similar to the Gitter implementation.

Has two options for posting, a simple text output identical to the default Gitter notification and a more complex Discord-specific card.
  • Loading branch information
JPRuskin authored and corbob committed Mar 24, 2024
1 parent 4990b79 commit be245be
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/Plugins/Discord.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<#
.SYNOPSIS
Publishes the package update status to Discord.
.LINK
https://discord.com/developers/docs/resources/webhook#execute-webhook
#>
[CmdletBinding(DefaultParameterSetName="Url")]
param(
[Parameter(Mandatory)]
$Info,

# This is the Discord Webhook ID
[Parameter(Mandatory, ParameterSetName="Id")]
[string]$WebhookId,

# This is the Discord webhook token.
[Parameter(Mandatory, ParameterSetName="Id")]
[string]$WebhookToken,

# This is the full custom webhook url created in Discord.
[Parameter(Mandatory, ParameterSetName="Url")]
[string]$WebHookUrl = "https://discord.com/api/webhooks/$($WebhookId)/$($WebhookToken)",

# If set, just sends a simple text message.
[switch]$Simple
)

if ($WebHookUrl -eq "https://discord.com/api/webhooks//") { return } # If we don't have a valid webhookurl we can't push status messages, so ignore.

$updatedPackages = @($Info.result.updated).Count
$publishedPackages = @($Info.result.pushed).Count
$failedPackages = $Info.error_count.total
$gistUrl = $Info.plugin_results.Gist -split '\n' | Select-Object -Last 1
$packageCount = $Info.result.all.Length

$Body = if ($Simple) {
@{
content =
"[Update Status: $($packageCount) packages.`n" +
"$($updatedPackages) updated, $($publishedPackages) Published, $($failedPackages) Failed]($($gistUrl))"
}
} else {
@{
embeds = @(
@{
title = "Packages Updated"
description = "$($packageCount) packages were updated$(if ($failedPackages -gt 0) {", with **$($failedPackages) failure**"})."
timestamp = Get-Date $Info.startTime -Format "yyyy-MM-ddThh:mm:ss.000zzz"
url = $gistUrl
color = if ($failedPackages -gt 0) {"15548997"} else {"0"}
fields = @(
@{name = "Total"; value = "$($packageCount)"; inline = $false}
@{name = "Updated"; value = "$($updatedPackages)"; inline = $true}
@{name = "Published"; value = "$($publishedPackages)"; inline = $true}
@{name = "Failed"; value = "$($failedPackages)"; inline = $true}
)
footer = @{
text = "Run took $($Info.minutes) minutes."
}
}
)
}
}

$arguments = @{
Body = $Body | ConvertTo-Json -Depth 5
UseBasicParsing = $true
Uri = $WebHookUrl
ContentType = 'application/json'
Method = 'POST'
}

Write-Host "Submitting message to Discord"
Invoke-RestMethod @arguments
Write-Host "Message submitted to Discord"
2 changes: 1 addition & 1 deletion src/Plugins/Gitter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Publishes the package update status to gitter.
.PARAMETER WebHookUrl
This is the cusotm webhook url created through gitter integrations.
This is the custom webhook url created through gitter integrations.
.PARAMETER MessageFormat
The format of the message that is meant to be published on gitter.
Expand Down

0 comments on commit be245be

Please sign in to comment.