/// <inheritdoc />
        public async Task <QuantifierResult> Quantify(QuantifierInput quantifierInput)
        {
            ArgumentCheck.ParameterIsNotNull(quantifierInput, nameof(quantifierInput));

            // execute quantifier for this context and this particular input
            return(await Compute(quantifierInput));
        }
        public void ParameterIsNotNullPositive()
        {
            // act
            ArgumentCheck.ParameterIsNotNull("value", nameof(ArgumentCheck));

            // assert that we don't throw exception
            Assert.True(true);
        }
        public static void RemoveCodeBlockSeparatorChanges(this GitFilePatch gitFilePatch)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            // todo later add language specific parsers
            gitFilePatch.DiffLines = gitFilePatch.DiffLines
                                     .Where(l => l.Type != LineChangeType.Normal)
                                     .Where(l => !IsLineCodeBlockSeparator(l.Content.Trim()));
        }
        public static void RemoveCommentsChanges(this GitFilePatch gitFilePatch)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            // todo later expand  to comments sections, and add language specific parsers
            gitFilePatch.DiffLines = gitFilePatch.DiffLines
                                     .Where(l => l.Type != LineChangeType.Normal)
                                     .Where(l => !IsLineComment(l.Content.Trim()));
        }
        public static void RemoveWhiteSpacesChanges(this GitFilePatch gitFilePatch)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            // first remove lines containing only white spaces
            gitFilePatch.DiffLines = gitFilePatch.DiffLines
                                     .Where(l => l.Type != LineChangeType.Normal)
                                     .Where(l => !string.IsNullOrEmpty(l.Content.Trim()));
        }
        internal static IServiceCollection RegisterServices(
            this IServiceCollection serviceCollection,
            IConfiguration configuration)
        {
            serviceCollection.AddLogging(b => b.AddConsole());
            var azureBlobSettings = configuration.GetSection(nameof(AzureBlobSettings)).Get <AzureBlobSettings>();

            serviceCollection.AddSingleton <IBlobStorage>(
                p => new BlobStorage(
                    azureBlobSettings.AccountName,
                    azureBlobSettings.AccountKey,
                    true));

            serviceCollection.Configure <GitHubAppFlavorSettings>(configuration.GetSection(nameof(GitHubAppFlavorSettings)));
            serviceCollection.Configure <AzureServiceBusSettings>(
                configuration.GetSection(nameof(AzureServiceBusSettings)));
            serviceCollection.AddSingleton <IReadOnlyDictionary <string, IGitHubJwtFactory> >(
                sp =>
            {
                // register a GitHubJwtFactory used to create tokens to access github for a particular org on behalf  of the  app
                var gitHubAppFlavorSettings = sp.GetRequiredService <IOptions <GitHubAppFlavorSettings> >().Value;
                ArgumentCheck.ParameterIsNotNull(gitHubAppFlavorSettings, nameof(gitHubAppFlavorSettings));

                var ret = new Dictionary <string, IGitHubJwtFactory>();
                foreach (var gitHubAppSettings in gitHubAppFlavorSettings.GitHubAppsSettings)
                {
                    // Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file
                    ret[gitHubAppSettings.Key] = new GitHubJwtFactory(
                        new StringPrivateKeySource(gitHubAppSettings.Value.PrivateKey),
                        new GitHubJwtFactoryOptions
                    {
                        AppIntegrationId  = int.Parse(gitHubAppSettings.Value.Id), // The GitHub App Id
                        ExpirationSeconds = 590                                    // 10 minutes is the maximum time allowed
                    });
                }

                return(ret);
            });
            serviceCollection.AddSingleton <IGitHubClientAdapterFactory, GitHubClientAdapterFactory>();
            serviceCollection.TryAddSingleton <IEventBus, AzureServiceBus>();

            serviceCollection.TryAddTransient <IFileSystem, FileSystem>();
            serviceCollection.TryAddEnumerable(
                new[]
            {
                ServiceDescriptor.Singleton <IGitHubEventHandler, InstallationEventHandler>(),
                ServiceDescriptor.Singleton <IGitHubEventHandler, InstallationRepositoriesEventHandler>()
            });
            serviceCollection.AddHostedService <GitHubEventHost>();

            serviceCollection.AddApmForWebHost(configuration, typeof(Registrar).Namespace);

            return(serviceCollection);
        }
        public static void RemoveRenamedChanges(this GitFilePatch gitFilePatch)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            if (gitFilePatch.ChangeType != GitChangeType.Renamed)
            {
                return;
            }

            gitFilePatch.DiscardFromCounting = true;
        }
Beispiel #8
0
        /// <summary>
        /// Validate the context, make sure the thresholds don't overlap,
        /// labels are distinct.
        /// </summary>
        /// <param name="context">The context to validate.</param>
        /// <returns>returns true in case is valid, otherwise false and errors will not be empty.</returns>
        public static Context Validate(this Context context)
        {
            ArgumentCheck.ParameterIsNotNull(context, nameof(context));

            if (context.Thresholds == null ||
                !context.Thresholds.Any())
            {
                throw new ThresholdException("No labels/thresholds were defined!");
            }

            // we have to have at least 3 labels/thresholds defined
            if (context.Thresholds.Count() < 3)
            {
                throw new ThresholdException("At least 3 labels/thresholds should be defined!");
            }

            // check all values are percentiles
            var maxValue = context.Thresholds.Max(t => t.Value);
            var minValue = context.Thresholds.Min(t => t.Value);

            if (maxValue > 1000 ||
                maxValue < 0 ||
                minValue > 1000 ||
                minValue < 0)
            {
                throw new ThresholdException("All values should be between 0 and 1000!");
            }

            // validate thresholds are not the same
            var sortedThresholds = context.Thresholds.OrderBy(t => t.Value).ToArray();
            var prev             = sortedThresholds[0].Value;

            for (var i = 1; i < sortedThresholds.Length; i++)
            {
                if (sortedThresholds[i].Value != prev)
                {
                    prev = sortedThresholds[i].Value;
                    continue;
                }

                // we don't allow 2 upper bound thresholds to have the same value
                throw new ThresholdException("At least two upper bound thresholds have the same values!");
            }

            // validate labels are not empty
            if (context.Thresholds.Any(t => string.IsNullOrWhiteSpace(t.Label)))
            {
                throw new ThresholdException("You can't have empty labels!");
            }

            // todo regex/paths validation
            return(context);
        }
        public OperationScope(Activity activity, TelemetryClient telemetryClient = null)
        {
            ArgumentCheck.ParameterIsNotNull(activity, nameof(activity));

            telemetry = telemetryClient;

            if (telemetry == null)
            {
                return;
            }

            OperationHolder = telemetry.StartOperation <T>(activity);
        }
Beispiel #10
0
        public GitHubClientAdapterFactory(
            IReadOnlyDictionary <string, IGitHubJwtFactory> gitHubJwtFactories,
            IOptions <GitHubAppFlavorSettings> gitHubAppFlavorSettings,
            IAppTelemetry appTelemetry)
        {
            ArgumentCheck.ParameterIsNotNull(gitHubJwtFactories, nameof(gitHubJwtFactories));
            ArgumentCheck.ParameterIsNotNull(gitHubAppFlavorSettings, nameof(gitHubAppFlavorSettings));
            ArgumentCheck.ParameterIsNotNull(appTelemetry, nameof(appTelemetry));

            this.gitHubJwtFactories      = gitHubJwtFactories;
            this.appTelemetry            = appTelemetry;
            this.gitHubAppFlavorSettings = gitHubAppFlavorSettings.Value;
        }
Beispiel #11
0
        public GitHubWebhookController(
            IOptions <GitHubAppFlavorSettings> gitHubAppFlavorSettings,
            IAppTelemetry appTelemetry,
            IEventBus eventBus)
        {
            ArgumentCheck.ParameterIsNotNull(gitHubAppFlavorSettings, nameof(gitHubAppFlavorSettings));
            ArgumentCheck.ParameterIsNotNull(gitHubAppFlavorSettings, nameof(gitHubAppFlavorSettings));
            ArgumentCheck.ParameterIsNotNull(appTelemetry, nameof(appTelemetry));

            this.appTelemetry            = appTelemetry;
            this.eventBus                = eventBus;
            this.gitHubAppFlavorSettings = gitHubAppFlavorSettings.Value;
        }
        public static void ComputeChanges(this GitFilePatch gitFilePatch)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            // if file change was discarded from the counted do nothing.
            if (gitFilePatch.DiscardFromCounting)
            {
                return;
            }

            gitFilePatch.QuantifiedLinesAdded   = gitFilePatch.DiffLines.Count(l => l.Type == LineChangeType.Add);
            gitFilePatch.QuantifiedLinesDeleted = gitFilePatch.DiffLines.Count(l => l.Type == LineChangeType.Delete);
        }
        /// <summary>
        /// Discard from counting if the file should be excluded.
        /// Handles both <see cref="Context.Context.Included"/> and <see cref="Context.Context.Excluded"/>.
        /// </summary>
        /// <param name="gitFilePatch">Git file patch.</param>
        /// <param name="includedPatterns">Gitignore style patterns to include.</param>
        /// <param name="excludedPatterns">Gitignore style patterns to exclude.</param>
        /// <returns>True if the file patch is excluded, false otherwise.</returns>
        public static bool RemoveNotIncludedOrExcluded(
            this GitFilePatch gitFilePatch,
            IEnumerable <string> includedPatterns,
            IEnumerable <string> excludedPatterns)
        {
            ArgumentCheck.ParameterIsNotNull(gitFilePatch, nameof(gitFilePatch));

            var ignore = new Ignore();

            // if included patterns are specified, do not consider excluded patterns
            var includedPatternList = includedPatterns?.ToList();

            if (includedPatternList != null && includedPatternList.Any())
            {
                ignore.Add(includedPatternList);

                // not ignored by ignore effectively means this is not a match
                // and must be excluded
                if (!ignore.IsIgnored(gitFilePatch.FilePath))
                {
                    gitFilePatch.DiscardFromCounting = true;
                    return(true);
                }

                return(false);
            }

            var excludedPatternList = excludedPatterns?.ToList();

            if (excludedPatternList == null)
            {
                return(false);
            }

            ignore.Add(excludedPatternList);
            if (ignore.IsIgnored(gitFilePatch.FilePath))
            {
                gitFilePatch.DiscardFromCounting = true;
                return(true);
            }

            return(false);
        }
        public CommandLine(string[] args)
        {
            ArgumentCheck.ParameterIsNotNull(args, nameof(args));

            if (args.Length == 1 &&
                (args[0] == "-?" ||
                 args[0] == "/?" ||
                 args[0] == "-h" ||
                 args[0] == "--help"))
            {
                PrintUsage();
                return;
            }

            for (var i = args.Length - 1; i >= 0; i--)
            {
                arguments.Push(args[i]);
            }

            ParseArgs();
        }
        /// <summary>
        /// Compute the percentile position of the value within  the data array.
        /// In other words showing the percent of values form data array bellow our given value.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="value">The given value.</param>
        /// <returns>returns the percentile.</returns>
        public static float Percentile(int[] data, int value)
        {
            ArgumentCheck.ParameterIsNotNull(data, nameof(data));

            var maxValue = data[^ 1];
 public void ParameterIsNotNullNegative()
 {
     // act, assert
     Assert.Throws <ArgumentNullException>(() => ArgumentCheck.ParameterIsNotNull(null, nameof(ArgumentCheck)));
 }