【问题标题】:Attach a file and create new workitem附加文件并创建新工作项
【发布时间】:2020-09-23 13:37:38
【问题描述】:

我希望使用 API 在 azure devops 中创建一个工作项。我能够创建带有标题、描述和区域路径、迭代路径的工作项。现在我想创建一个带有标题、描述、区域路径、迭代路径的工作项并附加一个文件并创建一个新的工作项。

创建工作项后有 API 可用于附加文件,但我想先附加文件并创建工作项

$Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }

$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$"+"$WorkItemType"+"?api-version=6.0"

$body="[
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.Title`",
    `"value`": `"$($WorkItemTitle)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.Description`",
    `"value`": `"This is for workitme testing`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AssignedTo`",
    `"value`": `"$($AssignUser)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AreaPath`",
    `"value`": `"$($AreaPath)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.IterationPath`",
    `"value`": `"$($IterationPath)`"
  },
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.AttachedFiles`",
    `"value`": `"spec.txt`"
  }
]"

Invoke-RestMethod -Uri $uri -Method POST -Headers $Header -ContentType "application/json-patch+json" -Body $body

我发现这个链接可以有人把它放到 powershell 上。我不明白用于附件的正文 url = attachment.Url link

$file = Get-ChildItem -Path "C:\Users\xx\Downloads\workitemAttachments\spec.txt"
$filename = $file.Name
$allFileBytes = [System.IO.File]::ReadAllBytes($file.FullName)
$body="[
    {
`"op`": `"add`",
Path = `"/relations/-`",
Value = new
{
`"rel`" = `"AttachedFile`",
`"url`" = `"$allFileBytes`",
`"attributes`" = `"{ `"comment`" = `"Comments for the file`"}`"
}
}
]"

$Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$type = "https://dev.azure.com/$organization/$project/_apis/wit/attachments?fileName=$filename&api-version=6.1-preview.3"
Invoke-RestMethod -Uri $type -Headers $Header -Method Post -Body $body -ContentType "application/json"

根据上面的链接,我尝试附加文件,但出现错误

【问题讨论】:

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


    【解决方案1】:

    试试下面的脚本:

    $connectionToken="{PAT token}" \\Put PAT token
        
    $file = Get-ChildItem -Path "C:\Users\xxx\Downloads\AusSouthEast.txt" \\Specify the file location
        
    $url="https://dev.azure.com/{org name}/_apis/wit/attachments?fileName=AusSouthEast.txt&api-version=6.1-preview.3"
        
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
        
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -InFile $file -ContentType application/octet-stream
    
    $AttachURL = $($response.url | ConvertTo-Json -Depth 100)
        
    $CreateUrl="https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/`${wit type}?api-version=6.1-preview.3"
        
    $WITBody=@"
    [
      {
        "op": "add",
        "path": "/fields/System.Title", \\WIT title
        "from": null,
        "value": "From Powershell"
      },
      {
        "op": "add",
        "path": "/relations/-",  \\WIT attachment
        "value": {
          "rel": "AttachedFile",
          "url": $AttachURL,
          "attributes": {
            "comment": "Spec for the task"
          }
        }
      }
    ]
    "@
        
    Invoke-RestMethod -Uri $CreateUrl -Headers @{Authorization = "Basic $token"} -Method Post -Body $WITBody -ContentType application/json-patch+json
    

    提示:

    1. 您不需要预先读取文件的字节数。只需在Invoke-RestMethod cmdlet 中使用-InFile 指向您的本地文件,然后指定 -ContentType application/octet-stream 表示
      上传文件包含二进制数据。

    2. 要回答你的第二个谜题,“不了解用于 attachment url = attachment.Url”。其实你上传后 带有 api 的附件,它们都由后端管理,并且 在将其链接到机智之前,不要与任何工作项相关联。 此外,这会导致您无法从 UI 中查看它们。找到它们的唯一方法是使用 URL 访问它们。

      在您成功上传附件后,像往常一样,您将 从响应正文中查看其存储的 URL。那是你可以的 用于链接到工作项。更多解释可以参考我之前的answer

    3. 当您按照此 api 创建具有特定 类型,您需要使用$ + type name 指向工作项类型。 但是$是powershell特殊字符之一,需要使用` 转义 $

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 假设我创建了一个新附件,然后我可以使用工作项标题、描述和附件等参数创建一个新工作项吗?我说对了吗?
      • @Sar 我想,是的。您可以在创建新工作项时创建附件并使用其 url。
      • 我发现这个链接可以有人把它放到 powershell 上。我不明白用于附件的正文 url = attachment.Url link
      • @Sar 创建附件时,您会收到带有 id 和 URL 的响应。您在新的工作项正文中使用此 URL。
      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多