Skip to content

Commit 73111e1

Browse files
committed
Adding Microsoft.AspNetCore.Http nuget to use IHttpCnotextAccessor to get a json body from request. New customfields migration.
1 parent b05f761 commit 73111e1

File tree

10 files changed

+55
-30
lines changed

10 files changed

+55
-30
lines changed

AzureDevopsTracker/AzureDevopsTracker.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
</PropertyGroup>
2525

2626
<ItemGroup>
27+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
2728
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
28-
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
29+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
2930
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
3031
</ItemGroup>
3132

AzureDevopsTracker/Configurations/Configuration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AzureDevopsTracker.Interfaces.Internals;
77
using AzureDevopsTracker.Services;
88
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Http;
910
using Microsoft.EntityFrameworkCore;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using System;
@@ -21,6 +22,8 @@ public static IServiceCollection AddAzureDevopsTracker(this IServiceCollection s
2122

2223
services.AddMessageIntegrations();
2324

25+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
26+
2427
services.AddScoped<AzureDevopsTrackerContext>();
2528
services.AddScoped<IWorkItemAdapter, WorkItemAdapter>();
2629
services.AddScoped<IWorkItemRepository, WorkItemRepository>();
@@ -44,7 +47,7 @@ private static IServiceCollection AddMessageIntegrations(this IServiceCollection
4447
case EMessengers.MICROSOFT_TEAMS:
4548
services.AddScoped<MessageIntegration, MicrosoftTeamsIntegration>();
4649
break;
47-
default:
50+
default:
4851
services.AddScoped<MessageIntegration, FakeIntegration>();
4952
break;
5053
}

AzureDevopsTracker/Data/Mapping/WorkItemCustomFieldMapping.cs renamed to AzureDevopsTracker/Data/Mapping/CustomFieldMapping.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
namespace AzureDevopsTracker.Data.Mapping
66
{
7-
public class WorkItemCustomFieldMapping : IEntityTypeConfiguration<WorkItemCustomField>
7+
public class CustomFieldMapping : IEntityTypeConfiguration<WorkItemCustomField>
88
{
99
public void Configure(EntityTypeBuilder<WorkItemCustomField> builder)
1010
{
1111
builder.HasKey(k => new { k.WorkItemId, k.Key });
12+
13+
builder.Property(p => p.Key)
14+
.HasColumnType("varchar(1000)");
15+
16+
builder.Property(p => p.Value)
17+
.HasColumnType("varchar(max)");
1218
}
1319
}
1420
}

AzureDevopsTracker/Entities/WorkItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void AddCustomField(WorkItemCustomField customField)
122122

123123
public void AddCustomFields(IEnumerable<WorkItemCustomField> customFields)
124124
{
125-
if (customFields is not null && !customFields.Any())
125+
if (customFields is null || !customFields.Any())
126126
return;
127127

128128
foreach (var customField in customFields)
@@ -131,7 +131,7 @@ public void AddCustomFields(IEnumerable<WorkItemCustomField> customFields)
131131

132132
public void UpdateCustomFields(IEnumerable<WorkItemCustomField> newCustomFields)
133133
{
134-
if (newCustomFields is not null && !newCustomFields.Any())
134+
if (newCustomFields is null || !newCustomFields.Any())
135135
return;
136136

137137
_workItemCustomFields.Clear();

AzureDevopsTracker/Entities/WorkItemCustomField.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class WorkItemCustomField
1414
public WorkItemCustomField(string workItemId, string key, string value)
1515
{
1616
WorkItemId = workItemId;
17-
Key = key;
18-
Value = value;
17+
Key = key.Truncate(1000);
18+
Value = value.Truncate();
1919
}
2020

2121
public void Update(string value)

AzureDevopsTracker/Extensions/HelperExtenions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public static bool IsNullOrEmpty(this string text)
1111
return string.IsNullOrEmpty(text?.Trim());
1212
}
1313

14+
public static string Truncate(this string value, int maxLength = 8000)
15+
{
16+
if (value.IsNullOrEmpty()) return value;
17+
return value.Length <= maxLength ? value : value[..maxLength];
18+
}
19+
1420
public static string ExtractEmail(this string user)
1521
{
1622
if (user is null)

AzureDevopsTracker/Migrations/20220620220028_CustomFields.Designer.cs renamed to AzureDevopsTracker/Migrations/20220621163011_CustomFields.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AzureDevopsTracker/Migrations/20220620220028_CustomFields.cs renamed to AzureDevopsTracker/Migrations/20220621163011_CustomFields.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
1313
columns: table => new
1414
{
1515
WorkItemId = table.Column<string>(type: "varchar(200)", nullable: false),
16-
Key = table.Column<string>(type: "varchar(200)", nullable: false),
17-
Value = table.Column<string>(type: "varchar(200)", nullable: true)
16+
Key = table.Column<string>(type: "varchar(1000)", nullable: false),
17+
Value = table.Column<string>(type: "varchar(max)", nullable: true)
1818
},
1919
constraints: table =>
2020
{

AzureDevopsTracker/Migrations/AzureDevopsStateTrackerContextModelSnapshot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
207207
.HasColumnType("varchar(200)");
208208

209209
b.Property<string>("Key")
210-
.HasColumnType("varchar(200)");
210+
.HasColumnType("varchar(1000)");
211211

212212
b.Property<string>("Value")
213-
.HasColumnType("varchar(200)");
213+
.HasColumnType("varchar(max)");
214214

215215
b.HasKey("WorkItemId", "Key");
216216

AzureDevopsTracker/Services/AzureDevopsTrackerService.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
using AzureDevopsTracker.Interfaces;
1010
using AzureDevopsTracker.Interfaces.Internals;
1111
using Microsoft.AspNetCore.Http;
12-
using Microsoft.Extensions.DependencyInjection;
13-
using Newtonsoft.Json;
1412
using System;
13+
using System.IO;
1514
using System.Linq;
15+
using System.Text;
1616
using System.Threading.Tasks;
1717

1818
namespace AzureDevopsTracker.Services
@@ -22,18 +22,18 @@ public class AzureDevopsTrackerService : IAzureDevopsTrackerService
2222
public readonly IWorkItemRepository _workItemRepository;
2323
public readonly IWorkItemAdapter _workItemAdapter;
2424
public readonly IChangeLogItemRepository _changeLogItemRepository;
25-
private readonly IServiceScopeFactory _serviceScopeFactory;
25+
private readonly IHttpContextAccessor _httpContextAccessor;
2626

2727
public AzureDevopsTrackerService(
2828
IWorkItemAdapter workItemAdapter,
2929
IWorkItemRepository workItemRepository,
3030
IChangeLogItemRepository changeLogItemRepository,
31-
IServiceScopeFactory serviceScopeFactory)
31+
IHttpContextAccessor httpContextAccessor)
3232
{
3333
_workItemAdapter = workItemAdapter;
3434
_workItemRepository = workItemRepository;
3535
_changeLogItemRepository = changeLogItemRepository;
36-
_serviceScopeFactory = serviceScopeFactory;
36+
_httpContextAccessor = httpContextAccessor;
3737
}
3838

3939
public async Task Create(CreateWorkItemDTO create, bool addWorkItemChange = true)
@@ -60,7 +60,7 @@ public async Task Create(CreateWorkItemDTO create, bool addWorkItemChange = true
6060

6161
CheckWorkItemAvailableToChangeLog(workItem, create.Resource.Fields);
6262

63-
ManipulateCustomFields(workItem);
63+
AddCustomFields(workItem);
6464

6565
await _workItemRepository.Add(workItem);
6666
await _workItemRepository.SaveChangesAsync();
@@ -108,7 +108,7 @@ public async Task Update(UpdatedWorkItemDTO update)
108108

109109
CheckWorkItemAvailableToChangeLog(workItem, update.Resource.Revision.Fields);
110110

111-
ManipulateCustomFields(workItem);
111+
AddCustomFields(workItem);
112112

113113
_workItemRepository.Update(workItem);
114114
await _workItemRepository.SaveChangesAsync();
@@ -140,7 +140,7 @@ public async Task Delete(DeleteWorkItemDTO delete)
140140
delete.Resource.Fields.Activity,
141141
delete.Resource.Fields.Lancado);
142142

143-
ManipulateCustomFields(workItem);
143+
AddCustomFields(workItem);
144144

145145
_workItemRepository.Update(workItem);
146146
await _workItemRepository.SaveChangesAsync();
@@ -172,7 +172,7 @@ public async Task Restore(RestoreWorkItemDTO restore)
172172
restore.Resource.Fields.Activity,
173173
restore.Resource.Fields.Lancado);
174174

175-
ManipulateCustomFields(workItem);
175+
AddCustomFields(workItem);
176176

177177
_workItemRepository.Update(workItem);
178178
await _workItemRepository.SaveChangesAsync();
@@ -188,15 +188,15 @@ public async Task<WorkItemDTO> GetByWorkItemId(string workItemId)
188188
}
189189

190190
#region Support Methods
191-
public void ManipulateCustomFields(WorkItem workItem)
191+
public void AddCustomFields(WorkItem workItem)
192192
{
193193
try
194194
{
195-
var json = GetRequestBody();
196-
if (json.IsNullOrEmpty())
195+
var jsonText = GetRequestBody();
196+
if (jsonText.IsNullOrEmpty())
197197
return;
198198

199-
var customFields = ReadJsonHelper.ReadJson(workItem.Id, json);
199+
var customFields = ReadJsonHelper.ReadJson(workItem.Id, jsonText);
200200
if (customFields is null || !customFields.Any())
201201
return;
202202

@@ -208,9 +208,18 @@ public void ManipulateCustomFields(WorkItem workItem)
208208

209209
public string GetRequestBody()
210210
{
211-
using var scope = _serviceScopeFactory.CreateScope();
212-
var httpContext = scope.ServiceProvider.GetService<IHttpContextAccessor>();
213-
return httpContext?.HttpContext?.Request?.Body?.ToString();
211+
string corpo;
212+
var request = _httpContextAccessor?.HttpContext?.Request;
213+
using (StreamReader reader = new(request.Body,
214+
encoding: Encoding.UTF8,
215+
detectEncodingFromByteOrderMarks: false,
216+
leaveOpen: true))
217+
{
218+
request.Body.Position = 0;
219+
corpo = reader.ReadToEndAsync()?.Result;
220+
}
221+
222+
return corpo;
214223
}
215224

216225
public WorkItemChange ToWorkItemChange(

0 commit comments

Comments
 (0)