コード例 #1
0
        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);
        }
コード例 #2
0
        public SetRepositoryMergePoliciesPopUp(string path,
                                               ILogger logger,
                                               string repository,
                                               string branch,
                                               List <MergePolicy> mergePolicies,
                                               IEnumerable <string> availableMergePolicyHelp)
            : base(path)
        {
            _logger   = logger;
            _yamlData = new RepositoryPoliciesData
            {
                Repository = GetCurrentSettingForDisplay(repository, "<required>", false),
                Branch     = GetCurrentSettingForDisplay(branch, "<required>", false),
            };
            _yamlData.MergePolicies = MergePoliciesPopUpHelpers.ConvertMergePolicies(mergePolicies);

            ISerializer serializer = new SerializerBuilder().Build();
            string      yaml       = serializer.Serialize(_yamlData);

            string[] lines = yaml.Split(Environment.NewLine);

            // Initialize line contents.  Augment the input lines with suggestions and explanation
            Contents = new Collection <Line>(new List <Line>
            {
                new Line("Use this form to set repository auto-merge policies for batchable subscriptions.", true),
                new Line("Batchable subscriptions share merge policies for all subscriptions that target the same repo and branch.", true),
                new Line("If the branch has at least one merge policy and a PR satisfies that merge policy, the PR is automatically merged.", true),
                new Line("", true),
                new Line("Fill out the following form. Suggested values for merge policies are shown below.", true),
                new Line()
            });
            foreach (string line in lines)
            {
                Contents.Add(new Line(line));
            }
            // Add helper comments
            Contents.Add(new Line("Available Merge Policies", true));
            foreach (string mergeHelp in availableMergePolicyHelp)
            {
                Contents.Add(new Line($"  {mergeHelp}", true));
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public AddSubscriptionPopUp(string path,
                                    ILogger logger,
                                    string channel,
                                    string sourceRepository,
                                    string targetRepository,
                                    string targetBranch,
                                    string updateFrequency,
                                    bool batchable,
                                    List <MergePolicy> mergePolicies,
                                    IEnumerable <string> suggestedChannels,
                                    IEnumerable <string> suggestedRepositories,
                                    IEnumerable <string> availableUpdateFrequencies,
                                    IEnumerable <string> availableMergePolicyHelp,
                                    string failureNotificationTags)
            : base(path)
        {
            _logger   = logger;
            _yamlData = new SubscriptionData
            {
                Channel                 = GetCurrentSettingForDisplay(channel, "<required>", false),
                SourceRepository        = GetCurrentSettingForDisplay(sourceRepository, "<required>", false),
                TargetRepository        = GetCurrentSettingForDisplay(targetRepository, "<required>", false),
                TargetBranch            = GetCurrentSettingForDisplay(targetBranch, "<required>", false),
                UpdateFrequency         = GetCurrentSettingForDisplay(updateFrequency, $"<'{string.Join("', '", Constants.AvailableFrequencies)}'>", false),
                Batchable               = GetCurrentSettingForDisplay(batchable.ToString(), batchable.ToString(), false),
                MergePolicies           = MergePoliciesPopUpHelpers.ConvertMergePolicies(mergePolicies),
                FailureNotificationTags = failureNotificationTags
            };

            ISerializer serializer = new SerializerBuilder().Build();
            string      yaml       = serializer.Serialize(_yamlData);

            string[] lines = yaml.Split(Environment.NewLine);

            // Initialize line contents.  Augment the input lines with suggestions and explanation
            Contents = new Collection <Line>(new List <Line>
            {
                new Line("Use this form to create a new subscription.", true),
                new Line("A subscription maps a build of a source repository that has been applied to a specific channel", true),
                new Line("onto a specific branch in a target repository.  The subscription has a trigger (update frequency)", true),
                new Line("and merge policy. If a subscription is batchable, no merge policy should be provided, and the", true),
                new Line("set-repository-policies command should be used instead to set policies at the repository and branch level. ", true),
                new Line("For non-batched subscriptions, providing a list of semicolon-delineated GitHub tags will tag these", true),
                new Line("logins when monitoring the pull requests, once one or more policy checks fail.", true),
                new Line("For additional information about subscriptions, please see", true),
                new Line("https://github.com/dotnet/arcade/blob/main/Documentation/BranchesChannelsAndSubscriptions.md", true),
                new Line("", true),
                new Line("Fill out the following form.  Suggested values for fields are shown below.", true),
                new Line()
            });
            foreach (string line in lines)
            {
                Contents.Add(new Line(line));
            }
            // Add helper comments
            Contents.Add(new Line($"Suggested repository URLs for '{SubscriptionData.sourceRepoElement}' or '{SubscriptionData.targetRepoElement}':", true));
            foreach (string suggestedRepo in suggestedRepositories)
            {
                Contents.Add(new Line($"  {suggestedRepo}", true));
            }
            Contents.Add(new Line("", true));
            Contents.Add(new Line("Suggested Channels", true));
            foreach (string suggestedChannel in suggestedChannels)
            {
                Contents.Add(new Line($"  {suggestedChannel}", true));
            }
            Contents.Add(new Line("", true));
            Contents.Add(new Line("Available Merge Policies", true));
            foreach (string mergeHelp in availableMergePolicyHelp)
            {
                Contents.Add(new Line($"  {mergeHelp}", true));
            }
        }
コード例 #5
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);
        }
コード例 #6
0
        public UpdateSubscriptionPopUp(string path,
                                       ILogger logger,
                                       Subscription subscription,
                                       IEnumerable <string> suggestedChannels,
                                       IEnumerable <string> suggestedRepositories,
                                       IEnumerable <string> availableUpdateFrequencies,
                                       IEnumerable <string> availableMergePolicyHelp)
            : base(path)
        {
            _logger = logger;

            _yamlData = new SubscriptionData
            {
                Id               = GetCurrentSettingForDisplay(subscription.Id.ToString(), subscription.Id.ToString(), false),
                Channel          = GetCurrentSettingForDisplay(subscription.Channel.Name, subscription.Channel.Name, false),
                SourceRepository = GetCurrentSettingForDisplay(subscription.SourceRepository, subscription.SourceRepository, false),
                Batchable        = GetCurrentSettingForDisplay(subscription.Policy.Batchable.ToString(), subscription.Policy.Batchable.ToString(), false),
                UpdateFrequency  = GetCurrentSettingForDisplay(subscription.Policy.UpdateFrequency.ToString(), subscription.Policy.UpdateFrequency.ToString(), false),
                Enabled          = GetCurrentSettingForDisplay(subscription.Enabled.ToString(), subscription.Enabled.ToString(), false),
            };

            _yamlData.MergePolicies = MergePoliciesPopUpHelpers.ConvertMergePolicies(subscription.Policy.MergePolicies);

            ISerializer serializer = new SerializerBuilder().Build();

            string yaml = serializer.Serialize(_yamlData);

            string[] lines = yaml.Split(Environment.NewLine);

            // Initialize line contents.  Augment the input lines with suggestions and explanation
            Contents = new Collection <Line>(new List <Line>
            {
                new Line($"Use this form to update the values of subscription '{subscription.Id}'.", true),
                new Line($"Note that if you are setting 'Is batchable' to true you need to remove all Merge Policies.", true),
                new Line()
            });

            foreach (string line in lines)
            {
                Contents.Add(new Line(line));
            }

            // Add helper comments
            Contents.Add(new Line($"Suggested repository URLs for '{SubscriptionData.sourceRepoElement}':", true));

            foreach (string suggestedRepo in suggestedRepositories)
            {
                Contents.Add(new Line($"  {suggestedRepo}", true));
            }

            Contents.Add(new Line("", true));
            Contents.Add(new Line("Suggested Channels", true));

            foreach (string suggestedChannel in suggestedChannels)
            {
                Contents.Add(new Line($"  {suggestedChannel}", true));
            }

            Contents.Add(new Line("", true));
            Contents.Add(new Line("Available Merge Policies", true));

            foreach (string mergeHelp in availableMergePolicyHelp)
            {
                Contents.Add(new Line($"  {mergeHelp}", true));
            }
        }