Example #1
0
        public override bool Send(string fileName, Stream file, Report report, SerializableException exception)
        {
            // Advanced method with ability to post variables along with file (do not forget to urlencode the query parameters)
            // http://www.codeproject.com/KB/cs/uploadfileex.aspx
            // http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
            // http://stackoverflow.com/questions/767790/how-do-i-upload-an-image-file-using-a-post-request-in-c
            // http://netomatix.com/HttpPostData.aspx

            /* upload.php file my look like the one below (note that uploaded files are not statically named in this case script may need modification)
             *
             * <?php
             * $uploadDir = 'Upload/';
             * $uploadFile = $uploadDir . basename($_FILES['file']['name']);
             * if (is_uploaded_file($_FILES['file']['tmp_name']))
             * {
             *     echo "File ". $_FILES['file']['name'] ." is successfully uploaded!\r\n";
             *     if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile))
             *     {
             *         echo "File is successfully stored! ";
             *     }
             *     else print_r($_FILES);
             * }
             * else
             * {
             *     echo "Upload Failed!";
             *     print_r($_FILES);
             * }
             * ?>
             */
            file.Position = 0;

            StreamUpload stream = StreamUpload.Create();
            if (PostReport)
            {
                stream.Add("report", report.ToString());
            }
            if (PostException)
            {
                stream.Add("exception", exception.ToString());
            }
            stream.Add(file, "file", fileName, "application/zip");
            string response = stream.Upload(this.Url).Response();

            Logger.Info("Response from HTTP server: " + response);
            file.Position = 0;

            return true;
        }
Example #2
0
        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.ToString();
            }

            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;
        }