public void Ctor_Property_Expectations()
        {
            var sut = new BranchConfiguration();

            AssertUtils.AssertGetSetProperty(sut, nameof(sut.Release), x => x.Should().BeEmpty(), (List <string>)null);
            AssertUtils.AssertGetSetProperty(sut, nameof(sut.Overrides), x => x.Should().BeEmpty(), (List <BranchOverrideConfiguration>)null);
        }
        public void Ctor_SetsDefaults()
        {
            // Arrange / Act
            var sut = new BranchConfiguration();

            // Assert
            sut.Label.Should().BeNull();
            sut.Match.Should().BeEmpty();
            sut.Metadata.Should().BeNull();
        }
Example #3
0
        private void GenerateOutputVariables(Results result, Dictionary <string, string> paramMatch, BranchConfiguration branchConfig)
        {
            // Handle output variables
            var gitInfo   = new Dictionary <string, string>();
            var paramHead = new Dictionary <string, string>();
            var paramVS   = new Dictionary <string, string>();

            gitInfo.Add("BranchName", result.GitInfo.BranchName);
            gitInfo.Add("ShortBranchName", result.GitInfo.ShortBranchName);
            gitInfo.Add("Path", result.GitInfo.Path);

            paramHead.Add("Author", result.GitInfo.LastAuthor);
            paramHead.Add("Date", result.GitInfo.LastCommitDate.ToString(CultureInfo.InvariantCulture));
            paramHead.Add("Sha", result.GitInfo.Head.Sha);
            paramHead.Add("Message", result.GitInfo.Head.Message);
            paramHead.Add("MessageShort", result.GitInfo.Head.MessageShort);

            paramVS.Add("CommitAuthor", result.VersionSource.Commit.Author);
            paramVS.Add("CommitDateTime", result.VersionSource.Commit.CommitDate.ToString(CultureInfo.InvariantCulture));
            paramVS.Add("CommitSha", result.VersionSource.Commit.Sha);
            paramVS.Add("CommitMessage", result.VersionSource.Commit.Message);
            paramVS.Add("CommitMessageShort", result.VersionSource.Commit.MessageShort);
            paramVS.Add("Message", result.VersionSource.Message);
            paramVS.Add("MessageShort", result.VersionSource.MessageShort);

            foreach (var output in branchConfig.Output)
            {
                var inputStream       = new AntlrInputStream(output.Value);
                var lexer             = new OutputLexer(inputStream);
                var commonTokenStream = new CommonTokenStream(lexer);
                var parser            = new OutputParser(commonTokenStream);

                parser.RemoveErrorListeners();
                parser.AddErrorListener(new OutputErrorListener()); // add ours

                var visitor     = new OutputVisitor(output.Key, output.Value, _paramArgs, gitInfo, paramHead, paramMatch, result.Output, paramVS);
                var parseOutput = visitor.Visit(parser.start());

                result.Output.Add(output.Key, parseOutput);
            }

            // Strip away temporary items, unless we are in diagnostic mode
            var res = new Dictionary <string, string>();

            foreach (var output in result.Output)
            {
                if (output.Key.StartsWith("~"))
                {
                    Logger.Debug($"Stripping temporary variable: {output.Key}='{output.Value}'");
                    continue;
                }
                res.Add(output.Key, output.Value);
            }

            result.Output = res;
        }
Example #4
0
        private static void ExecuteActions(Dictionary <string, string> paramMatch_, List <Commit> activeCommits, BranchConfiguration branchConfig)
        {
            // Find actions by iterating from oldest and to newest, executing actions per commit as we go along.
            // The oldest item is the version-source, and should not execute any actions.
            Dictionary <string, string> actions = null;

            using (LogProvider.OpenNestedContext("Execute actions"))
            {
                for (var i = activeCommits.Count() - 2; i >= 0; i--)
                {
                    var c = activeCommits[i];

                    if (c.IsMerge)
                    {
                        // Find OnMerge actions
                        // if OnMerge.Key has entry that matches this merge's from (or fallback *) then add actions
                        if (!string.IsNullOrWhiteSpace(c.FromBranchConfigName) &&
                            branchConfig.OnMerge.ContainsKey(c.FromBranchConfigName))
                        {
                            actions = branchConfig.OnMerge[c.FromBranchConfigName].ToDictionary(a => a.Key, a => a.Value);
                        }
                        else
                        {
                            actions = branchConfig.OnMerge.ContainsKey("*")
                                          ? branchConfig.OnMerge["*"].ToDictionary(a => a.Key, a => a.Value)
                                          : null;
                        }
                    }
                    else
                    {
                        if (branchConfig.OnCommit.Any())
                        {
                            // Add any OnCommit actions
                            actions = branchConfig.OnCommit.ToDictionary(a => a.Key, a => a.Value);
                        }
                    }

                    if (actions == null)
                    {
                        continue;
                    }

                    // Execute actions
                    foreach (var action in actions)
                    {
                        using (LogProvider.OpenNestedContext($"Handling action '{action.Key}': '{action.Value}'"))
                        {
                            // Match the action
                            var match = Regex.Match(action.Value, @"^(?<Op>[+-=])(?<Value>\d+)$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                            if (match.Success)
                            {
                                var op    = match.Groups["Op"].Value;
                                var value = match.Groups["Value"].Value;
                                if (op == "=")
                                {
                                    Logger.Debug($"Setting '{action.Key}' => {value}");
                                    paramMatch_[action.Key] = value;
                                }
                                else
                                {
                                    var temp = 0;
                                    if (paramMatch_.ContainsKey(action.Key))
                                    {
                                        temp = int.Parse(paramMatch_[action.Key]);
                                    }
                                    var intValue = int.Parse(match.Value) * (op == "-" ? -1 : 1);
                                    temp = temp + intValue;
                                    Logger.Debug($"Setting '{action.Key}' {op}= {value} => {temp}");
                                    paramMatch_[action.Key] = temp.ToString();
                                }
                            }
                            else
                            {
                                Logger.Error("The action was not understood. Should match '<[+-=]><number>'.");
                                throw new ArgumentException("The action was not understood. Should match '<[+-=]><number>'.");
                            }
                        }
                    }
                }
            }
        }