Skip to content

Commit 0998e64

Browse files
committed
Added automatic handling of application versions
1 parent c882e9a commit 0998e64

File tree

15 files changed

+71
-323
lines changed

15 files changed

+71
-323
lines changed

src/Server/Coderr.Server.App/Modules/Versions/Config/ApplicationVersionConfig.cs

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/Server/Coderr.Server.App/Modules/Versions/Config/ApplicationVersionConfigItem.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Server/Coderr.Server.App/Modules/Versions/Events/AttachVersionTagToIncident.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Server/Coderr.Server.App/Modules/Versions/Events/GetVersionFromReport.cs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using System;
2-
using System.Linq;
32
using System.Threading.Tasks;
43
using codeRR.Server.Api.Core.Incidents.Events;
5-
using codeRR.Server.Api.Core.Notifications;
6-
using codeRR.Server.App.Modules.Versions.Config;
7-
using codeRR.Server.Infrastructure.Configuration;
8-
using codeRR.Server.Infrastructure.Security;
94
using Coderr.Server.PluginApi.Config;
105
using DotNetCqs;
116
using Griffin.Container;
@@ -15,50 +10,26 @@ namespace codeRR.Server.App.Modules.Versions.Events
1510
[Component]
1611
internal class GetVersionFromReport : IMessageHandler<ReportAddedToIncident>
1712
{
18-
private const string NotifyNoVersion = "Versions.Configure";
19-
private const string NotifyNotRecognizedVersion = "Versions.ReConfigure";
20-
private IVersionRepository _repository;
21-
private ConfigurationStore _configStore;
13+
public const string AppAssemblyVersion = "AppAssemblyVersion";
14+
private readonly IVersionRepository _repository;
2215

23-
public GetVersionFromReport(IVersionRepository repository, ConfigurationStore configStore)
16+
public GetVersionFromReport(IVersionRepository repository)
2417
{
2518
_repository = repository;
26-
_configStore = configStore;
2719
}
2820

2921
public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
3022
{
31-
var assemblyName = GetVersionAssemblyName(e.Incident.ApplicationId);
32-
if (assemblyName == null)
23+
string version = null;
24+
foreach (var contextCollection in e.Report.ContextCollections)
3325
{
34-
var notice = new AddNotification(CoderrClaims.RoleSysAdmin,
35-
"There is no version assembly configured for " + e.Incident.ApplicationName +
36-
". Go to 'System Settings'/Versions and configure one")
37-
{
38-
HoldbackInterval = TimeSpan.FromDays(3),
39-
NotificationType = NotifyNoVersion
40-
};
41-
await context.SendAsync(notice);
42-
return;
26+
if (!contextCollection.Properties.TryGetValue(AppAssemblyVersion, out version))
27+
continue;
4328
}
4429

45-
var collection = Enumerable.FirstOrDefault(e.Report.ContextCollections, x => x.Name == "Assemblies");
46-
if (collection == null)
30+
if (version == null)
4731
return;
4832

49-
if (!collection.Properties.TryGetValue(assemblyName, out string version))
50-
{
51-
var notice = new AddNotification(CoderrClaims.RoleSysAdmin,
52-
"Assembly " + assemblyName + " is configured for application " + e.Incident.ApplicationName +
53-
". It do however not exist. Configure a new one at 'System Settings'/Versions.")
54-
{
55-
HoldbackInterval = TimeSpan.FromDays(3),
56-
NotificationType = NotifyNotRecognizedVersion
57-
};
58-
await context.SendAsync(notice);
59-
return;
60-
}
61-
6233
var isNewIncident = e.Incident.ReportCount <= 1;
6334
var versionEntity = await _repository.FindVersionAsync(e.Incident.ApplicationId, version)
6435
?? new ApplicationVersion(e.Incident.ApplicationId, e.Incident.ApplicationName,
@@ -72,13 +43,10 @@ public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
7243
else
7344
await _repository.UpdateAsync(versionEntity);
7445

75-
await IncreaseReportCounter(versionEntity.Id, isNewIncident);
76-
}
7746

78-
private string GetVersionAssemblyName(int applicationId)
79-
{
80-
var config = _configStore.Load<ApplicationVersionConfig>();
81-
return config?.GetAssemblyName(applicationId);
47+
_repository.SaveIncidentVersion(e.Incident.Id, versionEntity.Id);
48+
49+
await IncreaseReportCounter(versionEntity.Id, isNewIncident);
8250
}
8351

8452
private async Task IncreaseReportCounter(int versionId, bool isNewIncident)

src/Server/Coderr.Server.App/Modules/Versions/IVersionRepository.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public interface IVersionRepository
2828
/// <exception cref="DbException">Failed to insert row</exception>
2929
Task CreateAsync(ApplicationVersion entity);
3030

31+
/// <summary>
32+
/// Get version
33+
/// </summary>
34+
/// <param name="incidentId">Incident to get versions for</param>
35+
/// <returns>version if found; otherwise <c>null</c></returns>
36+
/// <exception cref="ArgumentNullException">applicationId;version</exception>
37+
/// <exception cref="DbException">Failed to query DB</exception>
38+
Task<IList<ApplicationVersion>> FindForIncidentAsync(int incidentId);
39+
3140
/// <summary>
3241
/// Get monthly exception report
3342
/// </summary>
@@ -51,13 +60,18 @@ public interface IVersionRepository
5160
Task<ApplicationVersion> FindVersionAsync(int applicationId, string version);
5261

5362
/// <summary>
54-
/// Get version
63+
/// Find all versions that we've received error reports for.
5564
/// </summary>
56-
/// <param name="incidentId">Incident to get versions for</param>
57-
/// <returns>version if found; otherwise <c>null</c></returns>
58-
/// <exception cref="ArgumentNullException">applicationId;version</exception>
59-
/// <exception cref="DbException">Failed to query DB</exception>
60-
Task<IList<ApplicationVersion>> FindForIncidentAsync(int incidentId);
65+
/// <param name="appId">application id</param>
66+
/// <returns>versions</returns>
67+
Task<IEnumerable<string>> FindVersionsAsync(int appId);
68+
69+
/// <summary>
70+
/// Save version (ignore if it have already been stored).
71+
/// </summary>
72+
/// <param name="incidentId">incident to attach version to</param>
73+
/// <param name="versionId">Id for the application version</param>
74+
void SaveIncidentVersion(int incidentId, int versionId);
6175

6276

6377
/// <summary>
@@ -77,12 +91,5 @@ public interface IVersionRepository
7791
/// <exception cref="ArgumentNullException">entity</exception>
7892
/// <exception cref="DbException">Failed to query DB</exception>
7993
Task UpdateAsync(ApplicationVersion entity);
80-
81-
/// <summary>
82-
/// Find all versions that we've received error reports for.
83-
/// </summary>
84-
/// <param name="appId">application id</param>
85-
/// <returns>versions</returns>
86-
Task<IEnumerable<string>> FindVersionsAsync(int appId);
8794
}
8895
}

src/Server/Coderr.Server.App/Modules/Versions/QuickFactsProvider.cs renamed to src/Server/Coderr.Server.App/Modules/Versions/VersionQuickFactProvider.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
namespace codeRR.Server.App.Modules.Versions
1212
{
1313
[Component]
14-
class QuickFactsProvider : IQuickfactProvider
14+
class VersionQuickFactProvider : IQuickfactProvider
1515
{
1616
private IVersionRepository _repository;
1717

18-
public QuickFactsProvider(IVersionRepository repository)
18+
public VersionQuickFactProvider(IVersionRepository repository)
1919
{
2020
_repository = repository;
2121
}
@@ -25,11 +25,6 @@ public async Task AssignAsync(int incidentId, ICollection<QuickFact> facts)
2525
var versions = await _repository.FindForIncidentAsync(incidentId);
2626
if (!versions.Any())
2727
{
28-
facts.Add(new QuickFact
29-
{
30-
Title = "Versions",
31-
Value = "<em class=\"text-muted\">(This feature is activated in admin)</em>"
32-
});
3328
return;
3429
}
3530

src/Server/Coderr.Server.SqlServer/Modules/Versions/VersionRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ JOIN ApplicationVersions ON (IncidentVersions.VersionId = ApplicationVersions.Id
7676
}
7777
}
7878

79+
public void SaveIncidentVersion(int incidentId, int versionId)
80+
{
81+
var sql = @"INSERT INTO IncidentVersions (IncidentId, VersionId)
82+
SELECT @incidentId, @versionId
83+
WHERE NOT EXISTS (
84+
select IncidentId
85+
from IncidentVersions
86+
WHERE VersionId=@versionId
87+
)";
88+
using (var cmd = _uow.CreateDbCommand())
89+
{
90+
cmd.CommandText = sql;
91+
cmd.AddParameter("incidentId", incidentId);
92+
cmd.AddParameter("versionId", versionId);
93+
cmd.ExecuteNonQuery();
94+
}
95+
}
96+
7997
public async Task UpdateAsync(ApplicationVersionMonth month)
8098
{
8199
await _uow.UpdateAsync(month);

0 commit comments

Comments
 (0)