关于 azure-devops
azure-devops 是使用 Azure DevOps REST API 的指导技能,涵盖工作项、代码库、管道及相关服务的操作。它提供常见请求模式,并强调生成代码不得包含 PAT 凭据。
主要功能
- 01工作项与 WIQL 操作
- 02代码库、分支和拉取请求 API
- 03构建、发布和管道管理
- 04认证与凭据安全指导
适用场景
- 01自动更新和查询工作项
- 02检查代码库并管理拉取请求
- 03启动构建并查看管道结果
- 04编写不硬编码 PAT 的 API 脚本
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ markpitt/ claude-skills --skill azure-devops安装后在 Claude Code 中输入/azure-devops确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
resources/advanced-integrations.mdartifacts-packages.mdboards-work-tracking.mdorganization-security.mdpipelines-cicd.mdrepos-version-control.mdtest-plans-quality.md详细介绍
Azure DevOps API 技能
安全
绝不要输出、建议或生成包含 PAT 明文值的代码。 始终通过环境变量(例如 $AZURE_DEVOPS_PAT)或 Azure Key Vault 等密钥管理器引用凭据。生成脚本或 curl 示例时,请使用占位变量引用,绝不要使用实际令牌字符串。
此技能全面介绍如何使用 Azure DevOps REST API,帮助你以编程方式访问所有 Azure DevOps Services 和 Azure DevOps Server 资源。
概览
Azure DevOps REST API 是一种 RESTful Web API,可用于访问和管理所有 Azure DevOps 服务中的工作项、存储库、管道、测试计划、工件等。
基础 URL: https://dev.azure.com/{organization}/{project}/_apis/{area}/{resource}?api-version={version}
- 组织: 你的 Azure DevOps 组织名称
- 项目: 项目名称(组织级资源可选)
- API 版本: 所有请求都必须指定(例如
7.1、7.0、6.0) - 身份验证: 个人访问令牌(PAT)、OAuth 2.0 或 Azure AD
快速入门
身份验证要求
Azure DevOps 支持多种身份验证方式:
- 个人访问令牌(PAT) - 最常用于脚本和集成
- OAuth 2.0 - 用于 Web 应用程序
- Azure Active Directory - 用于企业应用程序
- SSH 密钥 - 仅用于 Git 操作
基本 PAT 身份验证
GET https://dev.azure.com/{organization}/_apis/projects?api-version=7.1
Authorization: Basic {base64-encoded-PAT}
要对 PAT 进行编码:base64(":{PAT}") — 注意 PAT 前面的冒号。请始终从环境变量(例如 $AZURE_DEVOPS_PAT)读取 PAT,而不要在脚本或输出中硬编码。
常见请求模式
GET https://dev.azure.com/{organization}/{project}/_apis/{resource}?api-version=7.1
Authorization: Basic {encoded-PAT}
Content-Type: application/json
核心服务
Azure DevOps 分为多个主要服务领域。每个领域都有自己的 REST API:
Azure Boards - 工作项跟踪
工作项
- 创建工作项:
POST /{organization}/{project}/_apis/wit/workitems/${type}?api-version=7.1 - 获取工作项:
GET /{organization}/{project}/_apis/wit/workitems/{id}?api-version=7.1 - 更新工作项:
PATCH /{organization}/{project}/_apis/wit/workitems/{id}?api-version=7.1 - 删除工作项:
DELETE /{organization}/{project}/_apis/wit/workitems/{id}?api-version=7.1
请求正文使用 JSON Patch 格式:
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "New bug report"
},
{
"op": "add",
"path": "/fields/System.AssignedTo",
"value": "[email protected]"
}
]
查询
- 运行已保存的查询:
GET /{organization}/{project}/_apis/wit/wiql/{id}?api-version=7.1 - 运行 WIQL 查询:
POST /{organization}/{project}/_apis/wit/wiql?api-version=7.1{ "query": "SELECT [System.Id], [System.Title] FROM WorkItems WHERE [System.WorkItemType] = 'Bug' AND [System.State] = 'Active'" }
看板和待办列表
- 获取看板:
GET /{organization}/{project}/{team}/_apis/work/boards?api-version=7.1 - 获取待办列表项:
GET /{organization}/{project}/{team}/_apis/work/backlogs/{backlogId}/workItems?api-version=7.1 - 获取迭代:
GET /{organization}/{project}/{team}/_apis/work/teamsettings/iterations?api-version=7.1 - 获取容量:
GET /{organization}/{project}/{team}/_apis/work/teamsettings/iterations/{iterationId}/capacities?api-version=7.1
工作项类型和字段
- 列出工作项类型:
GET /{organization}/{project}/_apis/wit/workitemtypes?api-version=7.1 - 列出字段:
GET /{organization}/{project}/_apis/wit/fields?api-version=7.1 - 获取字段:
GET /{organization}/{project}/_apis/wit/fields/{fieldNameOrRefName}?api-version=7.1
区域和迭代路径
- 获取区域:
GET /{organization}/{project}/_apis/wit/classificationnodes/areas?api-version=7.1 - 获取迭代:
GET /{organization}/{project}/_apis/wit/classificationnodes/iterations?api-version=7.1 - 创建区域:
POST /{organization}/{project}/_apis/wit/classificationnodes/areas?api-version=7.1
Azure Repos - 源代码管理
Git 仓库
- 列出仓库:
GET /{organization}/{project}/_apis/git/repositories?api-version=7.1 - 获取仓库:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}?api-version=7.1 - 创建仓库:
POST /{organization}/{project}/_apis/git/repositories?api-version=7.1 - 删除仓库:
DELETE /{organization}/{project}/_apis/git/repositories/{repositoryId}?api-version=7.1
提交
- 获取提交:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?api-version=7.1 - 获取提交:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=7.1 - 获取提交更改:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=7.1
分支
- 获取分支:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=heads/&api-version=7.1 - 创建分支:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=7.1 - 删除分支:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=7.1[ { "name": "refs/heads/feature-branch", "oldObjectId": "0000000000000000000000000000000000000000", "newObjectId": "{commitId}" } ]
拉取请求
- 获取拉取请求:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=7.1 - 获取拉取请求:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=7.1 - 创建拉取请求:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=7.1{ "sourceRefName": "refs/heads/feature", "targetRefName": "refs/heads/main", "title": "PR Title", "description": "PR Description" } - 更新拉取请求:
PATCH /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=7.1 - 获取 PR 审核者:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/reviewers?api-version=7.1 - 添加 PR 审核者:
PUT /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/reviewers/{reviewerId}?api-version=7.1 - 获取 PR 工作项:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/workitems?api-version=7.1 - 获取 PR 讨论串:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/threads?api-version=7.1 - 添加 PR 评论:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}/threads?api-version=7.1
推送
- 获取推送记录:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=7.1 - 获取推送记录:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes/{pushId}?api-version=7.1
项目(文件和文件夹)
- 获取项目:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&api-version=7.1 - 获取项目内容:
GET /{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&download=true&api-version=7.1 - 批量获取项目:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/itemsbatch?api-version=7.1
策略
- 获取策略配置:
GET /{organization}/{project}/_apis/policy/configurations?api-version=7.1 - 创建策略:
POST /{organization}/{project}/_apis/policy/configurations?api-version=7.1
Azure Pipelines - CI/CD
Build Definitions (Pipelines)
- List definitions:
GET /{organization}/{project}/_apis/build/definitions?api-version=7.1 - Get definition:
GET /{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.1 - Create definition:
POST /{organization}/{project}/_apis/build/definitions?api-version=7.1 - Update definition:
PUT /{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.1 - Delete definition:
DELETE /{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.1
Builds
- Queue build:
POST /{organization}/{project}/_apis/build/builds?api-version=7.1{ "definition": { "id": 123 }, "sourceBranch": "refs/heads/main" } - Get builds:
GET /{organization}/{project}/_apis/build/builds?api-version=7.1 - Get build:
GET /{organization}/{project}/_apis/build/builds/{buildId}?api-version=7.1 - Update build:
PATCH /{organization}/{project}/_apis/build/builds/{buildId}?api-version=7.1 - Delete build:
DELETE /{organization}/{project}/_apis/build/builds/{buildId}?api-version=7.1 - Get build logs:
GET /{organization}/{project}/_apis/build/builds/{buildId}/logs?api-version=7.1 - Get build timeline:
GET /{organization}/{project}/_apis/build/builds/{buildId}/timeline?api-version=7.1 - Get build artifacts:
GET /{organization}/{project}/_apis/build/builds/{buildId}/artifacts?api-version=7.1
Release Definitions
- List definitions:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=7.1 - Get definition:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=7.1 - Create definition:
POST https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=7.1
Releases
- Create release:
POST https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1 - Get releases:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?api-version=7.1 - Get release:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}?api-version=7.1 - Update release:
PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}?api-version=7.1 - Get release environment:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/environments/{environmentId}?api-version=7.1 - Update release environment:
PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/environments/{environmentId}?api-version=7.1
Approvals
- Get approvals:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/approvals?api-version=7.1 - Update approval:
PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/approvals/{approvalId}?api-version=7.1
Agent Pools
- List pools:
GET /{organization}/_apis/distributedtask/pools?api-version=7.1 - Get pool:
GET /{organization}/_apis/distributedtask/pools/{poolId}?api-version=7.1 - Add pool:
POST /{organization}/_apis/distributedtask/pools?api-version=7.1
Agents
- List agents:
GET /{organization}/_apis/distributedtask/pools/{poolId}/agents?api-version=7.1 - Get agent:
GET /{organization}/_apis/distributedtask/pools/{poolId}/agents/{agentId}?api-version=7.1 - Update agent:
PATCH /{organization}/_apis/distributedtask/pools/{poolId}/agents/{agentId}?api-version=7.1
Variable Groups
- List variable groups:
GET /{organization}/{project}/_apis/distributedtask/variablegroups?api-version=7.1 - Get variable group:
GET /{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=7.1 - Create variable group:
POST /{organization}/{project}/_apis/distributedtask/variablegroups?api-version=7.1 - Update variable group:
PUT /{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=7.1
Task Groups
- List task groups:
GET /{organization}/{project}/_apis/distributedtask/taskgroups?api-version=7.1 - Get task group:
GET /{organization}/{project}/_apis/distributedtask/taskgroups/{taskGroupId}?api-version=7.1
Service Endpoints (Connections)
- List endpoints:
GET /{organization}/{project}/_apis/serviceendpoint/endpoints?api-version=7.1 - Get endpoint:
GET /{organization}/{project}/_apis/serviceendpoint/endpoints/{endpointId}?api-version=7.1 - Create endpoint:
POST /{organization}/{project}/_apis/serviceendpoint/endpoints?api-version=7.1
Azure Test Plans
Test Plans
- List test plans:
GET /{organization}/{project}/_apis/testplan/plans?api-version=7.1 - Get test plan:
GET /{organization}/{project}/_apis/testplan/plans/{planId}?api-version=7.1 - Create test plan:
POST /{organization}/{project}/_apis/testplan/plans?api-version=7.1 - Update test plan:
PATCH /{organization}/{project}/_apis/testplan/plans/{planId}?api-version=7.1
Test Suites
- List test suites:
GET /{organization}/{project}/_apis/testplan/plans/{planId}/suites?api-version=7.1 - Get test suite:
GET /{organization}/{project}/_apis/testplan/plans/{planId}/suites/{suiteId}?api-version=7.1 - Create test suite:
POST /{organization}/{project}/_apis/testplan/plans/{planId}/suites?api-version=7.1
Test Cases
- List test cases:
GET /{organization}/{project}/_apis/testplan/plans/{planId}/suites/{suiteId}/testcases?api-version=7.1 - Get test case:
GET /{organization}/{project}/_apis/testplan/plans/{planId}/suites/{suiteId}/testcases/{testCaseId}?api-version=7.1 - Add test cases:
POST /{organization}/{project}/_apis/testplan/plans/{planId}/suites/{suiteId}/testcases?api-version=7.1
Test Runs
- Create test run:
POST /{organization}/{project}/_apis/test/runs?api-version=7.1 - Get test runs:
GET /{organization}/{project}/_apis/test/runs?api-version=7.1 - Get test run:
GET /{organization}/{project}/_apis/test/runs/{runId}?api-version=7.1 - Update test run:
PATCH /{organization}/{project}/_apis/test/runs/{runId}?api-version=7.1
Test Results
- Get test results:
GET /{organization}/{project}/_apis/test/runs/{runId}/results?api-version=7.1 - Get test result:
GET /{organization}/{project}/_apis/test/runs/{runId}/results/{resultId}?api-version=7.1 - Update test results:
PATCH /{organization}/{project}/_apis/test/runs/{runId}/results?api-version=7.1 - Add test results:
POST /{organization}/{project}/_apis/test/runs/{runId}/results?api-version=7.1
Test Configurations
- List configurations:
GET /{organization}/{project}/_apis/testplan/configurations?api-version=7.1 - Get configuration:
GET /{organization}/{project}/_apis/testplan/configurations/{configurationId}?api-version=7.1
Azure Artifacts
源
- 列出源:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds?api-version=7.1 - 获取源:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}?api-version=7.1 - 创建源:
POST https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds?api-version=7.1 - 更新源:
PATCH https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}?api-version=7.1
包
- 列出包:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages?api-version=7.1 - 获取包:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages/{packageId}?api-version=7.1 - 删除包:
DELETE https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages/{packageId}?api-version=7.1
包版本
- 列出包版本:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages/{packageId}/versions?api-version=7.1 - 获取包版本:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages/{packageId}/versions/{versionId}?api-version=7.1 - 删除包版本:
DELETE https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/packages/{packageId}/versions/{versionId}?api-version=7.1
源权限
- 获取源权限:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/permissions?api-version=7.1 - 设置源权限:
PATCH https://feeds.dev.azure.com/{organization}/_apis/packaging/feeds/{feedId}/permissions?api-version=7.1
组织与项目管理
组织
- 列出组织: 可通过 Azure DevOps Profile API 获取
- 获取组织详细信息:
GET https://dev.azure.com/{organization}/_apis/projectcollections?api-version=7.1
项目
- 列出项目:
GET /{organization}/_apis/projects?api-version=7.1 - 获取项目:
GET /{organization}/_apis/projects/{projectId}?api-version=7.1 - 创建项目:
POST /{organization}/_apis/projects?api-version=7.1{ "name": "MyProject", "description": "Project description", "capabilities": { "versioncontrol": { "sourceControlType": "Git" }, "processTemplate": { "templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45" } } } - 更新项目:
PATCH /{organization}/_apis/projects/{projectId}?api-version=7.1 - 删除项目:
DELETE /{organization}/_apis/projects/{projectId}?api-version=7.1
团队
- 列出团队:
GET /{organization}/_apis/teams?api-version=7.1 - 获取团队:
GET /{organization}/_apis/projects/{projectId}/teams/{teamId}?api-version=7.1 - 创建团队:
POST /{organization}/_apis/projects/{projectId}/teams?api-version=7.1 - 更新团队:
PATCH /{organization}/_apis/projects/{projectId}/teams/{teamId}?api-version=7.1 - 删除团队:
DELETE /{organization}/_apis/projects/{projectId}/teams/{teamId}?api-version=7.1
团队成员
- 获取团队成员:
GET /{organization}/_apis/projects/{projectId}/teams/{teamId}/members?api-version=7.1 - 添加团队成员:
PUT /{organization}/_apis/projects/{projectId}/teams/{teamId}/members/{userId}?api-version=7.1 - 移除团队成员:
DELETE /{organization}/_apis/projects/{projectId}/teams/{teamId}/members/{userId}?api-version=7.1
流程
- 列出流程:
GET /{organization}/_apis/process/processes?api-version=7.1 - 获取流程:
GET /{organization}/_apis/process/processes/{processId}?api-version=7.1 - 创建流程:
POST /{organization}/_apis/process/processes?api-version=7.1
Security & Identity
Identities (Users & Groups)
- Read identities:
GET https://vssps.dev.azure.com/{organization}/_apis/identities?api-version=7.1 - Read identity:
GET https://vssps.dev.azure.com/{organization}/_apis/identities/{identityId}?api-version=7.1
Graph (Azure DevOps specific)
- List users:
GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?api-version=7.1-preview.1 - Get user:
GET https://vssps.dev.azure.com/{organization}/_apis/graph/users/{userDescriptor}?api-version=7.1-preview.1 - Create user:
POST https://vssps.dev.azure.com/{organization}/_apis/graph/users?api-version=7.1-preview.1 - Delete user:
DELETE https://vssps.dev.azure.com/{organization}/_apis/graph/users/{userDescriptor}?api-version=7.1-preview.1
Groups
- List groups:
GET https://vssps.dev.azure.com/{organization}/_apis/graph/groups?api-version=7.1-preview.1 - Get group:
GET https://vssps.dev.azure.com/{organization}/_apis/graph/groups/{groupDescriptor}?api-version=7.1-preview.1 - Create group:
POST https://vssps.dev.azure.com/{organization}/_apis/graph/groups?api-version=7.1-preview.1 - Delete group:
DELETE https://vssps.dev.azure.com/{organization}/_apis/graph/groups/{groupDescriptor}?api-version=7.1-preview.1
Group Memberships
- List memberships:
GET https://vssps.dev.azure.com/{organization}/_apis/graph/memberships/{subjectDescriptor}?api-version=7.1-preview.1 - Add membership:
PUT https://vssps.dev.azure.com/{organization}/_apis/graph/memberships/{subjectDescriptor}/{containerDescriptor}?api-version=7.1-preview.1 - Remove membership:
DELETE https://vssps.dev.azure.com/{organization}/_apis/graph/memberships/{subjectDescriptor}/{containerDescriptor}?api-version=7.1-preview.1
Access Control Lists (ACLs)
- Query ACLs:
GET /{organization}/_apis/accesscontrollists/{securityNamespaceId}?api-version=7.1 - Set ACLs:
POST /{organization}/_apis/accesscontrollists/{securityNamespaceId}?api-version=7.1 - Remove ACLs:
DELETE /{organization}/_apis/accesscontrollists/{securityNamespaceId}?api-version=7.1
Security Namespaces
- List security namespaces:
GET /{organization}/_apis/securitynamespaces?api-version=7.1 - Get security namespace:
GET /{organization}/_apis/securitynamespaces/{securityNamespaceId}?api-version=7.1
Permissions
- Query permissions:
GET /{organization}/_apis/permissions/{securityNamespaceId}/{permissions}?api-version=7.1 - Check permission:
GET /{organization}/_apis/security/permissions/{securityNamespaceId}?api-version=7.1
Extensions & Integrations
Extensions
- List installed extensions:
GET /{organization}/_apis/extensionmanagement/installedextensions?api-version=7.1 - Get installed extension:
GET /{organization}/_apis/extensionmanagement/installedextensions/{publisherName}/{extensionName}?api-version=7.1 - Install extension:
POST /{organization}/_apis/extensionmanagement/installedextensions?api-version=7.1 - Uninstall extension:
DELETE /{organization}/_apis/extensionmanagement/installedextensions/{publisherName}/{extensionName}?api-version=7.1
Service Hooks
- List subscriptions:
GET /{organization}/_apis/hooks/subscriptions?api-version=7.1 - Get subscription:
GET /{organization}/_apis/hooks/subscriptions/{subscriptionId}?api-version=7.1 - Create subscription:
POST /{organization}/_apis/hooks/subscriptions?api-version=7.1{ "publisherId": "tfs", "eventType": "git.push", "resourceVersion": "1.0", "consumerId": "webHooks", "consumerActionId": "httpRequest", "publisherInputs": { "projectId": "{projectId}" }, "consumerInputs": { "url": "https://example.com/webhook" } } - Delete subscription:
DELETE /{organization}/_apis/hooks/subscriptions/{subscriptionId}?api-version=7.1
Notifications
- List subscriptions:
GET /{organization}/_apis/notification/subscriptions?api-version=7.1 - Create subscription:
POST /{organization}/_apis/notification/subscriptions?api-version=7.1
其他服务
Wiki
- 列出 Wiki:
GET /{organization}/{project}/_apis/wiki/wikis?api-version=7.1 - 获取 Wiki:
GET /{organization}/{project}/_apis/wiki/wikis/{wikiId}?api-version=7.1 - 创建 Wiki:
POST /{organization}/{project}/_apis/wiki/wikis?api-version=7.1 - 获取 Wiki 页面:
GET /{organization}/{project}/_apis/wiki/wikis/{wikiId}/pages?path={path}&api-version=7.1 - 创建/更新 Wiki 页面:
PUT /{organization}/{project}/_apis/wiki/wikis/{wikiId}/pages?path={path}&api-version=7.1
搜索
- 搜索工作项:
POST /{organization}/{project}/_apis/search/workitemsearchresults?api-version=7.1 - 搜索代码:
POST /{organization}/{project}/_apis/search/codesearchresults?api-version=7.1
仪表板
- 列出仪表板:
GET /{organization}/{project}/{team}/_apis/dashboard/dashboards?api-version=7.1 - 获取仪表板:
GET /{organization}/{project}/{team}/_apis/dashboard/dashboards/{dashboardId}?api-version=7.1 - 创建仪表板:
POST /{organization}/{project}/{team}/_apis/dashboard/dashboards?api-version=7.1
小组件
- 列出小组件:
GET /{organization}/{project}/{team}/_apis/dashboard/dashboards/{dashboardId}/widgets?api-version=7.1 - 创建小组件:
POST /{organization}/{project}/{team}/_apis/dashboard/dashboards/{dashboardId}/widgets?api-version=7.1
审计
- 查询审计日志:
GET /{organization}/_apis/audit/auditlog?api-version=7.1-preview.1 - 下载审计日志:
GET /{organization}/_apis/audit/downloadlog?api-version=7.1-preview.1
常见操作
分页
Azure DevOps API 使用延续令牌进行分页:
包含延续令牌的响应:
{
"count": 100,
"value": [...],
"continuationToken": "MTIz"
}
下一个请求:
GET /{endpoint}?continuationToken=MTIz&api-version=7.1
部分端点使用 $top 和 $skip:
GET /{endpoint}?$top=100&$skip=100&api-version=7.1
筛选与查询
OData 风格筛选器(部分端点):
GET /{endpoint}?$filter=state eq 'Active'&api-version=7.1
工作项查询使用 WIQL(Work Item Query Language):
SELECT [System.Id], [System.Title], [System.State]
FROM WorkItems
WHERE [System.WorkItemType] = 'Bug'
AND [System.State] = 'Active'
AND [System.AssignedTo] = @Me
ORDER BY [System.ChangedDate] DESC
批量操作
部分 Azure DevOps API 支持批量操作:
批量获取工作项:
GET /{organization}/_apis/wit/workitemsbatch?ids=1,2,3,4,5&api-version=7.1
Git 项目批量操作:
POST /{organization}/{project}/_apis/git/repositories/{repositoryId}/itemsbatch?api-version=7.1
{
"itemDescriptors": [
{"path": "/file1.txt", "version": "main"},
{"path": "/file2.txt", "version": "main"}
]
}
使用 JSON Patch 进行更新
工作项和其他一些资源使用 JSON Patch(RFC 6902):
操作:
add- 添加字段或关系remove- 删除字段replace- 替换字段值test- 测试值(用于并发控制)copy- 复制值move- 移动值
示例:
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "New title"
},
{
"op": "replace",
"path": "/fields/System.State",
"value": "Active"
},
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://dev.azure.com/{org}/_apis/wit/workItems/123"
}
}
]
错误处理
Azure DevOps API 返回标准 HTTP 状态码:
200 OK- 成功201 Created- 资源已创建202 Accepted- 请求已接受(异步操作)204 No Content- 成功,无内容400 Bad Request- 请求无效401 Unauthorized- 需要身份验证403 Forbidden- 权限不足404 Not Found- 未找到资源409 Conflict- 冲突(例如,版本不匹配)429 Too Many Requests- 超出速率限制500 Internal Server Error- 服务器错误503 Service Unavailable- 服务不可用
错误响应格式:
{
"id": "request-id",
"innerException": null,
"message": "TF401019: The Git repository with name or identifier MyRepo does not exist or you do not have permissions for the operation you are attempting.",
"typeName": "Microsoft.TeamFoundation.Git.Server.GitRepositoryNotFoundException",
"typeKey": "GitRepositoryNotFoundException",
"errorCode": 0,
"eventId": 3000
}
速率限制
Azure DevOps 会实施速率限制:
- 全局限制: 因服务而异(通常每分钟 200-300 个请求)
- TSTU(Team Services Time Units): 用于衡量资源消耗
- Retry-After 标头: 指示发生 429 错误后何时重试
最佳实践:
- 实施指数退避
- 遵循
Retry-After标头 - 适时缓存响应
- 尽可能使用批量操作
权限与范围
PAT 权限范围
创建个人访问令牌时,请选择适当的权限范围:
- Agent Pools: 读取和管理
- Analytics: 读取
- Audit: 读取审核日志
- Build: 读取和执行
- Code: 完全访问、读取或状态
- Extensions: 读取和管理
- Graph: 读取
- Identity: 读取
- Marketplace: 获取、管理和发布
- Member Entitlement Management: 读取和写入
- Packaging: 读取、写入和管理
- Project and Team: 读取、写入和管理
- Release: 读取、写入、执行和管理
- Secure Files: 读取、创建和管理
- Service Connections: 读取、查询和管理
- Symbols: 读取
- Task Groups: 读取、创建和管理
- Test Management: 读取和写入
- Tokens: 读取和管理
- User Profile: 读取和写入
- Variable Groups: 读取、创建和管理
- Work Items: 完全访问、读取和写入
重要: 始终使用所需的最低权限范围。
OAuth 2.0 权限范围
对于 OAuth 应用,请使用以下格式的权限范围:
vso.work- 工作项(读取)vso.work_write- 工作项(写入)vso.code- 代码(读取)vso.code_write- 代码(写入)vso.build- 构建(读取)vso.build_execute- 构建(执行)
完整列表:https://docs.microsoft.com/azure/devops/integrate/get-started/authentication/oauth
API 版本控制
Azure DevOps API 使用显式版本控制:
版本:
7.1- 最新稳定版(推荐)7.0- 稳定版6.0- 稳定版5.1- 较早的稳定版- 带有
-preview后缀的版本(例如7.1-preview.1)- 预览功能
版本格式:
api-version=7.1- 7.1 的最新补丁版本api-version=7.1-preview.1- 7.1 的预览版 1
重要:
- 始终指定
api-version(所有请求都必须指定) - 预览 API 可能会更改或被移除
- 在生产环境中使用稳定版本
- 留意弃用通知
最佳实践
性能
- 获取多个项目时使用批量操作
- 对大型结果集实施分页
- 在支持的情况下,使用
$select指定字段 - 适时缓存响应
- 使用增量查询进行增量同步
- 正确利用延续令牌
安全
- 安全存储 PAT(使用 Azure Key Vault 或类似服务)
- 使用适当的权限范围(最低权限)
- 定期轮换 PAT(设置有效期)
- 仅使用 HTTPS
- 验证输入以防止注入攻击
- 实施适当的错误处理
- 记录安全事件
- 切勿将 PAT 提交到源代码管理系统
开发
- 使用最新的稳定 API 版本
- 使用重试逻辑处理速率限制
- 实施指数退避
- 使用预览 API 前,确认其稳定性
- 监控服务运行状况
- 使用 JSON Patch 进行更新
- 验证响应
- 正确处理分页
- 使用各种边界情况进行测试
- 使用清晰的错误消息
集成模式
- Webhook(服务钩子): 用于事件驱动型集成
- 轮询: 用于批处理(避免过度轮询)
- 计划任务: 用于定期同步操作
- 实时同步: 使用服务钩子和 API 调用
常见用例
创建工作项
POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Bug?api-version=7.1
Content-Type: application/json-patch+json
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "Critical bug in login flow"
},
{
"op": "add",
"path": "/fields/System.Description",
"value": "Users cannot log in with SSO"
},
{
"op": "add",
"path": "/fields/Microsoft.VSTS.Common.Priority",
"value": 1
}
]
创建拉取请求
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=7.1
Content-Type: application/json
{
"sourceRefName": "refs/heads/feature/new-feature",
"targetRefName": "refs/heads/main",
"title": "Add new feature",
"description": "This PR adds the new feature",
"reviewers": [
{"id": "reviewer-id-1"},
{"id": "reviewer-id-2"}
]
}
将构建加入队列
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.1
Content-Type: application/json
{
"definition": {
"id": 123
},
"sourceBranch": "refs/heads/main",
"parameters": "{\"param1\":\"value1\"}"
}
运行 WIQL 查询
POST https://dev.azure.com/{organization}/{project}/_apis/wit/wiql?api-version=7.1
Content-Type: application/json
{
"query": "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.WorkItemType] = 'Bug' AND [System.State] = 'Active' AND [System.AssignedTo] = @Me ORDER BY [System.Priority] ASC"
}
创建服务钩子订阅
POST https://dev.azure.com/{organization}/_apis/hooks/subscriptions?api-version=7.1
Content-Type: application/json
{
"publisherId": "tfs",
"eventType": "workitem.updated",
"resourceVersion": "1.0",
"consumerId": "webHooks",
"consumerActionId": "httpRequest",
"publisherInputs": {
"projectId": "{projectId}",
"workItemType": "Bug"
},
"consumerInputs": {
"url": "https://example.com/webhook",
"httpHeaders": "Content-Type:application/json"
}
}
工具与测试
REST 客户端工具
- Postman - 常用的 API 测试工具
- curl - 命令行工具
- PowerShell -
Invoke-RestMethod - Python -
requests库 - Azure DevOps CLI - 官方 CLI 工具
Azure DevOps CLI
安装并使用官方 CLI:
# Install
pip install azure-devops
# Login
az devops login --organization https://dev.azure.com/{organization}
# Configure defaults
az devops configure --defaults organization=https://dev.azure.com/{organization} project={project}
# Examples
az repos list
az pipelines build list
az boards work-item create --title "Bug" --type Bug
测试身份验证
使用环境变量测试 PAT 身份验证——切勿将令牌硬编码:
# Set once in your shell session (do not commit this to scripts or source control)
# export AZURE_DEVOPS_PAT="<your-pat>"
# Encode from environment variable
PAT_ENCODED=$(echo -n ":$AZURE_DEVOPS_PAT" | base64)
# Test
curl -H "Authorization: Basic $PAT_ENCODED" \
"https://dev.azure.com/{organization}/_apis/projects?api-version=7.1"
推荐: 使用 az devops login,它可以安全地处理凭据存储,无需手动编码。
可用的 SDK
- .NET -
Microsoft.TeamFoundationServer.Client、Microsoft.VisualStudio.Services.Client - Node.js -
azure-devops-node-api - Python -
azure-devops - Java - Azure DevOps Java SDK
渐进式加载
此技能全面涵盖 Azure DevOps API。针对特定任务:
- 确定服务领域(Boards、Repos、Pipelines、Test、Artifacts)
- 在本文档中找到相关章节
- 查阅 API 参考,了解详细的参数信息
- 在实现前,使用 Azure DevOps CLI 或 REST 客户端进行测试
参考链接
- 官方文档: https://docs.microsoft.com/rest/api/azure/devops/
- API 参考: https://docs.microsoft.com/rest/api/azure/devops/?view=azure-devops-rest-7.1
- 身份验证: https://docs.microsoft.com/azure/devops/integrate/get-started/authentication/authentication-guidance
- 服务挂钩: https://docs.microsoft.com/azure/devops/service-hooks/overview
- 速率限制: https://docs.microsoft.com/azure/devops/integrate/concepts/rate-limits
- API 版本控制: https://docs.microsoft.com/azure/devops/integrate/concepts/rest-api-versioning
- Azure DevOps CLI: https://docs.microsoft.com/cli/azure/devops
- Node.js SDK: https://github.com/microsoft/azure-devops-node-api
- Python SDK: https://github.com/microsoft/azure-devops-python-api
- 状态页: https://status.dev.azure.com/
API URL 模式
不同的 Azure DevOps 服务使用不同的基础 URL:
- 核心服务:
https://dev.azure.com/{organization}/ - 发布管理:
https://vsrm.dev.azure.com/{organization}/ - 包管理:
https://feeds.dev.azure.com/{organization}/ - 标识:
https://vssps.dev.azure.com/{organization}/ - 分析:
https://analytics.dev.azure.com/{organization}/
注意事项
- 此技能涵盖 Azure DevOps REST API 7.1 版(最新稳定版)
- 某些端点可能需要预览版 API
- 请始终查阅官方文档,了解最新变更
- API 版本和端点可能会随时间演进
- 速率限制和节流策略均适用
- 所有操作都需要适当的身份验证和权限
- 某些功能仅在 Azure DevOps Services 中提供,Server 版不提供