Skip to content

Commit

Permalink
Simple move of insert_bug.aspx logic to web api method
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaquette committed Jul 26, 2015
1 parent a33da41 commit abf82b5
Show file tree
Hide file tree
Showing 6 changed files with 1,193 additions and 929 deletions.
3 changes: 2 additions & 1 deletion src/BugTracker.Web/BugTracker.Web.csproj
Expand Up @@ -876,7 +876,7 @@
<Compile Include="btnet\BugQueryExecutor.cs" />
<Compile Include="btnet\BugQueryFilter.cs" />
<Compile Include="btnet\BugQueryResult.cs" />
<Compile Include="Controllers\BugController.cs" />
<Compile Include="Controllers\BugFromEmailController.cs" />
<Compile Include="Controllers\BugQueryController.cs" />
<Compile Include="Controls\BugList.ascx.cs">
<DependentUpon>BugList.ascx</DependentUpon>
Expand Down Expand Up @@ -907,6 +907,7 @@
<DependentUpon>LoggedOut.Master</DependentUpon>
</Compile>
<Compile Include="Models\Bug.cs" />
<Compile Include="Models\BugFromEmail.cs" />
<Compile Include="Models\Context.cs" />
<Compile Include="Models\BugPost.cs" />
<Compile Include="Models\BugPostAttachment.cs" />
Expand Down
243 changes: 236 additions & 7 deletions src/BugTracker.Web/Controllers/BugFromEmailController.cs
@@ -1,19 +1,248 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Data;
using System.IO;
using System.Web.Http;
using btnet.Mail;
using btnet.Models;
using btnet.Security;
using OpenPop.Mime;

namespace btnet.Controllers
{
[Authorize]
public class BugFromEmailController : ApiController
{
[HttpPost]
public IHttpActionResult Post()
{
return Ok();
public IHttpActionResult Post([FromBody] BugFromEmail bugFromEmail)
{
if (bugFromEmail != null && ModelState.IsValid)
{
if (bugFromEmail.ShortDescription == null)
{
bugFromEmail.ShortDescription = "";
}
else if (bugFromEmail.ShortDescription.Length > 200)
{
bugFromEmail.ShortDescription = bugFromEmail.ShortDescription.Substring(0, 200);
}

Message mimeMessage = null;

if (!string.IsNullOrEmpty(bugFromEmail.Message))
{
mimeMessage = Mime.GetMimeMessage(bugFromEmail.Message);

bugFromEmail.Comment = Mime.get_comment(mimeMessage);

string headers = Mime.get_headers_for_comment(mimeMessage);
if (headers != "")
{
bugFromEmail.Comment = string.Format("{0}{1}{2}", headers, Environment.NewLine, bugFromEmail.Comment);
}

bugFromEmail.FromAddress = Mime.get_from_addr(mimeMessage);

}
else
{
if (bugFromEmail.Comment == null)
{
bugFromEmail.Comment = string.Empty;
}
}

// Even though btnet_service.exe has already parsed out the bugid,
// we can do a better job here with SharpMimeTools.dll
string subject = "";

if (mimeMessage != null)
{
subject = Mime.get_subject(mimeMessage);

if (subject != "[No Subject]")
{
bugFromEmail.BugId = Mime.get_bugid_from_subject(ref subject);
}

bugFromEmail.CcAddress = Mime.get_cc(mimeMessage);
}

SQLString sql;

if (bugFromEmail.BugId != 0)
{
// Check if the bug is still in the database
// No comment can be added to merged or deleted bugids
// In this case a new bug is created, this to prevent possible loss of information

sql = new SQLString(@"select count(bg_id)
from bugs
where bg_id = @id");

sql = sql.AddParameterWithValue("id", Convert.ToString(bugFromEmail.BugId));

if (Convert.ToInt32(DbUtil.execute_scalar(sql)) == 0)
{
bugFromEmail.BugId = 0;
}
}


// Either insert a new bug or append a commment to existing bug
// based on presence, absence of bugid
if (bugFromEmail.BugId == 0)
{
// insert a new bug

if (mimeMessage != null)
{

// in case somebody is replying to a bug that has been deleted or merged
subject = subject.Replace(Util.get_setting("TrackingIdString", "DO NOT EDIT THIS:"), "PREVIOUS:");

bugFromEmail.ShortDescription = subject;
if (bugFromEmail.ShortDescription.Length > 200)
{
bugFromEmail.ShortDescription = bugFromEmail.ShortDescription.Substring(0, 200);
}

}

DataRow defaults = Bug.get_bug_defaults();

// If you didn't set these from the query string, we'll give them default values
if (!bugFromEmail.ProjectId.HasValue || bugFromEmail.ProjectId == 0) { bugFromEmail.ProjectId = (int)defaults["pj"]; }
bugFromEmail.OrganizationId = bugFromEmail.OrganizationId ?? User.Identity.GetOrganizationId();
bugFromEmail.CategoryId = bugFromEmail.CategoryId ?? (int)defaults["ct"];
bugFromEmail.PriorityId = bugFromEmail.PriorityId ?? (int)defaults["pr"];
bugFromEmail.StatusId = bugFromEmail.StatusId ?? (int)defaults["st"];
bugFromEmail.UdfId = bugFromEmail.UdfId ?? (int)defaults["udf"];

// but forced project always wins
if (User.Identity.GetForcedProjectId() != 0)
{
bugFromEmail.ProjectId = User.Identity.GetForcedProjectId();
}

Bug.NewIds newIds = Bug.insert_bug(
bugFromEmail.ShortDescription,
User.Identity,
"", // tags
bugFromEmail.ProjectId.Value,
bugFromEmail.OrganizationId.Value,
bugFromEmail.CategoryId.Value,
bugFromEmail.PriorityId.Value,
bugFromEmail.StatusId.Value,
bugFromEmail.AssignedTo ?? 0,
bugFromEmail.UdfId.Value,
bugFromEmail.Comment,
bugFromEmail.Comment,
bugFromEmail.FromAddress,
bugFromEmail.CcAddress,
"text/plain",
false, // internal only
null, // custom columns
false); // suppress notifications for now - wait till after the attachments

if (mimeMessage != null)
{
Mime.add_attachments(mimeMessage, newIds.bugid, newIds.postid, User.Identity);

Email.auto_reply(newIds.bugid, bugFromEmail.FromAddress, bugFromEmail.ShortDescription, bugFromEmail.ProjectId.Value);

}
else if (bugFromEmail.Attachment != null && bugFromEmail.Attachment.Length > 0)
{
Stream stream = new MemoryStream(bugFromEmail.Attachment);

Bug.insert_post_attachment(
User.Identity,
newIds.bugid,
stream,
bugFromEmail.Attachment.Length,
bugFromEmail.AttachmentFileName ?? string.Empty,
bugFromEmail.AttachmentDescription ?? string.Empty,
bugFromEmail.AttachmentContentType ?? string.Empty,
-1, // parent
false, // internal_only
false); // don't send notification yet
}

// your customizations
Bug.apply_post_insert_rules(newIds.bugid);

Bug.send_notifications(Bug.INSERT, newIds.bugid, User.Identity);
WhatsNew.add_news(newIds.bugid, bugFromEmail.ShortDescription, "added", User.Identity);

return Ok(newIds.bugid);
}
else // update existing bug
{

string statusResultingFromIncomingEmail = Util.get_setting("StatusResultingFromIncomingEmail", "0");


if (statusResultingFromIncomingEmail != "0")
{

sql = new SQLString(@"update bugs
set bg_status = @st
where bg_id = @bg
");

sql = sql.AddParameterWithValue("st", statusResultingFromIncomingEmail);
sql = sql.AddParameterWithValue("bg", bugFromEmail.BugId);
DbUtil.execute_nonquery(sql);

}

sql = new SQLString("select bg_short_desc from bugs where bg_id = @bg");

sql = sql.AddParameterWithValue("bg", bugFromEmail.BugId);
DataRow dr2 = DbUtil.get_datarow(sql);


// Add a comment to existing bug.
int postid = Bug.insert_comment(
bugFromEmail.BugId,
User.Identity.GetUserId(), // (int) dr["us_id"],
bugFromEmail.Comment,
bugFromEmail.Comment,
bugFromEmail.FromAddress,
bugFromEmail.CcAddress,
"text/plain",
false); // internal only

if (mimeMessage != null)
{
Mime.add_attachments(mimeMessage, bugFromEmail.BugId, postid, User.Identity);
}
else if (bugFromEmail.Attachment != null && bugFromEmail.Attachment.Length > 0)
{
Stream stream = new MemoryStream(bugFromEmail.Attachment);
Bug.insert_post_attachment(
User.Identity,
bugFromEmail.BugId,
stream,
bugFromEmail.Attachment.Length,
bugFromEmail.AttachmentFileName ?? string.Empty,
bugFromEmail.AttachmentDescription ?? string.Empty,
bugFromEmail.AttachmentContentType ?? string.Empty,
-1, // parent
false, // internal_only
false); // don't send notification yet
}

Bug.send_notifications(Bug.UPDATE, bugFromEmail.BugId, User.Identity);
WhatsNew.add_news(bugFromEmail.BugId, (string)dr2["bg_short_desc"], "updated", User.Identity);

return Ok(bugFromEmail.BugId);
}
}
else
{
return BadRequest(ModelState);
}
}
}
}
32 changes: 32 additions & 0 deletions src/BugTracker.Web/Models/BugFromEmail.cs
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace btnet.Models
{
public class BugFromEmail
{
public int? ProjectId { get; set; }
public string Comment { get; set; }

[Required]
public string FromAddress { get; set; }
public string CcAddress { get; set; }
public string Message { get; set; }
public byte[] Attachment { get; set; }
public string AttachmentContentType { get; set; }
public string AttachmentFileName { get; set; }
public string AttachmentDescription { get; set; }
public int BugId { get; set; }
public string ShortDescription { get; set; }

public int? OrganizationId { get; set; }
public int? CategoryId { get; set; }
public int? PriorityId { get; set; }
public int? StatusId { get; set; }
public int? AssignedTo { get; set; }
public int? UdfId { get; set;}
}
}

0 comments on commit abf82b5

Please sign in to comment.