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

Azure DevOps Server: create Wiki pages programmatically

我尝试在 Azure DevOps Server 2019.0.1 中基于 .net xml-documentation for Overview > Wiki 生成 Markdown Wiki 页面。

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

目前要发布维基页面,我必须手动将 .md 文件上传到存储库,然后 link 通过 将代码发布为 Wiki 选项。

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

结果如下:

编辑: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"}
    }
}

脚本尝试两种不同的方法 put/post 到服务器:

  1. System.Net.Http.Client

  2. Invoke-RestMethod -Method 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

您可以使用 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 页面。