Пример #1
0
        public void Test_Process_Does_Not_Call_MessageClient_If_The_Message_Was_Not_Formatted()
        {
            using (AutoMock autoMock = AutoMock.GetLoose(this.output.Capture()))
            {
                string mockFormatterResult;
                autoMock.Mock <IPipelineMessageFormatter>()
                .Setup(p => p.TryFormat(It.IsAny <JObject>(), out mockFormatterResult))
                .Returns(RequestProcessResult.CreateFailure("some-reason"));

                var processor = autoMock.Create <PipelineFailureGitlabProcessor>();
                var request   = new JObject
                {
                    [GitlabKeys.ObjectKind]       = GitlabKeys.ObjectKindPipeline,
                    [GitlabKeys.ObjectAttributes] = new JObject {
                        [GitlabKeys.Status] = GitlabKeys.StatusFailed
                    }
                };
                RequestProcessResult result = processor.Process(request);

                Assert.False(result.Success);
                Assert.False(result.NoResult);
                Assert.Equal("some-reason", result.Reason);

                autoMock.Mock <IPipelineMessageFormatter>()
                .Verify(p => p.TryFormat(request, out mockFormatterResult), Times.Once);

                autoMock.Mock <IMessageClient>()
                .Verify(p => p.ScheduleDelivery(It.IsAny <string>()), Times.Never);
            }
        }
        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);
        }
Пример #3
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);
        }
Пример #5
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);
        }