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);
        }
        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);
        }
        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);
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 8
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);
        }