public ProcessedArgs(string key, string name, string version, string organization, bool installLoaderTargets,
                             IAnalysisPropertyProvider cmdLineProperties, IAnalysisPropertyProvider globalFileProperties,
                             IAnalysisPropertyProvider scannerEnvProperties, ILogger logger)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            ProjectKey     = key;
            ProjectName    = name;
            ProjectVersion = version;
            Organization   = organization;

            CmdLineProperties         = cmdLineProperties ?? throw new ArgumentNullException(nameof(cmdLineProperties));
            this.globalFileProperties = globalFileProperties ?? throw new ArgumentNullException(nameof(globalFileProperties));
            ScannerEnvProperties      = scannerEnvProperties ?? throw new ArgumentNullException(nameof(scannerEnvProperties));
            InstallLoaderTargets      = installLoaderTargets;

            if (Organization == null && this.globalFileProperties.TryGetValue(SonarProperties.Organization, out var filePropertiesOrganization))
            {
                logger.LogError(Resources.ERROR_Organization_Provided_In_SonarQubeAnalysis_file);
                IsOrganizationValid = false;
            }
            else
            {
                IsOrganizationValid = true;
            }

            AggregateProperties = new AggregatePropertiesProvider(cmdLineProperties, globalFileProperties, ScannerEnvProperties);
            if (!AggregateProperties.TryGetValue(SonarProperties.HostUrl, out this.sonarQubeUrl))
            {
                this.sonarQubeUrl = "http://localhost:9000";
            }
        }
Esempio n. 2
0
        public ProcessedArgs(string key, string name, string version, string organization, bool installLoaderTargets,
                             IAnalysisPropertyProvider cmdLineProperties, IAnalysisPropertyProvider globalFileProperties,
                             IAnalysisPropertyProvider scannerEnvProperties)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException("key");
            }

            ProjectKey     = key;
            ProjectName    = name;
            ProjectVersion = version;
            Organization   = organization;

            CmdLineProperties         = cmdLineProperties ?? throw new ArgumentNullException("cmdLineProperties");
            this.globalFileProperties = globalFileProperties ?? throw new ArgumentNullException("globalFileProperties");
            ScannerEnvProperties      = scannerEnvProperties ?? throw new ArgumentNullException("scannerEnvProperties");
            InstallLoaderTargets      = installLoaderTargets;

            AggregateProperties = new AggregatePropertiesProvider(cmdLineProperties, globalFileProperties, ScannerEnvProperties);
            if (!AggregateProperties.TryGetValue(SonarProperties.HostUrl, out sonarQubeUrl))
            {
                sonarQubeUrl = "http://localhost:9000";
            }
        }
        /// <summary>
        /// Attempts to process the supplied command line arguments and reports any errors using the logger.
        /// Returns false if any parsing errors were encountered.
        /// </summary>
        public static bool TryProcessArgs(string[] commandLineArgs, ILogger logger, out IBootstrapperSettings settings)
        {
            if (commandLineArgs == null)
            {
                throw new ArgumentNullException("commandLineArgs");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            settings = null;

            IEnumerable <ArgumentInstance> arguments;

            // This call will fail if there are duplicate or missing arguments
            CommandLineParser parser = new CommandLineParser(Descriptors, true /* allow unrecognized arguments*/);
            bool parsedOk            = parser.ParseArguments(commandLineArgs, logger, out arguments);

            // Handler for command line analysis properties
            IAnalysisPropertyProvider cmdLineProperties;

            parsedOk &= CmdLineArgPropertyProvider.TryCreateProvider(arguments, logger, out cmdLineProperties);

            // Handler for property file
            IAnalysisPropertyProvider globalFileProperties;
            string asmPath = Path.GetDirectoryName(typeof(Bootstrapper.ArgumentProcessor).Assembly.Location);

            parsedOk &= FilePropertyProvider.TryCreateProvider(arguments, asmPath, logger, out globalFileProperties);

            AnalysisPhase phase;

            parsedOk &= TryGetPhase(commandLineArgs.Length, arguments, logger, out phase);

            Debug.Assert(!parsedOk || cmdLineProperties != null);
            Debug.Assert(!parsedOk || globalFileProperties != null);

            if (parsedOk)
            {
                Debug.Assert(cmdLineProperties != null);
                Debug.Assert(globalFileProperties != null);
                IAnalysisPropertyProvider properties = new AggregatePropertiesProvider(cmdLineProperties, globalFileProperties);

                IList <string> baseChildArgs = RemoveBootstrapperArgs(commandLineArgs);

                if (phase == AnalysisPhase.PreProcessing)
                {
                    settings = CreatePreProcessorSettings(baseChildArgs, properties, globalFileProperties, logger);
                }
                else
                {
                    settings = CreatePostProcessorSettings(baseChildArgs, properties, logger);
                }
            }

            return(settings != null);
        }
        public void AggProperties_NullOrEmptyList()
        {
            // 1. Null -> error
            Action act = () => new AggregatePropertiesProvider(null); act.Should().ThrowExactly <ArgumentNullException>();

            // 2. Empty list of providers -> valid but returns nothing
            var provider = new AggregatePropertiesProvider(new IAnalysisPropertyProvider[] { });

            provider.GetAllProperties().Should().BeEmpty();
            var success = provider.TryGetProperty("any key", out var actualProperty);

            success.Should().BeFalse("Not expecting a property to be returned");
            actualProperty.Should().BeNull("Returned property should be null");
        }
Esempio n. 5
0
        public void AggProperties_NullOrEmptyList()
        {
            // 1. Null -> error
            AssertException.Expects <ArgumentNullException>(() => new AggregatePropertiesProvider(null));

            // 2. Empty list of providers -> valid but returns nothing
            var provider = new AggregatePropertiesProvider(new IAnalysisPropertyProvider[] { });

            Assert.AreEqual(0, provider.GetAllProperties().Count());
            var success = provider.TryGetProperty("any key", out Property actualProperty);

            Assert.IsFalse(success, "Not expecting a property to be returned");
            Assert.IsNull(actualProperty, "Returned property should be null");
        }
        public void AggProperties_NullOrEmptyList()
        {
            // 1. Null -> error
            AssertException.Expects<ArgumentNullException>(() => new AggregatePropertiesProvider(null));

            // 2. Empty list of providers -> valid but returns nothing
            AggregatePropertiesProvider provider = new AggregatePropertiesProvider(new IAnalysisPropertyProvider[] { });

            Assert.AreEqual(0, provider.GetAllProperties().Count());
            Property actualProperty;
            bool success = provider.TryGetProperty("any key", out actualProperty);

            Assert.IsFalse(success, "Not expecting a property to be returned");
            Assert.IsNull(actualProperty, "Returned property should be null");
        }
        public void AggProperties_Aggregation()
        {
            // Checks the aggregation works as expected

            // 0. Setup
            ListPropertiesProvider provider1 = new ListPropertiesProvider();

            provider1.AddProperty("shared.key.A", "value A from one");
            provider1.AddProperty("shared.key.B", "value B from one");
            provider1.AddProperty("p1.unique.key.1", "p1 unique value 1");

            ListPropertiesProvider provider2 = new ListPropertiesProvider();

            provider2.AddProperty("shared.key.A", "value A from two");
            provider2.AddProperty("shared.key.B", "value B from two");
            provider2.AddProperty("p2.unique.key.1", "p2 unique value 1");

            ListPropertiesProvider provider3 = new ListPropertiesProvider();

            provider3.AddProperty("shared.key.A", "value A from three"); // this provider only has one of the shared values
            provider3.AddProperty("p3.unique.key.1", "p3 unique value 1");


            // 1. Ordering
            AggregatePropertiesProvider aggProvider = new AggregatePropertiesProvider(provider1, provider2, provider3);

            aggProvider.AssertExpectedPropertyCount(5);

            aggProvider.AssertExpectedPropertyValue("shared.key.A", "value A from one");
            aggProvider.AssertExpectedPropertyValue("shared.key.B", "value B from one");

            aggProvider.AssertExpectedPropertyValue("p1.unique.key.1", "p1 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p2.unique.key.1", "p2 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p3.unique.key.1", "p3 unique value 1");

            // 2. Reverse the order and try again
            aggProvider = new AggregatePropertiesProvider(provider3, provider2, provider1);

            aggProvider.AssertExpectedPropertyCount(5);

            aggProvider.AssertExpectedPropertyValue("shared.key.A", "value A from three");
            aggProvider.AssertExpectedPropertyValue("shared.key.B", "value B from two");

            aggProvider.AssertExpectedPropertyValue("p1.unique.key.1", "p1 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p2.unique.key.1", "p2 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p3.unique.key.1", "p3 unique value 1");
        }
        public void AggProperties_Aggregation()
        {
            // Checks the aggregation works as expected

            // 0. Setup
            ListPropertiesProvider provider1 = new ListPropertiesProvider();
            provider1.AddProperty("shared.key.A", "value A from one");
            provider1.AddProperty("shared.key.B", "value B from one");
            provider1.AddProperty("p1.unique.key.1", "p1 unique value 1");

            ListPropertiesProvider provider2 = new ListPropertiesProvider();
            provider2.AddProperty("shared.key.A", "value A from two");
            provider2.AddProperty("shared.key.B", "value B from two");
            provider2.AddProperty("p2.unique.key.1", "p2 unique value 1");

            ListPropertiesProvider provider3 = new ListPropertiesProvider();
            provider3.AddProperty("shared.key.A", "value A from three"); // this provider only has one of the shared values
            provider3.AddProperty("p3.unique.key.1", "p3 unique value 1");


            // 1. Ordering
            AggregatePropertiesProvider aggProvider = new AggregatePropertiesProvider(provider1, provider2, provider3);

            aggProvider.AssertExpectedPropertyCount(5);

            aggProvider.AssertExpectedPropertyValue("shared.key.A", "value A from one");
            aggProvider.AssertExpectedPropertyValue("shared.key.B", "value B from one");

            aggProvider.AssertExpectedPropertyValue("p1.unique.key.1", "p1 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p2.unique.key.1", "p2 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p3.unique.key.1", "p3 unique value 1");

            // 2. Reverse the order and try again
            aggProvider = new AggregatePropertiesProvider(provider3, provider2, provider1);

            aggProvider.AssertExpectedPropertyCount(5);

            aggProvider.AssertExpectedPropertyValue("shared.key.A", "value A from three");
            aggProvider.AssertExpectedPropertyValue("shared.key.B", "value B from two");

            aggProvider.AssertExpectedPropertyValue("p1.unique.key.1", "p1 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p2.unique.key.1", "p2 unique value 1");
            aggProvider.AssertExpectedPropertyValue("p3.unique.key.1", "p3 unique value 1");
        }
        private async Task <ArgumentsAndRuleSets> FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings)
        {
            ArgumentsAndRuleSets argumentsAndRuleSets = new ArgumentsAndRuleSets();

            try
            {
                this.logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration);

                // Respect sonar.branch setting if set
                args.TryGetSetting(SonarProperties.ProjectBranch, out var projectBranch);

                // Fetch the SonarQube project properties
                argumentsAndRuleSets.ServerSettings = await server.GetProperties(args.ProjectKey, projectBranch);

                // Fetch installed plugins
                var availableLanguages = await server.GetAllLanguages();

                foreach (var plugin in plugins)
                {
                    if (!availableLanguages.Contains(plugin.Language))
                    {
                        continue;
                    }

                    var qualityProfile = await server.TryGetQualityProfile(args.ProjectKey, projectBranch, args.Organization, plugin.Language);

                    // Fetch project quality profile
                    if (!qualityProfile.Item1)
                    {
                        this.logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey);
                        continue;
                    }

                    // Fetch rules (active and not active)
                    var activeRules = await server.GetActiveRules(qualityProfile.Item2);

                    if (!activeRules.Any())
                    {
                        this.logger.LogDebug(Resources.RAP_NoActiveRules, plugin.Language);
                    }

                    var inactiveRules = await server.GetInactiveRules(qualityProfile.Item2, plugin.Language);

                    // Generate Roslyn analyzers settings and rulesets
                    var analyzerProvider = this.factory.CreateRoslynAnalyzerProvider();
                    Debug.Assert(analyzerProvider != null, "Factory should not return null");

                    // Will be null if the processing of server settings and active rules resulted in an empty ruleset

                    // Use the aggregate of local and server properties when generating the analyzer configuration
                    // See bug 699: https://github.com/SonarSource/sonar-scanner-msbuild/issues/699
                    var serverProperties = new ListPropertiesProvider(argumentsAndRuleSets.ServerSettings);
                    var allProperties    = new AggregatePropertiesProvider(args.AggregateProperties, serverProperties);
                    var analyzer         = analyzerProvider.SetupAnalyzer(settings, allProperties, activeRules, inactiveRules, plugin.Language);

                    if (analyzer != null)
                    {
                        argumentsAndRuleSets.AnalyzersSettings.Add(analyzer);
                    }
                }
            }
            catch (WebException ex)
            {
                if (Utilities.HandleHostUrlWebException(ex, args.SonarQubeUrl, this.logger))
                {
                    argumentsAndRuleSets.IsSuccess = false;
                    return(argumentsAndRuleSets);
                }

                throw;
            }
            finally
            {
                Utilities.SafeDispose(server);
            }

            argumentsAndRuleSets.IsSuccess = true;
            return(argumentsAndRuleSets);
        }