Edit

Share via


Create a custom consumer for service hooks

Azure DevOps Services | Azure DevOps Server | Azure DevOps Server 2022 | Azure DevOps Server 2020

Use service hooks to notify non-Microsoft systems about events that occur in your project. A custom consumer sends an HTTP message to the endpoint defined in your extension's manifest.

Tip

If you're starting a new Azure DevOps extension, try these maintained sample collections first—they work with current product builds and cover modern scenarios (for example, adding tabs on pull request pages).

If a sample doesn't work in your organization, install it into a personal or test organization and compare the extension manifest's target IDs and API versions with the current docs. For reference and APIs, see:

This article walks through developing an extension that implements a sample consumer service, which includes the following events and actions.

  • Supported events that trigger the following actions:
    • Code pushed
    • Pull request created
    • Pull request updated
  • Supported actions to take when events occur:
    • Send an HTTP message

Note

This article refers to the home directory for your project as home.

Diagram that shows a sample consumer service sending HTTP messages for code push and pull request events.

For more information, see the Extension example GitHub repo. For a list of all supported events you can use as triggers for your custom consumer extension, see List of event types.

Tip

Check out our newest documentation on extension development using the Azure DevOps Extension SDK.

How service hooks work

Service hook publishers define a set of events. Subscriptions listen for the events and define actions that run when an event triggers.

Diagram that shows the service hook flow: publishers emit events, subscriptions match events, and actions run when an event matches a subscription.

This diagram shows the general service hook flow: publishers emit events, subscriptions match events, and actions run when a match occurs. In this article's example, an extension implements the consumer. When a supported event occurs, the consumer's configured action sends an HTTP message to the endpoint you specify in the extension manifest.

Create the extension

  1. See how to create your extension from scratch.

  2. Add the specific contribution for custom consumer implementation to your basic manifest file. See the following example of how your manifest should look after you add the contribution.

{
    "manifestVersion": 1,
    "id": "samples-service-hooks-consumer",
    "version": "0.1.2",
    "name": "Service Hooks Sample",
    "description": "A simple extension that demonstrates how to contribute a consumer service into service hooks.",
    "publisher": "fabrikam",
    "public": false,
    "icons": {
        "default": "images/logo.png"
    },
    "scopes": [],
    "files": [
        {
            "path": "images",
            "addressable": true
        }
    ],
    "content": {
        "details": {
            "path": "readme.md"
        }
    },
    "categories": [
        "Developer samples"
    ],
    "targets": [
        {
            "id": "Microsoft.VisualStudio.Services"
        }
    ],
    "contributions": [
        {
            "id": "consumer",
            "type": "ms.vss-servicehooks.consumer",
            "targets": [
                "ms.vss-servicehooks.consumers"
            ],
            "properties": {
                "id": "consumer",
                "name": "Sample Consumer",
                "description": "Sample consumer service",
                "informationUrl": "https://aka.ms/vsoextensions",
                "inputDescriptors": [
                    {
                        "id": "url",
                        "isRequired": true,
                        "name": "URL",
                        "description": "URL to post event payload to",
                        "inputMode": "textbox"
                    }
                ],
                "actions": [
                    {
                        "id": "performAction",
                        "name": "Perform action",
                        "description": "Posts a standard event payload",
                        "supportedEventTypes": [
                            "git.push",
                            "git.pullrequest.created",
                            "git.pullrequest.updated"
                        ],
                        "publishEvent": {
                            "url": "{{{url}}}",
                            "resourceDetailsToSend": "all",
                            "messagesToSend": "all",
                            "detailedMessagesToSend": "all"
                        }
                    }
                ]
            }
        }
    ]
}

Note

Remember to update the publisher property.

For each contribution in your extension, the manifest defines the following items.

  • Type of contribution - consumer service (ms.vss-servicehooks.consumer) in this case.
  • Contribution target - consumer services (ms.vss-servicehooks.consumers) in this case.
  • Properties that are specific to each type of contribution.

Consumers have the following properties.

Property Description
id Unique ID for your consumer service.
name Name of the custom consumer, which is visible during service hook subscription creation.
description Describes your consumer service.
informationUrl Find more info about your extension.
inputDescriptors Inputs to be used by users that are creating subscriptions with the consumer service.
actions Describes the actions to take and which events trigger those actions.

Actions for your consumer have the following properties:

Property Description
id ID for your action service.
name Name of the action.
description Detailed description of the action.
supportedEventTypes Array of trigger types for which this action can be used. For more information, see List of event types.
publishEvent.url The endpoint URL that receives the HTTP message. You can template this value with tokens from the inputDescriptors; users provide the actual values when they create the subscription.
  1. Deploy your extension to your Azure DevOps organization and test it.

Next steps