1212using NLog ;
1313using Quantori . AllureTestOpsClient . Model ;
1414using Refit ;
15- using Scenario = Gherkin . Ast . Scenario ;
16- using Step = Gherkin . Ast . Step ;
1715using AllureTestCaseStep = Quantori . AllureTestOpsClient . Model . Step ;
1816using AllureScenario = Quantori . AllureTestOpsClient . Model . Scenario ;
17+ using Scenario = Gherkin . Ast . Scenario ;
18+ using Step = Gherkin . Ast . Step ;
19+ using Tag = Quantori . AllureTestOpsClient . Model . Tag ;
1920
2021namespace GherkinSyncTool . Synchronizers . AllureTestOps . Content ;
2122
@@ -31,6 +32,7 @@ public class CaseContentBuilder
3132 private List < WorkflowSchema > _workflowSchemas ;
3233 private Item _automatedWorkflowId ;
3334 private Item _manualWorkflowId ;
35+ private List < Tag > _testTags ;
3436
3537 public List < WorkflowSchema > WorkflowSchemas =>
3638 _workflowSchemas ??= _allureClientWrapper . GetAllWorkflowSchemas ( _allureTestOpsSettings . ProjectId ) . ToList ( ) ;
@@ -42,6 +44,7 @@ public class CaseContentBuilder
4244 _automatedWorkflowId ??= WorkflowSchemas . FirstOrDefault ( schema => schema . Type . Equals ( TestType . Automated ) ) ! . Workflow ;
4345
4446 public Item ManualWorkflow => _manualWorkflowId ??= WorkflowSchemas . FirstOrDefault ( schema => schema . Type . Equals ( TestType . Manual ) ) ! . Workflow ;
47+ public List < Tag > AllureTestTags => _testTags ??= _allureClientWrapper . GetAllTestTags ( ) ;
4548
4649 public CaseContentBuilder ( AllureClientWrapper allureClientWrapper , Context context )
4750 {
@@ -61,14 +64,50 @@ public CreateTestCaseRequestExtended BuildCaseRequest(Scenario scenario, IFeatur
6164 StatusId = AddStatus ( scenario , featureFile ) ,
6265 WorkflowId = AddWorkflow ( scenario , featureFile ) ,
6366 Description = AddDescription ( scenario , featureFile ) ,
64- Scenario = AddScenario ( scenario , featureFile )
67+ Scenario = AddScenario ( scenario , featureFile ) ,
68+ Tags = AddTags ( scenario , featureFile )
6569 } ,
6670 StepsAttachments = AddStepAttachments ( scenario , featureFile )
6771 } ;
6872
6973 return createTestCaseRequestExtended ;
7074 }
7175
76+ private List < Tag > AddTags ( Scenario scenario , IFeatureFile featureFile )
77+ {
78+ var allTags = GherkinHelper . GetAllTags ( scenario , featureFile ) ;
79+ //Remove tags that will duplicate existing fields
80+ RemoveTags ( allTags , TagsConstants . Reference , TagsConstants . Automated , TagsConstants . Status ) ;
81+
82+ var result = new List < Tag > ( ) ;
83+ if ( allTags . Any ( ) )
84+ {
85+ foreach ( var tag in allTags )
86+ {
87+ var tagName = tag . Name . Replace ( "@" , "" ) ;
88+ var allureTag = AllureTestTags . FirstOrDefault ( t => t . Name . Equals ( tagName , StringComparison . InvariantCultureIgnoreCase ) ) ;
89+ if ( allureTag is null )
90+ {
91+ var newTag = _allureClientWrapper . AddTestTags ( tagName ) ;
92+ AllureTestTags . Add ( newTag ) ;
93+ result . Add ( newTag ) ;
94+ continue ;
95+ }
96+ result . Add ( new Tag { Id = allureTag . Id , Name = tagName } ) ;
97+ }
98+ }
99+
100+ return result ;
101+ }
102+
103+ private void RemoveTags ( List < Gherkin . Ast . Tag > allTags , params string [ ] tagsToRemove )
104+ {
105+ foreach ( var tagToRemove in tagsToRemove )
106+ {
107+ allTags . RemoveAll ( tag => tag . Name . Contains ( tagToRemove , StringComparison . InvariantCultureIgnoreCase ) ) ;
108+ }
109+ }
110+
72111 private Dictionary < int , ByteArrayPart > AddStepAttachments ( Scenario scenario , IFeatureFile featureFile )
73112 {
74113 var attachments = ExtractAttachments ( scenario . Steps . ToList ( ) ) ;
@@ -78,7 +117,7 @@ private Dictionary<int, ByteArrayPart> AddStepAttachments(Scenario scenario, IFe
78117 {
79118 var backgroundStepCount = background . Steps . Count ( ) ;
80119 var attachmentsShifted = attachments . ToDictionary ( attachment => attachment . Key + backgroundStepCount , attachment => attachment . Value ) ;
81-
120+
82121 var backgroundAttachments = ExtractAttachments ( background . Steps . ToList ( ) ) ;
83122 return backgroundAttachments . Concat ( attachmentsShifted ) . ToDictionary ( pair => pair . Key , pair => pair . Value ) ;
84123 }
@@ -97,13 +136,13 @@ private Dictionary<int, ByteArrayPart> ExtractAttachments(List<Step> steps)
97136 switch ( step . Argument )
98137 {
99138 case DocString docString :
100-
139+
101140 var textBytes = Encoding . ASCII . GetBytes ( docString . Content ) ;
102141 result . Add ( index , new ByteArrayPart ( textBytes , "Text" , "text/plain" ) ) ;
103142 break ;
104-
143+
105144 case DataTable table :
106-
145+
107146 var csvBytes = Encoding . ASCII . GetBytes ( ConvertToCsvTable ( table . Rows . ToList ( ) ) ) ;
108147 result . Add ( index , new ByteArrayPart ( csvBytes , "Table" , "text/csv" ) ) ;
109148 break ;
@@ -171,11 +210,11 @@ private string AddDescription(Scenario scenario, IFeatureFile featureFile)
171210 if ( background is not null && ( ! string . IsNullOrWhiteSpace ( background . Name ) || ! string . IsNullOrWhiteSpace ( background . Description ) ) )
172211 {
173212 description . AppendLine ( $ "**{ background . Keyword } :** { background . Name } ") ;
174- if ( ! string . IsNullOrWhiteSpace ( background . Description ) ) description . AppendLine ( background . Description ) ;
213+ if ( ! string . IsNullOrWhiteSpace ( background . Description ) ) description . AppendLine ( background . Description ) ;
175214 }
176215
177216 description . AppendLine ( $ "**Feature file:** { featureFile . RelativePath } ") ;
178-
217+
179218 var examples = scenario . Examples . ToList ( ) ;
180219
181220 if ( examples . Any ( ) )
@@ -210,6 +249,7 @@ private string ConvertToMarkdownTable(List<TableRow> tableRows)
210249 {
211250 table . Append ( $ "{ cell . Value } |") ;
212251 }
252+
213253 table . AppendLine ( ) ;
214254 table . Append ( "|" ) ;
215255 //Header delimiter
0 commit comments