【问题标题】:Is there a way to use tf.exe to set git permissions on multiple repositories in Azure Repos?有没有办法使用 tf.exe 在 Azure Repos 中的多个存储库上设置 git 权限?
【发布时间】:2019-06-03 07:10:22
【问题描述】:

我最近从 Teamcity 迁移到 Azure DevOps,昨天我迁移了大约 60 个存储库。

我使用 tf.exe 和这个命令对我们的核心项目存储库设置了一些权限

tf git permission /deny:CreateBranch 
              /group:[FabrikamProject]\Contributors 
              /collection:https://dev.azure.com/fabrikam-fiber/ 
              /teamproject:FabrikamProject 
              /repository:FabrikamRepo

理想情况下,我需要对其他存储库应用相同的权限。

我想知道是否有办法使用 tf.exe 来接受存储库参数的通配符,例如:

/repository:* - 这不起作用

我真的不想手动检查它们,但必须这样做。

【问题讨论】:

    标签: git tfs azure-devops azure-pipelines


    【解决方案1】:

    我不知道通配符是否有效,但您可以迭代存储库并使用小型 PowerShell 脚本运行命令(使用 Rest API):

    $reposJson = Invoke-RestMethod -Method Get -Uri https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.0-preview.1 -ContentType application/json
    
    $repos = $reposJson | ConvertFrom-Json
    
    $tfExe = "path/to/exe"
    
    $repos.value.ForEach({
    
    & $txExe git permission /deny:CreateBranch /group:[FabrikamProject]\Contributors /collection:{collection} /teamproject:{project} /repository:$_.name
    
    })
    

    你只需要认证,你可以使用-Credential {email}(在Invoke-RestMethod中),然后会有一个窗口提示输入密码或者这样使用PAT:

    $personalAccessToken = "your-personal-access-token-here"
    $header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))}
    

    并在Invoke-RestMethod 中添加-Headers $header

    【讨论】:

    • 我遇到了这个问题。在对tf 的调用中,我必须使用$($_.name) 而不是$_.name 作为repository 的值。此外,$tfExe 如果在 Visual Studio 命令 shell 中完成,则不需要。
    【解决方案2】:

    您只需删除此参数即可获得所需的行为,而不是 /repository:*

    文档没有明确说明,但是要设置项目级别的权限(所有现有和新的存储库),您应该避免指定特定的存储库。 我试过了,效果很好。

    所以tf 代码如下所示:

    tf git permission /deny:CreateBranch 
                  /group:[FabrikamProject]\Contributors 
                  /collection:https://dev.azure.com/fabrikam-fiber/ 
                  /teamproject:FabrikamProject 
                  #You should remove this => /repository:FabrikamRepo
    

    文档显示了如何获取项目级权限信息的示例。其他示例参考存储库级别的权限设置,相关区别是指定了/repository 参数。

    https://docs.microsoft.com/en-us/azure/devops/repos/tfvc/git-permission-command?view=azure-devops#view-project-level-permissions

    【讨论】:

    • 我试过:tf git permission /allow:CreateBranch /group:[Company]\Contributors /collection:dev.azure.com/Company /teamproject:Company /branch:tweak 并收到此错误:A Git必须指定存储库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多