【发布时间】: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