从TFS克隆所有远程存储库

时间:2019-08-29 04:21:40

标签: git tfs azure-devops-rest-api

是否有一种方法可以从以1个帐户上传的所有项目中克隆所有master个分支。

我需要每周备份master分支中的所有代码。 有没有办法使用git,Powershell或其他任何方式来完成此任务?

请注意,我需要在Windows环境中执行此任务。

3 个答案:

答案 0 :(得分:2)

您可以使用PowerShell和TFS Rest API

首先,使用Projects - List API获取项目,然后使用Repositories - List API获取每个项目的存储库,并仅克隆主数据库。

例如,执行此操作的简单PowerShell脚本:

$collection = "http://tfs-server:8080/tfs/collection-name"

$projectsUrl = "$collection/_apis/projects?api-version=4.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -UseDefaultCredentials -ContentType application/json

$projects.value.ForEach({

    $reposUrl = "$collectionurl/$($_.name)/_apis/git/repositories?api-version=4.0"
    $repos = Invoke-RestMethod -Uri $reposUrl -Method Get -UseDefaultCredentials -ContentType application/json
    $repos.value.ForEach({
    git clone $_.remoteUrl --branch master --single-branch

    })
})

答案 1 :(得分:1)

我没有直接在TFS中看到该功能,但是如果VSTS API也可用于本地TFS实例,则可以:

答案 2 :(得分:0)

我创建了一个 github gist,它在 Azure DevOps 中实现了这一点。你可以找到它here。在刷新 Windows 或类似的东西后,我用它来设置我的新机器。

原始提交时的代码:

#Ensure you create a PAT, following the instructions here: https://dev.to/omiossec/getting-started-with-azure-devops-api-with-powershell-59nn
#Additional Credit: https://blog.rsuter.com/script-to-clone-all-git-repositories-from-your-vsts-collection/
#I suggest executing from C:/Projects. This script will create a folder for each Team Project/Client with repos within each.
#Finally note that git clone operations count as "Errors" in powershell, and appear red. It's more work than is worth it to change it.

param(
    [string] $email = $(Throw "--Email is required."), #required parameter
    [string] $pat = $(Throw "--PAT is Required"), #required parameter
    [string] $url = $(Throw "--Collection URL is required.") #required parameter pointing to the default collection, URL is generally https://{tenant}.visualstudio.com/defaultcollection
)
$originalDirectory = Get-Location

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $email,$pat)))
$headers = @{
    "Authorization" = ("Basic {0}" -f $base64AuthInfo)
    "Accept" = "application/json"
}

Add-Type -AssemblyName System.Web
$gitcred = ("{0}:{1}" -f  [System.Web.HttpUtility]::UrlEncode($username),$password)

#Write-Host "Retrieving Repositories...`n"
#$resp = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/git/repositories?api-version=1.0" -f $url)
#$repoJson = convertFrom-JSON $resp.Content
#Write-Host $repoJson

Write-Host "Getting Projects..."
$projectsUrl = "$collection/_apis/projects?api-version=4.0"
$projectResponse = Invoke-Webrequest -Headers $headers -Uri ("{0}/_apis/projects?api-version=4.0" -f $url)
$projects = ConvertFrom-Json $projectResponse

$projects.value.ForEach({
    $folderToCreate = Join-Path -Path $originalDirectory -ChildPath $_.name

    if (!(Test-Path $folderToCreate -PathType Container)) {
        Write-Host "Creating folder for Project $($_.name)"
        New-Item -ItemType Directory -Force -Path $folderToCreate
    } else {
        Write-Host "Skipping folder creation for project $($_.name), as it already exists"
    }
    Set-Location $folderToCreate

    $reposUrl = "$url/$($_.name)/_apis/git/repositories?api-version=4.0"
    $reposResponse = Invoke-Webrequest -Headers $headers -Uri $reposUrl
    $repos = ConvertFrom-Json $reposResponse    

    $repos.value.ForEach({
        $name = $_.name
        Write-Host "Cloning $name Repos"

        try {            
            $credUrl = $_.remoteUrl -replace "://", ("://{0}@" -f $gitcred)
            git clone $credUrl --branch master --single-branch    
            #git clone $_.remoteUrl --branch master --single-branch #this will automatically create/use GitForWindows token after a login prompt if you have issues with the upper 2 lines
        }
        catch {
            Write-Host $PSItem.Exception.Message -ForegroundColor RED
            Write-Host "Error at URL $_.remoteUrl"
            Set-Location $originalDirectory
        }        
    })
    Write-Host "Cleaning URL Space encoding for repo folders..."
    Get-ChildItem $folderToCreate | 
        Where {$_.Name -Match '%20'} | 
            Rename-Item -NewName {$_.name -replace '%20',' ' } #Rename-Item  { $_.Name -replace "%20"," " }
    Set-Location $originalDirectory
})
相关问题