Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding Microsoft.AspNetCore.Http nuget to use IHttpCnotextAccessor to…
… get a json body from request. New customfields migration.
  • Loading branch information
DiegoGalante committed Jun 21, 2022
commit 73111e1acaea1fb422fce1ca0f9b8aa3af61e1ed
3 changes: 2 additions & 1 deletion AzureDevopsTracker/AzureDevopsTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>

Expand Down
5 changes: 4 additions & 1 deletion AzureDevopsTracker/Configurations/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AzureDevopsTracker.Interfaces.Internals;
using AzureDevopsTracker.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
Expand All @@ -21,6 +22,8 @@ public static IServiceCollection AddAzureDevopsTracker(this IServiceCollection s

services.AddMessageIntegrations();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

services.AddScoped<AzureDevopsTrackerContext>();
services.AddScoped<IWorkItemAdapter, WorkItemAdapter>();
services.AddScoped<IWorkItemRepository, WorkItemRepository>();
Expand All @@ -44,7 +47,7 @@ private static IServiceCollection AddMessageIntegrations(this IServiceCollection
case EMessengers.MICROSOFT_TEAMS:
services.AddScoped<MessageIntegration, MicrosoftTeamsIntegration>();
break;
default:
default:
services.AddScoped<MessageIntegration, FakeIntegration>();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace AzureDevopsTracker.Data.Mapping
{
public class WorkItemCustomFieldMapping : IEntityTypeConfiguration<WorkItemCustomField>
public class CustomFieldMapping : IEntityTypeConfiguration<WorkItemCustomField>
{
public void Configure(EntityTypeBuilder<WorkItemCustomField> builder)
{
builder.HasKey(k => new { k.WorkItemId, k.Key });

builder.Property(p => p.Key)
.HasColumnType("varchar(1000)");

builder.Property(p => p.Value)
.HasColumnType("varchar(max)");
}
}
}
4 changes: 2 additions & 2 deletions AzureDevopsTracker/Entities/WorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void AddCustomField(WorkItemCustomField customField)

public void AddCustomFields(IEnumerable<WorkItemCustomField> customFields)
{
if (customFields is not null && !customFields.Any())
if (customFields is null || !customFields.Any())
return;

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

public void UpdateCustomFields(IEnumerable<WorkItemCustomField> newCustomFields)
{
if (newCustomFields is not null && !newCustomFields.Any())
if (newCustomFields is null || !newCustomFields.Any())
return;

_workItemCustomFields.Clear();
Expand Down
4 changes: 2 additions & 2 deletions AzureDevopsTracker/Entities/WorkItemCustomField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class WorkItemCustomField
public WorkItemCustomField(string workItemId, string key, string value)
{
WorkItemId = workItemId;
Key = key;
Value = value;
Key = key.Truncate(1000);
Value = value.Truncate();
}

public void Update(string value)
Expand Down
6 changes: 6 additions & 0 deletions AzureDevopsTracker/Extensions/HelperExtenions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static bool IsNullOrEmpty(this string text)
return string.IsNullOrEmpty(text?.Trim());
}

public static string Truncate(this string value, int maxLength = 8000)
{
if (value.IsNullOrEmpty()) return value;
return value.Length <= maxLength ? value : value[..maxLength];
}

public static string ExtractEmail(this string user)
{
if (user is null)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
columns: table => new
{
WorkItemId = table.Column<string>(type: "varchar(200)", nullable: false),
Key = table.Column<string>(type: "varchar(200)", nullable: false),
Value = table.Column<string>(type: "varchar(200)", nullable: true)
Key = table.Column<string>(type: "varchar(1000)", nullable: false),
Value = table.Column<string>(type: "varchar(max)", nullable: true)
},
constraints: table =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("varchar(200)");

b.Property<string>("Key")
.HasColumnType("varchar(200)");
.HasColumnType("varchar(1000)");

b.Property<string>("Value")
.HasColumnType("varchar(200)");
.HasColumnType("varchar(max)");

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

Expand Down
41 changes: 25 additions & 16 deletions AzureDevopsTracker/Services/AzureDevopsTrackerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
using AzureDevopsTracker.Interfaces;
using AzureDevopsTracker.Interfaces.Internals;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureDevopsTracker.Services
Expand All @@ -22,18 +22,18 @@ public class AzureDevopsTrackerService : IAzureDevopsTrackerService
public readonly IWorkItemRepository _workItemRepository;
public readonly IWorkItemAdapter _workItemAdapter;
public readonly IChangeLogItemRepository _changeLogItemRepository;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IHttpContextAccessor _httpContextAccessor;

public AzureDevopsTrackerService(
IWorkItemAdapter workItemAdapter,
IWorkItemRepository workItemRepository,
IChangeLogItemRepository changeLogItemRepository,
IServiceScopeFactory serviceScopeFactory)
IHttpContextAccessor httpContextAccessor)
{
_workItemAdapter = workItemAdapter;
_workItemRepository = workItemRepository;
_changeLogItemRepository = changeLogItemRepository;
_serviceScopeFactory = serviceScopeFactory;
_httpContextAccessor = httpContextAccessor;
}

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

CheckWorkItemAvailableToChangeLog(workItem, create.Resource.Fields);

ManipulateCustomFields(workItem);
AddCustomFields(workItem);

await _workItemRepository.Add(workItem);
await _workItemRepository.SaveChangesAsync();
Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task Update(UpdatedWorkItemDTO update)

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

ManipulateCustomFields(workItem);
AddCustomFields(workItem);

_workItemRepository.Update(workItem);
await _workItemRepository.SaveChangesAsync();
Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task Delete(DeleteWorkItemDTO delete)
delete.Resource.Fields.Activity,
delete.Resource.Fields.Lancado);

ManipulateCustomFields(workItem);
AddCustomFields(workItem);

_workItemRepository.Update(workItem);
await _workItemRepository.SaveChangesAsync();
Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task Restore(RestoreWorkItemDTO restore)
restore.Resource.Fields.Activity,
restore.Resource.Fields.Lancado);

ManipulateCustomFields(workItem);
AddCustomFields(workItem);

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

#region Support Methods
public void ManipulateCustomFields(WorkItem workItem)
public void AddCustomFields(WorkItem workItem)
{
try
{
var json = GetRequestBody();
if (json.IsNullOrEmpty())
var jsonText = GetRequestBody();
if (jsonText.IsNullOrEmpty())
return;

var customFields = ReadJsonHelper.ReadJson(workItem.Id, json);
var customFields = ReadJsonHelper.ReadJson(workItem.Id, jsonText);
if (customFields is null || !customFields.Any())
return;

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

public string GetRequestBody()
{
using var scope = _serviceScopeFactory.CreateScope();
var httpContext = scope.ServiceProvider.GetService<IHttpContextAccessor>();
return httpContext?.HttpContext?.Request?.Body?.ToString();
string corpo;
var request = _httpContextAccessor?.HttpContext?.Request;
using (StreamReader reader = new(request.Body,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: false,
leaveOpen: true))
{
request.Body.Position = 0;
corpo = reader.ReadToEndAsync()?.Result;
}

return corpo;
}

public WorkItemChange ToWorkItemChange(
Expand Down