Exemplo n.º 1
0
        public override bool Send(string fileName, Stream file, Report report, SerializableException exception)
        {
            HttpWebRequest request;

            if (string.IsNullOrEmpty(this.ApiKey))
            {
                request = (HttpWebRequest)WebRequest.Create(new Uri(this.Url + "issues.xml"));
            }
            else
            {
                request = (HttpWebRequest)WebRequest.Create(new Uri(this.Url + "issues.xml?key=" + this.ApiKey));
            }

            // Used POST as per http://www.redmine.org/projects/redmine/wiki/Rest_Issues#Creating-an-issue
            request.Method      = "POST";
            request.ContentType = "application/xml";
            request.Accept      = "application/xml";
            request.ServicePoint.Expect100Continue = false;             // Patch #11593. Some servers seem to have problem accepting the Expect: 100-continue header

            // Redmine v1.1.1 REST XML templates (* indicate required fields)
            // Note: <*_id> fields always ask for numeric values

            /* <?xml version="1.0"?>
             * <issue>
             *   *<subject>Example</subject>
             *   *<project_id>myproject</project_id>
             *   <tracker_id></tracker_id> <!-- Bug=1/Issue=2/etc. -->
             *   <priority_id></priority_id> <!-- Normal=4 -->
             *   <category_id></category_id>
             *   <description></description>
             *   <fixed_version_id></fixed_version_id>
             *   <assigned_to_id></assigned_to_id>
             *   <parent_id></parent_id>
             *   <status_id></status_id>
             *   <author_id></author_id>
             *   <start_date></start_date>
             *   <due_date></due_date>
             *   <done_ratio></done_ratio>
             *   <estimated_hours></estimated_hours>
             *   <created_on></created_on>
             *   <updated_on></updated_on>
             * </issue>
             */
            var subject = "NBug: " + report.GeneralInfo.HostApplication + " (" + report.GeneralInfo.HostApplicationVersion + "): "
                          + report.GeneralInfo.ExceptionType + " @ " + report.GeneralInfo.TargetSite;

            var description = "<pre>" + report + Environment.NewLine + Environment.NewLine + exception.ToXmlString() + "</pre>";

            var redmineRequestXml = new XElement("issue", new XElement("project_id", this.ProjectId));

            if (!string.IsNullOrEmpty(this.TrackerId))
            {
                redmineRequestXml.Add(new XElement("tracker_id", this.TrackerId));
            }

            if (!string.IsNullOrEmpty(this.PriorityId))
            {
                redmineRequestXml.Add(new XElement("priority_id", this.PriorityId));
            }

            if (!string.IsNullOrEmpty(this.CategoryId))
            {
                redmineRequestXml.Add(new XElement("category_id", this.CategoryId));
            }

            // Add the subject and make sure that it is less than 255 characters or Redmine trows an HTTP error code 422 : Unprocessable Entity
            if (!string.IsNullOrEmpty(this.CustomSubject))
            {
                var sbj = this.CustomSubject + " : " + subject;
                redmineRequestXml.Add(sbj.Length > 254 ? new XElement("subject", sbj.Substring(0, 254)) : new XElement("subject", sbj));
            }
            else
            {
                redmineRequestXml.Add(subject.Length > 254 ? new XElement("subject", subject.Substring(0, 254)) : new XElement("subject", subject));
            }

            if (!string.IsNullOrEmpty(description))
            {
                redmineRequestXml.Add(new XElement("description", description));
            }

            if (!string.IsNullOrEmpty(this.FixedVersionId))
            {
                redmineRequestXml.Add(new XElement("fixed_version_id", this.FixedVersionId));
            }

            if (!string.IsNullOrEmpty(this.AssignedToId))
            {
                redmineRequestXml.Add(new XElement("assigned_to_id", this.AssignedToId));
            }

            if (!string.IsNullOrEmpty(this.ParentId))
            {
                redmineRequestXml.Add(new XElement("parent_id", this.ParentId));
            }

            if (!string.IsNullOrEmpty(this.StatusId))
            {
                redmineRequestXml.Add(new XElement("status_id", this.StatusId));
            }

            if (!string.IsNullOrEmpty(this.AuthorId))
            {
                redmineRequestXml.Add(new XElement("author_id", this.AuthorId));
            }

            var bytes = Encoding.UTF8.GetBytes(redmineRequestXml.ToString());

            request.ContentLength = bytes.Length;

            using (var putStream = request.GetRequestStream())
            {
                putStream.Write(bytes, 0, bytes.Length);
            }

            // Log the response from Redmine RESTful service
            using (var response = (HttpWebResponse)request.GetResponse())
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    Logger.Info("Response from Redmine Issue Tracker: " + reader.ReadToEnd());
                }

            return(true);
        }
Exemplo n.º 2
0
Arquivo: Mantis.cs Projeto: vbjay/NBug
        public override bool Send(string fileName, Stream file, Report report, SerializableException exception)
        {
            if (string.IsNullOrEmpty(Username) || Username.Trim().Length == 0)
            {
                throw new ArgumentNullException("Username");
            }

            if (string.IsNullOrEmpty(Password) || Password.Trim().Length == 0)
            {
                throw new ArgumentNullException("Password");
            }

            if (string.IsNullOrEmpty(Url) || Url.Trim().Length == 0)
            {
                throw new ArgumentNullException("Url");
            }

            if (ProjectId <= 0)
            {
                throw new ArgumentNullException("ProjectId");
            }

            if (string.IsNullOrEmpty(Category) || Category.Trim().Length == 0)
            {
                throw new ArgumentNullException("Category");
            }

            if (string.IsNullOrEmpty(Description) || Description.Trim().Length == 0)
            {
                Description = exception.ToXmlString();
            }

            if (string.IsNullOrEmpty(Summary) || Summary.Trim().Length == 0)
            {
                Summary = report.GeneralInfo.ExceptionMessage + " @ " + report.GeneralInfo.TargetSite;
            }

            if (string.IsNullOrEmpty(AdditionalInformation) || AdditionalInformation.Trim().Length == 0)
            {
                AdditionalInformation = report.ToString();
            }

            var service = new MantisConnectService(new BasicHttpBinding(), new EndpointAddress(Url));
            var user    = service.mc_login(Username, Password);

            Logger.Info(String.Format("Successfully logged in to Mantis bug tracker as {0}", user.account_data.real_name));
            var issue = new IssueData();

            issue.date_submitted          = DateTime.Now;
            issue.date_submittedSpecified = true;
            issue.version  = report.GeneralInfo.HostApplicationVersion;
            issue.os       = Environment.OSVersion.ToString();
            issue.os_build = Environment.OSVersion.Version.ToString();
            issue.platform = Environment.OSVersion.Platform.ToString();
            issue.reporter = user.account_data;
            issue.project  = new ObjectRef()
            {
                id = ProjectId.ToString(CultureInfo.InvariantCulture), name = String.Empty
            };
            issue.category               = Category;
            issue.summary                = Summary;
            issue.description            = Description;
            issue.additional_information = AdditionalInformation;
            issue.steps_to_reproduce     = StepsToReproduce;
            issue.severity               = new ObjectRef()
            {
                id = Severity.ToString(CultureInfo.InvariantCulture), name = Severity.ToString(CultureInfo.InvariantCulture)
            };
            issue.status = new ObjectRef()
            {
                id = Status.ToString(CultureInfo.InvariantCulture), name = Status.ToString(CultureInfo.InvariantCulture)
            };
            bool success = false;

            if (AddVersionIfNotExists)
            {
                var versions = service.mc_project_get_versions(Username, Password, ProjectId.ToString(CultureInfo.InvariantCulture));
                if (versions.Count() == 0 ||
                    !versions.Any(x => x.name == report.GeneralInfo.HostApplicationVersion.ToString(CultureInfo.InvariantCulture)))
                {
                    var version = new ProjectVersionData
                    {
                        name                = report.GeneralInfo.HostApplicationVersion.ToString(CultureInfo.InvariantCulture),
                        project_id          = ProjectId.ToString(CultureInfo.InvariantCulture),
                        released            = true,
                        releasedSpecified   = true,
                        date_order          = DateTime.Now,
                        date_orderSpecified = true,
                        description         = "Added by NBug"
                    };
                    var versionId = service.mc_project_version_add(Username, Password, version);
                    Logger.Info(String.Format("Successfully added new version id {0} to Mantis bug tracker", versionId));
                }
            }

            int bugId;

            Int32.TryParse(service.mc_issue_add(Username, Password, issue), out bugId);

            if (bugId > 0)
            {
                Logger.Info(String.Format("Successfully added new issue id {0} to Mantis bug tracker", bugId));
                success = true;
                if (SendAttachment)
                {
                    if (file != null && file.Length > 0)
                    {
                        if (!SuccessIfAttachmentFails)
                        {
                            success = false;
                        }
                        try
                        {
                            var attachment = new byte[file.Length];
                            file.Position = 0;
                            file.Read(attachment, 0, Convert.ToInt32(file.Length));
                            var attachmentId = service.mc_issue_attachment_add(Username, Password, bugId.ToString(CultureInfo.InvariantCulture), fileName, "application/zip", attachment);
                            Logger.Info(String.Format("Successfully added attachment id {0} for isssue id {1} to Mantis bug tracker", attachmentId, bugId));
                            success = true;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(String.Format("Failed to upload attachment with issue id {0} to Mantis bug tracker{1}{2}", bugId, Environment.NewLine, ex.Message));
                            if (!SuccessIfAttachmentFails)
                            {
                                throw;
                            }
                        }
                    }
                }
            }

            return(success);
        }
Exemplo n.º 3
0
Arquivo: Mail.cs Projeto: vbjay/NBug
        public override bool Send(string fileName, Stream file, Report report, SerializableException exception)
        {
            if (string.IsNullOrEmpty(this.From) || string.IsNullOrEmpty(this.To))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(this.ReplyTo))
            {
                this.ReplyTo = this.From;
            }

            if (this.Port <= 0)
            {
                this.Port = this.UseSsl ? 465 : 25;
            }

            if (!this.UseAttachment)
            {
                this.UseAttachment = false;
            }

            // Make sure that we can use authentication even with emtpy username and password
            if (!string.IsNullOrEmpty(this.Username))
            {
                this.UseAuthentication = true;
            }

            using (var smtpClient = new SmtpClient())
                using (var message = new MailMessage())
                {
                    if (!string.IsNullOrEmpty(this.SmtpServer))
                    {
                        smtpClient.Host = this.SmtpServer;
                    }

                    smtpClient.Port = this.Port;

                    if (this.UseAuthentication)
                    {
                        smtpClient.Credentials = new NetworkCredential(this.Username, this.Password);
                    }

                    smtpClient.EnableSsl = this.UseSsl;

                    if (!string.IsNullOrEmpty(this.Cc))
                    {
                        message.CC.Add(this.Cc);
                    }

                    if (!string.IsNullOrEmpty(this.Bcc))
                    {
                        message.Bcc.Add(this.Bcc);
                    }

                    message.Priority = this.Priority;

                    message.To.Add(this.To);
                    message.ReplyToList.Add(this.ReplyTo);
                    message.From = !string.IsNullOrEmpty(this.FromName) ? new MailAddress(this.From, this.FromName) : new MailAddress(this.From);

                    if (this.UseAttachment)
                    {
                        // ToDo: Report file name should be attached to the report file object itself, file shouldn't be accessed directly!
                        file.Position = 0;
                        message.Attachments.Add(new Attachment(file, fileName));
                    }

                    if (!string.IsNullOrEmpty(this.CustomSubject))
                    {
                        message.Subject = this.CustomSubject;
                    }
                    else
                    {
                        message.Subject = "NBug: " + report.GeneralInfo.HostApplication + " (" + report.GeneralInfo.HostApplicationVersion + "): "
                                          + report.GeneralInfo.ExceptionType + " @ " + report.GeneralInfo.TargetSite;
                    }

                    if (!string.IsNullOrEmpty(this.CustomBody))
                    {
                        message.Body = this.CustomBody + Environment.NewLine + Environment.NewLine + report + Environment.NewLine + Environment.NewLine + exception;
                    }
                    else
                    {
                        message.Body = report + Environment.NewLine + Environment.NewLine + exception.ToXmlString();
                    }

                    smtpClient.Send(message);
                    Logger.Trace("Submitted bug report email to: " + this.To);

                    return(true);
                }
        }