Skip to content

Commit da5a17f

Browse files
Allure TesOps: remove deleted scenarios. (#58)
* Allure TesOps: remove deleted scenarios. * Fixing NullRef error
1 parent ea98c61 commit da5a17f

File tree

14 files changed

+187
-75
lines changed

14 files changed

+187
-75
lines changed

CHANGELOG.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +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.19.0] - 2022-09-29
8-
9-
### Chaged
10-
- Allure TestOps: custom fields logic. Introduced the `CustomFields` settings parameter.
7+
[3.20.0] - 2022-10-03
118

129
### Added
13-
- Allure TestOps: The scenario test layer can be configured with settings or tags.
14-
- Allure TestOps: Ability to put background steps to precondition.
10+
- Allure TestOps: The tool will mark removed scenarios as deleted.

GherkinSyncTool.Models/Configuration/GherkinSyncToolConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class GherkinSyncToolConfig : IConfigs
77
{
88
public string BaseDirectory { get; set; }
99
public string TagIdPrefix { get; set; } = "@tc:";
10-
public FormattingSettings FormattingSettings { get; set; }
10+
public FormattingSettings FormattingSettings { get; set; } = new();
1111
public void ValidateConfigs()
1212
{
1313
if (string.IsNullOrEmpty(BaseDirectory))

GherkinSyncTool.Models/Utils/GherkinHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static List<Tag> GetAllTags(Scenario scenario, IFeatureFile featureFile)
3838
return allTags;
3939
}
4040

41-
public static ulong GetTagId(Tag tagId)
41+
public static long GetTagId(Tag tagId)
4242
{
4343
if (tagId is null) throw new ArgumentNullException(nameof(tagId));
44-
return ulong.Parse(Regex.Match(tagId.Name, @"\d+").Value);
44+
return long.Parse(Regex.Match(tagId.Name, @"\d+").Value);
4545
}
4646

4747
public static string FormatTagId(string id)

GherkinSyncTool.Synchronizers.AllureTestOps/AllureTestOpsSynchronizer.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class AllureTestOpsSynchronizer : ISynchronizer
2323
private readonly AllureClientWrapper _allureClientWrapper;
2424
private readonly Context _context;
2525
private readonly CaseContentBuilder _caseContentBuilder;
26+
private readonly AllureTestOpsSettings _allureTestOpsSettings =
27+
ConfigurationManager.GetConfiguration<AllureTestOpsConfigs>().AllureTestOpsSettings;
2628

2729
public AllureTestOpsSynchronizer(AllureClientWrapper allureClientWrapper, Context context, CaseContentBuilder caseContentBuilder)
2830
{
@@ -37,7 +39,7 @@ public void Sync(List<IFeatureFile> featureFiles)
3739
Log.Info("# Start synchronization with Allure TestOps");
3840

3941
var allureTestCases = _allureClientWrapper.GetAllTestCases().ToList();
40-
var featureFilesTagIds = new List<ulong>();
42+
var featureFilesTagIds = new List<long>();
4143

4244
foreach (var featureFile in featureFiles)
4345
{
@@ -65,19 +67,21 @@ public void Sync(List<IFeatureFile> featureFiles)
6567
// Update scenarios that have tag id
6668
if (tagId is not null)
6769
{
68-
var caseIdFromFile = GherkinHelper.GetTagId(tagId);
69-
featureFilesTagIds.Add(caseIdFromFile);
70+
var id = GherkinHelper.GetTagId(tagId);
71+
featureFilesTagIds.Add(id);
7072

71-
var allureTestCase = allureTestCases.FirstOrDefault(c => c.Id == caseIdFromFile);
73+
var allureTestCase = allureTestCases.FirstOrDefault(c => c.Id == id);
7274
if (allureTestCase is null)
7375
{
74-
Log.Warn($"Case with id {caseIdFromFile} not found. Recreating missing case");
76+
Log.Warn($"Case with id {id} not found. Recreating missing case");
7577
var newTestCase = CreateNewTestCase(caseRequestExtended);
7678
if (newTestCase is null) continue;
77-
79+
7880
var formattedTagId = GherkinHelper.FormatTagId(newTestCase.Id.ToString());
7981
TextFilesEditMethods.ReplaceLineInTheFile(featureFile.AbsolutePath,
8082
tagId.Location.Line - 1 + insertedTagIdsCount, formattedTagId);
83+
84+
featureFilesTagIds.Add(newTestCase.Id);
8185
}
8286
else
8387
{
@@ -94,14 +98,32 @@ public void Sync(List<IFeatureFile> featureFiles)
9498
}
9599
}
96100
}
97-
//TODO:
98-
//DeleteNotExistingScenarios(allureTestCases, featureFilesTagIds);
101+
102+
DeleteNotExistingScenarios(featureFilesTagIds);
99103
Log.Debug(@$"Synchronization with Allure TestOps finished in: {stopwatch.Elapsed:mm\:ss\.fff}");
100104
}
101105

102-
private void DeleteNotExistingScenarios(List<TestCaseContent> allureTestCases, List<ulong> featureFilesTagIds)
106+
private void DeleteNotExistingScenarios(List<long> featureFilesIDs)
103107
{
104-
throw new NotImplementedException("The method should be implemented in future versions");
108+
var query = $"tag is \"{TagsConstants.ToolId + _allureTestOpsSettings.GherkinSyncToolId}\"";
109+
var allureTestCases = _allureClientWrapper.SearchAllTestCasesWithQuery(query).ToList();
110+
111+
var allureIDs = allureTestCases.Select(c => c.Id);
112+
var idsToDelete = allureIDs.Except(featureFilesIDs).ToList();
113+
if (!idsToDelete.Any())
114+
{
115+
return;
116+
}
117+
118+
try
119+
{
120+
_allureClientWrapper.RemoveTestCases(idsToDelete);
121+
}
122+
catch (AllureException e)
123+
{
124+
Log.Error(e, $"The cases has not been deleted: {string.Join(", ", idsToDelete)}");
125+
_context.IsRunSuccessful = false;
126+
}
105127
}
106128

107129
private TestCase CreateNewTestCase(CreateTestCaseRequestExtended caseRequestExtended)

GherkinSyncTool.Synchronizers.AllureTestOps/Client/AllureClientWrapper.cs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ public IEnumerable<TestCaseContent> GetAllTestCases()
3131
{
3232
return GetAllContent(i => _allureClient.GetTestCasesAsync(_allureTestOpsSettings.ProjectId, i).Result);
3333
}
34+
3435
public IEnumerable<CustomFieldSchemaContent> GetCustomFieldSchema()
3536
{
3637
return GetAllContent(i => _allureClient.GetCustomFieldSchemaAsync(_allureTestOpsSettings.ProjectId, i).Result);
3738
}
38-
39+
3940
public IEnumerable<TestLayerSchemaContent> GetTestLayerSchema()
4041
{
4142
return GetAllContent(i => _allureClient.GetTestLayerSchemaAsync(_allureTestOpsSettings.ProjectId, i).Result);
4243
}
43-
44+
4445
public IEnumerable<CustomFieldItem> GetCustomFieldValues(long customFieldId)
4546
{
4647
return GetAllContent(i => _allureClient.GetCustomFieldValuesAsync(customFieldId, i).Result);
4748
}
48-
49+
4950
public CustomFieldItem CreateNewCustomFieldValue(CustomFieldItem customFieldItem)
5051
{
5152
var response = _allureClient.CreateCustomFieldValueAsync(customFieldItem).Result;
@@ -76,7 +77,7 @@ public TestCase CreateTestCase(CreateTestCaseRequest caseRequest)
7677
return response.Content;
7778
}
7879

79-
public TestCaseOverview GetTestCaseOverview(ulong id)
80+
public TestCaseOverview GetTestCaseOverview(long id)
8081
{
8182
var response = _allureClient.GetTestCaseOverviewAsync(id).Result;
8283
ValidateResponse(response);
@@ -107,7 +108,7 @@ public void UpdateTestCase(TestCaseContent currentCase, CreateTestCaseRequestExt
107108
if (caseToUpdate.StepsAttachments.Any())
108109
{
109110
UpdateTestCaseStepAttachments(caseToUpdate, testCaseOverview);
110-
}
111+
}
111112
}
112113
}
113114

@@ -122,14 +123,14 @@ public void UpdateTestCase(TestCaseContent currentCase, CreateTestCaseRequestExt
122123

123124
if (!contentEqual || !scenarioIsEqual)
124125
{
125-
var response = _allureClient.UpdateTestCaseAsync(currentCase.Id, caseToUpdate.CreateTestCaseRequest).Result;
126+
var response = _allureClient.UpdateTestCaseAsync(testCaseOverview.Id, caseToUpdate.CreateTestCaseRequest).Result;
126127
ValidateResponse(response);
127128
updated = true;
128129
}
129130

130131
Log.Info(updated
131-
? $"Updated: [{currentCase.Id}] {caseToUpdate.CreateTestCaseRequest.Name}"
132-
: $"Up-to-date: [{currentCase.Id}] {currentCase.Name}");
132+
? $"Updated: [{testCaseOverview.Id}] {caseToUpdate.CreateTestCaseRequest.Name}"
133+
: $"Up-to-date: [{testCaseOverview.Id}] {testCaseOverview.Name}");
133134
}
134135

135136
private bool AreTestCaseScenariosEqual(TestCaseOverview testCaseOverview, CreateTestCaseRequestExtended caseToUpdate)
@@ -141,7 +142,7 @@ private bool AreTestCaseScenariosEqual(TestCaseOverview testCaseOverview, Create
141142

142143
if (testCaseOverview.Scenario is null && caseToUpdate.CreateTestCaseRequest.Scenario.Steps is not null)
143144
{
144-
return false;
145+
return false;
145146
}
146147

147148
if (testCaseOverview.Scenario!.Steps.Count != caseToUpdate.CreateTestCaseRequest.Scenario.Steps!.Count)
@@ -177,7 +178,8 @@ private bool AreTestCaseScenariosEqual(TestCaseOverview testCaseOverview, Create
177178
return false;
178179
}
179180

180-
var allureAttachment = _allureClient.GetTestCaseAttachmentContentAsync(stepsFromAllure[i].Attachments.FirstOrDefault()!.Id).Result;
181+
var allureAttachment = _allureClient.GetTestCaseAttachmentContentAsync(stepsFromAllure[i].Attachments.FirstOrDefault()!.Id)
182+
.Result;
181183

182184
if (!allureAttachment.Content!.Equals(attachment))
183185
{
@@ -208,6 +210,11 @@ public IEnumerable<WorkflowContent> GetAllWorkflows()
208210
{
209211
return GetAllContent(i => _allureClient.GetWorkflowAsync(i).Result);
210212
}
213+
214+
public IEnumerable<TestCase> SearchAllTestCasesWithQuery(string rql, bool deleted = false)
215+
{
216+
return GetAllContent(i => _allureClient.SearchTestCasesAsync(_allureTestOpsSettings.ProjectId, rql, deleted, i).Result);
217+
}
211218

212219
public List<Attachment> UploadTestCaseAttachments(long testCaseId, IEnumerable<ByteArrayPart> content)
213220
{
@@ -236,17 +243,17 @@ public void UploadStepAttachments(CreateTestCaseRequestExtended caseRequestExten
236243

237244
ValidateResponse(response);
238245
}
239-
246+
240247
public List<Tag> GetAllTestTags()
241248
{
242249
var response = _allureClient.GetTagsAsync().Result;
243250
ValidateResponse(response);
244251
return response.Content;
245252
}
246-
253+
247254
public Tag AddTestTags(string name)
248255
{
249-
var response = _allureClient.CreateTagAsync(new Tag {Name = name}).Result;
256+
var response = _allureClient.CreateTagAsync(new Tag { Name = name }).Result;
250257
ValidateResponse(response);
251258
return response.Content;
252259
}
@@ -259,9 +266,9 @@ private void UpdateTestCaseStepAttachments(CreateTestCaseRequestExtended caseToU
259266
foreach (var id in attachment.Select(step => step.Attachments.FirstOrDefault()!.Id))
260267
{
261268
RemoveTestCaseAttachment(id);
262-
}
269+
}
263270
}
264-
271+
265272
AddTestCaseStepAttachments(caseToUpdate, testCaseOverview.Id);
266273
}
267274

@@ -270,37 +277,40 @@ private bool AreTestCasesContentsEqual(TestCaseOverview currentCase, CreateTestC
270277
if (!currentCase.Name.Equals(caseToUpdate.CreateTestCaseRequest.Name)) return false;
271278
if (!currentCase.Automated.Equals(caseToUpdate.CreateTestCaseRequest.Automated)) return false;
272279
if (!currentCase.Status.Id.Equals(caseToUpdate.CreateTestCaseRequest.StatusId)) return false;
273-
if(currentCase.Description is null && caseToUpdate.CreateTestCaseRequest.Description is not null) return false;
274-
if(currentCase.Description is not null && caseToUpdate.CreateTestCaseRequest.Description is null) return false;
280+
if (currentCase.Description is null && caseToUpdate.CreateTestCaseRequest.Description is not null) return false;
281+
if (currentCase.Description is not null && caseToUpdate.CreateTestCaseRequest.Description is null) return false;
275282
if (currentCase.Description is not null && caseToUpdate.CreateTestCaseRequest.Description is not null)
276283
{
277-
if (!currentCase.Description.Equals(caseToUpdate.CreateTestCaseRequest.Description)) return false;
284+
if (!currentCase.Description.Equals(caseToUpdate.CreateTestCaseRequest.Description)) return false;
278285
}
286+
279287
if (currentCase.Layer is null && caseToUpdate.CreateTestCaseRequest.TestLayerId is not null) return false;
280288
if (currentCase.Layer is not null && caseToUpdate.CreateTestCaseRequest.TestLayerId is null) return false;
281289
if (currentCase.Layer is not null && caseToUpdate.CreateTestCaseRequest.TestLayerId is not null)
282290
{
283-
if (!currentCase.Layer.Id.Equals(caseToUpdate.CreateTestCaseRequest.TestLayerId)) return false;
291+
if (!currentCase.Layer.Id.Equals(caseToUpdate.CreateTestCaseRequest.TestLayerId)) return false;
284292
}
293+
285294
var caseToUpdateTagIds = caseToUpdate.CreateTestCaseRequest.Tags.Select(tag => tag.Id).ToList();
286295
var currentCaseTagIds = currentCase.Tags.Select(tag => tag.Id).ToList();
287296

288297
if (currentCaseTagIds.Count != caseToUpdateTagIds.Count) return false;
289298
if (currentCaseTagIds.Except(caseToUpdateTagIds).Any()) return false;
290-
299+
291300
if (currentCase.CustomFields.Count != caseToUpdate.CreateTestCaseRequest.CustomFields.Count) return false;
292-
if (currentCase.CustomFields.Select(item => item.Name).Except(caseToUpdate.CreateTestCaseRequest.CustomFields.Select(item => item.Name)).Any()) return false;
301+
if (currentCase.CustomFields.Select(item => item.Name).Except(caseToUpdate.CreateTestCaseRequest.CustomFields.Select(item => item.Name))
302+
.Any()) return false;
293303

294304
if (_allureTestOpsSettings.BackgroundToPrecondition)
295305
{
296-
if(currentCase.Precondition is null && caseToUpdate.CreateTestCaseRequest.Precondition is not null) return false;
297-
if(currentCase.Precondition is not null && caseToUpdate.CreateTestCaseRequest.Precondition is null) return false;
306+
if (currentCase.Precondition is null && caseToUpdate.CreateTestCaseRequest.Precondition is not null) return false;
307+
if (currentCase.Precondition is not null && caseToUpdate.CreateTestCaseRequest.Precondition is null) return false;
298308
if (currentCase.Precondition is not null && caseToUpdate.CreateTestCaseRequest.Precondition is not null)
299309
{
300-
if (!currentCase.Precondition.Equals(caseToUpdate.CreateTestCaseRequest.Precondition)) return false;
310+
if (!currentCase.Precondition.Equals(caseToUpdate.CreateTestCaseRequest.Precondition)) return false;
301311
}
302312
}
303-
313+
304314
return true;
305315
}
306316

@@ -332,5 +342,24 @@ private void AddTestCaseStepAttachments(CreateTestCaseRequestExtended caseReques
332342
caseRequestExtended.CreateTestCaseRequest.Scenario.Steps[stepNumber].Attachments = new List<Attachment> { attachments[i] };
333343
}
334344
}
345+
346+
public void RemoveTestCases(List<long> idsToDelete)
347+
{
348+
var request = new TestCaseBulk
349+
{
350+
Selection = new Selection
351+
{
352+
ProjectId = _allureTestOpsSettings.ProjectId,
353+
Inverted = false,
354+
LeafsInclude = idsToDelete
355+
}
356+
};
357+
var response = _allureClient.RemoveTestCasesAsync(request).Result;
358+
ValidateResponse(response);
359+
foreach (var id in idsToDelete)
360+
{
361+
Log.Info($"Deleted: [{id}]");
362+
}
363+
}
335364
}
336365
}

GherkinSyncTool.Synchronizers.AllureTestOps/Content/CaseContentBuilder.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private string AddPreconditions(IFeatureFile featureFile)
125125
}
126126

127127
var customFieldsSettings = _allureTestOpsSettings.TestLayer;
128-
if (customFieldsSettings is not null)
128+
if (!string.IsNullOrWhiteSpace(customFieldsSettings))
129129
{
130130
var testLayer = GetTestLayer(scenario, customFieldsSettings);
131131
return testLayer?.TestLayer.Id;
@@ -283,8 +283,20 @@ private List<Tag> AddTags(Scenario scenario, IFeatureFile featureFile)
283283
};
284284
tagsToRemove.AddRange(_allureTestOpsSettings.CustomFields.Select(field => field.Name));
285285
RemoveTags(allTags, tagsToRemove);
286-
286+
287+
288+
var gstId = TagsConstants.ToolId + _allureTestOpsSettings.GherkinSyncToolId;
289+
287290
var result = new List<Tag>();
291+
292+
var gstTag = AllureTestTags.FirstOrDefault(t => t.Name.Equals(gstId, StringComparison.InvariantCultureIgnoreCase));
293+
if (gstTag is null)
294+
{
295+
gstTag = _allureClientWrapper.AddTestTags(gstId);
296+
AllureTestTags.Add(gstTag);
297+
}
298+
result.Add(gstTag);
299+
288300
if (allTags.Any())
289301
{
290302
foreach (var tag in allTags)
@@ -293,13 +305,12 @@ private List<Tag> AddTags(Scenario scenario, IFeatureFile featureFile)
293305
var allureTag = AllureTestTags.FirstOrDefault(t => t.Name.Equals(tagName, StringComparison.InvariantCultureIgnoreCase));
294306
if (allureTag is null)
295307
{
296-
var newTag = _allureClientWrapper.AddTestTags(tagName);
297-
AllureTestTags.Add(newTag);
298-
result.Add(newTag);
308+
allureTag = _allureClientWrapper.AddTestTags(tagName);
309+
AllureTestTags.Add(allureTag);
299310
continue;
300311
}
301312

302-
result.Add(new Tag { Id = allureTag.Id, Name = tagName });
313+
result.Add(allureTag);
303314
}
304315
}
305316

GherkinSyncTool.Synchronizers.AllureTestOps/Model/AllureTestOpsConfigs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class AllureTestOpsSettings
4444
public string GherkinSyncToolId { get; set; }
4545
public string TestLayer { get; set; }
4646
public List<CustomField> CustomFields { get; set; }
47-
public bool BackgroundToPrecondition { get; set; }
47+
public bool BackgroundToPrecondition { get; set; } = false;
4848
}
4949

5050
public class CustomField

GherkinSyncTool.Synchronizers.AllureTestOps/Model/TagsConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public static class TagsConstants
66
public const string Status = "@Status:";
77
public const string Reference = "@Reference:";
88
public const string Layer = "@Layer:";
9+
//@ is not needed
10+
public const string ToolId = "GST:";
911
}
1012
}

0 commit comments

Comments
 (0)