使用 Bitbucket API 进行各种操作相关接口示例
一、示例1-Pull Request相关接口
以下是每个操作的请求和响应示例,并添加了字段的具体中文介绍。
1. 创建 Pull Request
请求 URL
POST https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests
请求 Body 示例
{
"title": "Pull request title", // Pull Request 的标题
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"description": "Pull request description", // Pull Request 的描述
"close_source_branch": true // 是否在合并后关闭源分支
}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"description": "Pull request description", // Pull Request 的描述
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
2. 获取 Pull Request 列表
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests
响应示例
{
"pagelen": 10, // 每页显示的项目数
"values": [
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
],
"page": 1, // 当前页码
"size": 1 // Pull Request 的总数
}
3. 获取 Pull Request 详细信息
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"description": "Pull request description", // Pull Request 的描述
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
4. 获取 Pull Request 的评论
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/comments
响应示例
{
"pagelen": 10, // 每页显示的项目数
"values": [
{
"id": 1, // 评论的唯一标识符
"content": {
"raw": "This is a comment." // 评论内容
},
"user": {
"display_name": "Reviewer Name", // 评论者的显示名称
"uuid": "{user-uuid}" // 评论者的唯一标识符
},
"created_on": "2021-01-01T12:00:00.000Z", // 评论创建时间
"updated_on": "2021-01-01T12:00:00.000Z" // 评论最后更新时间
}
],
"page": 1, // 当前页码
"size": 1 // 评论总数
}
5. 添加评论到 Pull Request
请求 URL
POST https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/comments
请求 Body 示例
{
"content": {
"raw": "This is a comment." // 评论内容
}
}
响应示例
{
"id": 1, // 评论的唯一标识符
"content": {
"raw": "This is a comment." // 评论内容
},
"user": {
"display_name": "Reviewer Name", // 评论者的显示名称
"uuid": "{user-uuid}" // 评论者的唯一标识符
},
"created_on": "2021-01-01T12:00:00.000Z", // 评论创建时间
"updated_on": "2021-01-01T12:00:00.000Z" // 评论最后更新时间
}
6. 更新 Pull Request
请求 URL
PUT https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}
请求 Body 示例
{
"title": "Updated pull request title", // 更新后的 Pull Request 标题
"description": "Updated description" // 更新后的 Pull Request 描述
}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Updated pull request title", // 更新后的 Pull Request 标题
"description": "Updated description", // 更新后的 Pull Request 描述
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
7. 合并 Pull Request
请求 URL
POST https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/merge
请求 Body 示例
{
"message": "Merging the pull request" // 合并 Pull Request 的消息
}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"description": "Pull request description", // Pull Request 的描述
"state": "MERGED", // Pull Request 的状态(MERGED 表示已合并)
"author": {
"display
_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"merge_commit": {
"hash": "abcdef123456" // 合并提交的哈希值
},
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
通过这些示例代码和详细字段介绍,你可以更清晰地理解如何使用 Bitbucket API 进行各种操作,并预期相应的结果。根据具体需求,可以在此基础上进行扩展和定制。如果有更多具体的问题或需要进一步的帮助,请随时告知!
二、示例2-分支、项目、人员相关接口
以下是分支、项目、人员相关的示例代码,并添加了每个具体字段的中文描述。
1. 获取项目列表
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}
响应示例
{
"pagelen": 10, // 每页显示的项目数
"values": [
{
"name": "My Project", // 项目名称
"full_name": "{username}/{repo_slug}", // 项目的全名(包括用户名和仓库名)
"uuid": "{repository-uuid}", // 项目的唯一标识符
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}" // 项目的网页链接
}
},
"owner": {
"display_name": "Owner Name", // 项目所有者的显示名称
"uuid": "{owner-uuid}" // 项目所有者的唯一标识符
},
"description": "Project description", // 项目描述
"created_on": "2021-01-01T12:00:00.000Z", // 项目创建时间
"updated_on": "2021-01-01T12:00:00.000Z" // 项目最后更新时间
}
],
"page": 1, // 当前页码
"size": 1 // 项目总数
}
2. 获取分支列表
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/refs/branches
响应示例
{
"pagelen": 10, // 每页显示的分支数
"values": [
{
"name": "main", // 分支名称
"target": {
"hash": "abcdef123456", // 分支指向的提交哈希
"author": {
"user": {
"display_name": "Author Name", // 提交者的显示名称
"uuid": "{user-uuid}" // 提交者的唯一标识符
}
},
"date": "2021-01-01T12:00:00.000Z", // 提交日期
"message": "Initial commit", // 提交信息
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/commits/abcdef123456" // 提交的网页链接
}
}
}
}
],
"page": 1, // 当前页码
"size": 1 // 分支总数
}
3. 获取人员列表(仓库成员)
请求 URL
GET https://api.bitbucket.org/2.0/teams/{teamname}/members
响应示例
{
"pagelen": 10, // 每页显示的成员数
"values": [
{
"user": {
"display_name": "Member Name", // 成员的显示名称
"uuid": "{user-uuid}", // 成员的唯一标识符
"links": {
"html": {
"href": "https://bitbucket.org/{user}" // 成员的网页链接
}
}
}
}
],
"page": 1, // 当前页码
"size": 1 // 成员总数
}
4. 获取 Pull Request 的评审人员
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/participants
响应示例
{
"pagelen": 10, // 每页显示的评审人员数
"values": [
{
"user": {
"display_name": "Reviewer Name", // 评审人员的显示名称
"uuid": "{user-uuid}" // 评审人员的唯一标识符
},
"role": "REVIEWER", // 角色(如 REVIEWER 表示评审者)
"approved": true // 是否已批准
}
],
"page": 1, // 当前页码
"size": 1 // 评审人员总数
}
5. 获取 Pull Request 列表
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests
响应示例
{
"pagelen": 10, // 每页显示的 Pull Request 数
"values": [
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
],
"page": 1, // 当前页码
"size": 1 // Pull Request 的总数
}
6. 获取 Pull Request 详细信息
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"description": "Pull request description", // Pull Request 的描述
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
7. 获取 Pull Request 的评论
请求 URL
GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/comments
响应示例
{
"pagelen": 10, // 每页显示的评论数
"values": [
{
"id": 1, // 评论的唯一标识符
"content": {
"raw": "This is a comment." // 评论内容
},
"user": {
"display_name": "Reviewer Name", // 评论者的显示名称
"uuid": "{user-uuid}" // 评论者的唯一标识符
},
"created_on": "2021-01-01T12:00:00.000Z", // 评论创建时间
"updated_on": "2021-01-01T12:00:00.000Z" // 评论最后更新时间
}
],
"page": 1, // 当前页码
"size": 1 // 评论总数
}
8. 添加评论到 Pull Request
请求 URL
POST https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/comments
请求 Body 示例
{
"content": {
"raw": "This is a comment." // 评论内容
}
}
响应示例
{
"id": 1, // 评论的唯一标识符
"content": {
"raw": "This is a comment." // 评论内容
},
"user": {
"display_name": "Reviewer Name", // 评论者的显示名称
"uuid": "{user-uuid}" // 评论者的唯一标识符
},
"created_on": "2021-01-01T12:00:00.000Z",
// 评论创建时间
"updated_on": "2021-01-01T12:00:00.000Z" // 评论最后更新时间
}
9. 更新 Pull Request
请求 URL
PUT https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}
请求 Body 示例
{
"title": "Updated pull request title", // 更新后的 Pull Request 标题
"description": "Updated description" // 更新后的 Pull Request 描述
}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Updated pull request title", // 更新后的 Pull Request 标题
"description": "Updated description", // 更新后的 Pull Request 描述
"state": "OPEN", // Pull Request 的状态(如 OPEN, MERGED, DECLINED)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
10. 合并 Pull Request
请求 URL
POST https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequest_id}/merge
请求 Body 示例
{
"message": "Merging the pull request" // 合并 Pull Request 的消息
}
响应示例
{
"id": 1, // Pull Request 的唯一标识符
"title": "Pull request title", // Pull Request 的标题
"description": "Pull request description", // Pull Request 的描述
"state": "MERGED", // Pull Request 的状态(MERGED 表示已合并)
"author": {
"display_name": "Author Name", // 作者的显示名称
"uuid": "{user-uuid}" // 作者的唯一标识符
},
"source": {
"branch": {
"name": "feature-branch" // 源分支的名称
}
},
"destination": {
"branch": {
"name": "main" // 目标分支的名称
}
},
"created_on": "2021-01-01T12:00:00.000Z", // Pull Request 创建时间
"updated_on": "2021-01-01T12:00:00.000Z", // Pull Request 最后更新时间
"merge_commit": {
"hash": "abcdef123456" // 合并提交的哈希值
},
"links": {
"html": {
"href": "https://bitbucket.org/{username}/{repo_slug}/pull-requests/1" // Pull Request 的网页链接
}
}
}
通过这些示例代码和详细字段介绍,你可以更清晰地理解如何使用 Bitbucket API 进行各种操作,并预期相应的结果。根据具体需求,可以在此基础上进行扩展和定制。如果有更多具体的问题或需要进一步的帮助,请随时告知!