11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Reflection ;
56using System . Text ;
@@ -33,6 +34,8 @@ public class CaseContentBuilder
3334 private Item _automatedWorkflowId ;
3435 private Item _manualWorkflowId ;
3536 private List < Tag > _testTags ;
37+ private List < CustomFieldSchemaContent > _customFieldSchema ;
38+ private Dictionary < long , List < CustomFieldItem > > _customFieldsValues = new ( ) ;
3639
3740 public List < WorkflowSchema > WorkflowSchemas =>
3841 _workflowSchemas ??= _allureClientWrapper . GetAllWorkflowSchemas ( _allureTestOpsSettings . ProjectId ) . ToList ( ) ;
@@ -44,7 +47,8 @@ public class CaseContentBuilder
4447 _automatedWorkflowId ??= WorkflowSchemas . FirstOrDefault ( schema => schema . Type . Equals ( TestType . Automated ) ) ! . Workflow ;
4548
4649 public Item ManualWorkflow => _manualWorkflowId ??= WorkflowSchemas . FirstOrDefault ( schema => schema . Type . Equals ( TestType . Manual ) ) ! . Workflow ;
47- public List < Tag > AllureTestTags => _testTags ??= _allureClientWrapper . GetAllTestTags ( ) ;
50+ public List < Tag > AllureTestTags => _testTags ??= _allureClientWrapper . GetAllTestTags ( ) ;
51+ public List < CustomFieldSchemaContent > CustomFieldSchema => _customFieldSchema ??= _allureClientWrapper . GetCustomFieldSchema ( ) . ToList ( ) ;
4852
4953 public CaseContentBuilder ( AllureClientWrapper allureClientWrapper , Context context )
5054 {
@@ -65,20 +69,125 @@ public CreateTestCaseRequestExtended BuildCaseRequest(Scenario scenario, IFeatur
6569 WorkflowId = AddWorkflow ( scenario , featureFile ) ,
6670 Description = AddDescription ( scenario , featureFile ) ,
6771 Scenario = AddScenario ( scenario , featureFile ) ,
68- Tags = AddTags ( scenario , featureFile )
72+ Tags = AddTags ( scenario , featureFile ) ,
73+ CustomFields = AddCustomFields ( scenario , featureFile )
6974 } ,
7075 StepsAttachments = AddStepAttachments ( scenario , featureFile )
7176 } ;
7277
7378 return createTestCaseRequestExtended ;
7479 }
7580
81+ private List < CustomFieldItem > AddCustomFields ( Scenario scenario , IFeatureFile featureFile )
82+ {
83+ var result = new List < CustomFieldItem > ( ) ;
84+ AddFeatureCustomFiled ( featureFile , result ) ;
85+ AddEpicCustomFiled ( featureFile , result ) ;
86+ AddComponentCustomField ( featureFile , scenario , result ) ;
87+ return result ;
88+ }
89+
90+ private void AddComponentCustomField ( IFeatureFile featureFile , Scenario scenario , List < CustomFieldItem > result )
91+ {
92+ var allTags = GherkinHelper . GetAllTags ( scenario , featureFile ) ;
93+ var componentTag = allTags . LastOrDefault ( tag => tag . Name . Contains ( TagsConstants . Component , StringComparison . InvariantCultureIgnoreCase ) ) ;
94+
95+ if ( componentTag is not null )
96+ {
97+ var componentField = GetField ( "Component" ) ;
98+
99+ var componentValues = componentTag . Name . Replace ( TagsConstants . Component , "" ) . Split ( "," ) ;
100+ foreach ( var componentValue in componentValues )
101+ {
102+ AddCustomField ( result , componentField , componentValue ) ;
103+ }
104+
105+ return ;
106+ }
107+
108+ var settingsComponent = _allureTestOpsSettings . Component ;
109+ if ( settingsComponent is not null )
110+ {
111+ var componentField = GetField ( "Component" ) ;
112+ AddCustomField ( result , componentField , settingsComponent ) ;
113+ }
114+ }
115+
116+ private CustomFieldSchemaContent GetField ( string fieldName )
117+ {
118+ var componentField = CustomFieldSchema . FirstOrDefault ( content => content . CustomField . Name . Equals ( fieldName ) ) ;
119+
120+ if ( componentField is null )
121+ {
122+ Log . Error ( $ "{ fieldName } field is not found.") ;
123+ _context . IsRunSuccessful = false ;
124+ return componentField ;
125+ }
126+
127+ var values = _allureClientWrapper . GetCustomFieldValues ( componentField . CustomField . Id ) ;
128+
129+ if ( ! _customFieldsValues . ContainsKey ( componentField . CustomField . Id ) )
130+ {
131+ _customFieldsValues . Add ( componentField . CustomField . Id , values . ToList ( ) ) ;
132+ }
133+
134+ return componentField ;
135+ }
136+
137+ private void AddCustomField ( List < CustomFieldItem > result , CustomFieldSchemaContent customField , string cfValue )
138+ {
139+ var value = _customFieldsValues [ customField . CustomField . Id ] . FirstOrDefault ( item => item . Name . Equals ( cfValue ) ) ;
140+ if ( value is null )
141+ {
142+ var customFieldValue = _allureClientWrapper . CreateNewCustomFieldValue ( new CustomFieldItem ( )
143+ {
144+ Name = cfValue ,
145+ CustomField = new Item
146+ {
147+ Id = customField . CustomField . Id
148+ }
149+ } ) ;
150+ value = customFieldValue ;
151+ _customFieldsValues [ customField . CustomField . Id ] . Add ( value ) ;
152+ }
153+
154+ result . Add ( new CustomFieldItem
155+ {
156+ Id = value . Id ,
157+ Name = value . Name ,
158+ CustomField = new Item
159+ {
160+ Id = value . CustomField . Id
161+ }
162+ } ) ;
163+ }
164+
165+ private void AddEpicCustomFiled ( IFeatureFile featureFile , List < CustomFieldItem > result )
166+ {
167+ var epicField = GetField ( "Epic" ) ;
168+ var values = _allureClientWrapper . GetCustomFieldValues ( epicField . CustomField . Id ) ;
169+ if ( ! _customFieldsValues . ContainsKey ( epicField . CustomField . Id ) )
170+ {
171+ _customFieldsValues . Add ( epicField . CustomField . Id , values . ToList ( ) ) ;
172+ }
173+
174+ var epic = Path . GetDirectoryName ( featureFile . RelativePath ) ;
175+
176+ AddCustomField ( result , epicField , epic ) ;
177+ }
178+
179+ private void AddFeatureCustomFiled ( IFeatureFile featureFile , List < CustomFieldItem > result )
180+ {
181+ var featureField = GetField ( "Feature" ) ;
182+ AddCustomField ( result , featureField , featureFile . Document . Feature . Name ) ;
183+ }
184+
76185 private List < Tag > AddTags ( Scenario scenario , IFeatureFile featureFile )
77186 {
78187 var allTags = GherkinHelper . GetAllTags ( scenario , featureFile ) ;
79188 //Remove tags that will duplicate existing fields
80- RemoveTags ( allTags , TagsConstants . Reference , TagsConstants . Automated , TagsConstants . Status ) ;
81-
189+ RemoveTags ( allTags , TagsConstants . Reference , TagsConstants . Automated , TagsConstants . Status , TagsConstants . Component ) ;
190+
82191 var result = new List < Tag > ( ) ;
83192 if ( allTags . Any ( ) )
84193 {
@@ -93,7 +202,8 @@ private List<Tag> AddTags(Scenario scenario, IFeatureFile featureFile)
93202 result . Add ( newTag ) ;
94203 continue ;
95204 }
96- result . Add ( new Tag { Id = allureTag . Id , Name = tagName } ) ;
205+
206+ result . Add ( new Tag { Id = allureTag . Id , Name = tagName } ) ;
97207 }
98208 }
99209
@@ -104,7 +214,7 @@ private void RemoveTags(List<Gherkin.Ast.Tag> allTags, params string[] tagsToRem
104214 {
105215 foreach ( var tagToRemove in tagsToRemove )
106216 {
107- allTags . RemoveAll ( tag => tag . Name . Contains ( tagToRemove , StringComparison . InvariantCultureIgnoreCase ) ) ;
217+ allTags . RemoveAll ( tag => tag . Name . Contains ( tagToRemove , StringComparison . InvariantCultureIgnoreCase ) ) ;
108218 }
109219 }
110220
0 commit comments