Bitbucket 给我们提供了很多API, 我们可以利用它来做一些自动化的工作, 比如commit就有这些API:
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit
-
获取两个commit的diff:
API :GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/diff/{spec}
- {username} 就是username, 此次repo所属的用户(私有库)
- {repo_slug} 就是打开bitbucket库后,显示在URL里的库名
- {spec} 就是commit, 可以是2个commit, 这样写A..B, 也可以是一个commit, 这样写A, 一个commit对比的对象是A 的parent commit。
举例:
curl -u yourbitbucketemail@xx.com:yourpassword https://api.bitbucket.org/2.0/repositories/lt-dev/report-center/diff/d3178a2
curl -u yourbitbucketemail@xx.com:yourpassword https://api.bitbucket.org/2.0/repositories/lt-dev/report-center/diff/d3178a2..5b5fda5
- 获取一个branch的所有commit:
https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/commits?until={branch}
举例:
curl -u yourbitbucketemail@xx.com:yourpassword https://api.bitbucket.org/2.0/repositories/lt-dev/db-update/commits?until=test1&limit=0&start=0
- 获取一个pull request 的diff:
https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/pullrequests/{pullrequestID}/diff
举例:
curl -u yourbitbucketemail@xx.com:yourpassword https://api.bitbucket.org/2.0/repositories/lt-dev/backend/pullrequests/7272/diff
- 发起一个pull request, 记得带上content-type.
curl https://api.bitbucket.org/2.0/repositories/lt-dev/db-update/pullrequests
-u yourbitbucketemail@xx.com:yourpassword
--request POST
--header 'Content-Type: application/json'
--data '{
"title": "API TEST",
"source": {
"branch": {
"name": "test1"
}
},
"destination": {
"branch": {
"name": "test2"
}
}
}'
得到的结果跟你运行git log是一样的,可以用正则表达式做进一步的信息提取工作。
比如我用golang, 提取本次diff改动到的文件:
resBody:={diffcontent}
regex := regexp.MustCompile(`diff --git a(.+) b/`)
allDiffs := regex.FindAllStringSubmatch(resBody, -1)
for _, difs := range allDiffs {
for index, dfs := range difs {
if index > 0 {
diffFiles = append(diffFiles, dfs)
}
}
}
sort.Strings(diffFiles)
return diffFiles
有疑问加站长微信联系(非本文作者)