Azure DevOps Server:以编程方式创建Wiki页面

时间:2019-07-05 09:13:50

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

我尝试在Azure DevOps Server 2019.0.1中基于Overview > Wiki的.net xml文档生成markdown Wiki页面。

要在每个构建中从.md生成.xml,我使用PowerShell脚本调用的XmlCommentMarkDownGenerator

当前要发布维基页面,我必须手动将.md文件上传到存储库,并通过将代码发布为Wiki 选项将其链接。

enter image description here

是否可以在构建过程中以编程方式将.md文件发布为Wikipage,而不将其添加到主存储库中?我想让Wikipage与每个构建过程保持最新。

结果如下:

enter image description here

编辑:PowerShell脚本:

Add-Type -AssemblyName System.Net.Http

#CREATE HTTP CLIENT
$client = New-Object -TypeName System.Net.Http.Httpclient
Write-Host "Request Azure Devops API:" -ForegroundColor Yellow

#PERSONAL ACCESS TOKEN
[string] $PersonalAccessToken = "uwadlzqp6j7i5n35dazsswmwp6gth2w2sxb55h3zrlc2bi7jn5ad";
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))

#CONFIGURE CLIENT
$json = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
$client.DefaultRequestHeaders.Accept.Add($json)
$client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $token)

#REQUEST
$uri = "http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/"
$task = $client.GetAsync($uri)
Write-Host "URI: $uri" -ForegroundColor Yellow

#WAIT FOR THE ASYNC CALL TO FINISH
$task.Wait()

#USE THE RESULT
if ($task.IsCompleted) {

    #DO YOUR THING HERE.
    [System.Net.Http.HttpResponseMessage] $msg = $task.Result
    $result = $msg.Content.ReadAsStringAsync()
    $converted = ConvertFrom-Json –InputObject $result.Result

    #LOOP RESULTS
    for ($i=0; $i -lt $converted.count; $i++) {
        $entry = $converted.value[$i]
        Write-Host "Name:`t $($entry.name)"
        Write-Host "Url:`t $($entry.url)"
        Write-Host

        #REQUEST WIKI
        $task = $client.GetAsync($entry.url + "/pages?")
        $task.Wait()
        [System.Net.Http.HttpResponseMessage] $msg = $task.Result
        $result = $msg.Content.ReadAsStringAsync()
        $converted = ConvertFrom-Json –InputObject $result.Result
        $converted.content = "Hallo"

        $wiki = $entry.name
        #$uri_new = $entry.url + "/pages?api-version=5.0"
        $uri_new = $entry.url + "/pages?pagePath=FrameworkA?api-version=5.0"
        Write-Host "Overwrite Url: $uri_new" -ForegroundColor Red

        $reconvert = ConvertTo-Json –InputObject $converted
        [System.Net.Http.HttpContent] $content = [System.Net.Http.StringContent]::new($reconvert)
        $client.PutAsync($uri_new, $content)


        Write-Host "SECOND TRY: Invoke-RestMethod" -ForegroundColor Green
        $content_type = $client.DefaultRequestHeaders.Accept.ToString()
        $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCredentials -Body @{"content" = "hallo"}
    }
}

脚本尝试了两种不同的方法来放置/发布到服务器:

  1. System.Net.Http.Client

  2. Invoke-RestMethod-方法PUT

输出为:

Request Azure Devops API:
URI: http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/
Name:    FrameworkA.wiki
Url:     http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3

Overwrite Url: http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0


Result                 : StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
                         {
                           Pragma: no-cache
                           X-TFS-ProcessId: c79f89a2-e776-41e0-8134-a18dfacad964
                           ActivityId: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-TFS-Session: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-E2EID: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-UserData: d82854f4-53e6-4455-a653-e0c8e665cef1:user
                           X-FRAME-OPTIONS: SAMEORIGIN
                           Lfs-Authenticate: NTLM
                           X-Content-Type-Options: nosniff
                           X-Cache: MISS from ptc-bld-squid
                           X-Cache-Lookup: MISS from ptc-bld-squid:3128
                           Connection: keep-alive
                           Cache-Control: no-cache
                           Date: Mon, 08 Jul 2019 10:53:01 GMT
                           P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
                           Server: Microsoft-IIS/10.0
                           Via: 1.1 ptc-bld-squid (squid/3.5.23)
                           X-AspNet-Version: 4.0.30319
                           X-Powered-By: ASP.NET
                           Content-Length: 92
                           Allow: GET
                           Content-Type: application/json; charset=utf-8
                           Expires: -1
                         }
Id                     : 277695
Exception              : 
Status                 : RanToCompletion
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             : 
IsFaulted              : False
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

SECOND TRY: Invoke-RestMethod
Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
In C:\Users\user\Desktop\Azure DevOps Console\DevOps API Call.ps1:60 Zeichen:21
+ ... $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1 个答案:

答案 0 :(得分:2)

您可以使用Azure DevOps Server Rest API语法创建或更新Wiki页面:

PUT https://dev.azure.com/{organization}}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}?api-version=5.0

身体:

{
    "content": "Test Page"
}

因此,您可以在构建中添加使用上述API的PowerShell任务并创建Wiki页面。