Created
June 21, 2020 16:41
-
-
Save smaglio81/bafaecd365a62d9690975e36be9bc674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-AzDServiceHook { | |
[CmdletBinding(DefaultParameterSetName = "ProjectName")] | |
param( | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectId", ValueFromPipelineByPropertyName = $true)] | |
[string] $ProjectId, | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectName", ValueFromPipelineByPropertyName = $true)] | |
[string] $ProjectName, | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectId", ValueFromPipelineByPropertyName = $true)] | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectName", ValueFromPipelineByPropertyName = $true)] | |
[ValidateSet("git.push","git.pullrequest.created","git.pullrequest.updated","build.complete")] | |
[string] $EventType, | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectId", ValueFromPipelineByPropertyName = $true)] | |
[Parameter(Mandatory = $true, ParameterSetName = "ProjectName", ValueFromPipelineByPropertyName = $true)] | |
[string] $ToUrl, | |
[Parameter(Mandatory = $true, ParameterSetName = "SubscriptionObject", ValueFromPipelineByPropertyName = $true)] | |
[Microsoft.VisualStudio.Services.ServiceHooks.WebApi.Subscription] $Subscription | |
) | |
if($PSCmdlet.ParameterSetName -ne "SubscriptionObject") { | |
$uri = New-Object System.Uri $ToUrl | |
if([string]::IsNullOrEmpty($ProjectId)) { | |
$project = Get-AzDProject -ProjectName $ProjectName | |
$projectId = $project.Id.ToString() | |
} | |
$newSub = New-Object Microsoft.VisualStudio.Services.ServiceHooks.WebApi.Subscription | |
$newSub.PublisherId = "tfs" | |
$newSub.EventType = $eventType | |
$newSub.EventDescription = "Any branch on any repository." | |
$newSub.ResourceVersion = "1.0" | |
$newSub.ConsumerId = "webHooks" | |
$newSub.ConsumerActionId = "httpRequest" | |
$newSub.ActionDescription = "To host $($uri.Host)" | |
$publisherInputs = New-Object 'System.Collections.Generic.Dictionary[System.String, System.String]' | |
$publisherInputs.Add("projectId", $projectId) | |
$publisherInputs.Add("tfsSubscriptionId", "") | |
switch($EventType) { | |
"git.push" { | |
$publisherInputs.Add("branch", "") | |
$publisherInputs.Add("repository", "") | |
$publisherInputs.Add("pushedBy", "") | |
} | |
"git.pullrequest.created" { | |
$publisherInputs.Add("branch", "") | |
$publisherInputs.Add("repository", "") | |
$publisherInputs.Add("pullrequestCreatedBy", "") | |
$publisherInputs.Add("pullrequestReviewersContains", "") | |
} | |
"git.pullrequest.updated" { | |
$publisherInputs.Add("branch", "") | |
$publisherInputs.Add("repository", "") | |
$publisherInputs.Add("pullrequestCreatedBy", "") | |
$publisherInputs.Add("pullrequestReviewersContains", "") | |
$publisherInputs.Add("notificationType", "") | |
} | |
"build.complete" { | |
$publisherInputs.Add("definitionName", "") | |
$publisherInputs.Add("buildStatus", "") | |
} | |
} | |
$newSub.PublisherInputs = $publisherInputs | |
$securityHeaderValue = Get-AzDSecurityHeaderValue # you'll come up with something | |
$consumerInputs = New-Object 'System.Collections.Generic.Dictionary[System.String, System.String]' | |
$consumerInputs.Add("acceptUntrustedCerts", "true") | |
$consumerInputs.Add("httpHeaders", "your-security-header:$securityHeaderValue") | |
$consumerInputs.Add("url", $ToUrl) | |
$newSub.ConsumerInputs = $consumerInputs | |
$Subscription = $newSub | |
} | |
$output = ($Subscription | ft -AutoSize Id, EventType, ConsumerId, Status, ActionDescription | Out-String) -split [Environment]::NewLine | |
Write-Verbose "Creating: $($output[3])" | |
$shpClient = $global:AzDModule.ServiceHooksPublisherClient | |
$response = $shpClient.CreateSubscriptionAsync($Subscription) | |
if($null -ne $response.Exception) { | |
if($null -ne $response.Exception.InnerException) { | |
throw $response.Exception.InnerException.Message | |
} else { | |
throw $response.Exception.Message | |
} | |
} | |
$result = $response.Result | |
return $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment