在 Bitbucket 中创建 Pull Request 时,可以使用 REST API 2.0 添加附件。以下是使用 Python 语言实现此操作的示例代码:
import requests
repo_slug = 'your-repo-slug'
pull_request_id = 'pr-id'
access_token = 'your-access-token' # 需要有能够访问 Pull Request 的权限
url = f'https://api.bitbucket.org/2.0/repositories//{repo_slug}/pullrequests/{pull_request_id}/attachments'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'multipart/form-data'
}
data = {
'file': ('file-name.png', open('path/to/file.png', 'rb'))
}
response = requests.post(url, headers=headers, files=data)
if response.status_code == 201:
print('Attachment added to Pull Request.')
else:
print('Error adding attachment to Pull Request.')
上述代码中,需要替换 repo_slug
和 access_token
变量的值。其中 repo_slug
为仓库名称,pull_request_id
为正在进行的 Pull Request 的 ID。access_token
是一个 API 访问令牌,需要有访问 Pull Request 的权限。
在 headers
字典中包含了访问令牌和上传文件的 MIME 类型信息。在 data
字典中包含了上传文件的数据。
使用以上代码,您可以通过 REST API 2.0 向 Pull Request 添加附件。