通过 API 创建 Azure DevOps 项目 Wiki 页面时出错 - HTTP 404 问题

问题描述 投票:0回答:2

在 Azure DevOps 中,我遇到了无法通过 API 创建项目 Wiki 页面的问题。我尝试使用 REST API 创建 Project Wiki 页面,但始终收到 404 HTTP 错误。我正在使用带有“curl”命令的 bash 任务从构建管道执行此操作。

这是我正在使用的管道:

trigger:
  branches:
    include:
    - '*'

jobs:
- job: WikiJob
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - bash: |
      #!/bin/bash
      set -e

      API_VERSION=7.1

      # Get the current project from the pipeline context
      project=$(System.TeamProject)

      # Define the Wiki details
      wikiRepo=${project}.wiki

      # Use the pipeline's built-in PAT as the Personal Access Token
      personalAccessToken=$(System.AccessToken)

      url="$SystemURI/$project/_apis/wiki/wikis/$wikiRepo/pages?path=my_page&api-version=$API_VERSION"
      
      echo URL $url

      # Prepare the content update request
      updateContent='{"content":"Foo Bar"}'

      response=$(curl -i -X PUT \
        -H "Authorization: Bearer $personalAccessToken" \
        -H "Content-Type: application/json" \
        --data "$updateContent" \
        --url "$url")

      echo response $response
    env:
      CollectionUri: $(System.CollectionUri)
      SystemURI: $(System.CollectionUri)

我可以确认变量已正确填充,并且 $wikiRepo 中定义的 Wiki 确实存在。

但是,我始终收到 HTTP 代码 404 和以下错误消息:

 {"$id":"1","innerException":null,"message":"TF401019: The Git repository with name or identifier e2b297b8-965a-4a29-8541-bfbc0e6314a0 does not exist or you do not have permissions for the operation you are attempting.","typeName":"Microsoft.TeamFoundation.Git.Server.GitRepositoryNotFoundException, Microsoft.TeamFoundation.Git.Server","typeKey":"GitRepositoryNotFoundException","errorCode":0,"eventId":3000}

错误中提到的Git repo在项目中不存在。

构建服务用户具有贡献者权限,这应该允许其添加和编辑 Wiki 页面。

示例中的 Wiki 页面

my_page
不存在。我希望创建该页面。

azure azure-devops azure-pipelines azure-devops-rest-api
2个回答
0
投票

在某些情况下,即使构建服务被添加为贡献者,也无法访问 wiki。尝试使用 PAT 而不是

System.AccessToken
。也许这是否是一个错误..,但这是一个在这里添加的好案例:https://developercommunity.visualstudio.com/AzureDevOps


0
投票

您可以检查以下事项来解决问题:

  1. 确保您提供了 Wiki 存储库的正确 URL,其中包含正确的组织名称、项目名称和存储库名称。

  2. 如果您在经典构建管道中运行 bash 任务,请确保您已在作业级别启用选项“允许脚本访问 OAuth 令牌”。

  3. 在 Wiki 的安全中心,确保两个身份“

    {Project Name} Build Service ({Organization Name})
    ”和“
    Project Collection Build Service ({Organization Name})
    ”的“
    Read
    ”和“
    Contribute
    ”权限设置为“
    Allow
    '.

© www.soinside.com 2019 - 2024. All rights reserved.