Skip to content

Commit

Permalink
Merge pull request #64 from quantori/feature/add-ability-to-use-more-…
Browse files Browse the repository at this point in the history
…than-20000-work-items

Azure DevOps: add ability to use more than 20000 work items
  • Loading branch information
LukaRukhadze authored Feb 28, 2023
2 parents 5d85968 + cff238c commit 0e6db1a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

[3.20.2] - 2022-12-08
[3.20.3] - 2023-02-28

### Added
- TestRail: Ability to work with multiple test suite projects.
- Azure DevOps: Ability to work with more, than 20000 work items
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,30 @@ public WitBatchRequest BuildUpdateTestCaseBatchRequest(int id, JsonPatchDocument

public IEnumerable<int> GetAllTestCasesIds()
{
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();

// wiql - Work Item Query Language
var wiql = new Wiql
{
Query = $@"Select [{WorkItemFields.Id}]
var query = $@"Select [{WorkItemFields.Id}]
From WorkItems
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'"
};
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'
AND [System.TeamProject] = '{_azureDevopsSettings.Project}'";

var workItemIds = workItemTrackingHttpClient.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project).Result;
var workItems = GetListOfWorkItemsWithQuery(query);

return workItemIds.WorkItems.Select(reference => reference.Id);
return workItems.Select(reference => reference.Id);
}

public IEnumerable<int> GetSyncedTestCasesIds()
{
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();

// wiql - Work Item Query Language
var wiql = new Wiql
{
Query = $@"Select [{WorkItemFields.Id}]
var query = $@"Select [{WorkItemFields.Id}]
From WorkItems
Where [System.WorkItemType] = '{WorkItemTypes.TestCase}'
AND [{WorkItemFields.State}] <> '{TestCaseState.Closed}'
AND [{WorkItemFields.Tags}] Contains '{Tags.GherkinSyncToolIdTagPrefix + _azureDevopsSettings.GherkinSyncToolId}'"
};
AND [System.TeamProject] = '{_azureDevopsSettings.Project}'
AND [{WorkItemFields.Tags}]
Contains '{Tags.GherkinSyncToolIdTagPrefix + _azureDevopsSettings.GherkinSyncToolId}'";

var workItemIds = workItemTrackingHttpClient.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project).Result;
var workItems = GetListOfWorkItemsWithQuery(query);

return workItemIds.WorkItems.Select(reference => reference.Id);
return workItems.Select(reference => reference.Id);
}

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

if (witBatchResponse.Code != 200)
{
Log.Error($"Something went wrong with the test case synchronization. Title: {request[i].GetFields()[WorkItemFields.Title]}");
Log.Error($"Status code: {witBatchResponse.Code}{Environment.NewLine}Body: {witBatchResponse.Body}");
Log.Error(
$"Something went wrong with the test case synchronization. Title: {request[i].GetFields()[WorkItemFields.Title]}");
Log.Error(
$"Status code: {witBatchResponse.Code}{Environment.NewLine}Body: {witBatchResponse.Body}");

_context.IsRunSuccessful = false;
continue;
Expand Down Expand Up @@ -205,5 +199,47 @@ private WorkItemTrackingHttpClient GetWorkItemTrackingHttpClient()

return workItemTrackingHttpClient;
}

private IList<WorkItemReference> GetListOfWorkItemsWithQuery(string query)
{
var results = new List<WorkItemReference>();
var workItemTrackingHttpClient = GetWorkItemTrackingHttpClient();
var moreResults = true;
var lastIdInList = 0;

while (moreResults)
{
var wiql = new Wiql()
{
Query = $@"{query}
And [{WorkItemFields.Id}] > {lastIdInList}
ORDER BY [{WorkItemFields.Id}] ASC"
};

//Max number of workItems is 19999, until it gets fixed from the microsoft side
var currentResults = workItemTrackingHttpClient
.QueryByWiqlAsync(wiql, _azureDevopsSettings.Project, top: 19999)
.Result.WorkItems.ToList();

var currentResultsLength = currentResults.Count;

if (currentResultsLength < 19999)
{
moreResults = false;
}

var lastItemInList = currentResults.LastOrDefault();

if (lastItemInList == null)
{
break;
}

lastIdInList = lastItemInList.Id;
results.AddRange(currentResults);
}

return results;
}
}
}
}
2 changes: 1 addition & 1 deletion GherkinSyncTool/GherkinSyncTool.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyVersion>3.20.2</AssemblyVersion>
<AssemblyVersion>3.20.3</AssemblyVersion>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<PackAsTool>true</PackAsTool>
Expand Down

0 comments on commit 0e6db1a

Please sign in to comment.