Esempio n. 1
0
        private ValidationResult PopulateBranchNameTemplate(
            SettingsContainer settings)
        {
            var settingsFromFile = FileSettingsCache.GetSettings();
            var value            = Concat.FirstValue(BranchNameTemplate, settingsFromFile.BranchNameTemplate);

            if (string.IsNullOrWhiteSpace(value))
            {
                settings.BranchSettings.BranchNameTemplate = null;
                return(ValidationResult.Success);
            }

            // Validating git branch names: https://stackoverflow.com/a/12093994/1661209
            // We validate the user defined branch name prefix in combination with a actual branch name that NuKeeper could create.
            // We want to validate the combination since the prefix doesn't need to fully comply with the rules (E.G. 'nukeeper/' is not allowed soley as a branch name).

            var tokenErrors = new StringBuilder();
            var tokenSet    = Regex.Matches(value, @"{(\w+)}").Select(match => match.Groups[1].Value);

            foreach (var token in tokenSet)
            {
                if (!BranchNamer.IsValidTemplateToken(token))
                {
                    tokenErrors.Append($",{token}");
                }
            }

            // Check for valid placeholders
            if (tokenErrors.Length > 0)
            {
                return(ValidationResult.Failure(
                           $"Provided branch template has unknown tokens: '{tokenErrors.ToString().Trim(',')}'."));
            }

            // Test if the generated branchname would be ok.
            // We assume tokens will be generated in valid values, so we use dummy values here
            var tokenValues = new Dictionary <string, string>();

            foreach (var token in BranchNamer.TemplateTokens)
            {
                tokenValues.Add(token, "dummy");
            }

            var validationValue = BranchNamer.MakeName(tokenValues, value);

            if (!Regex.IsMatch(validationValue, @"^(?!@$|build-|/|.*([/.]\.|//|@\{|\\))[^\000-\037\177 ~^:?*[]+/[^\000-\037\177 ~^:?*[]+(?<!\.lock|[/.])$"))
            {
                return(ValidationResult.Failure(
                           $"Provided branch template '{value}' does not comply with branch naming rules."));
            }

            settings.BranchSettings.BranchNameTemplate = value;
            return(ValidationResult.Success);
        }