示例#1
0
        public async Task Run_adds_issue_links_to_footers(string footerText, int id, string projectPath)
        {
            // ARRANGE
            m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git");

            m_ClientMock.Commits
            .SetupGetAsync()
            .ReturnsTestCommit(sha => $"https://example.com/{sha}");

            m_ClientMock.Issues
            .Setup(x => x.GetAsync(MatchProjectId(projectPath), id))
            .ReturnsAsync(
                new Issue()
            {
                WebUrl = $"https://example.com/{projectPath}/issues/{id}"
            }
                );

            var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            var changeLog = new ApplicationChangeLog()
            {
                GetSingleVersionChangeLog(
                    "1.2.3",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: TestGitIds.Id1, footers: new []
                {
                    new ChangeLogEntryFooter(new CommitMessageFooterName("Issue"), new PlainTextElement(footerText))
                })
                    )
            };

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            var entries = changeLog.ChangeLogs.SelectMany(x => x.AllEntries).ToArray();

            Assert.All(entries, entry =>
            {
                Assert.All(entry.Footers.Where(x => x.Name == new CommitMessageFooterName("Issue")), footer =>
                {
                    var expectedUri = new Uri($"https://example.com/{projectPath}/issues/{id}");

                    var referenceTextElement = Assert.IsType <GitLabReferenceTextElement>(footer.Value);
                    Assert.Equal(expectedUri, referenceTextElement.Uri);
                    Assert.Equal(footerText, referenceTextElement.Text);
                    Assert.Equal(GitLabReferenceType.Issue, referenceTextElement.Reference.Type);
                    Assert.Equal(projectPath, referenceTextElement.Reference.Project.ProjectPath);
                    Assert.Equal(id, referenceTextElement.Reference.Id);
                });
            });

            m_ClientMock.Issues.Verify(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <int>()), Times.Once);
            m_ClientMock.Issues.Verify(x => x.GetAsync(MatchProjectId(projectPath), id), Times.Once);
        }
示例#2
0
        public async Task Run_adds_issue_links_to_footers(string footerText, int id, string projectPath)
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(new[] { new GitRemote("origin", "http://gitlab.com/owner/repo.git") });

            m_CommitsClientMock
            .Setup(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <string>()))
            .Returns(
                (ProjectId projectId, string sha) => Task.FromResult(new Commit()
            {
                WebUrl = $"https://example.com/{sha}"
            })
                );
            m_IssuesClientMock
            .Setup(x => x.GetAsync(MatchProjectId(projectPath), id))
            .Returns(
                Task.FromResult(new Issue()
            {
                WebUrl = $"https://example.com/{projectPath}/issues/{id}"
            })
                );

            var sut = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            var changeLog = new ApplicationChangeLog()
            {
                GetSingleVersionChangeLog(
                    "1.2.3",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: "01", footers: new []
                {
                    new ChangeLogEntryFooter(new CommitMessageFooterName("Issue"), footerText)
                })
                    )
            };

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            var entries = changeLog.ChangeLogs.SelectMany(x => x.AllEntries).ToArray();

            Assert.All(entries, entry =>
            {
                Assert.All(entry.Footers.Where(x => x.Name == new CommitMessageFooterName("Issue")), footer =>
                {
                    Assert.NotNull(footer.WebUri);
                    var expectedUri = new Uri($"https://example.com/{projectPath}/issues/{id}");
                    Assert.Equal(expectedUri, footer.WebUri);
                });
            });

            m_IssuesClientMock.Verify(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <int>()), Times.Once);
            m_IssuesClientMock.Verify(x => x.GetAsync(MatchProjectId(projectPath), id), Times.Once);
        }
示例#3
0
        public async Task Run_adds_a_link_to_all_commits_if_url_can_be_parsed()
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(new[] { new GitRemote("origin", "http://gitlab.com/user/repo.git") });

            m_CommitsClientMock
            .Setup(x => x.GetAsync(MatchProjectId("user/repo"), It.IsAny <string>()))
            .Returns(
                (ProjectId id, string sha) => Task.FromResult(new Commit()
            {
                WebUrl = $"https://example.com/{sha}"
            })
                );

            var sut = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            var changeLog = new ApplicationChangeLog()
            {
                GetSingleVersionChangeLog(
                    "1.2.3",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: "01"),
                    GetChangeLogEntry(summary: "Entry2", commit: "02")
                    ),
                GetSingleVersionChangeLog(
                    "4.5.6",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: "03"),
                    GetChangeLogEntry(summary: "Entry2", commit: "04")
                    )
            };

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            var entries = changeLog.ChangeLogs.SelectMany(x => x.AllEntries).ToArray();

            Assert.All(entries, entry =>
            {
                Assert.NotNull(entry.CommitWebUri);
                var expectedUri = new Uri($"https://example.com/{entry.Commit}");
                Assert.Equal(expectedUri, entry.CommitWebUri);

                m_CommitsClientMock.Verify(x => x.GetAsync(MatchProjectId("user/repo"), entry.Commit.Id), Times.Once);
            });

            m_CommitsClientMock.Verify(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <string>()), Times.Exactly(entries.Length));
        }
示例#4
0
        public async Task Run_does_not_add_link_if_milestone_cannot_be_found(string footerText, string projectPath)
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(new[] { new GitRemote("origin", "http://gitlab.com/owner/repo.git") });

            m_CommitsClientMock
            .Setup(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <string>()))
            .Returns(
                (ProjectId projectId, string sha) => Task.FromResult(new Commit()
            {
                WebUrl = $"https://example.com/{sha}"
            })
                );
            m_ProjectsClientMock
            .Setup(x => x.GetMilestonesAsync(It.IsAny <ProjectId>(), It.IsAny <Action <MilestonesQueryOptions> >()))
            .Throws(new GitLabException(HttpStatusCode.NotFound));

            var sut = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            var changeLog = new ApplicationChangeLog()
            {
                GetSingleVersionChangeLog(
                    "1.2.3",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: "01", footers: new []
                {
                    new ChangeLogEntryFooter(new CommitMessageFooterName("Merge-Request"), footerText)
                })
                    )
            };

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            var entries = changeLog.ChangeLogs.SelectMany(x => x.AllEntries).ToArray();

            Assert.All(entries, entry =>
            {
                Assert.All(entry.Footers.Where(x => x.Name == new CommitMessageFooterName("Milestone")), footer =>
                {
                    Assert.Null(footer.WebUri);
                });
            });

            m_ProjectsClientMock.Verify(x => x.GetMilestonesAsync(It.IsAny <ProjectId>(), It.IsAny <Action <MilestonesQueryOptions> >()), Times.Once);
            m_ProjectsClientMock.Verify(x => x.GetMilestonesAsync(MatchProjectId(projectPath), It.IsAny <Action <MilestonesQueryOptions> >()), Times.Once);
        }
示例#5
0
        public async Task Run_creates_client_through_client_factory(string hostName)
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(new[] { new GitRemote("origin", $"http://{hostName}/owner/repo.git") });

            var sut = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            // ACT
            var result = await sut.RunAsync(new ApplicationChangeLog());

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            m_ClientFactoryMock.Verify(x => x.CreateClient(It.IsAny <string>()), Times.Once);
            m_ClientFactoryMock.Verify(x => x.CreateClient(hostName), Times.Once);
        }
示例#6
0
        public async Task Run_does_nothing_if_remote_url_cannot_be_parsed(string url)
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(new[] { new GitRemote("origin", url) });

            var sut       = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);
            var changeLog = new ApplicationChangeLog();

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Skipped, result);

            m_ClientFactoryMock.Verify(x => x.CreateClient(It.IsAny <string>()), Times.Never);
        }
示例#7
0
        public async Task Run_does_nothing_if_repository_does_not_have_remotes()
        {
            // ARRANGE
            m_RepositoryMock.Setup(x => x.Remotes).Returns(Enumerable.Empty <GitRemote>());

            var sut       = new GitLabLinkTask(m_Logger, m_RepositoryMock.Object, m_ClientFactoryMock.Object);
            var changeLog = new ApplicationChangeLog();

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Skipped, result);

            m_ClientFactoryMock.Verify(x => x.CreateClient(It.IsAny <string>()), Times.Never);
        }
示例#8
0
        public async Task Run_does_not_add_link_if_merge_request_cannot_be_found(string footerText, string projectPath)
        {
            // ARRANGE
            m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git");

            m_ClientMock.Commits
            .SetupGetAsync()
            .ReturnsTestCommit(sha => $"https://example.com/{sha}");

            m_ClientMock.MergeRequests
            .Setup(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <Action <ProjectMergeRequestsQueryOptions> >()))
            .ThrowsNotFound();

            var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object);

            var changeLog = new ApplicationChangeLog()
            {
                GetSingleVersionChangeLog(
                    "1.2.3",
                    null,
                    GetChangeLogEntry(summary: "Entry1", commit: TestGitIds.Id1, footers: new []
                {
                    new ChangeLogEntryFooter(new CommitMessageFooterName("Merge-Request"), new PlainTextElement(footerText))
                })
                    )
            };

            // ACT
            var result = await sut.RunAsync(changeLog);

            // ASSERT
            Assert.Equal(ChangeLogTaskResult.Success, result);

            var entries = changeLog.ChangeLogs.SelectMany(x => x.AllEntries).ToArray();

            Assert.All(entries, entry =>
            {
                Assert.All(entry.Footers.Where(x => x.Name == new CommitMessageFooterName("Merge-Request")), footer =>
                {
                    Assert.False(footer.Value is IWebLinkTextElement, "Footer value should not contain a link");
                });
            });

            m_ClientMock.MergeRequests.Verify(x => x.GetAsync(It.IsAny <ProjectId>(), It.IsAny <Action <ProjectMergeRequestsQueryOptions> >()), Times.Once);
            m_ClientMock.MergeRequests.Verify(x => x.GetAsync(MatchProjectId(projectPath), It.IsAny <Action <ProjectMergeRequestsQueryOptions> >()), Times.Once);
        }