-
Notifications
You must be signed in to change notification settings - Fork 50
/
ValidationHandler.cs
55 lines (51 loc) · 2.34 KB
/
ValidationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Threading.Tasks;
using App.Metrics;
using App.Metrics.Counter;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Tweek.Publishing.Service.Utils;
using Tweek.Publishing.Service.Validation;
using static Tweek.Publishing.Service.Utils.ShellHelper;
namespace Tweek.Publishing.Service.Handlers
{
public class ValidationHandler
{
private static readonly CounterOptions Validation = new CounterOptions
{Context = "publishing", Name = "validation"};
private static readonly MetricTags Success = new MetricTags("Status", "Successful");
private static readonly MetricTags Failure = new MetricTags("Status", "Failure");
public static Func<HttpRequest, HttpResponse, RouteData, Task> Create(ShellExecutor executor, GitValidationFlow gitValidationFlow, ILogger logger, IMetrics metrics) =>
async (req, res, routedata) =>
{
var oldCommit = req.Query["oldrev"].ToString().Trim();
var newCommit = req.Query["newrev"].ToString().Trim();
if (!IsCommitIdString(oldCommit) || !IsCommitIdString(newCommit))
{
res.StatusCode = 400;
await res.WriteAsync("Invalid commit id");
return;
}
var quarantinePath = req.Query["quarantinepath"].ToString();
var objectsDir = quarantinePath.Substring(quarantinePath.IndexOf("./objects"));
var gitExecutor = executor.CreateCommandExecutor("git", pStart =>
{
pStart.Environment["GIT_ALTERNATE_OBJECT_DIRECTORIES"] = "./objects";
pStart.Environment["GIT_OBJECT_DIRECTORY"] = objectsDir;
});
try
{
await gitValidationFlow.Validate(oldCommit, newCommit, gitExecutor);
metrics.Measure.Counter.Increment(Validation, Success);
}
catch (Exception ex)
{
metrics.Measure.Counter.Increment(Validation, Failure);
res.StatusCode = 400;
logger.LogWarning("Validation failed, {error}", ex.Message);
await res.WriteAsync(ex.Message);
}
};
}
}