Ejemplo n.º 1
0
        internal static UserInfo TryRead(JToken user, JTokenErrors errors)
        {
            if (errors == null)
            {
                throw new ArgumentNullException(nameof(errors));
            }

            UserInfo result = null;

            if (user != null)
            {
                string name     = user.RequireString(GitlabKeys.Name, errors);
                string username = user.RequireString(GitlabKeys.UserName, errors);

                if (!errors.HasAny)
                {
                    result = new UserInfo
                    {
                        Name     = name,
                        UserName = username
                    };
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void Test_TryRead_Should_Read_CommitInfo()
        {
            var data = new JObject
            {
                [GitlabKeys.Id]      = "456788985169456945879",
                [GitlabKeys.Url]     = "http://example.com",
                [GitlabKeys.Message] = "a-message",
                [GitlabKeys.Author]  = new JObject
                {
                    [GitlabKeys.Name]  = "a-name",
                    [GitlabKeys.Email] = "an-email"
                }
            };

            var        errors = new JTokenErrors();
            CommitInfo info   = CommitInfo.TryRead(data, errors);

            Assert.False(errors.HasAny);

            Assert.Equal("4567889", info.Hash);
            Assert.Equal("http://example.com", info.Url);
            Assert.Equal("a-message", info.Message);
            Assert.Equal("a-name", info.AuthorName);
            Assert.Equal("an-email", info.AuthorEmail);
        }
Ejemplo n.º 3
0
        internal static CommitInfo TryRead(JToken commit, JTokenErrors errors)
        {
            if (errors == null)
            {
                throw new ArgumentNullException(nameof(errors));
            }

            CommitInfo result = null;

            if (commit != null)
            {
                string id      = commit.RequireString(GitlabKeys.Id, errors);
                string message = commit.RequireString(GitlabKeys.Message, errors);
                string url     = commit.RequireString(GitlabKeys.Url, errors);

                JToken author      = commit.RequireChild(GitlabKeys.Author, errors);
                string authorName  = author?.RequireString(GitlabKeys.Name, errors);
                string authorEmail = author?.RequireString(GitlabKeys.Email, errors);

                if (!errors.HasAny)
                {
                    result = new CommitInfo
                    {
                        Hash        = id.Substring(0, 7),
                        AuthorName  = authorName,
                        AuthorEmail = authorEmail,
                        Message     = message,
                        Url         = url
                    };
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        public void Test_TryRead_Returns_Null_If_Commit_Is_Null()
        {
            var      errors = new JTokenErrors();
            UserInfo info   = UserInfo.TryRead(null, errors);

            Assert.Null(info);
            Assert.False(errors.HasAny);
        }
        public void Test_RequireString_Returns_An_Existing_String()
        {
            var errors    = new JTokenErrors();
            var container = new JObject
            {
                ["prop1"] = "prop1-value"
            };
            string str1 = container.RequireString("prop1", errors);

            Assert.Equal("prop1-value", str1);
            Assert.False(errors.HasAny);
        }
        public RequestProcessResult Process(JObject request)
        {
            RequestProcessResult result;

            string objectKind = request[GitlabKeys.ObjectKind]?.ToString();

            this.logger.LogTrace("The request object kind was determined as: \"{0}\"", objectKind);

            if (string.Equals(objectKind, GitlabKeys.ObjectKindNote, StringComparison.InvariantCultureIgnoreCase))
            {
                var errors = new JTokenErrors();

                string noteableType = request.RequireChild(GitlabKeys.ObjectAttributes, errors)?.RequireString(GitlabKeys.NoteableType, errors);
                this.logger.LogDebug("The noteable type was determined as \"{0}\"", noteableType);

                if (errors.HasAny)
                {
                    string error = errors.Compose();
                    result = RequestProcessResult.CreateFailure(error);
                    this.logger.LogDebug("The request processing was rejected with message: \"{0}\"", error);
                }
                else
                {
                    if (this.CanHandle(noteableType))
                    {
                        result = this.TryFormat(request, out string message);
                        if (result.Success)
                        {
                            this.logger.LogDebug("Successfully formatted the message: \"{0}\"", message);
                            this.messageClient.ScheduleDelivery(message);
                        }
                        else
                        {
                            this.logger.LogDebug("Could not format the message: {@0}", result);
                        }
                    }
                    else
                    {
                        this.logger.LogDebug("Can not handle the request with the \"{0}\" noteable type", noteableType);
                        result = RequestProcessResult.CreateNoResult();
                    }
                }
            }
            else
            {
                this.logger.LogTrace("Can not handle the request of the \"{0}\" object kind", objectKind);
                result = RequestProcessResult.CreateNoResult();
            }

            return(result);
        }
        public void Test_RequireString_Returns_Errors_For_Plain_Objects_And_Missing_Properties()
        {
            var    errors    = new JTokenErrors();
            var    container = new JObject();
            string str1      = container.RequireString("prop1", errors);

            Assert.Null(str1);
            Assert.True(errors.HasAny);

            const string expected = "1. The json object is missing the field: \"prop1\"";
            string       error    = errors.Compose();

            Assert.Equal(expected, error);
        }
Ejemplo n.º 8
0
        public void Test_TryRead_Should_Not_Read_If_Commit_Lacks_Data(string name, string username)
        {
            var data = new JObject
            {
                [GitlabKeys.Name]     = name,
                [GitlabKeys.UserName] = username
            };

            var      errors = new JTokenErrors();
            UserInfo info   = UserInfo.TryRead(data, errors);

            Assert.Null(info);
            Assert.True(errors.HasAny);
        }
        public void Test_TryRead_Should_Not_Read_If_Project_Lacks_Data(string projectName, string projectUrl, string path)
        {
            var data = new JObject
            {
                [GitlabKeys.Name]              = projectName,
                [GitlabKeys.WebUrl]            = projectUrl,
                [GitlabKeys.PathWithNamespace] = path
            };

            var         errors = new JTokenErrors();
            ProjectInfo info   = ProjectInfo.TryRead(data, errors);

            Assert.Null(info);
            Assert.True(errors.HasAny);
        }
Ejemplo n.º 10
0
        public void Test_TryRead_Should_Not_Read_If_Commit_Lacks_Data(string hash, string url, string msg)
        {
            var data = new JObject
            {
                [GitlabKeys.Id]      = hash,
                [GitlabKeys.Url]     = url,
                [GitlabKeys.Message] = msg
            };

            var        errors = new JTokenErrors();
            CommitInfo info   = CommitInfo.TryRead(data, errors);

            Assert.Null(info);
            Assert.True(errors.HasAny);
        }
        public void Test_TryRead_Should_Read_ProjectInfo()
        {
            var data = new JObject
            {
                [GitlabKeys.Name]              = "a-project-name",
                [GitlabKeys.WebUrl]            = "a-web-url/namespace",
                [GitlabKeys.PathWithNamespace] = "/namespace"
            };

            var         errors = new JTokenErrors();
            ProjectInfo info   = ProjectInfo.TryRead(data, errors);

            Assert.Equal("a-project-name", info.Name);
            Assert.Equal("a-web-url/namespace", info.Url);
            Assert.Equal("a-web-url", info.Server);
        }
Ejemplo n.º 12
0
        public void Test_TryRead_Should_Read_UserInfo()
        {
            var data = new JObject
            {
                [GitlabKeys.Name]     = "name",
                [GitlabKeys.UserName] = "user-name"
            };

            var      errors = new JTokenErrors();
            UserInfo info   = UserInfo.TryRead(data, errors);

            Assert.False(errors.HasAny);

            Assert.Equal("name", info.Name);
            Assert.Equal("user-name", info.UserName);
        }
        public void Test_RequireString_Returns_Errors_For_Nested_Objects()
        {
            var errors    = new JTokenErrors();
            var container = new JObject {
                ["child"] = new JObject()
            };
            string str1 = container["child"].RequireString("prop1", errors);

            Assert.Null(str1);
            Assert.True(errors.HasAny);

            const string expected = "1. The json object is missing the field: \"child.prop1\"";
            string       error    = errors.Compose();

            Assert.Equal(expected, error);
        }
Ejemplo n.º 14
0
        public RequestProcessResult TryFormat(JObject request, out string message)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var errors = new JTokenErrors();

            JToken authorNode     = request.RequireChild(GitlabKeys.User, errors);
            JToken assigneeNode   = request.RequireChild(GitlabKeys.Assignee, errors);
            JToken projectNode    = request.RequireChild(GitlabKeys.Project, errors);
            JToken attributesNode = request.RequireChild(GitlabKeys.ObjectAttributes, errors);

            UserInfo    author      = UserInfo.TryRead(authorNode, errors);
            UserInfo    assignee    = UserInfo.TryRead(assigneeNode, errors);
            ProjectInfo projectInfo = ProjectInfo.TryRead(projectNode, errors);

            string sourceBranch = attributesNode?.RequireString(GitlabKeys.SourceBranch, errors);
            string targetBranch = attributesNode?.RequireString(GitlabKeys.TargetBranch, errors);
            string state        = attributesNode?.RequireString(GitlabKeys.State, errors);
            string title        = attributesNode?.RequireString(GitlabKeys.Title, errors);
            string url          = attributesNode?.RequireString(GitlabKeys.Url, errors);
            string iid          = attributesNode?.RequireString(GitlabKeys.Iid, errors);
            string createdAt    = attributesNode?[GitlabKeys.CreatedAt]?.ToString();
            string updatedAt    = attributesNode?[GitlabKeys.UpdatedAt]?.ToString();

            RequestProcessResult result;

            if (errors.HasAny)
            {
                string error = errors.Compose();
                this.logger.LogDebug("The request processing was rejected with error: \"{0}\"", error);

                message = null;
                result  = RequestProcessResult.CreateFailure(error);
            }
            else
            {
                state   = MergeRequestMessageFormatter.GetMergeRequestState(state, createdAt, updatedAt);
                message = $"[{projectInfo.Name}]({projectInfo.Url}). The merge request [#{iid} {title}]({url}) (branch [{sourceBranch}]({projectInfo.Url}/tree/{sourceBranch}) to [{targetBranch}]({projectInfo.Url}/tree/{targetBranch})) was {state} by [{author.Name}]({projectInfo.Server}{author.UserName}).\r\nAssignee [{assignee.Name}]({projectInfo.Server}{assignee.UserName}).";
                this.logger.LogTrace("Composed the message: \"{0}\"", message);
                result = RequestProcessResult.CreateSuccess();
            }

            return(result);
        }
        public RequestProcessResult TryFormat(JObject request, out string message)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var errors = new JTokenErrors();

            JToken author       = request.RequireChild(GitlabKeys.User, errors);
            JToken project      = request.RequireChild(GitlabKeys.Project, errors);
            JToken attributes   = request.RequireChild(GitlabKeys.ObjectAttributes, errors);
            JToken mergeRequest = request.RequireChild(GitlabKeys.MergeRequest, errors);

            string authorName  = author?.RequireString(GitlabKeys.Name, errors);
            string projectName = project?.RequireString(GitlabKeys.Name, errors);
            string projectUrl  = project?.RequireString(GitlabKeys.WebUrl, errors);

            string snippetText = attributes?.RequireString(GitlabKeys.Note, errors);
            string snippetUrl  = attributes?.RequireString(GitlabKeys.Url, errors);
            string createdAt   = attributes?[GitlabKeys.CreatedAt]?.ToString();
            string updatedAt   = attributes?[GitlabKeys.UpdatedAt]?.ToString();

            string mrTitle = mergeRequest?.RequireString(GitlabKeys.Title, errors);
            string mrIid   = mergeRequest?.RequireString(GitlabKeys.Iid, errors);

            RequestProcessResult result;

            if (errors.HasAny)
            {
                string error = errors.Compose();
                this.logger.LogDebug("The request processing was rejected with error: \"{0}\"", error);

                message = null;
                result  = RequestProcessResult.CreateFailure(error);
            }
            else
            {
                string commentAction = CommentRequestMessageFormatter.GetCommentAction(createdAt, updatedAt);
                message = $"[{projectName}]({projectUrl}). *{authorName}* has [{commentAction}]({snippetUrl}) on the MR [#{mrIid} {mrTitle}]({projectUrl}/merge_requests/{mrIid})!\r\n\r\n{snippetText}";
                this.logger.LogTrace("Composed the message: \"{0}\"", message);
                result = RequestProcessResult.CreateSuccess();
            }

            return(result);
        }
        public void Test_TryRead_Should_Not_Read_If_Could_Not_Retrieve_Server_Url_Plain()
        {
            var data = new JObject
            {
                [GitlabKeys.Name]              = "a-project-name",
                [GitlabKeys.WebUrl]            = "a-web-url",
                [GitlabKeys.PathWithNamespace] = "a-very-long-path"
            };

            var         errors = new JTokenErrors();
            ProjectInfo info   = ProjectInfo.TryRead(data, errors);

            Assert.Null(info);
            Assert.True(errors.HasAny);

            const string expectedError = "1. Can not retrieve the server url out of \"web_url\"";

            Assert.Equal(expectedError, errors.Compose());
        }
Ejemplo n.º 17
0
        internal static ProjectInfo TryRead(JToken project, JTokenErrors errors)
        {
            if (errors == null)
            {
                throw new ArgumentNullException(nameof(errors));
            }

            ProjectInfo result = null;

            if (project != null)
            {
                string projectName       = project.RequireString(GitlabKeys.Name, errors);
                string projectUrl        = project.RequireString(GitlabKeys.WebUrl, errors);
                string pathWithNamespace = project.RequireString(GitlabKeys.PathWithNamespace, errors);

                if (!errors.HasAny)
                {
                    string server = null;
                    if (projectUrl.EndsWith(pathWithNamespace))
                    {
                        server = projectUrl.Substring(0, projectUrl.Length - pathWithNamespace.Length);
                    }
                    else
                    {
                        errors.Add(project.Parent == null
                                       ? $"Can not retrieve the server url out of \"{GitlabKeys.WebUrl}\""
                                       : $"Can not retrieve the server url out of \"{project.Parent.Path}.{GitlabKeys.WebUrl}\"");
                    }

                    if (!errors.HasAny)
                    {
                        result = new ProjectInfo
                        {
                            Name   = projectName,
                            Url    = projectUrl,
                            Server = server
                        };
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 18
0
        public void Test_TryRead_Should_Not_Read_If_Commit_Lacks_Author_Data(string name, string email)
        {
            var data = new JObject
            {
                [GitlabKeys.Id]      = "4567889",
                [GitlabKeys.Url]     = "http://example.com",
                [GitlabKeys.Message] = "a-message",
                [GitlabKeys.Author]  = new JObject
                {
                    [GitlabKeys.Name]  = name,
                    [GitlabKeys.Email] = email
                }
            };

            var        errors = new JTokenErrors();
            CommitInfo info   = CommitInfo.TryRead(data, errors);

            Assert.Null(info);
            Assert.True(errors.HasAny);
        }
        public void Test_JTokenErrors_Functional()
        {
            var errors = new JTokenErrors();

            Assert.False(errors.HasAny);

            errors.Add("error text 1");
            Assert.True(errors.HasAny);

            string text1 = errors.Compose();

            Assert.Equal("1. error text 1", text1);

            errors.Add("error text 2");
            Assert.True(errors.HasAny);

            string text2 = errors.Compose();

            Assert.Equal($"1. error text 1{Environment.NewLine}2. error text 2", text2);
        }
Ejemplo n.º 20
0
        public RequestProcessResult TryFormat(JObject request, out string message)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var errors = new JTokenErrors();

            JToken projectNode    = request.RequireChild(GitlabKeys.Project, errors);
            JToken attributesNode = request.RequireChild(GitlabKeys.ObjectAttributes, errors);
            JToken commitNode     = request.RequireChild(GitlabKeys.Commit, errors);

            ProjectInfo project    = ProjectInfo.TryRead(projectNode, errors);
            string      branchName = attributesNode?.RequireString(GitlabKeys.Ref, errors);
            string      pipelineId = attributesNode?.RequireString(GitlabKeys.Id, errors);
            CommitInfo  commit     = CommitInfo.TryRead(commitNode, errors);

            RequestProcessResult result;

            if (errors.HasAny)
            {
                string error = errors.Compose();
                this.logger.LogDebug("The request processing was rejected with error: \"{0}\"", error);

                message = null;
                result  = RequestProcessResult.CreateFailure(error);
            }
            else
            {
                message = $"[{project.Name}]({project.Url}). The pipeline [{pipelineId}]({project.Url}/pipelines/{pipelineId}) has failed for the branch [{branchName}]({project.Url}/tree/{branchName})!\r\n\r\n"
                          + $"The last commit [{commit.Hash}]({commit.Url}) by *{commit.AuthorName}*\r\n{commit.Message}";

                this.logger.LogTrace("Composed the message: \"{0}\"", message);
                result = RequestProcessResult.CreateSuccess();
            }

            return(result);
        }