【问题标题】:Programmatically adding Iterations and Calendar events to Azure DevOps以编程方式将迭代和日历事件添加到 Azure DevOps
【发布时间】:2020-11-06 10:34:34
【问题描述】:

我正在尝试将迭代和日历假期添加到我的 Azure DevOps 项目中,但无法解决。

使用 Azure DevOps CLI 和一些找到 here 的 PowerShell 脚本代码,然后运行它,窗口会立即关闭。在任何项目中都不会创建任何内容,也不会发生控制台写入:

Param
(
    [string]$PAT = [my PAT],
    [string]$Organization = [my organization],
    [string]$Project = 'DevTeam',
    [string]$TeamName = 'DevTeam Team',
    [DateTime]$StartDate = Get-Date,
    [int]$NumberOfSprints = 18
)

echo $PAT | az devops login --org $Organization

Write-Host '===Configuring connection to organization and Team Project'
az devops configure --defaults organization=$Organization project=$Project

For ($i=1; $i -le $NumberOfSprints; $i++) 
{
    $StartDateIteration = $StartDate.AddDays(($i - 1) * 14)
    $FinishDateIteration = $StartDateIteration.AddDays(20)
    $Sprint = $StartDateIteration + '-' + $FinishDateIteration
    $createIteration = az boards iteration project create --name $Sprint --start-date $StartDateIteration --finish-date $FinishDateIteration --org $Organization --project $Project | ConvertFrom-Json
    $addIteration = az boards iteration team add --id $createIteration.Identifier --team $TeamName --org $Organization --project $Project | ConvertFrom-Json
    Write-Host $addIteration.name 'created on path'$addIteration.path
}

所以我找到了使用 REST API 的 this。我在 Postman 中为 REST API 快速写了一些东西,但这也返回了一个错误:

预请求脚本:

var occurences = 18;
var start_date = "11/11/2020";
for(i=1; i <= occurences; i++){
    var repeat_every = 21*i; //repeat every number of days/weeks/months
    var first = new Date(start_date);
    first.setDate( first.getDate() + repeat_every );
    var last = new Date(first.setDate(first.getDate() - 1));
    var name = first.toDateString() + "-" + last.toDateString();
    pm.environment.set("first", first);
    pm.environment.set("last", last);
    pm.environment.set("name", name);
};

JSON POST 正文到“dev.azure.com/[我的组织]/DevTeam/_apis/work/teamsettings/iterations?api-version=6.0”

{
  "id": [my identifier from "GET dev.azure.com/[my organization]/DevTeam/_apis/wit/classificationnodes/iterations?api-version=6.0],
  "name": {{name}},
  "path": "DevTeam\\Iteration",
  "attributes": {
    "startDate": {{first}},
    "finishDate": {{last}}
  }
}

错误:

{
    "$id": "1",
    "innerException": null,
    "message": "TF400898: An Internal Error Occurred. Activity Id: bbe88905-2083-47e2-9b2c-d87be87c4adb.",
    "typeName": "Newtonsoft.Json.JsonReaderException, Newtonsoft.Json",
    "typeKey": "JsonReaderException",
    "errorCode": 0,
    "eventId": 0
}
  1. 我怎样才能使其中任何一个成功运行?
  2. 这会自动在日历上添加迭代吗?我们的想法是整合日历并让 DevOps 跟踪我们的工作时间和休息时间,以便我们可以将其用于 Sprint 计划。

谢谢。

【问题讨论】:

  • 不是适合您的解决方案,但很多屏幕 UI 都使用相同的 API。您可以尝试手动添加一个来模仿您正在编写的脚本。然后使用浏览器调试器检查网络流量。
  • 嗨@aplane1290。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this

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


【解决方案1】:

我想分享一下 Rest API 方法。

Rest API : Iterations - Post Team Iteration 仅用于将迭代添加到团队。

如果需要创建或更改现有的迭代,需要结合以下两个API:

创建新迭代:Classification Nodes - Create Or Update

更新现有迭代:Classification Nodes - Update

以下是 PowerShell 脚本示例:

创建新的迭代并将迭代添加到团队中:

$token = "PAT"

$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/wit/classificationnodes/Iterations?api-version=5.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  "name": "kevin5 Iteration",
  "attributes": {
    "startDate": "2014-10-27T00:00:00Z",
    "finishDate": "2014-10-31T00:00:00Z"
  }
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

$IterationId = $response.identifier

echo $IterationId

$url2 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/{TeamName}/_apis/work/teamsettings/iterations?api-version=6.0"

$body = "{                
      `"id`": `"$IterationId`"
          }"

$response = Invoke-RestMethod -Uri $url2 -Headers @{Authorization = "Basic $token"} -Method Post -Body $body -ContentType application/json

更新现有迭代并将迭代添加到团队:

$token = "PAT"

$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/wit/classificationnodes/iterations/{IterationName}?api-version=5.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  "name": "Edit Iteration",
  "attributes": {
    "startDate": "2014-10-27T00:00:00Z",
    "finishDate": "2014-10-31T00:00:00Z"
  }
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PATCH -Body $JSON -ContentType application/json

$IterationId = $response.identifier

echo $IterationId

$url2 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/{TeamName}/_apis/work/teamsettings/iterations?api-version=6.0"

$body = "{                
      `"id`": `"$IterationId`"
          }"

$response = Invoke-RestMethod -Uri $url2 -Headers @{Authorization = "Basic $token"} -Method Post -Body $body -ContentType application/json

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多