附加文件并创建新的工作项

问题描述

我正在寻找使用API​​在天蓝色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`"
  },`"path`": `"/fields/System.AssignedTo`",`"value`": `"$($AssignUser)`"
  },`"path`": `"/fields/System.AreaPath`",`"value`": `"$($AreaPath)`"
  },`"path`": `"/fields/System.IterationPath`",`"value`": `"$($IterationPath)`"
  },`"path`": `"/fields/System.AttachedFiles`",`"value`": `"spec.txt`"
  }
]"

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

我发现此链接可以使某人进入Powershell。我不明白用于附件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"
@H_502_7@

基于上述链接,我尝试附加文件,但出现错误

解决方法

尝试以下脚本:

$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. 您不需要文件的预读字节。只需使用-InFile cmdlet中的Invoke-RestMethod指向您的本地文件,然后指定 -ContentType application/octet-stream表示
    上传文件包含二进制数据。

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

    正常成功上传附件后,您将 从响应正文中查看其存储的URL。那是你可以的 用于链接到工作项。您可以参考我以前的answer以获得更多说明。

  3. 当您遵循此api创建具有特定内容的工作项时 类型,则需要使用$ + type name指向工作项类型。 但是$是powershell特殊字符之一,您需要使用` 逃避 $

,

您必须:

  1. 创建新附件:Attachments - CreateWork Item attachment REST API 5.0 in Powershell
  2. 将附件添加到工作项:Work Items - Update - Add an attachment