Skip to content

Commit 0e6db1a

Browse files
authored
Merge pull request #64 from quantori/feature/add-ability-to-use-more-than-20000-work-items
Azure DevOps: add ability to use more than 20000 work items
2 parents 5d85968 + cff238c commit 0e6db1a

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
[3.20.2] - 2022-12-08
7+
[3.20.3] - 2023-02-28
88

99
### Added
10-
- TestRail: Ability to work with multiple test suite projects.
10+
- Azure DevOps: Ability to work with more, than 20000 work items

GherkinSyncTool.Synchronizers.AzureDevOps/Client/AzureDevopsClient.cs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,30 @@ public WitBatchRequest BuildUpdateTestCaseBatchRequest(int id, JsonPatchDocument
5151

5252
public IEnumerable<int> GetAllTestCasesIds()
5353
{
54-
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();
55-
56-
// wiql - Work Item Query Language
57-
var wiql = new Wiql
58-
{
59-
Query = $@"Select [{WorkItemFields.Id}]
54+
var query = $@"Select [{WorkItemFields.Id}]
6055
From WorkItems
61-
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'"
62-
};
56+
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'
57+
AND [System.TeamProject] = '{_azureDevopsSettings.Project}'";
6358

64-
var workItemIds = workItemTrackingHttpClient.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project).Result;
59+
var workItems = GetListOfWorkItemsWithQuery(query);
6560

66-
return workItemIds.WorkItems.Select(reference => reference.Id);
61+
return workItems.Select(reference => reference.Id);
6762
}
68-
63+
6964
public IEnumerable<int> GetSyncedTestCasesIds()
7065
{
71-
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();
7266

73-
// wiql - Work Item Query Language
74-
var wiql = new Wiql
75-
{
76-
Query = $@"Select [{WorkItemFields.Id}]
67+
var query = $@"Select [{WorkItemFields.Id}]
7768
From WorkItems
7869
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'
7970
AND [{WorkItemFields.State}] <> '{TestCaseState.Closed}'
80-
AND [{WorkItemFields.Tags}] Contains '{Tags.GherkinSyncToolIdTagPrefix + _azureDevopsSettings.GherkinSyncToolId}'"
81-
};
71+
AND [System.TeamProject] = '{_azureDevopsSettings.Project}'
72+
AND [{WorkItemFields.Tags}]
73+
Contains '{Tags.GherkinSyncToolIdTagPrefix + _azureDevopsSettings.GherkinSyncToolId}'";
8274

83-
var workItemIds = workItemTrackingHttpClient.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project).Result;
75+
var workItems = GetListOfWorkItemsWithQuery(query);
8476

85-
return workItemIds.WorkItems.Select(reference => reference.Id);
77+
return workItems.Select(reference => reference.Id);
8678
}
8779

8880
public List<WorkItem> ExecuteWorkItemBatch(List<WitBatchRequest> request)
@@ -156,8 +148,10 @@ private List<WorkItem> SendWorkItemBatch(List<WitBatchRequest> request)
156148

157149
if (witBatchResponse.Code != 200)
158150
{
159-
Log.Error($"Something went wrong with the test case synchronization. Title: {request[i].GetFields()[WorkItemFields.Title]}");
160-
Log.Error($"Status code: {witBatchResponse.Code}{Environment.NewLine}Body: {witBatchResponse.Body}");
151+
Log.Error(
152+
$"Something went wrong with the test case synchronization. Title: {request[i].GetFields()[WorkItemFields.Title]}");
153+
Log.Error(
154+
$"Status code: {witBatchResponse.Code}{Environment.NewLine}Body: {witBatchResponse.Body}");
161155

162156
_context.IsRunSuccessful = false;
163157
continue;
@@ -205,5 +199,47 @@ private WorkItemTrackingHttpClient GetWorkItemTrackingHttpClient()
205199

206200
return workItemTrackingHttpClient;
207201
}
202+
203+
private IList<WorkItemReference> GetListOfWorkItemsWithQuery(string query)
204+
{
205+
var results = new List<WorkItemReference>();
206+
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();
207+
var moreResults = true;
208+
var lastIdInList = 0;
209+
210+
while (moreResults)
211+
{
212+
var wiql = new Wiql()
213+
{
214+
Query = $@"{query}
215+
And [{WorkItemFields.Id}] > {lastIdInList}
216+
ORDER BY [{WorkItemFields.Id}] ASC"
217+
};
218+
219+
//Max number of workItems is 19999, until it gets fixed from the microsoft side
220+
var currentResults = workItemTrackingHttpClient
221+
.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project, top: 19999)
222+
.Result.WorkItems.ToList();
223+
224+
var currentResultsLength = currentResults.Count;
225+
226+
if (currentResultsLength < 19999)
227+
{
228+
moreResults = false;
229+
}
230+
231+
var lastItemInList = currentResults.LastOrDefault();
232+
233+
if (lastItemInList == null)
234+
{
235+
break;
236+
}
237+
238+
lastIdInList = lastItemInList.Id;
239+
results.AddRange(currentResults);
240+
}
241+
242+
return results;
243+
}
208244
}
209-
}
245+
}

GherkinSyncTool/GherkinSyncTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<AssemblyVersion>3.20.2</AssemblyVersion>
4+
<AssemblyVersion>3.20.3</AssemblyVersion>
55
<OutputType>Exe</OutputType>
66
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
77
<PackAsTool>true</PackAsTool>

0 commit comments

Comments
 (0)