Fixes filling bugs from Api

This commit is contained in:
Paul Schneider 2019-08-26 15:06:57 +01:00
commit d6575729e3
7 changed files with 3111 additions and 39 deletions

View file

@ -1,9 +1,10 @@
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authorization;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Logging;
using Yavsc.Models;
@ -24,45 +25,39 @@ namespace Yavsc.ApiControllers
_context = context;
}
[HttpPost]
[Route("~/api/bugsignal")]
[Produces("application/json")]
IActionResult Signal(BugReport report)
[HttpPost("signal")]
[Authorize]
public IActionResult Signal([FromBody] BugReport report)
{
// TODO check ApiKey, check duplicate, and save report
// var obj = JsonConvert.DeserializeObject(report.ExceptionObjectJson);
_logger.LogError("bug reported : "+report.ExceptionObjectJson);
_logger.LogWarning("bug reported : "+report.ExceptionObjectJson);
if (report.ExceptionObjectJson!=null)
if (report.ExceptionObjectJson.Length > 10240)
report.ExceptionObjectJson = report.ExceptionObjectJson.Substring(0,10240);
if (!ModelState.IsValid)
return HttpBadRequest();
var existent = _context.Bug.Include(b=>b.False).FirstOrDefault(
f=> f.False.ShortName == report.Component &&
f.Description == report.ExceptionObjectJson &&
f.Status != BugStatus.Rejected
{
_logger.LogError("Bag request :");
_logger.LogError(JsonConvert.SerializeObject(ModelState));
return new BadRequestObjectResult(ModelState);
}
var existent = _context.Bug.FirstOrDefault( b =>
b.Description == report.ExceptionObjectJson &&
b.Title == report.Component &&
b.Status != BugStatus.Rejected
);
if (existent != null)
{
_logger.LogInformation("bug already reported");
return Ok(existent);
existent = new Bug {
Description = report.ExceptionObjectJson
};
var existentFalse = _context.Feature.FirstOrDefault
(f=>f.ShortName == report.Component);
if (existentFalse==null)
{
existentFalse = new Models.IT.Evolution.Feature {
ShortName = report.Component,
Description = $"Generated by bug report, {DateTime.Now.ToLongDateString()}"
};
_context.Feature.Add(existentFalse) ;
}
existent.FeatureId = existentFalse.Id;
existent.False = existentFalse;
_context.SaveChanges();
return CreatedAtRoute("Signal", new { id = existent.Id }, existent);
}
_logger.LogInformation("Filling a new bug report");
var bug = new Bug {
Title = report.Component,
Description = report.ExceptionObjectJson
};
_context.Bug.Add(bug);
_context.SaveChanges();
return CreatedAtRoute("GetBug", new { id = bug.Id }, bug);
}
// GET: api/BugApi