public override int ProcessContents(IList <Line> contents)
        {
            SubscriptionData outputYamlData;

            try
            {
                string        yamlString = contents.Aggregate <Line, string>("", (current, line) => $"{current}{System.Environment.NewLine}{line.Text}");
                IDeserializer serializer = new DeserializerBuilder().Build();
                outputYamlData = serializer.Deserialize <SubscriptionData>(yamlString);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to parse input yaml.  Please see help for correct format.");
                return(Constants.ErrorCode);
            }

            _yamlData.Batchable = ParseSetting(outputYamlData.Batchable, _yamlData.Batchable, false);
            _yamlData.Enabled   = ParseSetting(outputYamlData.Enabled, _yamlData.Enabled, false);
            // Make sure Batchable and Enabled are valid bools
            if (!bool.TryParse(outputYamlData.Batchable, out bool batchable) || !bool.TryParse(outputYamlData.Enabled, out bool enabled))
            {
                _logger.LogError("Either Batchable or Enabled is not a valid boolean values.");
                return(Constants.ErrorCode);
            }

            // Validate the merge policies
            if (!MergePoliciesPopUpHelpers.ValidateMergePolicies(MergePoliciesPopUpHelpers.ConvertMergePolicies(outputYamlData.MergePolicies), _logger))
            {
                return(Constants.ErrorCode);
            }

            _yamlData.MergePolicies = outputYamlData.MergePolicies;

            // Parse and check the input fields
            _yamlData.Channel = ParseSetting(outputYamlData.Channel, _yamlData.Channel, false);
            if (string.IsNullOrEmpty(_yamlData.Channel))
            {
                _logger.LogError("Channel must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.SourceRepository = ParseSetting(outputYamlData.SourceRepository, _yamlData.SourceRepository, false);
            if (string.IsNullOrEmpty(_yamlData.SourceRepository))
            {
                _logger.LogError("Source repository URL must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.UpdateFrequency = ParseSetting(outputYamlData.UpdateFrequency, _yamlData.UpdateFrequency, false);
            if (string.IsNullOrEmpty(_yamlData.UpdateFrequency) ||
                !Constants.AvailableFrequencies.Contains(_yamlData.UpdateFrequency, StringComparer.OrdinalIgnoreCase))
            {
                _logger.LogError($"Frequency should be provided and should be one of the following: " +
                                 $"'{string.Join("', '",Constants.AvailableFrequencies)}'");
                return(Constants.ErrorCode);
            }

            return(Constants.SuccessCode);
        }
        public override int ProcessContents(IList <Line> contents)
        {
            RepositoryPoliciesData outputYamlData;

            try
            {
                // Join the lines back into a string and deserialize as YAML.
                string        yamlString = contents.Aggregate <Line, string>("", (current, line) => $"{current}{System.Environment.NewLine}{line.Text}");
                IDeserializer serializer = new DeserializerBuilder().Build();
                outputYamlData = serializer.Deserialize <RepositoryPoliciesData>(yamlString);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to parse input yaml.  Please see help for correct format.");
                return(Constants.ErrorCode);
            }

            // Validate the merge policies
            if (!MergePoliciesPopUpHelpers.ValidateMergePolicies(MergePoliciesPopUpHelpers.ConvertMergePolicies(outputYamlData.MergePolicies), _logger))
            {
                return(Constants.ErrorCode);
            }

            _yamlData.MergePolicies = outputYamlData.MergePolicies;

            _yamlData.Repository = ParseSetting(outputYamlData.Repository, _yamlData.Repository, false);
            if (string.IsNullOrEmpty(_yamlData.Repository))
            {
                _logger.LogError("Repository URL must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.Branch = ParseSetting(outputYamlData.Branch, _yamlData.Branch, false);
            if (string.IsNullOrEmpty(_yamlData.Branch))
            {
                _logger.LogError("Branch must be non-empty");
                return(Constants.ErrorCode);
            }

            return(Constants.SuccessCode);
        }
Пример #3
0
        public override int ProcessContents(IList <Line> contents)
        {
            SubscriptionData outputYamlData;

            try
            {
                // Join the lines back into a string and deserialize as YAML.
                // TODO: Alter the popup/ux manager to pass along the raw file to avoid this unnecessary
                // operation once authenticate ends up as YAML.
                string        yamlString = contents.Aggregate <Line, string>("", (current, line) => $"{current}{System.Environment.NewLine}{line.Text}");
                IDeserializer serializer = new DeserializerBuilder().Build();
                outputYamlData = serializer.Deserialize <SubscriptionData>(yamlString);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to parse input yaml.  Please see help for correct format.");
                return(Constants.ErrorCode);
            }

            // Validate the merge policies
            if (!MergePoliciesPopUpHelpers.ValidateMergePolicies(MergePoliciesPopUpHelpers.ConvertMergePolicies(outputYamlData.MergePolicies), _logger))
            {
                return(Constants.ErrorCode);
            }

            _yamlData.MergePolicies = outputYamlData.MergePolicies;

            // Parse and check the input fields
            _yamlData.Channel = ParseSetting(outputYamlData.Channel, _yamlData.Channel, false);
            if (string.IsNullOrEmpty(_yamlData.Channel))
            {
                _logger.LogError("Channel must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.SourceRepository = ParseSetting(outputYamlData.SourceRepository, _yamlData.SourceRepository, false);
            if (string.IsNullOrEmpty(_yamlData.SourceRepository))
            {
                _logger.LogError("Source repository URL must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.TargetRepository = ParseSetting(outputYamlData.TargetRepository, _yamlData.TargetRepository, false);
            if (string.IsNullOrEmpty(_yamlData.TargetRepository))
            {
                _logger.LogError("Target repository URL must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.TargetBranch = ParseSetting(outputYamlData.TargetBranch, _yamlData.TargetBranch, false);
            if (string.IsNullOrEmpty(_yamlData.TargetBranch))
            {
                _logger.LogError("Target branch must be non-empty");
                return(Constants.ErrorCode);
            }

            _yamlData.Batchable = outputYamlData.Batchable;

            _yamlData.FailureNotificationTags = outputYamlData.FailureNotificationTags;

            _yamlData.UpdateFrequency = ParseSetting(outputYamlData.UpdateFrequency, _yamlData.UpdateFrequency, false);
            if (string.IsNullOrEmpty(_yamlData.UpdateFrequency) ||
                !Constants.AvailableFrequencies.Contains(_yamlData.UpdateFrequency, StringComparer.OrdinalIgnoreCase))
            {
                _logger.LogError($"Frequency should be provided and should be one of the following: " +
                                 $"'{string.Join("', '",Constants.AvailableFrequencies)}'");
                return(Constants.ErrorCode);
            }

            return(Constants.SuccessCode);
        }