Exemple #1
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);
        }
Exemple #2
0
        public void Test_TryRead_Returns_Null_If_Commit_Is_Null()
        {
            var        errors = new JTokenErrors();
            CommitInfo info   = CommitInfo.TryRead(null, errors);

            Assert.Null(info);
            Assert.False(errors.HasAny);
        }
Exemple #3
0
        public void Test_TryRead_Should_Throw_If_Called_With_Illegal_Args()
        {
            void Caller() => CommitInfo.TryRead(new JObject(), null);

            var ex = Assert.Throws <ArgumentNullException>((Action)Caller);

            Assert.Equal("errors", ex.ParamName);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
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);
        }