Exemplo n.º 1
0
        private SonarRunResult RunProcess(string fullPath, string workingDirectory, string arguments)
        {
            if (!Directory.Exists(workingDirectory))
            {
                this.LogBuildError(string.Format("Working directory for sonar analysis {0} not found", workingDirectory));
            }

            using (Process proc = new Process())
            {
                var result = new SonarRunResult();

                proc.StartInfo.FileName = fullPath;

                proc.StartInfo.UseShellExecute        = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError  = true;
                proc.StartInfo.Arguments = arguments;
                this.LogBuildMessage("Running " + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments, BuildMessageImportance.High);

                if (!string.IsNullOrEmpty(workingDirectory))
                {
                    proc.StartInfo.WorkingDirectory = workingDirectory;
                }

                proc.Start();

                string outputStream = proc.StandardOutput.ReadToEnd();
                string errorStream  = proc.StandardError.ReadToEnd();

                IBuildDetail build = this.ActivityContext.GetExtension <IBuildDetail>();
                if (!string.IsNullOrEmpty(build.LogLocation))
                {
                    // log the full output of sonar to the build logs folder
                    string logFolder    = Path.GetDirectoryName(build.LogLocation);
                    string sonarLogFile = Path.Combine(logFolder, "Sonar.log");
                    File.WriteAllText(sonarLogFile, outputStream);
                    File.AppendAllText(sonarLogFile, "\n");
                    File.AppendAllText(sonarLogFile, errorStream);
                }
                else
                {
                    this.LogBuildWarning("Drop location not defined. Will not write to log file");
                    return(null);
                }

                result.HasBreakingAlerts = outputStream.Contains("[BUILD BREAKER]");

                if (outputStream.Length > 0)
                {
                    this.LogBuildMessage(outputStream);
                }

                if (errorStream.Length > 0)
                {
                    this.LogBuildWarning(errorStream);
                }

                proc.WaitForExit();
                result.ReturnCode = proc.ExitCode;
                return(result);
            }
        }
Exemplo n.º 2
0
        private SonarRunResult RunProcess(string fullPath, string workingDirectory, string arguments)
        {
            if (!Directory.Exists(workingDirectory))
            {
                this.LogBuildError(string.Format("Working directory for sonar analysis {0} not found", workingDirectory));
            }

            using (Process proc = new Process())
            {
                var result = new SonarRunResult();

                proc.StartInfo.FileName = fullPath;

                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.Arguments = arguments;
                this.LogBuildMessage("Running " + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments, BuildMessageImportance.High);

                if (!string.IsNullOrEmpty(workingDirectory))
                {
                    proc.StartInfo.WorkingDirectory = workingDirectory;
                }

                proc.Start();

                string outputStream = proc.StandardOutput.ReadToEnd();
                string errorStream = proc.StandardError.ReadToEnd();

                IBuildDetail build = this.ActivityContext.GetExtension<IBuildDetail>();
                if (!string.IsNullOrEmpty(build.LogLocation))
                {
                    // log the full output of sonar to the build logs folder
                    string logFolder = Path.GetDirectoryName(build.LogLocation);
                    string sonarLogFile = Path.Combine(logFolder, "Sonar.log");
                    File.WriteAllText(sonarLogFile, outputStream);
                    File.AppendAllText(sonarLogFile, "\n");
                    File.AppendAllText(sonarLogFile, errorStream);
                }
                else
                {
                    this.LogBuildWarning("Drop location not defined. Will not write to log file");
                    return null;
                }
                
                result.HasBreakingAlerts = outputStream.Contains("[BUILD BREAKER]");

                if (outputStream.Length > 0)
                {
                    this.LogBuildMessage(outputStream);
                }

                if (errorStream.Length > 0)
                {
                    this.LogBuildWarning(errorStream);
                }

                proc.WaitForExit();
                result.ReturnCode = proc.ExitCode;
                return result;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the logic for this custom activity
        /// </summary>
        protected override void InternalExecute()
        {
            Workspace workspace       = this.BuildWorkspace.Get(this.ActivityContext);
            string    sonarRunnerPath = this.SonarRunnerPath.Get(this.ActivityContext);

            if (!File.Exists(sonarRunnerPath))
            {
                this.LogBuildError("Sonar runner file not found: " + sonarRunnerPath);
                return;
            }

            foreach (string projectToAnalyze in this.ProjectsToAnalyze.Get(this.ActivityContext))
            {
                if (!string.IsNullOrWhiteSpace(projectToAnalyze))
                {
                    string localProjectPath = workspace.GetLocalItemForServerItem(projectToAnalyze);
                    string localFolderPath  = System.IO.Path.GetDirectoryName(localProjectPath);

                    if (this.GeneratePropertiesIfMissing.Get(this.ActivityContext))
                    {
                        string sonarPropertiesPath    = Path.Combine(localFolderPath, this.SonarPropertiesFileName.Get(this.ActivityContext));
                        string templatePropertiesPath = this.SonarPropertiesTemplatePath.Get(this.ActivityContext);
                        if (!File.Exists(sonarPropertiesPath))
                        {
                            this.LogBuildMessage("sonar.properties file not found in working folder.");
                            if (!string.IsNullOrWhiteSpace(templatePropertiesPath))
                            {
                                if (File.Exists(templatePropertiesPath))
                                {
                                    this.LogBuildMessage("Generating sonar properties file from " + templatePropertiesPath);
                                    string properties = this.TransformSonarProperties(
                                        templatePropertiesPath,
                                        localProjectPath);
                                    this.LogBuildMessage(properties, BuildMessageImportance.Low);
                                    File.WriteAllText(sonarPropertiesPath, properties, Encoding.ASCII);
                                }
                                else
                                {
                                    this.LogBuildError("Sonar properties template file not found " + templatePropertiesPath);
                                }
                            }
                            else
                            {
                                this.LogBuildError("No sonar properties file found and no template path set up.");
                            }
                        }
                    }

                    string arguments = string.Format("/c \"{0}\"", this.SonarRunnerPath.Get(this.ActivityContext));

                    SonarRunResult result = this.RunProcess("cmd.exe", localFolderPath, arguments);

                    if (result.HasBreakingAlerts)
                    {
                        this.LogBuildError("Sonar analysis has raised critical alerts.");
                        if (this.FailBuildOnAlert.Get(this.ActivityContext))
                        {
                            IBuildDetail buildDetail = this.ActivityContext.GetExtension <IBuildDetail>();
                            buildDetail.Status = BuildStatus.Failed;
                            buildDetail.Save();
                        }
                    }
                    else
                    {
                        if (result.ReturnCode == 0)
                        {
                            this.LogBuildMessage("Sonar analysis successful.", BuildMessageImportance.High);
                        }
                        else
                        {
                            this.LogBuildError("Sonar analysis has failed.");
                        }
                    }
                }
            }
        }