Azure DevOps Go API
repository·dev·Indexed 42 days ago
https://github.com/microsoft/azure-devops-go-apiGo language bindings for interacting with and managing Azure DevOps services. This library acts as a thin wrapper around the official Azure DevOps REST APIs, providing typed clients for functional areas such as Core, CIX (Continuous Integration eXperience), and Cloud Load Test. It supports authentication via Personal Access Tokens (PAT) or anonymous connections and includes utilities for pagination using ContinuationTokens and custom HTTP client configuration via ClientOptionFunc.
What's inside Azure DevOps Go API
- This library acts as a thin wrapper around the official Azure DevOps REST APIs. While the Go library provides typed clients and methods, the underlying logic and available endpoints are defined by the Azure DevOps REST API specification. For detailed information on specific API behaviors, parameters, and endpoints, refer to the Azure DevOps REST API reference.
How to get help and report issues
devIf you encounter bugs or wish to request new features for the Azure DevOps Go API, use GitHub Issues. Before creating a new issue, search the existing issue tracker to ensure your topic has not already been addressed, which helps prevent duplicate reports.Get started with the Azure DevOps Go API
devTo interact with Azure DevOps, you must establish a connection using your organization's URL and a Personal Access Token (PAT). Once a connection is established, you can create specific clients (e.g.,
core.NewClient) to perform operations within different functional areas of Azure DevOps.Note that many API responses use pagination via a
ContinuationToken. When a response contains aContinuationToken, you should pass it into the next API call using the correspondingArgsstruct to retrieve the subsequent page of results.package main import ( "context" "log" "strconv" "github.com/microsoft/azure-devops-go-api/azuredevops/v7" "github.com/microsoft/azure-devops-go-api/azuredevops/v7/core" ) func main() { organizationUrl := "https://dev.azure.com/myorg" // todo: replace value with your organization url personalAccessToken := "XXXXXXXXXXXXXXXXXXXXXXX" // todo: replace value with your PAT // Create a connection to your organization connection := azuredevops.NewPatConnection(organizationUrl, personalAccessToken) ctx := context.Background() // Create a client to interact with the Core area coreClient, err := core.NewClient(ctx, connection) if err != nil { log.Fatal(err) } // Get first page of the list of team projects for your organization responseValue, err := coreClient.GetProjects(ctx, core.GetProjectsArgs{}) if err != nil { log.Fatal(err) } index := 0 for responseValue != nil { // Log the page of team project names for _, teamProjectReference := range (*responseValue).Value { log.Printf("Name[%v] = %v", index, *teamProjectReference.Name) index++ } // if continuationToken has a value, then there is at least one more page of projects to get if responseValue.ContinuationToken != "" { continuationToken, err := strconv.Atoi(responseValue.ContinuationToken) if err != nil { log.Fatal(err) } // Get next page of team projects projectArgs := core.GetProjectsArgs{ ContinuationToken: &continuationToken, } responseValue, err = coreClient.GetProjects(ctx, projectArgs) if err != nil { log.Fatal(err) } } else { responseValue = nil } } }Use ETags for Dashboard Widget concurrency
devWhen retrieving or updating widgets, the API uses ETags to ensure version consistency.
- When calling
GetWidgets, the returnedWidgetsVersionedListcontains anETagfield. - When performing a
ReplaceWidgetsorUpdateWidgetsoperation, you can provide thisETagin theReplaceWidgetsArgsorUpdateWidgetsArgsto ensure you are modifying the expected version of the dashboard's widget collection.
- When calling
How the Client manages requests and sessions
devThe
Clientacts as the central engine for all API interactions. It handles:- URL Generation: It uses
ApiResourceLocation(retrieved from the server) androuteValuesto build the correct REST endpoint. - Request Construction: It automatically injects
Authorization,Accept(withapi-version),Content-Type, andUser-Agentheaders. - Session Management: It tracks a
SessionId(a UUID). If the Azure DevOps server provides a new session ID in theX-TFS-Sessionheader of a response, the client updates its internalSessionIdto ensure subsequent requests remain within the same session. - Error Handling: It uses
UnwrapErrorto parse error responses from the server, converting them into structuredWrappedErrortypes.
- URL Generation: It uses
Handle Azure DevOps API errors
devWhen a request fails (status code outside the 200-299 range),SendRequestcallsUnwrapError. This method attempts to parse the error body into aWrappedErrorstructure. If the error is returned as plain text, it returns aWrappedErrorcontaining the status and the text message. If it is JSON, it parses the error details provided by the Azure DevOps service.Initialize a V6 Policy Client
devTo manage policy configurations in Azure DevOps using the V6 API, use the
NewClientfunction. This requires an existingazuredevops.Connectionobject. The client provides methods to create, update, delete, and retrieve policy configurations and their revisions.import ( "context" "github.com/microsoft/azure-devops-go-api/azuredevops/v6" "github.com/microsoft/azure-devops-go-api/azuredevops/v6/policy" ) // Assuming 'connection' is an already initialized *azuredevops.Connection client, err := policy.NewClient(ctx, connection) if err != nil { // handle error }Initialize an Azure DevOps client
devTo interact with Azure DevOps, you must create a
Clientinstance. You can do this usingNewClientfor standard configurations orNewClientWithOptionsif you need to provide customClientOptionFuncmodifiers (such as a customhttp.Client).Both methods require a
*Connectionobject (which contains authorization and TLS settings) and abaseUrlstring representing the Azure DevOps organization URL.// Using NewClient client := azuredevops.NewClient(connection, "https://dev.azure.com/your-org") // Using NewClientWithOptions for custom configuration client := azuredevops.NewClientWithOptions(connection, "https://dev.azure.com/your-org", azuredevops.WithHTTPClient(myCustomHttpClient))Initialize a CIX client
devTo use the CIX (Continuous Integration eXperience) client, use the
NewClientfunction. This requires an existingazuredevops.Connectionobject. TheNewClientfunction retrieves the appropriate client for the connection's base URL and returns aClientinterface used to manage project connections and resources.client := cix.NewClient(ctx, connection)Initialize the Extension Management Client
devTo manage installed extensions in Azure DevOps, use
NewClientfrom theextensionmanagementpackage. This requires an existingazuredevops.Connectionobject. The client provides access to preview APIs for installing, uninstalling, listing, and updating extensions.package main import ( "context" "github.com/microsoft/azure-devops-go-api/azuredevops/v6" "github.com/microsoft/azure-devops-go-api/azuredevops/v6/extensionmanagement" ) func main() { ctx := context.Background() // connection is an existing *azuredevops.Connection client, err := extensionmanagement.NewClient(ctx, connection) if err != nil { panic(err) } _ = client }Initialize the Contributions Client
devTo interact with the Azure DevOps Contributions API (used for querying installed extensions and contribution nodes), use the
NewClientfunction. This requires an existingazuredevops.Connectionobject.Note that all methods in this client are currently marked as [Preview API].
import ( "context" "github.com/microsoft/azure-devops-go-api/azuredevops/v7" "github.com/microsoft/azure-devops-go-api/azuredevops/v7/contributions" ) // Assuming 'connection' is an already initialized *azuredevops.Connection client, err := contributions.NewClient(ctx, connection) if err != nil { // handle error }client, err := contributions.NewClient(ctx, connection)Initialize the Cloud Load Test client
devTo use the Cloud Load Test API, create a new client using
NewClient. This requires acontext.Contextand an existingazuredevops.Connectionobject. The client provides methods to manage agent groups, test definitions, test drops, and test runs.client, err := cloudloadtest.NewClient(ctx, connection) if err != nil { // handle error }