美文网首页
利用BitBucket API 获取 [commit/branc

利用BitBucket API 获取 [commit/branc

作者: V_Jan | 来源:发表于2019-12-16 10:19 被阅读0次

    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}
    • {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
    
    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??

    相关文章

      网友评论

          本文标题:利用BitBucket API 获取 [commit/branc

          本文链接:https://www.haomeiwen.com/subject/omernctx.html