如何通过Azure DevOps API获取特定仓库对应的发布信息,并识别发布管道关联的仓库以实现审批自动化?
Hey Stephanie, let's break down your Azure DevOps API questions step by step—this kind of automation for release approvals is super common, so I’ve got actionable steps for you.
To map a repo to its related releases, you’ll need to chain a few API calls (since releases are tied to build pipelines, which are tied to repos):
Step 1: Grab your target repo’s ID
First, fetch all repos in your project and filter for the one you care about. Use this API endpoint:GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=7.1-preview.1Look for the
idfield in the response object matching your repo name—you’ll need this for the next step.Step 2: Find build pipelines (definitions) linked to the repo
Next, pull all build definitions in your project and filter those that reference your target repo. Use:GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=7.1-preview.7Each build definition has a
repositoryobject with anidfield. Filter the results to only keep definitions whererepository.idmatches your repo’s ID from Step 1. Save these build definition IDs—they’re your link to releases.Step 3: Fetch releases tied to those build pipelines
Finally, get all releases and filter for ones triggered by your target build definitions. Use:GET https://dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1-preview.8Check the
artifactsarray in each release object. Look fordefinitionReference.definition.id—this matches the build definition ID from Step 2. Any release with a matching ID is tied to your target repo.Pro tip: To avoid filtering locally, you can pass multiple build definition IDs directly in the API call with
definitionId=123,456(replace with your IDs) to narrow results upfront.
Great news—you didn’t miss a hidden API! The build definition API itself includes all repository details for a pipeline.
To get the repo linked to a specific build pipeline, call:
GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.1-preview.7
In the response, look for the repository object—this includes:
id: The repo’s unique IDname: The repo’s display nameurl: The repo’s API endpointtype: The repo type (e.g., Git)
For YAML pipelines, you might also see a yamlRepository field with additional context, but the repository field is the primary source for repo association regardless of pipeline type.
A quick shortcut: If you want to find all pipelines linked to a specific repo in one go, use the build definition API with a filter:
GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions?$filter=repository/id eq '{repoId}'&api-version=7.1-preview.7
This returns only build definitions tied to your target repo, no manual filtering needed.
Don’t forget: All these API calls require a Personal Access Token (PAT) with at least Build (Read) and Release (Read) permissions to work.
内容的提问来源于stack exchange,提问作者Stephanie

