forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevOpsService.cs
More file actions
1341 lines (1221 loc) · 67.2 KB
/
DevOpsService.cs
File metadata and controls
1341 lines (1221 loc) · 67.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Core;
using Azure.Identity;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.OAuth;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.VisualStudio.Services.WebApi;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Azure.Sdk.Tools.Cli.Configuration;
using Azure.Sdk.Tools.Cli.Models;
using Azure.Sdk.Tools.Cli.Models.Responses.Package;
using Azure.Sdk.Tools.Cli.Models.Responses;
using System.Globalization;
using Azure.Sdk.Tools.Cli.Models.AzureDevOps;
namespace Azure.Sdk.Tools.Cli.Services
{
public interface IDevOpsConnection
{
public BuildHttpClient GetBuildClient();
public WorkItemTrackingHttpClient GetWorkItemClient();
public ProjectHttpClient GetProjectClient();
}
public class DevOpsConnection(IAzureService azureService) : IDevOpsConnection
{
private BuildHttpClient _buildClient;
private WorkItemTrackingHttpClient _workItemClient;
private ProjectHttpClient _projectClient;
private AccessToken? _token;
private void RefreshConnection()
{
if (_token != null && _token?.ExpiresOn > DateTimeOffset.Now.AddMinutes(5))
{
return;
}
var credential = azureService.GetCredential();
try
{
_token = credential.GetToken(new TokenRequestContext([Constants.AZURE_DEVOPS_TOKEN_SCOPE]), CancellationToken.None);
}
catch
{
credential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TenantId = null });
// Retry with interactive browser credential if the initial credential fails
_token = credential.GetToken(new TokenRequestContext([Constants.AZURE_DEVOPS_TOKEN_SCOPE]), CancellationToken.None);
}
// If we still don't have a token, throw an exception
if (_token == null)
{
throw new Exception("Failed to get devops access token. " +
"Ensure you have access to the azure-sdk devops project (http://aka.ms/azsdk/access)" +
"and are logged in via az cli, az powershell, vs/vscode or interactive browser sign-in.");
}
var connection = new VssConnection(new Uri(Constants.AZURE_SDK_DEVOPS_BASE_URL), new VssOAuthAccessTokenCredential(_token?.Token));
_buildClient = connection.GetClient<BuildHttpClient>();
_workItemClient = connection.GetClient<WorkItemTrackingHttpClient>();
_projectClient = connection.GetClient<ProjectHttpClient>();
}
public BuildHttpClient GetBuildClient()
{
RefreshConnection();
return _buildClient;
}
public WorkItemTrackingHttpClient GetWorkItemClient()
{
RefreshConnection();
return _workItemClient;
}
public ProjectHttpClient GetProjectClient()
{
RefreshConnection();
return _projectClient;
}
}
public interface IDevOpsService
{
public Task<List<ReleasePlanWorkItem>> ListOverdueReleasePlansAsync();
public Task<ReleasePlanWorkItem> GetReleasePlanAsync(int releasePlanId);
public Task<ReleasePlanWorkItem> GetReleasePlanForWorkItemAsync(int workItemId);
public Task<ReleasePlanWorkItem> GetReleasePlanAsync(string pullRequestUrl);
public Task<List<ReleasePlanWorkItem>> GetReleasePlansForProductAsync(string productTreeId, string specApiVersion, string sdkReleaseType, bool isTestReleasePlan = false);
public Task<WorkItem> CreateReleasePlanWorkItemAsync(ReleasePlanWorkItem releasePlan);
public Task<Build> RunSDKGenerationPipelineAsync(string apiSpecBranchRef, string typespecProjectRoot, string apiVersion, string sdkReleaseType, string language, int workItemId, string sdkRepoBranch = "");
public Task<Build> GetPipelineRunAsync(int buildId);
public Task<string> GetSDKPullRequestFromPipelineRunAsync(int buildId, string language, int workItemId);
public Task<bool> AddSdkInfoInReleasePlanAsync(int workItemId, string language, string sdkGenerationPipelineUrl, string sdkPullRequestUrl, string generationStatus = "");
public Task<bool> UpdateReleasePlanSDKDetailsAsync(int workItemId, List<SDKInfo> sdkLanguages);
public Task<bool> UpdateApiSpecStatusAsync(int workItemId, string status);
public Task<bool> UpdateSpecPullRequestAsync(int releasePlanWorkItemId, string specPullRequest);
public Task<bool> LinkNamespaceApprovalIssueAsync(int releasePlanWorkItemId, string url);
public Task<PackageWorkitemResponse> GetPackageWorkItemAsync(string packageName, string language, string packageVersion = "");
public Task<List<PackageWorkitemResponse>> ListPartialPackageWorkItemAsync(string packageName, string language);
public Task<Build> RunPipelineAsync(int pipelineDefinitionId, Dictionary<string, string> templateParams, string apiSpecBranchRef = "main");
public Task<Dictionary<string, List<string>>> GetPipelineLlmArtifacts(string project, int buildId);
public Task<WorkItem> UpdateWorkItemAsync(int workItemId, Dictionary<string, string> fields);
}
public partial class DevOpsService(ILogger<DevOpsService> logger, IDevOpsConnection connection) : IDevOpsService
{
private static readonly string RELEASE_PLANER_APP_TEST = "Release Planner App Test";
private List<WorkItemRelationType>? _cachedRelationTypes;
private static readonly string[] SUPPORTED_SDK_LANGUAGES = { "Dotnet", "JavaScript", "Python", "Java", "Go" };
[GeneratedRegex("\\|\\s(Beta|Stable|GA)\\s\\|\\s([\\S]+)\\s\\|\\s([\\S]+)\\s\\|")]
private static partial Regex SdkReleaseDetailsRegex();
private async Task<List<WorkItemRelationType>> GetCachedRelationTypes() =>
_cachedRelationTypes ??= await connection.GetWorkItemClient().GetRelationTypesAsync();
public async Task<List<ReleasePlanWorkItem>> ListOverdueReleasePlansAsync()
{
try
{
var query = $"SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{Constants.AZURE_SDK_DEVOPS_RELEASE_PROJECT}'";
query += $" AND [System.Tags] NOT CONTAINS '{RELEASE_PLANER_APP_TEST}'";
query += " AND [System.WorkItemType] = 'Release Plan'";
query += " AND [System.State] IN ('In Progress','Not Started','New')";
query += " AND [Custom.SDKReleasemonth] <> ''";
var releasePlanWorkItems = await FetchWorkItemsPagedAsync(query);
var releasePlans = await Task.WhenAll(releasePlanWorkItems.Select(workItem => MapWorkItemToReleasePlanAsync(workItem)));
var today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
var overduePlans = releasePlans.Where(releasePlan =>
{
if (DateTime.TryParseExact(releasePlan.SDKReleaseMonth, "MMMM yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var releaseDate))
{
var normalizedReleaseDate = new DateTime(releaseDate.Year, releaseDate.Month, 1);
return normalizedReleaseDate < today;
}
return false;
}).ToList();
return overduePlans;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to list overdue release plans");
throw new Exception("Failed to list overdue release plans. Error: {ex}", ex);
}
}
public async Task<ReleasePlanWorkItem> GetReleasePlanForWorkItemAsync(int workItemId)
{
logger.LogInformation("Fetching release plan work with id {workItemId}", workItemId);
var workItem = await connection.GetWorkItemClient().GetWorkItemAsync(workItemId, expand: WorkItemExpand.All);
if (workItem?.Id == null)
{
throw new InvalidOperationException($"Work item {workItemId} not found.");
}
var releasePlan = await MapWorkItemToReleasePlanAsync(workItem);
releasePlan.WorkItemUrl = workItem.Url;
releasePlan.WorkItemId = workItem?.Id ?? 0;
return releasePlan;
}
public async Task<ReleasePlanWorkItem> GetReleasePlanAsync(int releasePlanId)
{
// First find the API spec work item
var query = $"SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{Constants.AZURE_SDK_DEVOPS_RELEASE_PROJECT}' AND [Custom.ReleasePlanID] = '{releasePlanId}' AND [System.WorkItemType] = 'Release Plan' AND [System.State] NOT IN ('Closed','Duplicate','Abandoned')";
var releasePlanWorkItems = await FetchWorkItemsAsync(query);
if (releasePlanWorkItems.Count == 0)
{
throw new Exception($"Failed to find release plan work item with release plan Id {releasePlanId}");
}
return await MapWorkItemToReleasePlanAsync(releasePlanWorkItems[0]);
}
public async Task<List<ReleasePlanWorkItem>> GetReleasePlansForProductAsync(string productTreeId, string specApiVersion, string sdkReleaseType, bool isTestReleasePlan=false)
{
try
{
var query = $"SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{Constants.AZURE_SDK_DEVOPS_RELEASE_PROJECT}'";
query += $" AND [System.Tags] {(isTestReleasePlan ? "CONTAINS" : "NOT CONTAINS")} '{RELEASE_PLANER_APP_TEST}'";
query += $" AND [Custom.ProductServiceTreeID] = '{productTreeId}'";
query += $" AND [Custom.SDKtypetobereleased] = '{sdkReleaseType}'";
query += " AND [System.WorkItemType] = 'Release Plan'";
query += " AND [System.State] IN ('New','Not Started','In Progress')";
var releasePlanWorkItems = await FetchWorkItemsAsync(query);
if (releasePlanWorkItems.Count == 0)
{
logger.LogInformation("Release plan does not exist for the given product id {productTreeId}",productTreeId);
return new List<ReleasePlanWorkItem>();
}
var releasePlans = new List<ReleasePlanWorkItem>();
foreach (var workItem in releasePlanWorkItems)
{
var releasePlan = await MapWorkItemToReleasePlanAsync(workItem);
if (releasePlan.SpecAPIVersion == specApiVersion)
{
releasePlans.Add(releasePlan);
}
}
return releasePlans;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get release plans for product id {productTreeId}", productTreeId);
throw new Exception($"Failed to get release plans for product id {productTreeId}. Error: {ex.Message}");
}
}
private async Task<ReleasePlanWorkItem> MapWorkItemToReleasePlanAsync(WorkItem workItem)
{
var releasePlan = new ReleasePlanWorkItem()
{
WorkItemId = workItem.Id ?? 0,
WorkItemUrl = workItem.Url,
WorkItemHtmlUrl = workItem.Url?.Replace("_apis/wit/workItems", "_workitems/edit") ?? string.Empty,
Title = workItem.Fields.TryGetValue("System.Title", out object? value) ? value?.ToString() ?? string.Empty : string.Empty,
Status = workItem.Fields.TryGetValue("System.State", out value) ? value?.ToString() ?? string.Empty : string.Empty,
ServiceTreeId = workItem.Fields.TryGetValue("Custom.ServiceTreeID", out value) ? value?.ToString() ?? string.Empty : string.Empty,
ProductTreeId = workItem.Fields.TryGetValue("Custom.ProductServiceTreeID", out value) ? value?.ToString() ?? string.Empty : string.Empty,
SDKReleaseMonth = workItem.Fields.TryGetValue("Custom.SDKReleasemonth", out value) ? value?.ToString() ?? string.Empty : string.Empty,
IsManagementPlane = workItem.Fields.TryGetValue("Custom.MgmtScope", out value) ? value?.ToString() == "Yes" : false,
IsDataPlane = workItem.Fields.TryGetValue("Custom.DataScope", out value) ? value?.ToString() == "Yes" : false,
ReleasePlanLink = workItem.Fields.TryGetValue("Custom.ReleasePlanLink", out value) ? value?.ToString() ?? string.Empty : string.Empty,
ReleasePlanId = workItem.Fields.TryGetValue("Custom.ReleasePlanID", out value) ? int.Parse(value?.ToString() ?? "0") : 0,
SDKReleaseType = workItem.Fields.TryGetValue("Custom.SDKtypetobereleased", out value) ? value?.ToString() ?? string.Empty : string.Empty,
IsCreatedByAgent = workItem.Fields.TryGetValue("Custom.IsCreatedByAgent", out value) && "Copilot".Equals(value?.ToString()),
ReleasePlanSubmittedByEmail = workItem.Fields.TryGetValue("Custom.ReleasePlanSubmittedby", out value) ? value?.ToString() ?? string.Empty : string.Empty,
SDKLanguages = workItem.Fields.TryGetValue("Custom.SDKLanguages", out value) ? value?.ToString() ?? string.Empty : string.Empty,
IsSpecApproved = workItem.Fields.TryGetValue("Custom.APISpecApprovalStatus", out value) && "Approved".Equals(value?.ToString()),
LanguageExclusionRequesterNote = workItem.Fields.TryGetValue("Custom.ReleaseExclusionRequestNote", out value) ? value?.ToString() ?? string.Empty : string.Empty,
LanguageExclusionApproverNote = workItem.Fields.TryGetValue("Custom.ReleaseExclusionApprovalNote", out value) ? value?.ToString() ?? string.Empty : string.Empty,
APISpecProjectPath = workItem.Fields.TryGetValue("Custom.ApiSpecProjectPath", out value) ? value?.ToString() ?? string.Empty : string.Empty,
Owner = workItem.Fields.TryGetValue("Custom.PrimaryPM", out value) ? value?.ToString() ?? string.Empty : string.Empty,
};
foreach (var lang in SUPPORTED_SDK_LANGUAGES)
{
var sdkGenPipelineUrl = workItem.Fields.TryGetValue($"Custom.SDKGenerationPipelineFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var sdkPullRequestUrl = workItem.Fields.TryGetValue($"Custom.SDKPullRequestFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var packageName = workItem.Fields.TryGetValue($"Custom.{lang}PackageName", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var generationStatus = workItem.Fields.TryGetValue($"Custom.GenerationStatusFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var releaseStatus = workItem.Fields.TryGetValue($"Custom.ReleaseStatusFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var pullRequestStatus = workItem.Fields.TryGetValue($"Custom.SDKPullRequestStatusFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
var exclusionStatus = workItem.Fields.TryGetValue($"Custom.ReleaseExclusionStatusFor{lang}", out value) ? value?.ToString() ?? string.Empty : string.Empty;
releasePlan.SDKInfo.Add(
new SDKInfo()
{
Language = MapLanguageIdToName(lang),
GenerationPipelineUrl = sdkGenPipelineUrl,
SdkPullRequestUrl = sdkPullRequestUrl,
GenerationStatus = generationStatus,
ReleaseStatus = releaseStatus,
PullRequestStatus = pullRequestStatus,
PackageName = packageName,
ReleaseExclusionStatus = exclusionStatus
}
);
}
// Get details from API spec work item
try
{
logger.LogInformation("Fetching API spec work item for release plan work item {workItemId}", releasePlan.WorkItemId);
var apiSpecWorkItem = await GetApiSpecWorkItemAsync(releasePlan.WorkItemId);
if (apiSpecWorkItem != null && apiSpecWorkItem.Fields != null)
{
releasePlan.ActiveSpecPullRequest = apiSpecWorkItem.Fields.TryGetValue("Custom.ActiveSpecPullRequestUrl", out Object? specPr) ? specPr?.ToString() ?? string.Empty : string.Empty;
releasePlan.SpecAPIVersion = apiSpecWorkItem.Fields.TryGetValue("Custom.APISpecversion", out Object? apiVersion) ? apiVersion?.ToString() ?? string.Empty : string.Empty;
releasePlan.SpecType = apiSpecWorkItem.Fields.TryGetValue("Custom.APISpecDefinitionType", out Object? specType) ? specType?.ToString() ?? string.Empty : string.Empty;
}
else
{
logger.LogWarning("API spec work item not found for release plan work item {workItemId}", releasePlan.WorkItemId);
}
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get API spec work item for release plan work item {WorkItemId}", releasePlan.WorkItemId);
}
return releasePlan;
}
public async Task<ReleasePlanWorkItem> GetReleasePlanAsync(string pullRequestUrl)
{
// First find the API spec work item
try
{
var query = $"SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{Constants.AZURE_SDK_DEVOPS_RELEASE_PROJECT}' AND [Custom.ActiveSpecPullRequestUrl] = '{pullRequestUrl}' AND [System.WorkItemType] = 'API Spec' AND [System.State] NOT IN ('Closed','Duplicate','Abandoned','Finished')";
var apiSpecWorkItems = await FetchWorkItemsAsync(query);
if (apiSpecWorkItems.Count == 0)
{
logger.LogInformation("Release plan does not exist for the given pull request URL.");
return null;
}
foreach (var workItem in apiSpecWorkItems)
{
if (workItem.Relations.Any())
{
var parent = workItem.Relations.FirstOrDefault(w => w.Rel.Equals("System.LinkTypes.Hierarchy-Reverse"));
if (parent == null)
{
continue;
}
// Get parent work item and make sure it is release plan work item
var parentWorkItemId = int.Parse(parent.Url.Split('/').Last());
var parentWorkItem = await connection.GetWorkItemClient().GetWorkItemAsync(parentWorkItemId);
if (parentWorkItem == null || !parentWorkItem.Fields.TryGetValue("System.WorkItemType", out Object? parentType))
{
continue;
}
if (parentType.Equals("Release Plan"))
{
return await MapWorkItemToReleasePlanAsync(parentWorkItem);
}
}
}
return null;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get release plan for pull request URL {PullRequestUrl}", pullRequestUrl);
throw new Exception($"Failed to get release plan for pull request URL {pullRequestUrl}.", ex);
}
}
public async Task<WorkItem> CreateReleasePlanWorkItemAsync(ReleasePlanWorkItem releasePlan)
{
int releasePlanWorkItemId = 0;
int apiSpecWorkItemId = 0;
var workItemClient = connection.GetWorkItemClient();
try
{
// Create release plan work item
var releasePlanTitle = $"Release plan for {releasePlan.ProductName ?? releasePlan.ProductTreeId}";
logger.LogInformation("Creating release plan with title: {releasePlanTitle}", releasePlanTitle);
var releasePlanWorkItem = await CreateWorkItemAsync(releasePlan, "Release Plan", releasePlanTitle);
releasePlanWorkItemId = releasePlanWorkItem?.Id ?? 0;
if (releasePlanWorkItemId == 0)
{
throw new Exception("Failed to create release plan work item");
}
// Create API spec work item
var apiSpecTitle = $"API spec for {releasePlan.ProductName ?? releasePlan.ProductTreeId} - version {releasePlan.SpecAPIVersion}";
logger.LogInformation("Creating api spec with title: {apiSpecTitle}", apiSpecTitle);
var apiSpecWorkItem = await CreateWorkItemAsync(releasePlan.ToApiSpecWorkItem(), "API Spec", apiSpecTitle, parentId: releasePlanWorkItemId);
apiSpecWorkItemId = apiSpecWorkItem.Id ?? 0;
if (apiSpecWorkItemId == 0)
{
throw new Exception("Failed to create API spec work item");
}
// Update release plan status to in progress
releasePlanWorkItem = await UpdateWorkItemAsync(releasePlanWorkItemId, new Dictionary<string, string>
{
{ "System.State", "In Progress" }
});
if (releasePlanWorkItem != null)
{
return releasePlanWorkItem;
}
throw new Exception("Failed to create API spec work item");
}
catch (Exception ex)
{
const string errorMessage = "Failed to create release plan and API spec work items";
logger.LogError(ex, errorMessage);
// Delete created work items if both release plan and API spec work items were not created and linked
if (releasePlanWorkItemId != 0)
{
await workItemClient.DeleteWorkItemAsync(releasePlanWorkItemId);
}
if (apiSpecWorkItemId != 0)
{
await workItemClient.DeleteWorkItemAsync(apiSpecWorkItemId);
}
throw new Exception(errorMessage, ex);
}
}
private async Task<WorkItem> CreateWorkItemAsync(WorkItemBase workItem, string workItemType, string title, int? parentId = null, int? relatedId = null)
{
workItem.Title = title;
var workItemsFieldJson = JsonSerializer.Serialize(workItem);
logger.LogDebug("Input work item json: {releasePlanJson}", workItemsFieldJson);
var specDocument = workItem.GetPatchDocument();
logger.LogInformation("Creating {workItemType} work item", workItemType);
logger.LogDebug("Sending work item request to DevOps: {@specDocument}", specDocument);
var createdWorkItem = await connection.GetWorkItemClient().CreateWorkItemAsync(specDocument, Constants.AZURE_SDK_DEVOPS_RELEASE_PROJECT, workItemType);
if (createdWorkItem == null)
{
throw new Exception("Failed to create Work Item");
}
if (createdWorkItem.Id != null)
{
if (parentId != null)
{
// Create parent-child relation
await CreateWorkItemRelationAsync(createdWorkItem.Id.Value, "parent", targetId: parentId);
}
if (relatedId != null)
{
await CreateWorkItemRelationAsync(createdWorkItem.Id.Value, "related", targetId: relatedId);
}
}
return createdWorkItem;
}
private async Task<WorkItem> CreateWorkItemRelationAsync(int id, string relationType, int? targetId = null, string? targetUrl = null)
{
// Create generic work item relation(s) based on target ID and/or URL
if (targetId == null && string.IsNullOrWhiteSpace(targetUrl))
{
throw new Exception("To create work item relation, either Target ID or Target URL must be provided.");
}
var workItemClient = connection.GetWorkItemClient();
// Resolve relation type system name/reference
// ex: Child, Parent, Related, etc map to the appropriate name.
var relationTypeSystemName = await ResolveRelationTypeSystemName(relationType);
var patchDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();
// Handle target ID
if (targetId != null)
{
var wiql = new Wiql { Query = $"SELECT [System.Id] FROM WorkItems WHERE ([System.Id] = {targetId})" };
var queryResult = await workItemClient.QueryByWiqlAsync(wiql);
var targetWorkItems = queryResult.WorkItems ?? [];
// Query should only come up with one work item
if (!targetWorkItems.Any())
{
throw new Exception($"Work item with ID {targetId} does not exist.");
}
var targetWorkItem = targetWorkItems.First();
// targetWorkItem contains only Id + Url; URL is enough to create relation
patchDocument.Add(new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/relations/-",
Value = new WorkItemRelation
{
Rel = relationTypeSystemName,
Url = targetWorkItem.Url
}
});
}
// Handle target URLs (comma-separated)
else if (!string.IsNullOrWhiteSpace(targetUrl))
{
patchDocument.Add(new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/relations/-",
Value = new WorkItemRelation
{
Rel = relationTypeSystemName,
Url = targetUrl
}
});
}
return await workItemClient.UpdateWorkItemAsync(patchDocument, id);
}
private async Task<string> ResolveRelationTypeSystemName(string relationType)
{
var relationTypes = await GetCachedRelationTypes();
// Match service-provided relation type by display name (case-insensitive)
var match = relationTypes.FirstOrDefault(rt => string.Equals(rt.Name, relationType, StringComparison.OrdinalIgnoreCase));
if(match != null && match.ReferenceName != null)
{
return match.ReferenceName;
}
throw new Exception($"Relation Type '{relationType}' is not valid.");
}
public static string MapLanguageToId(string language)
{
var lang = language.ToLower();
return lang switch
{
".net" => "Dotnet",
"csharp" => "Dotnet",
"js" => "JavaScript",
"javascript" => "JavaScript",
"python" => "Python",
"java" => "Java",
"go" => "Go",
_ => language
};
}
public static string MapLanguageIdToName(string language)
{
var lang = language.ToLower();
return lang switch
{
"dotnet" => ".NET",
"csharp" => ".NET",
".net" => ".NET",
"typescript" => "JavaScript",
"python" => "Python",
"javascript" => "JavaScript",
"java" => "Java",
"go" => "Go",
_ => language
};
}
public async Task<bool> AddSdkInfoInReleasePlanAsync(int workItemId, string language, string sdkGenerationPipelineUrl, string sdkPullRequestUrl, string generationStatus = "")
{
// Adds SDK generation and pull request link in release plan work item.
try
{
if (string.IsNullOrEmpty(language) || workItemId == 0 || (string.IsNullOrEmpty(sdkGenerationPipelineUrl) && string.IsNullOrEmpty(sdkPullRequestUrl)))
{
logger.LogError("Please provide the language, work item ID, and either the SDK generation pipeline URL or the SDK pull request URL to add SDK info to a work item.");
return false;
}
var workItem = await connection.GetWorkItemClient().GetWorkItemAsync(workItemId);
if (workItem == null)
{
throw new Exception($"Work item {workItemId} not found.");
}
var jsonLinkDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();
// Add work item as child of release plan work item
if (!string.IsNullOrEmpty(sdkGenerationPipelineUrl))
{
jsonLinkDocument.Add(
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = $"/fields/Custom.SDKGenerationPipelineFor{MapLanguageToId(language)}",
Value = sdkGenerationPipelineUrl
});
}
if (!string.IsNullOrEmpty(generationStatus))
{
jsonLinkDocument.Add(
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = $"/fields/Custom.GenerationStatusFor{MapLanguageToId(language)}",
Value = generationStatus
});
}
if (!string.IsNullOrEmpty(sdkPullRequestUrl))
{
jsonLinkDocument.Add(
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = $"/fields/Custom.SDKPullRequestFor{MapLanguageToId(language)}",
Value = sdkPullRequestUrl
});
}
int maxTryCount = 5, retryCount = 0;
while (retryCount < maxTryCount)
{
try
{
// DevOps SDK internally caches the revision number of the work item and throws conflict error if it is outdated.
// Work around is to fetch the work item again before updating it.
await connection.GetWorkItemClient().GetWorkItemAsync(workItemId);
await connection.GetWorkItemClient().UpdateWorkItemAsync(jsonLinkDocument, workItemId);
return true;
}
catch (Exception ex)
{
// Retry once if there is a conflict error
logger.LogWarning("Conflict error while updating work item {workItemId}, retrying update work item again.", workItemId);
retryCount++;
if (retryCount == maxTryCount)
{
throw new Exception($"Failed to update DevOps work item after multiple retries. Error: {ex.Message}");
}
await Task.Delay(1000 * retryCount); // Exponential backoff
}
}
return false;
}
catch (Exception ex)
{
throw new Exception($"Failed to update SDK generation details to work item [{workItemId}]. Error: {ex.Message}");
}
}
private async Task<List<WorkItem>> FetchWorkItemsAsync(string query)
{
try
{
var workItemClient = connection.GetWorkItemClient();
var result = await workItemClient.QueryByWiqlAsync(new Wiql { Query = query });
logger.LogInformation("Work item query result: {result}", result);
if (result != null && result.WorkItems != null && result.WorkItems.Any())
{
var ids = result.WorkItems.Select(wi => wi.Id).ToList();
logger.LogInformation("Fetching work item details: {workItemIds}", string.Join(',', ids));
return await workItemClient.GetWorkItemsAsync(ids, expand: WorkItemExpand.All);
}
else
{
logger.LogWarning("No work items found.");
return [];
}
}
catch (Exception ex)
{
throw new Exception($"Failed to get work item. Error: {ex.Message}", ex);
}
}
private async Task<List<WorkItem>> FetchWorkItemsPagedAsync(string query, int top = 100000, int batchSize = 200)
{
try
{
var workItemClient = connection.GetWorkItemClient();
var result = await workItemClient.QueryByWiqlAsync(new Wiql { Query = query }, top: top);
logger.LogInformation("Work item query result: {result}", result);
if (result != null && result.WorkItems != null && result.WorkItems.Any())
{
var ids = result.WorkItems.Select(wi => wi.Id).ToList();
logger.LogInformation("Work item query returned {workItemIdCount} ids", ids.Count);
logger.LogInformation("Fetching work item details: {workItemIds}", string.Join(',', ids));
var workItems = new List<WorkItem>();
for (int i = 0; i < ids.Count; i += batchSize)
{
var batchIds = ids.Skip(i).Take(batchSize).ToList();
var batch = await workItemClient.GetWorkItemsAsync(batchIds, expand: WorkItemExpand.All);
workItems.AddRange(batch);
}
return workItems;
}
else
{
logger.LogWarning("No work items found.");
return [];
}
}
catch (Exception ex)
{
throw new Exception($"Failed to get work item. Error: {ex.Message}", ex);
}
}
private static int GetPipelineDefinitionId(string language)
{
language = MapLanguageToId(language);
return language.ToLower() switch
{
"python" => 7423,
"javascript" => 7422,
"go" => 7426,
"java" => 7421,
"dotnet" => 7412,
_ => 0,
};
}
public static bool IsSDKGenerationSupported(string language)
{
language = MapLanguageToId(language);
return language.ToLower() switch
{
"python" => true,
"dotnet" => true,
"javascript" => true,
"java" => true,
"go" => true,
_ => false,
};
}
public async Task<Build> RunSDKGenerationPipelineAsync(string apiSpecBranchRef, string typespecProjectRoot, string apiVersion, string sdkReleaseType, string language, int workItemId, string sdkRepoBranch = "")
{
int pipelineDefinitionId = GetPipelineDefinitionId(language);
if (pipelineDefinitionId == 0)
{
throw new Exception($"Failed to get SDK generation pipeline for {language}.");
}
var templateParams = new Dictionary<string, string>
{
{ "ConfigType", "TypeSpec"},
{ "ConfigPath", $"{typespecProjectRoot}/tspconfig.yaml" },
{ "ApiVersion", apiVersion },
{ "SdkReleaseType", sdkReleaseType },
{ "CreatePullRequest", "true" },
{ "ReleasePlanWorkItemId", $"{workItemId}"}
};
if (!string.IsNullOrEmpty(sdkRepoBranch))
{
templateParams["SdkRepoBranch"] = sdkRepoBranch;
}
var build = await RunPipelineAsync(pipelineDefinitionId, templateParams, apiSpecBranchRef);
var pipelineRunUrl = GetPipelineUrl(build.Id);
logger.LogInformation("Started pipeline run {pipelineRunUrl} to generate SDK.", pipelineRunUrl);
if (workItemId != 0)
{
logger.LogInformation("Adding SDK generation pipeline link to release plan");
await AddSdkInfoInReleasePlanAsync(workItemId, MapLanguageToId(language), pipelineRunUrl, "", "In progress");
}
return build;
}
public async Task<Build> RunPipelineAsync(int pipelineDefinitionId, Dictionary<string, string> templateParams, string apiSpecBranchRef = "main")
{
if (pipelineDefinitionId == 0)
{
throw new ArgumentException($"Invalid pipeline definition ID.");
}
var buildClient = connection.GetBuildClient();
var projectClient = connection.GetProjectClient();
var definition = await buildClient.GetDefinitionAsync(Constants.AZURE_SDK_DEVOPS_INTERNAL_PROJECT, pipelineDefinitionId);
var project = await projectClient.GetProject(Constants.AZURE_SDK_DEVOPS_INTERNAL_PROJECT);
// Queue SDK generation pipeline
logger.LogInformation("Queueing pipeline {pipelineName}.", definition.Name);
var build = await buildClient.QueueBuildAsync(new Build()
{
Definition = definition,
Project = project,
SourceBranch = apiSpecBranchRef,
TemplateParameters = templateParams
});
return build;
}
public async Task<Build> GetPipelineRunAsync(int buildId)
{
var buildClient = connection.GetBuildClient();
return await buildClient.GetBuildAsync(Constants.AZURE_SDK_DEVOPS_INTERNAL_PROJECT, buildId);
}
public async Task<string> GetSDKPullRequestFromPipelineRunAsync(int buildId, string language, int workItemId)
{
var buildClient = connection.GetBuildClient();
var timeLine = await buildClient.GetBuildTimelineAsync(Constants.AZURE_SDK_DEVOPS_INTERNAL_PROJECT, buildId);
var createPrJob = timeLine.Records.FirstOrDefault(r => r.Name == "Create pull request") ?? null;
if (createPrJob == null)
{
return $"Failed to generate SDK. SDK pull request link is not available for pipeline run, Pipeline link {timeLine.Url}";
}
// Get SDK pull request from create pull request job attachment
if (createPrJob.Result == TaskResult.Succeeded)
{
var contentStream = await buildClient.GetAttachmentAsync(Constants.AZURE_SDK_DEVOPS_INTERNAL_PROJECT, buildId, timeLine.Id, createPrJob.Id, "Distributedtask.Core.Summary", "Pull Request Created");
if (contentStream != null)
{
var content = new StreamReader(contentStream);
var pullRequestUrl = ParseSDKPullRequestUrl(content.ReadToEnd());
if (workItemId != 0)
{
logger.LogInformation("Adding SDK pull request to release plan");
await AddSdkInfoInReleasePlanAsync(workItemId, MapLanguageToId(language), GetPipelineUrl(buildId), pullRequestUrl.FullUrl, "Completed");
}
return pullRequestUrl.FullUrl;
}
}
// Check if there is any warning related to Generate SDK or create pr jobs. Ignore all 1ES jobs to avoid showing irrelevant warning.
StringBuilder sb = new($"Failed to generate SDK pull request for {language}.");
foreach (var job in timeLine.Records.Where(t => !t.Name.Contains("1ES")))
{
if (job.Issues.Count > 0)
{
job.Issues.ForEach(issue => sb.AppendLine(issue.Message));
}
}
return sb.ToString();
}
public static string GetPipelineUrl(int buildId)
{
return $"{Constants.AZURE_SDK_DEVOPS_BASE_URL}/internal/_build/results?buildId={buildId}";
}
public static ParsedSdkPullRequest ParseSDKPullRequestUrl(string sdkGenerationSummary)
{
Regex regex = new Regex("https:\\/\\/github.com\\/([Aa]zure)\\/(azure-sdk-for-[a-z]+)\\/pull\\/([0-9]+)");
var match = regex.Match(sdkGenerationSummary);
if (match.Success)
{
return new ParsedSdkPullRequest
{
RepoOwner = match.Groups[1].Value,
RepoName = match.Groups[2].Value,
PrNumber = int.Parse(match.Groups[3].Value),
FullUrl = match.Value
};
}
return new ParsedSdkPullRequest();
}
/// <summary>
/// Add the list of SDK languages to release plan. Release plan uses this language list to track SDK release.
/// </summary>
/// <param name="workItemId"></param>
/// <param name="sdkLanguages"></param>
/// <returns>bool</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
public async Task<bool> UpdateReleasePlanSDKDetailsAsync(int workItemId, List<SDKInfo> sdkLanguages)
{
// Adds SDK languages in release plan work item.
try
{
if (workItemId == 0 || sdkLanguages == null || sdkLanguages.Count == 0)
{
throw new ArgumentException("Please provide the work item ID and a list of languages to add SDK info to a work item.");
}
HashSet<string> languageNames = [.. sdkLanguages.Select(s => s.Language)];
var releasePlan = await GetReleasePlanForWorkItemAsync(workItemId);
if (releasePlan?.SDKInfo != null)
{
languageNames.UnionWith(releasePlan.SDKInfo.Select(s => s.Language));
}
var languages = string.Join(",", languageNames);
logger.LogInformation("Selected languages to generate SDK: {Languages}", languages);
var jsonLinkDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument
{
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/fields/Custom.SDKLanguages",
Value = languages
}
};
foreach (var sdk in sdkLanguages)
{
// Add package name in release plan for each language
if (!string.IsNullOrEmpty(sdk.Language) && !string.IsNullOrEmpty(sdk.PackageName))
{
jsonLinkDocument.Add(
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = $"/fields/Custom.{MapLanguageToId(sdk.Language)}PackageName",
Value = sdk.PackageName
}
);
}
}
await connection.GetWorkItemClient().UpdateWorkItemAsync(jsonLinkDocument, workItemId);
logger.LogInformation("Updated SDK languages to work item {WorkItemId}.", workItemId);
return true;
}
catch (Exception ex)
{
throw new Exception($"Failed to update SDK languages to work item [{workItemId}]. Error: {ex.Message}");
}
}
/// <summary>
/// Update API specification pull request status in the release plan. Release planner uses this status to mark
/// API readiness as completed when API specification pull request is approved.
/// </summary>
/// <param name="workItemId"></param>
/// <param name="status"></param>
/// <returns>bool</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
public async Task<bool> UpdateApiSpecStatusAsync(int workItemId, string status)
{
// Update API spec work item status in release plan
try
{
var workItem = await connection.GetWorkItemClient().GetWorkItemAsync(workItemId);
if (workItem == null)
{
throw new ArgumentException($"release plan work item with id {workItemId} not found.");
}
if (string.IsNullOrEmpty(status))
{
throw new ArgumentException("Please provide a status to update the work item.");
}
var jsonLinkDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument()
{
new JsonPatchOperation
{
Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
Path = "/fields/Custom.APISpecApprovalStatus",
Value = status
}
};
await connection.GetWorkItemClient().UpdateWorkItemAsync(jsonLinkDocument, workItemId);
return true;
}
catch (Exception ex)
{
throw new Exception($"Failed to update API spec status to work item [{workItemId}]. Error: {ex.Message}");
}
}
/// <summary>
/// Get API spec work item for a given release plan work item.
/// </summary>
private async Task<WorkItem> GetApiSpecWorkItemAsync(int releasePlanWorkItemId)
{
var releasePlanWorkItem = await connection.GetWorkItemClient().GetWorkItemAsync(releasePlanWorkItemId, expand: WorkItemExpand.All);
if (releasePlanWorkItem?.Id == null)
{
throw new InvalidOperationException($"Work item {releasePlanWorkItemId} not found.");
}
if (releasePlanWorkItem.Relations == null || !releasePlanWorkItem.Relations.Any(r => r.Rel.Equals("System.LinkTypes.Hierarchy-Forward")))
{
throw new InvalidOperationException("Release plan work item does not have any child work item");
}
//Find API spec work item
foreach (var relation in releasePlanWorkItem.Relations.Where(r => r.Rel.Equals("System.LinkTypes.Hierarchy-Forward")))
{
// Get parent work item and make sure it is release plan work item
var childWorkItemId = int.Parse(relation.Url.Split('/').Last());
var childWorkItem = await connection.GetWorkItemClient().GetWorkItemAsync(childWorkItemId);
if (childWorkItem == null || !childWorkItem.Fields.TryGetValue("System.WorkItemType", out Object? workItemType))
{
continue;
}
if (workItemType.Equals("API Spec"))
{
return childWorkItem;
}
}
throw new InvalidOperationException($"API spec work item not found for release plan work item {releasePlanWorkItemId}.");
}
/// <summary>
/// Update the active spec pull request link in API spec work item.
/// </summary>
/// <param name="releasePlanWorkItemId"></param>
/// <param name="specPullRequest"></param>
/// <returns>bool</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
public async Task<bool> UpdateSpecPullRequestAsync(int releasePlanWorkItemId, string specPullRequest)
{
// Update Active spec PR and add link to spec pr list
try
{
if (releasePlanWorkItemId == 0 || string.IsNullOrEmpty(specPullRequest))
{
throw new ArgumentException("Please provide the work item ID and a spec pull request URL to update the work item.");
}
// Find API spec work item
var apiSpecWorkItem = await GetApiSpecWorkItemAsync(releasePlanWorkItemId);
int apiSpecWorkItemId = apiSpecWorkItem.Id ?? 0;
if (apiSpecWorkItemId == 0)
{
throw new Exception($"API spec work item not found for release plan work item {releasePlanWorkItemId}.");
}
// Get current REST API review links and append new spec pull request link
var currentLinks = apiSpecWorkItem.Fields.TryGetValue("Custom.RESTAPIReviews", out Object? value) ? value?.ToString() ?? string.Empty : string.Empty;
StringBuilder sb = new StringBuilder(currentLinks);
if (sb.Length > 0)
{