0

Wondering if someone knows how to do this as I've been struggling to do so.

I'm attempting to put together a PowerShell script that will hopefully update any service hooks which we have setup in Azure DevOps, so their statuses become enabled. Occasionally ADO will disable them, I think due to inactivity for a long period of time, which is a pain.

So far I have the following but I'm getting an error saying

"The notification subscription "2092cfc4-a95b-4800-976e-67ccf9deb4b1" for service hook subscription "2e0a69e7-c4aa-44ad-89d7-ca1e3809585e" no longer exists."

enter image description here

I understand that but not sure on how to fix it. I'm sure it exists because if I take $uri2, populate the subscriptionId with one of the $item variables in the loop and then put into a browser, I get the json returned like this so it's finding it right?

enter image description here

I've been referencing the various documentation on this page: https://learn.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/replace%20subscription?view=azure-devops-rest-5.1

I thought that I'd first need to get a list of all the web hook subscriptions and then iterate through each one grabbing the Id, using that Id within another request to update the service hook. I suspect half the problem is to do with my understanding on how it's done, if at all possible so thought I'd ask here.

Has anyone accomplished this or perhaps have some example script on how you might go about updating a field? Not sure if I need to pass more data into the body or not.

cls

$User = 'myUser'
$PersonalAccessToken = 'myPatToken'
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f  $User, $PersonalAccessToken)))
$vstsAccount = "myOrgName"

$uri1 = "https://dev.azure.com/$vstsAccount/_apis/hooks/subscriptions?api-version=5.1"

$hooks = Invoke-RestMethod -Method Get -ContentType application/json -Uri $uri1 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)}

foreach ($item in $hooks.value) {

    $body = @{
        "publisherId" = "tfs"
        "eventType" = "$($item.eventType)"
        "resourceVersion" = "1.0"
        "consumerId" = "webHooks"
        "consumerActionId" = "httpRequest"
        "status" = "enabled"
    }

    $bodyJson = $body | ConvertTo-Json

    write-host "current status: $($item.status)"
    write-host "$($item.id)"
    write-host "$($item.eventType)"

    $uri2 = "https://dev.azure.com/$vstsAccount/_apis/hooks/subscriptions/$($item.id)?api-version=5.1"
    write-host $uri2
    Invoke-RestMethod -Method Put -ContentType application/json -Uri $uri2 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $bodyJson
    
}
Paul Wilson
  • 45
  • 1
  • 5

1 Answers1

0

Update Service Hook in Azure DevOps Using PowerShell

I could reproduce this issue with your request body.

That because we lost some essential parameters in the request body:

enter image description here

You can see from the picture above that if I comment out the parameter projectId, then I will get the same error as you.

According to the sample of REST API Subscriptions - Replace Subscription, we could get the request body:

{
  "publisherId": "tfs",
  "eventType": "build.complete",
  "resourceVersion": "1.0-preview.1",
  "consumerId": "webHooks",
  "consumerActionId": "httpRequest",
  "publisherInputs": {
    "buildStatus": "Failed",
    "definitionName": "MyWebSite CI",
    "projectId": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c"
  },
  "consumerInputs": {
    "url": "https://myservice/myhookeventreceiver"
  }
}

So, we could use this request body to invoke the REST API.

Note:

  • Use : instead of = in your request body.
  • Replace the value match your subscription in the request body and add additional parameters according to your needs.

In addition, I see that you use foreach to update all Subscriptions, but you need to pay special attention that not all Subscription's publisherId, consumerId, consumerActionId are the same. You should pay extra attention to this when modifying in bulk.

The test result:

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    That's fantastic, I thought it would be something like that but just wasn't sure. You've given me all the info I need to fix it now so thanks again. I shall make the changes needed and get it working. – Paul Wilson Jun 26 '20 at 09:39
  • 1
    Sure, I think I did that right, clicking the green tick :) – Paul Wilson Jun 26 '20 at 11:00