使用Azure DevOps API从标记获取分支名称 [英] Get the branch name from tag using Azure DevOps api

查看:87
本文介绍了使用Azure DevOps API从标记获取分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有关联的标签,有人可以帮助我获取实际的分支名称吗?

对于我来说,Azure DevOps API最好.预先感谢.

到目前为止,我已经获得了提供所有特别回购标签的API,但这不能解决我的问题.

  https://dev.azure.com/{{organization}}/{{project}}/_apis/git/repositories/{{repository_id}}/refs?filter = tags/& api-版本= 6.0 

但是它没有给我任何与之相关的分支信息.

解决方案

没有这样的REST API可以通过标签或提交来获取分支名称.在Azure DevOps中,基于提交ID创建标签,并且提交或标签可以属于多个分支.让我们通过下图进行说明:

  A --- B --- E --- F主控\/开发人员 

这是一个具有master和dev分支的分支结构.并且dev分支与提交F合并到master中.如果要基于提交D获取分支名称,则将获得两个分支名称.提交D属于两个分支:dev(第一个父级)和master(第二个父级).

我们可以通过条件和多个REST API获取分支名称,也可以通过git cmd获取分支名称.

a .列出所有标签并获取 objectId .

  GET https://dev.azure.com/{组织名称}/{项目名称}/_ apis/git/存储库/{repo ID}/refs?filter = tags/& api-version =6.0 

b .获取

Can someone help me to get the actual branch name if I have the tag associate with it?

Azure DevOps API would be best in my case. Thanks in advance.

SO far I get the API that gives all the tags for particularly repo but this does not solve my issue.

https://dev.azure.com/{{organization}}/{{project}}/_apis/git/repositories/{{repository_id}}/refs?filter=tags/&api-version=6.0

But it does not give me any branch information associated with it.

解决方案

There isn’t such REST API to get branch name by a tag or commit. In the Azure DevOps, Tags are created based on the commit id, and a commit or tag can belong more than one branches. Let’s illustrate by below graph:

A---B---E---F master
     \     /
      C---D   dev

This is a branch structure with master and dev branch. And dev branch merge into master with commit F. If you want to get the branch name based on commit D, you will get two branch names. Commit D belongs to two branch: dev (first parent) and master (second parent).

We can get the branch name via condition and multiple REST APIs, also we could get the branch name via git cmd.

a. List all tags and get the objectId.

GET https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{repo ID}/refs?filter=tags/&api-version=6.0

b. Get the tag info and commit ID via objectId

Note: the field taggedObject.objectId is commit ID

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/annotatedtags/{objectId}?api-version=6.0-preview.1

Workaround 1

Now we could get the commit details and get the branch name(s) contain the given commit, we can use git command:

git branch --contains <commit>

Workaround 2

We could list all commits info on a branch via this API

GET https://dev.azure.com/{Org name}/_apis/git/repositories/{repositoryId}/commits?searchCriteria.itemVersion.version={barnch name}&api-version=6.0

And now, we get commit id which contain the tag and all commit ID on a branch, add if{} or eq() to check it, if it return True, we could know this branch contain the tag

Update1

My test repo contains branch main and test01, the branch main contains the tag Tag01, the branch test01 contains the tag Tag01 and tag02

Power shell script:

$ListAllTagsURL="https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{Repo ID}/refs?filter=tags/&api-version=6.0"
$PAT="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$ListAllTags = Invoke-RestMethod -Uri $ListAllTagsURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get

#List all tags name
#write-host $ListAllTags.value.name
foreach($Tag in $ListAllTags.value){
    if($Tag.name -eq "{tag name such as refs/tags/Tag01}"){
        $objectId = $Tag.objectId
        $TagName = $Tag.name
    }
}

#Get tag details and commit ID
$TagURL="https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{Repo ID}/annotatedtags/$($objectId)?api-version=6.0-preview.1"
$TagInfo = Invoke-RestMethod -Uri $TagURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
$TagCommitID = $TagInfo.taggedObject.objectId
#write-host $TagCommitID 


#List all branch
$url="https://dev.azure.com/{Org name}/{Project name}/_apis/git/repositories/{Repo ID}/refs?filter=heads&api-version=6.1-preview.1"
$BranchInfo = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
foreach($Branch in $BranchInfo.value){
#write-host $Branch.name
$BranchName = $Branch.name.split("/",3)[-1]
#write-host $BranchName

#List all commit ID via Branch Name
$BranchDetailUrl="https://dev.azure.com/v-viliu/_apis/git/repositories/{Repo ID}/commits?searchCriteria.itemVersion.version=$($BranchName)&api-version=6.0"
$BranchDetailInfo = Invoke-RestMethod -Uri $BranchDetailUrl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
#write-host $BranchDetailInfo.value.commitId

foreach($CommitID in $BranchDetailInfo.value.commitId){
    If($CommitID -eq $TagCommitID){
        write-host $BranchName "Contain this tag" $TagName
    }

}

}

Result:

这篇关于使用Azure DevOps API从标记获取分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆