Automation VSTS REST API用于创建新项目

时间:2018-01-09 20:47:48

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

我试图学习如何使用REST API,但我在第一个方面。不知道使用什么工具与VSTS REST API交互或如何配置它的部分。

任何类似的帮助"你应该使用这个工具"和"这就是你连接它的方式"。

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

1 个答案:

答案 0 :(得分:0)

您可以使用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的脚本,您可以使用以下任何一种,具体取决于您使用SSL设置的TFS。

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

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

项目如下所示 enter image description here

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

相关问题