【问题标题】:Azure DevOps Server: create Wiki pages programmaticallyAzure DevOps 服务器:以编程方式创建 Wiki 页面
【发布时间】:2019-07-05 09:13:50
【问题描述】:

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

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

目前要发布 wikipage,我必须手动将 .md 文件上传到存储库,并通过 Publish Code as Wiki 选项将其链接。

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

结果如下:

编辑: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 -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 azure-pipelines azure-devops-rest-api azure-devops-wiki


    【解决方案1】:

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

    【讨论】:

    • 感谢您的指导。我按照Etienne Tremblay 中的步骤使用 REST API,生成并添加一个新 PAT 并尝试他的演示脚本。但是认证响应是ERROR: The requested URL could not be retrieved - Access Denied.。我知道这个错误可能有很多原因,但是设置$orgUrl = "https://132.200.14.216:8070"$personalToken = "uwawlzqp6j7i5n25dazspwmnp63th2w2axb5563hrlc2bi7yr5za" 是否正确。我不认为我可以在这里做错什么? Azure DevOps 服务器还需要配置吗?
    • 如果是本地 Azure DevOps 服务器,请尝试使用 powershell Invoke-RestMethod-UseDefaultCredentials(并且不使用 PAT)
    • 更进一步,我得到了许可。但是现在 API 在PUT 之后返回一个 json,但有一个例外:System.Net.WebException: The remote server returned an error: (403) Inadmissible。我将 http 内容定义为:@{"content" = "FrameworkA"}。此方法允许我更新现有的 wiki 页面还是?这意味着_apis/wiki/wikis/FrameworkX.wiki 中给出的页面必须存在?另一个问题是我想为path=$($path) 上传一个本地路径。那可能吗?我需要哪种格式?我想上传基于markdown 文件的wiki 页面。
    • @MarTin 请使用您尝试过的确切脚本编辑问题(或打开一个新问题)
    • 我的最终网址是:.../4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0?在我的浏览器中,我得到以下信息:{"path":"/","order":0,"isParentPage":true,"gitItemPath":"/","subPages":[],"url":"LONG URL","remoteUrl":"LONG URL","content":""}。我在 PowerShell 中得到了同样的结果。我认为下一步是按照您的描述覆盖content 变量,但我收到错误消息:The requested resource does not support http method 'PUT'. from Invoke-RestMethod -Method PUT -UseDefaultCredentials
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多