自动化 VSTS REST API 创建新项目

Automation VSTS REST API to create new project

我正在尝试学习如何使用 REST API,但我还处于第一步。不知道使用什么工具与 VSTS REST 交互的部分 API 或如何配置它。

关于 "you should use this tool" 和 "this is how you connect to it" 的任何帮助。

小目标是能够在我拥有的 VSTS 帐户中获取项目列表,然后我可以从那里进行构建。

您可以使用 PowerShell 轻松使用 TFS/VSTS 的 REST API。

这是一个示例代码,用于列出所有项目

param( 
    [Parameter(Mandatory=$true)] 
    [string] $tfsUri, 
    [Parameter(Mandatory=$true)]  
    [string] $token 
    <#[Parameter(Mandatory=$true)] 
    [string] $User, 
    [Parameter(Mandatory=$true)] 
    [string] $Password#> 
) 


<# Base64-encodes the Personal Access Token (PAT) appropriately  #>
$User='' 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$token)));  
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};  
<#---------------------------------------------------------------------- #>
<# 
$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force    
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)        
#> 

$reportName = 'TeamProjectList.html' 

$teamProjectsReport = '<!DOCTYPE html><html><head> 
<!--mce:0--> 
</head><body>' 

$teamProjectsReport = $teamProjectsReport + '<h2><u><center>' + 'Team Project List' + '</center></u></h2>' 
$teamProjectsReport | Out-File -Force $reportName 
$teamProjectsReport = ''; 


$Uri = $tfsUri + '/_apis/projectCollections?api-version=1.0' 

$tfsCollections = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Uri -Headers $header  #-Credential $credential  


foreach($tfsCollection in $tfsCollections.value) 
{ 
    $top=100; 
    $skip=0; 

    $collectionName = $tfsCollection.name; 

    if ($tfsUri.Contains('visualstudio.com')) 
    { 
        $collectionName = 'DefaultCollection' 
    } 

    write-host '===============================' 
    write-host $tfsCollection.Name 
    $teamProjectsReport = '<ul><h4>'+ $collectionName + '</h4>'; 
    write-host '-------------------------------' 

    $collectionProjectList = @(); 

    while($true) 
    { 
        $Uri = $tfsUri + '/' + $collectionName +  '/_apis/projects?$top='+ $top + '&$skip='+ $skip + '&api-version=1.0' 

        $tfsProjects = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Uri -Headers $header #-Credential $credential  

        $skip+=$top; 

        if($tfsProjects.count -le 0) 
        { 
            $orderedProjects = $collectionProjectList| Sort-Object -Property name 
            foreach($tfsProject in $orderedProjects) 
            { 
                write-host $tfsProject.Name 
                $teamProjectsReport = $teamProjectsReport + '<li> <a target="_blank" href="' + $tfsUri + '/' + $collectionName +  '/'+ $tfsProject.Name + '" >' +  $tfsProject.Name + '</a></li>'; 
            } 

            $teamProjectsReport = $teamProjectsReport + '</ul>'      
            $teamProjectsReport | Out-File -Append -Force $reportName 
            $teamProjectsReport = ''; 

            break; 
        } 

        $collectionProjectList += $tfsProjects.value         
    } 

    write-host '-------------------------------' 
} 

$teamProjectsReport = $teamProjectsReport + '</ul></body></html>'      
$teamProjectsReport | Out-File -Append -Force $reportName

要在 VSTS 或 TFS 2017 或 2018 中执行此 PowerShell,您可以创建一个包含所有范围的 Persona Access Token (PAT)(此处需要所有范围才能访问集合级别信息)。您可以使用您的 VSTS 帐户调用脚本,如下所示。

.\GetAllProjects.ps1 -tfsUri 'https://youraccount.visualstudio.com' -token 'xxxxxxxxPATxxxxxxxxxxxxxx'

要在 2017 年、2018 年调用 TFS 脚本,您可以使用以下任一方法,具体取决于您的 TFS 是使用 SSL 设置的。

.\GetAllProjects.ps1 -tfsUri 'https://yourtfs/tfs' -token 'xxxxxxxxPATxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

.\GetAllProjects.ps1 -tfsUri 'http://yourtfs:8080/tfs' -token 'xxxxxxxxPATxxxxxxxxxxxxxxxxxxxxxxxxxxx'

项目如下

有关可用脚本的更多信息here