Skip to content

Commit 5e8bcc7

Browse files
authored
convert priorities for AzureDevOps (#378)
* convert priorities for AzureDevOps Somewhere in history, we stopped importing priorities. Put that back. * Respond to feedback. Simplify and use case insensitive comparison.
1 parent 3a4c912 commit 5e8bcc7

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

DotNet.DocsTools/GitHubObjects/StoryPointSize.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public record StoryPointSize
2424
["Dec"] = 12 // 2
2525
};
2626

27+
private static int? PriorityValue(string? projectPriority)
28+
{
29+
// Start by 1, because our DevOps instance uses 1 - 4
30+
if (projectPriority?.Contains("0") == true) return 1;
31+
if (projectPriority?.Contains("1") == true) return 2;
32+
if (projectPriority?.Contains("2") == true) return 3;
33+
if (projectPriority?.Contains("3") == true) return 4;
34+
return default;
35+
}
36+
2737
public static int MonthOrdinal(string month) => s_months[month];
2838

2939
public static bool TryGetMonthOrdinal(string month, out int ordinal)
@@ -42,13 +52,15 @@ public static bool TryGetMonthOrdinal(string month, out int ordinal)
4252
// size may or may not have been set yet:
4353
string size = "🐂 Medium";
4454
string? sprintMonth = default;
55+
int? priority = default;
4556
foreach (JsonElement field in projectItem.Descendent("fieldValues", "nodes").EnumerateArray())
4657
{
4758
if (field.TryGetProperty("name", out JsonElement fieldValue))
4859
{
4960
string? fieldName = field.Descendent("field", "name").GetString();
5061
if (fieldName == "Sprint") sprintMonth = fieldValue.GetString();
5162
if (fieldName == "Size") size = fieldValue.GetString() ?? "🐂 Medium";
63+
if (fieldName == "Priority") priority = PriorityValue(fieldValue.GetString()) ?? 2;
5264
}
5365
}
5466
if ((projectTitle is not null) &&
@@ -60,7 +72,7 @@ public static bool TryGetMonthOrdinal(string month, out int ordinal)
6072
string month = sprintMonth ?? components[1];
6173
if (int.TryParse(components[yearIndex], out int year))
6274
{
63-
sz = new StoryPointSize(year, month.Substring(0, 3), size);
75+
sz = new StoryPointSize(year, month.Substring(0, 3), size, priority);
6476
}
6577
}
6678
} else
@@ -70,16 +82,18 @@ public static bool TryGetMonthOrdinal(string month, out int ordinal)
7082
return sz;
7183
}
7284

73-
private StoryPointSize(int CalendarYear, string Month, string Size)
85+
private StoryPointSize(int CalendarYear, string Month, string Size, int? Priority)
7486
{
7587
this.CalendarYear = CalendarYear;
7688
this.Month = Month;
7789
this.Size = Size;
90+
this.Priority = Priority;
7891
}
7992

8093
public int CalendarYear { get; }
8194
public string Month { get; }
8295
public string Size { get; }
96+
public int? Priority { get; }
8397

8498
public bool IsPastIteration
8599
{

actions/sequester/Quest2GitHub.Tests/QuestWorkItemTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void QuestItemConstructorEnsuresTitleIsTruncated(string title)
2121
IterationPath = "Test/Path",
2222
AssignedToId = Guid.NewGuid(),
2323
StoryPoints = 1,
24+
Priority = 1,
2425
Tags = []
2526
};
2627

actions/sequester/Quest2GitHub/Models/IssueExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ month descending
4040
return null;
4141
}
4242

43+
public static int? GetPriority(this QuestIssueOrPullRequest issue, StoryPointSize? storySize)
44+
{
45+
if (storySize?.Priority is not null) return storySize.Priority;
46+
47+
// Well, check for priority on the issue itself:
48+
foreach(var label in issue.Labels)
49+
{
50+
//Start at 1, because DevOps uses 1 - 4.
51+
if (label.Name.StartsWith("P", true, null))
52+
{
53+
if (label.Name.Contains("0")) return 1;
54+
if (label.Name.Contains("1")) return 2;
55+
if (label.Name.Contains("2")) return 3;
56+
if (label.Name.Contains("3")) return 4;
57+
}
58+
}
59+
return default;
60+
}
61+
4362
public static QuestIteration? ProjectIteration(this StoryPointSize storyPoints, IEnumerable<QuestIteration> iterations)
4463
{
4564
// New form: Content\Gallium\FY24Q1\07

actions/sequester/Quest2GitHub/Models/QuestWorkItem.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public required string Title
7676
/// </remarks>
7777
public required int? StoryPoints { get; init; }
7878

79+
/// <summary>
80+
/// The priority
81+
/// </summary>
82+
/// <remarks>
83+
/// This is retrieved from Microsoft.VSTS.Common.Priority
84+
/// </remarks>
85+
public required int Priority { get; init; }
86+
7987
/// <summary>
8088
/// The ID of the parent work item.
8189
/// </summary>
@@ -230,6 +238,13 @@ public static async Task<QuestWorkItem> CreateWorkItemAsync(QuestIssueOrPullRequ
230238
Value = iterationSize.QuestStoryPoint(),
231239
});
232240
}
241+
patchDocument.Add(new JsonPatchDocument
242+
{
243+
Operation = Op.Add,
244+
From = default,
245+
Path = "/fields/Microsoft.VSTS.Common.Priority",
246+
Value = issue.GetPriority(iterationSize)
247+
});
233248

234249
var tags = issue.WorkItemTagsForIssue(tagMap);
235250
if (tags.Any())
@@ -401,7 +416,9 @@ public static QuestWorkItem WorkItemFromJson(JsonElement root)
401416
JsonElement assignedNode = fields.Descendent("System.AssignedTo", "id");
402417
int? storyPoints = fields.TryGetProperty("Microsoft.VSTS.Scheduling.StoryPoints", out JsonElement storyPointNode) ?
403418
(int)double.Truncate(storyPointNode.GetDouble()) : null;
404-
string? assignedID = (assignedNode.ValueKind is JsonValueKind.String) ?
419+
int priority = fields.TryGetProperty("Microsoft.VSTS.Common.Priority", out JsonElement priorityNode) ?
420+
(int)priorityNode.GetInt32() : 2;
421+
string ? assignedID = (assignedNode.ValueKind is JsonValueKind.String) ?
405422
assignedNode.GetString() : null;
406423
string tagElement = fields.TryGetProperty("System.Tags", out JsonElement tagsNode) ?
407424
tagsNode.GetString()! : string.Empty;
@@ -418,6 +435,7 @@ public static QuestWorkItem WorkItemFromJson(JsonElement root)
418435
IterationPath = iterationPath,
419436
AssignedToId = (assignedID is not null) ? new Guid(assignedID) : null,
420437
StoryPoints = storyPoints,
438+
Priority = priority,
421439
Tags = tags
422440
};
423441
}

actions/sequester/Quest2GitHub/QuestGitHubService.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,18 @@ public async Task ProcessIssues(string organization, string repository, int dura
7878
Console.WriteLine("----- Finished processing issues. --------");
7979
Console.WriteLine(" ----- Regenerating bearer token ------");
8080
await ghClient.RegenerateBearerToken();
81-
Console.WriteLine("----- Starting processing pull requests. --------");
82-
var prQueryEnumerable = QueryIssuesOrPullRequests<QuestPullRequest>();
83-
await ProcessItems(prQueryEnumerable);
84-
Console.WriteLine("----- Finished processing pull requests. --------");
81+
try
82+
{
83+
Console.WriteLine("----- Starting processing pull requests. --------");
84+
var prQueryEnumerable = QueryIssuesOrPullRequests<QuestPullRequest>();
85+
await ProcessItems(prQueryEnumerable);
86+
Console.WriteLine("----- Finished processing pull requests. --------");
87+
} catch (InvalidOperationException e)
88+
{
89+
Console.WriteLine("Warning: GitHub query for Pull Requests failed.");
90+
Console.WriteLine(e);
91+
}
92+
Console.WriteLine($"Imported {totalImport} issues. Skipped {totalSkipped}");
8593

8694
async Task ProcessItems(IAsyncEnumerable<QuestIssueOrPullRequest> items)
8795
{
@@ -112,7 +120,6 @@ async Task ProcessItems(IAsyncEnumerable<QuestIssueOrPullRequest> items)
112120
}
113121
}
114122
}
115-
Console.WriteLine($"Imported {totalImport} issues. Skipped {totalSkipped}");
116123

117124
async IAsyncEnumerable<QuestIssueOrPullRequest> QueryIssuesOrPullRequests<T>() where T : QuestIssueOrPullRequest, IGitHubQueryResult<T, QuestIssueOrPullRequestVariables>
118125
{
@@ -431,6 +438,16 @@ private async Task RetrieveLabelIdsAsync(string org, string repo)
431438
Value = iterationSize.QuestStoryPoint(),
432439
});
433440
}
441+
int? priority = ghIssue.GetPriority(iterationSize);
442+
if (priority.HasValue && priority != questItem.Priority)
443+
{
444+
patchDocument.Add(new JsonPatchDocument
445+
{
446+
Operation = Op.Add,
447+
Path = "/fields/Microsoft.VSTS.Common.Priority",
448+
Value = priority.Value
449+
});
450+
}
434451
var tags = from t in ghIssue.WorkItemTagsForIssue(tagMap)
435452
where !questItem.Tags.Contains(t)
436453
select t;

0 commit comments

Comments
 (0)