首页 > 解决方案 > 寻找回购分支令牌

问题描述

使用存储库的安全 API 时,各个分支都使用从分支的“人类”名称派生的字母数字标识符进行引用。

此标识符是令牌的最后一部分。例如 master 分支,标识符总是6d0061007300740065007200. 这不是后端系统分配的 GUID,而是计算出来的值

我查看了 API 文档,但找不到任何方法来查找此标识符。

只是想知道我是否遗漏了什么或看错了地方?有谁知道是否有地方可以找到这些信息?

标签: azure-devopsazure-devops-rest-api

解决方案


这是一个用 PowerShell 编写的粗略函数,但它可以将分支名称转换为必需的十六进制代码。

Function get-AzDoBranchTokenFromName {  
    <#
    .SYNOPSIS
    Convert branch name to token

    .DESCRIPTION
    Azure DevOps Services security stores Access Control Lists and other items
    for Branches using a alphanumeric identifier derived from the text name of
    the branch.  This function will convert the name, like "master", into the 
    appropriate string, like 6d0061007300740065007200

    .PARAMETER branchName
    The human name of the git repository branch

    .EXAMPLE
    get-AzDoBranchTokenFromName -branchName "master"

    .NOTES
    The fun part is trying to go backwards (from hex to string).  Need to 
    work on that yet. 
    #>

    param(  
        # The sting you wish to Convert
        [Parameter(Mandatory=$true)]
        [string]
        $branchName   
    )
    # convert a string to an array of bytes    
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($branchName)    
    # create a new variable twice as long as $bytes
    $Hex = [System.Text.StringBuilder]::new($Bytes.Length * 2)    
    # take each byte, format it as two hex characters and shove it into $Hex
    ForEach ($byte in $bytes) {      
        if ($byte -eq 47) {
            #Write-Output "YATZEE!!!!"
            $Hex.Append("/") | Out-Null
        }else{
        $Hex.AppendFormat("{0:x2}", $byte) | Out-Null    
        }
    }
    # convert $Hex back to a string    
    $Hex.ToString()
}

推荐阅读