利用BitBucket API 获取 [commit/branc

2019-12-16  本文已影响0人  V_Jan

Bitbucket 给我们提供了很多API, 我们可以利用它来做一些自动化的工作, 比如commit就有这些API:
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit

  1. 获取两个commit的diff
    API :GET https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/diff/{spec}
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
  1. 获取一个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
  1. 获取一个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
  1. 发起一个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

======20200721更新=====
记录个暂时没找到答案的问题:
当用Bitbucket API 做2个branch的Diff的时候(逆向Diff),发现有个文件。但在Bitbucket页面做用同样的branch做pull request的时候却没有Diff??

上一篇下一篇

猜你喜欢

热点阅读