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

                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.True(result.Success);
                Assert.False(result.NoResult);
                Assert.Null(result.Reason);

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

                autoMock.Mock <IMessageClient>()
                .Verify(p => p.ScheduleDelivery(mockFormatterResult), Times.Once);
            }
        }
        public void Test_ProcessRequest_Returns_The_First_Successful_Result()
        {
            var m1 = new Mock <IGitlabProcessor>();

            m1.Setup(p => p.Process(It.IsAny <JObject>()))
            .Returns(RequestProcessResult.CreateNoResult());  //no result
            var m2 = new Mock <IGitlabProcessor>();

            m2.Setup(p => p.Process(It.IsAny <JObject>()))
            .Returns(RequestProcessResult.CreateSuccess());  //success
            var m3 = new Mock <IGitlabProcessor>();

            m3.Setup(p => p.Process(It.IsAny <JObject>()))
            .Returns(RequestProcessResult.CreateNoResult());  //should not have been called

            using (AutoMock mock = AutoMock.GetLoose(builder =>
            {
                builder.RegisterXUnit(this.output);
                builder.RegisterInstance(m1.Object);
                builder.RegisterInstance(m2.Object);
                builder.RegisterInstance(m3.Object);
            }))
            {
                var service = mock.Create <GitlabProcessServiceImpl>();

                JObject request             = JObject.Parse("{prop:'value'}");
                RequestProcessResult result = service.ProcessRequest(request);

                Assert.True(result.Success);

                m1.Verify(p => p.Process(request), Times.Once);
                m2.Verify(p => p.Process(request), Times.Once);
                m3.Verify(p => p.Process(request), Times.Never);
            }
        }
Пример #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);
        }
        public void Test_Process_Calls_MessageClient_If_The_Message_Was_Formatted()
        {
            using (AutoMock autoMock = AutoMock.GetLoose(this.output.Capture()))
            {
                autoMock.Mock <IMockCanHandle>()
                .Setup(p => p.CanHandle(It.IsAny <string>()))
                .Returns(true);

                string mockFormatterResult = "some-result";
                autoMock.Mock <IMockFormatter>()
                .Setup(p => p.TryFormat(It.IsAny <JObject>(), out mockFormatterResult))
                .Returns(RequestProcessResult.CreateSuccess());

                var processor = autoMock.Create <MockGitlabProcessor>();
                var request   = new JObject
                {
                    [GitlabKeys.ObjectKind]       = GitlabKeys.ObjectKindNote,
                    [GitlabKeys.ObjectAttributes] = new JObject {
                        [GitlabKeys.NoteableType] = GitlabKeys.NoteableTypeSnippet
                    }
                };
                RequestProcessResult result = processor.Process(request);

                Assert.True(result.Success);
                Assert.False(result.NoResult);
                Assert.Null(result.Reason);

                autoMock.Mock <IMockCanHandle>()
                .Verify(p => p.CanHandle(GitlabKeys.NoteableTypeSnippet), Times.Once);

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

                autoMock.Mock <IMessageClient>()
                .Verify(p => p.ScheduleDelivery(mockFormatterResult), Times.Once);
            }
        }