Esempio n. 1
0
        private string SubmitJiraSupportRequest(SupportRequestModel model)
        {
            var    currentUser = CheckPoint.Instance.IsAuthenticated ? CheckPoint.Instance.GetUser(this.tdb) : null;
            string reporter    = string.Empty;

            if (currentUser != null)
            {
                reporter = currentUser.Email;
            }
            else if (!string.IsNullOrEmpty(model.Email))
            {
                reporter = model.Email;
            }

            try
            {
                var priorityConfig = JiraSection.GetSection().Priorities[model.Priority];
                var typeConfig     = JiraSection.GetSection().Types[model.Type];

                if (priorityConfig == null)
                {
                    throw new Exception("Cannot find a configured JIRA priority mapping for " + model.Priority);
                }

                if (typeConfig == null)
                {
                    throw new Exception("Cannot find a configured JIRA type mapping for " + model.Type);
                }

                string issueTypeId = typeConfig.Id;

                if (string.IsNullOrEmpty(issueTypeId))
                {
                    issueTypeId = AppSettings.DefaultJiraTaskType;
                }

                JIRAIssueData issueData = new JIRAIssueData();
                issueData.project     = AppSettings.JiraProject;
                issueData.type        = issueTypeId;
                issueData.summary     = model.Summary;
                issueData.description = model.Details;
                issueData.reporter    = reporter;
                issueData.priority    = priorityConfig.Id;

                return(this.SubmitJIRAIssue(issueData, TRIFOLIA_SUPPORT_LABEL));
            }
            catch (Exception submitException)
            {
                Log.For(this).Error("Failed to submit JIRA support request", submitException);
                throw new Exception("Could not submit JIRA support request. Please notify the administrator.");
            }
        }
Esempio n. 2
0
        private string SubmitJIRAIssue(JIRAIssueData aIssue, string aIssueLabel)
        {
            bool   validReporter = true;
            string encoded       = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(AppSettings.JiraUsername + ":" + AppSettings.JiraPassword));

            if (!string.IsNullOrEmpty(AppSettings.JiraUserEndpoint))
            {
                var userCheck = (HttpWebRequest)WebRequest.Create(AppSettings.JiraUserEndpoint + "?username="******"GET";
                userCheck.Headers.Add("Authorization", "Basic " + encoded);

                try
                {
                    var userCheckResponse = userCheck.GetResponse();

                    using (var streamReader = new StreamReader(userCheckResponse.GetResponseStream()))
                    {
                        var resultsJSON = streamReader.ReadToEnd();
                        var results     = JsonConvert.DeserializeObject <List <JiraUserSearchResponse> >(resultsJSON);

                        if (results.Count != 1)
                        {
                            aIssue.description = string.Format("{0}\\n\\nSubmitted By: {1}", aIssue.description, aIssue.reporter);
                            validReporter      = false;
                        }
                        else
                        {
                            aIssue.reporter = results[0].key;
                        }
                    }
                }
                catch (WebException wex)
                {
                    if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.Unauthorized)
                    {
                        Log.For(this).Error("Request to JIRA returned Unauthorized");
                        throw new Exception("Error submitting support request to JIRA (System Configuration Error)");
                    }

                    aIssue.description = string.Format("{0}\\n\\nSubmitted By: {1}", aIssue.description, aIssue.reporter);
                    validReporter      = false;
                }
            }
            else
            {
                aIssue.description = string.Format("{0}\\n\\nSubmitted By: {1}", aIssue.description, aIssue.reporter);
                validReporter      = false;
            }

            var webRequest = (HttpWebRequest)WebRequest.Create(AppSettings.JiraIssueEndpoint);

            webRequest.ContentType = "application/json";
            webRequest.Method      = "POST";
            webRequest.Headers.Add("Authorization", "Basic " + encoded);

            using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                JiraIssue jiraIssue = new JiraIssue();
                jiraIssue.fields.project.key  = aIssue.project;
                jiraIssue.fields.summary      = aIssue.summary;
                jiraIssue.fields.priority.id  = aIssue.priority;
                jiraIssue.fields.description  = aIssue.description;
                jiraIssue.fields.issuetype.id = aIssue.type;

                if (validReporter)
                {
                    jiraIssue.fields.reporter.name = aIssue.reporter;
                }

                if (!string.IsNullOrEmpty(AppSettings.JiraLabels))
                {
                    string[] labels = AppSettings.JiraLabels.Split(',');
                    jiraIssue.fields.labels.AddRange(labels);
                }

                string requestJSON = JsonConvert.SerializeObject(jiraIssue);
                streamWriter.Write(requestJSON);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var response = webRequest.GetResponse();

            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                var key    = result.Substring(result.IndexOf("\"key\":") + 7, result.IndexOf(",\"self\":") - result.IndexOf("\"key\":") - 8);
                return(key);
            }
        }