Skip to content

Commit

Permalink
Improve slack notitification cmdlets
Browse files Browse the repository at this point in the history
Specifically, ability to disable specific triggers
  • Loading branch information
chris-peterson committed Feb 20, 2023
1 parent 85bf901 commit bce886a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/GitlabCli/GitlabCli.psd1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@{
ModuleVersion = '1.86.0'
ModuleVersion = '1.86.1'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE'
ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab'
ReleaseNotes = 'https://github.com/chris-peterson/pwsh-gitlab/issues/67'
ReleaseNotes = 'tweaks to slack integration settings'
}
}

Expand Down
65 changes: 43 additions & 22 deletions src/GitlabCli/Integrations.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function Enable-GitlabProjectSlackNotification {

[Parameter()]
[string]
$Username = '',
$Username,

[Parameter()]
[string]
Expand All @@ -115,18 +115,23 @@ function Enable-GitlabProjectSlackNotification {

[Parameter()]
[string]
[ValidateSet('true', 'false')]
$NotifyOnlyBrokenPipelines = 'true',
[ValidateSet($null, 'true', 'false')]
$NotifyOnlyBrokenPipelines,

[Parameter()]
[ValidateSet('true', 'false')]
[ValidateSet($null, 'true', 'false')]
[string]
$JobEvents = 'false',
$JobEvents,

[Parameter(ParameterSetName='SpecificEvents', Position=1, Mandatory)]
[ValidateSet('commit', 'confidential_issue', 'confidential_note', 'deployment', 'issue', 'merge_request', 'note', 'pipeline', 'push', 'tag_push', 'wiki_page')]
[string []]
$OnEvent,
$Enable,

[Parameter(ParameterSetName='SpecificEvents', Position=1, Mandatory)]
[ValidateSet('commit', 'confidential_issue', 'confidential_note', 'deployment', 'issue', 'merge_request', 'note', 'pipeline', 'push', 'tag_push', 'wiki_page')]
[string []]
$Disable,

[Parameter(ParameterSetName='AllEvents')]
[switch]
Expand All @@ -140,36 +145,52 @@ function Enable-GitlabProjectSlackNotification {
$Project = Get-GitlabProject $ProjectId -SiteUrl $SiteUrl

if ($AllEvents) {
$OnEvent = @('commit', 'confidential_issue', 'confidential_note', 'deployment', 'issue', 'merge_request', 'note', 'pipeline', 'push', 'tag_push', 'wiki_page')
$Enable = @('commit', 'confidential_issue', 'confidential_note', 'issue', 'merge_request', 'note', 'pipeline', 'push', 'tag_push', 'wiki_page')
}

if (-not $Webhook) {
$ExistingIntegration = $null
try {
$ExistingIntegration = Get-GitlabProjectIntegration -ProjectId $Project.Id -Integration 'slack'
$Webhook = $ExistingIntegration.Properties.webhook
$Webhook = $(Get-GitlabProjectIntegration -ProjectId $Project.Id -Integration 'slack').Properties.webhook
}
catch {
Write-Error "Webhook was not supplied and could not be derived from an existing integration"
throw "Webhook could not be derived from an existing integration (provide via -Webhook parameter)"
}
}

$Settings = @{
channel = $Channel
webhook = $Webhook
username = $Username
branches_to_be_notified = $BranchesToBeNotified
notify_only_broken_pipelines = $NotifyOnlyBrokenPipelines
job_events = $JobEvents
webhook = $Webhook
}
if ($PSBoundParameters.ContainsKey('Username')) {
$Settings.username = $Username
}
if ($BranchesToBeNotified) {
$Settings.branches_to_be_notified = $BranchesToBeNotified
}
if ($NotifyOnlyBrokenPipelines) {
$Settings.notify_only_broken_pipelines = $NotifyOnlyBrokenPipelines
}
if ($JobEvents) {
$Settings.job_events = $JobEvents
}

$OnEvent | ForEach-Object {
$Settings."$($_)_channel" = $Channel
$Settings."$($_)_events" = 'true'
$Settings."$($_)s_events" = 'true' # this GitLab API has plurality inconsistencies
$ShouldPluralize = @(
'confidential_issue',
'issue',
'merge_request'
)

$Enable | ForEach-Object {
$Settings."$_`_channel" = $Channel
$EventProperty = $ShouldPluralize.Contains($_) ? "$($_)s_events" : "$($_)_events"
$Settings.$EventProperty = 'true'
}
$Disable | ForEach-Object {
$Settings."$_`_channel" = $Channel
$EventProperty = $ShouldPluralize.Contains($_) ? "$($_)s_events" : "$($_)_events"
$Settings.$EventProperty = 'false'
}

if ($PSCmdlet.ShouldProcess("slack notifications for $($Project.PathWithNamespace)", "notify $Channel on ($($OnEvent -join ', '))")) {
if ($PSCmdlet.ShouldProcess("slack notifications for $($Project.PathWithNamespace)", "notify $Channel ($($Settings | ConvertTo-Json)))")) {
Update-GitlabProjectIntegration -ProjectId $Project.Id -Integration 'slack' -Settings $Settings
}
}

0 comments on commit bce886a

Please sign in to comment.