Skip to content

Commit 7e59bf0

Browse files
Allure Testops: add fields (#56)
* Add feature field * Add epic field * Add component field
1 parent 6cf65e7 commit 7e59bf0

File tree

11 files changed

+233
-35
lines changed

11 files changed

+233
-35
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.17.0] - 2022-09-21
7+
[3.18.0] - 2022-09-23
88

99
### Added
10-
- Allure TestOps: Add tags to a test case
10+
- Allure TestOps: Add fields

GherkinSyncTool.Synchronizers.AllureTestOps/AllureTestOpsSynchronizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private TestCase CreateNewTestCase(CreateTestCaseRequestExtended caseRequestExte
109109
TestCase newTestCase = null;
110110
try
111111
{
112-
newTestCase = _allureClientWrapper.AddTestCase(caseRequestExtended.CreateTestCaseRequest);
112+
newTestCase = _allureClientWrapper.CreateTestCase(caseRequestExtended.CreateTestCaseRequest);
113113
}
114114
catch (AllureException e)
115115
{

GherkinSyncTool.Synchronizers.AllureTestOps/Client/AllureClientWrapper.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ public IEnumerable<TestCaseContent> GetAllTestCases()
3131
{
3232
return GetAllContent(i => _allureClient.GetTestCasesAsync(_allureTestOpsSettings.ProjectId, i).Result);
3333
}
34+
public IEnumerable<CustomFieldSchemaContent> GetCustomFieldSchema()
35+
{
36+
return GetAllContent(i => _allureClient.GetCustomFieldSchemaAsync(_allureTestOpsSettings.ProjectId, i).Result);
37+
}
38+
39+
public IEnumerable<CustomFieldItem> GetCustomFieldValues(long customFieldId)
40+
{
41+
return GetAllContent(i => _allureClient.GetCustomFieldValuesAsync(customFieldId, i).Result);
42+
}
43+
44+
public CustomFieldItem CreateNewCustomFieldValue(CustomFieldItem customFieldItem)
45+
{
46+
var response = _allureClient.CreateCustomFieldValueAsync(customFieldItem).Result;
47+
ValidateResponse(response);
48+
return response.Content;
49+
}
3450

3551
private void ValidateResponse(IApiResponse response)
3652
{
@@ -47,7 +63,7 @@ private void ValidateResponse(IApiResponse response)
4763
}
4864
}
4965

50-
public TestCase AddTestCase(CreateTestCaseRequest caseRequest)
66+
public TestCase CreateTestCase(CreateTestCaseRequest caseRequest)
5167
{
5268
var response = _allureClient.CreateTestCaseAsync(caseRequest).Result;
5369
ValidateResponse(response);
@@ -256,7 +272,10 @@ private static bool AreTestCasesContentsEqual(TestCaseOverview currentCase, Crea
256272

257273
if (currentCaseTagIds.Count != caseToUpdateTagIds.Count) return false;
258274
if (currentCaseTagIds.Except(caseToUpdateTagIds).Any()) return false;
259-
275+
276+
if (currentCase.CustomFields.Count != caseToUpdate.CreateTestCaseRequest.CustomFields.Count) return false;
277+
if (currentCase.CustomFields.Select(item => item.Name).Except(caseToUpdate.CreateTestCaseRequest.CustomFields.Select(item => item.Name)).Any()) return false;
278+
260279
return true;
261280
}
262281

GherkinSyncTool.Synchronizers.AllureTestOps/Content/CaseContentBuilder.cs

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using 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

GherkinSyncTool.Synchronizers.AllureTestOps/Model/AllureTestOpsConfigs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ public class AllureTestOpsSettings
4242
public string AccessToken { get; set; }
4343
public int ProjectId { get; set; } = -1;
4444
public string GherkinSyncToolId { get; set; }
45+
public string Component { get; set; }
4546
}
4647
}

GherkinSyncTool.Synchronizers.AllureTestOps/Model/TagsConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public static class TagsConstants
55
public const string Automated = "@Automated";
66
public const string Status = "@Status:";
77
public const string Reference = "@Reference:";
8+
public const string Component = "@Component:";
89
}
910
}

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.17.0</AssemblyVersion>
4+
<AssemblyVersion>3.18.0</AssemblyVersion>
55
<OutputType>Exe</OutputType>
66
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
77
<PackAsTool>true</PackAsTool>

0 commit comments

Comments
 (0)