public async Task <ActionResult> SendIssue(Submit model, [FromForm(Name = "AttachmentField")] IFormFile iFormFile) { string apiString = "";//Omitted using var client = new HttpClient { BaseAddress = new Uri(apiString + "rest/api/2/issue") }; client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Basic " + "");//Omitted Issue issue = new Issue(); issue.fields = new Fields(); issue.fields.project = new Project(); issue.fields.issuetype = new Issuetype(); issue.fields.project.key = _user.jiraKey; issue.fields.summary = model.SummaryField; issue.fields.description = model.DescriptionField; issue.fields.issuetype.id = "10001"; var json = JsonConvert.SerializeObject(issue); HttpContent issueContent = new StringContent(json, Encoding.UTF8, "application/json"); //Use if JSon is not created properly //System.Diagnostics.Debug.WriteLine(json); HttpResponseMessage Res = await client.PostAsync(apiString + "rest/api/2/issue", issueContent); var EmpResponse = Res.Content.ReadAsStringAsync().Result; //Creates an issue for us to lookup the key Issue returned = JsonConvert.DeserializeObject <Issue>(EmpResponse); System.Diagnostics.Debug.WriteLine((iFormFile != null)); if ((iFormFile != null) && FileValidationController.AttachmentContentValidation(iFormFile) && FileValidationController.AttachmentExtensionValidation(iFormFile)) { client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check"); //Creates a temporary file with the contents of the uploaded file var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await iFormFile.CopyToAsync(stream); } MultipartFormDataContent content = new MultipartFormDataContent(); HttpContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)); content.Add(fileContent, "file", "Attachment"); Res = await client.PostAsync(apiString + "rest/api/2/issue/" + returned.key + "/attachments", content); EmpResponse = Res.Content.ReadAsStringAsync().Result; } //Crafts the Slack Notification Message await api.Chat.PostMessage(new SlackNet.WebApi.Message { Channel = channel, Text = "New Issue: " + returned.key + "\n" + model.SummaryField + "\n" + model.DescriptionField + "\nSubmitted by: " + _user.firstName + " " + _user.lastName }); return(RedirectToRoute("Dashboard")); }
public async Task <ActionResult> EditIssue(Edit model, [FromForm(Name = "AttachmentField")] IFormFile iFormFile) { var issueId = (string)TempData["id"]; //Creating API call address and headers using var client = new HttpClient { BaseAddress = new Uri(apiString) }; client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Basic " + "");//Omitted //Call to API to retrieve issue details HttpResponseMessage Res = await client.GetAsync(apiString + issueId); var Response = Res.Content.ReadAsStringAsync().Result; //Sets model issue to data returned from api call UserDashboard.Issue issue = JsonConvert.DeserializeObject <UserDashboard.Issue>(Response); //Sets model description to additional information passed from view issue.fields.description += "\nEdit to issue:\n" + model.EditText; //Turns model data into json object then content data for api call var json = JsonConvert.SerializeObject(issue); HttpContent payload = new StringContent(json, Encoding.UTF8, "application/json"); //Determines if there is an uploaded file and if it complies to validation via FileValidationController if ((iFormFile != null) && FileValidationController.AttachmentContentValidation(iFormFile) && FileValidationController.AttachmentExtensionValidation(iFormFile)) { client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check"); //Creates a temporary file with the contents of the uploaded file var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await iFormFile.CopyToAsync(stream); } MultipartFormDataContent content = new MultipartFormDataContent(); HttpContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)); content.Add(fileContent, "file", "Attachment"); Res = await client.PostAsync(apiString + issueId + "/attachments", content); var EmpResponse = Res.Content.ReadAsStringAsync().Result; Res = await client.PutAsync(apiString + issueId, payload); EmpResponse = Res.Content.ReadAsStringAsync().Result; } else { Res = await client.PutAsync(apiString + issueId, payload); var EmpResponse = Res.Content.ReadAsStringAsync().Result; } //Crafts the Slack Notification Message await api.Chat.PostMessage(new SlackNet.WebApi.Message { Channel = channel, Text = "Edit Issue: " + issueId + "\n" + model.EditText + "\nSubmitted by: " + _user.firstName + " " + _user.lastName }); return(RedirectToRoute("Dashboard")); }