diff --git a/src/ChangeLog.Test/Integrations/GitHub/GitHubLinkTaskTest.cs b/src/ChangeLog.Test/Integrations/GitHub/GitHubLinkTaskTest.cs index 7c82fdc2..fa7cbb68 100644 --- a/src/ChangeLog.Test/Integrations/GitHub/GitHubLinkTaskTest.cs +++ b/src/ChangeLog.Test/Integrations/GitHub/GitHubLinkTaskTest.cs @@ -80,7 +80,7 @@ public void Serialize(IXunitSerializationInfo info) } private readonly ILogger m_Logger; - private readonly ChangeLogConfiguration m_DefaultConfiguration; + private readonly GitHubClientMock m_GithubClientMock; private readonly Mock m_GitHubClientFactoryMock; @@ -88,8 +88,6 @@ public GitHubLinkTaskTest(ITestOutputHelper testOutputHelper) { m_Logger = new XunitLogger(testOutputHelper); - m_DefaultConfiguration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); - m_GithubClientMock = new(); m_GitHubClientFactoryMock = new(MockBehavior.Strict); @@ -105,7 +103,7 @@ public void Logger_must_not_be_null() // ARRANGE // ACT - var ex = Record.Exception(() => new GitHubLinkTask(null!, m_DefaultConfiguration, Mock.Of(MockBehavior.Strict), Mock.Of(MockBehavior.Strict))); + var ex = Record.Exception(() => new GitHubLinkTask(null!, new ChangeLogConfiguration(), Mock.Of(MockBehavior.Strict), Mock.Of(MockBehavior.Strict))); // ASSERT var argumentNullException = Assert.IsType(ex); @@ -131,7 +129,7 @@ public void GitRepository_must_not_be_null() // ARRANGE // ACT - var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, m_DefaultConfiguration, null!, Mock.Of(MockBehavior.Strict))); + var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, new ChangeLogConfiguration(), null!, Mock.Of(MockBehavior.Strict))); // ASSERT var argumentNullException = Assert.IsType(ex); @@ -144,7 +142,7 @@ public void GitHubClientFactoty_must_not_be_null() // ARRANGE // ACT - var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, m_DefaultConfiguration, Mock.Of(MockBehavior.Strict), null!)); + var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, new ChangeLogConfiguration(), Mock.Of(MockBehavior.Strict), null!)); // ASSERT var argumentNullException = Assert.IsType(ex); @@ -155,10 +153,14 @@ public void GitHubClientFactoty_must_not_be_null() public async Task Run_does_nothing_if_repository_does_not_have_remotes() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupEmptyRemotes(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog(); // ACT @@ -169,6 +171,39 @@ public async Task Run_does_nothing_if_repository_does_not_have_remotes() m_GitHubClientFactoryMock.Verify(x => x.CreateClient(It.IsAny()), Times.Never); } + [Theory] + [InlineData(ChangeLogConfiguration.IntegrationProvider.None)] + [InlineData(ChangeLogConfiguration.IntegrationProvider.GitLab)] + public async Task Task_is_skipped_if_GitHub_integration_is_disabled(ChangeLogConfiguration.IntegrationProvider integrationProvider) + { + // ARRANGE + var repoMock = new Mock(MockBehavior.Strict); + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = integrationProvider; + } + + var changeLog = new ApplicationChangeLog() + { + GetSingleVersionChangeLog( + version: "1.2.3", + entries: new [] + { + GetChangeLogEntry() + }) + }; + + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); + + // ACT + var result = await sut.RunAsync(changeLog); + + // ASSERT + Assert.Equal(ChangeLogTaskResult.Skipped, result); + repoMock.Verify(x => x.Remotes, Times.Never); + m_GitHubClientFactoryMock.Verify(x => x.CreateClient(It.IsAny()), Times.Never); + } + [Theory] [InlineData("origin", "not-a-url")] [InlineData("origin", "http://not-a-github-url.com")] @@ -178,7 +213,10 @@ public async Task Run_does_nothing_if_remote_url_cannot_be_parsed(string remoteN { // ARRANGE var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); - configuration.Integrations.GitHub.RemoteName = remoteName; + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + configuration.Integrations.GitHub.RemoteName = remoteName; + } var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes(remoteName, url); @@ -411,7 +449,10 @@ public async Task Run_uses_the_expected_remote_url(GitHubProjectInfoTestCase tes // Configure remote name to use var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); - configuration.Integrations.GitHub = testCase.Configuration; + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + configuration.Integrations.GitHub = testCase.Configuration; + } // Prepare changelog var changeLog = new ApplicationChangeLog() @@ -458,6 +499,12 @@ public async Task Run_uses_the_expected_remote_url(GitHubProjectInfoTestCase tes public async Task Run_adds_issue_links_to_footers(string footerText, int issueNumber, string owner, string repo) { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -469,7 +516,7 @@ public async Task Run_adds_issue_links_to_footers(string footerText, int issueNu .Setup(x => x.Get(owner, repo, issueNumber)) .ReturnsTestIssue(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -515,6 +562,11 @@ public async Task Run_adds_issue_links_to_footers(string footerText, int issueNu public async Task Run_adds_pull_request_links_to_footers(string footerText, int prNumber, string owner, string repo) { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -531,7 +583,7 @@ public async Task Run_adds_pull_request_links_to_footers(string footerText, int .ReturnsTestPullRequest(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -578,6 +630,11 @@ public async Task Run_adds_pull_request_links_to_footers(string footerText, int public async Task Run_ignores_footers_which_cannot_be_parsed(string footerText) { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -585,7 +642,7 @@ public async Task Run_ignores_footers_which_cannot_be_parsed(string footerText) .SetupGet() .ReturnsTestCommit(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -626,6 +683,11 @@ public async Task Run_ignores_footers_which_cannot_be_parsed(string footerText) public async Task Run_does_not_add_a_links_to_footers_if_no_issue_or_pull_request_cannot_be_found(string footerText, string owner, string repo) { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -641,7 +703,7 @@ public async Task Run_does_not_add_a_links_to_footers_if_no_issue_or_pull_reques .SetupGet() .ThrowsNotFound(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -681,10 +743,15 @@ public async Task Run_does_not_add_a_links_to_footers_if_no_issue_or_pull_reques public async Task Run_creates_client_through_client_factory(string hostName) { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", $"http://{hostName}/owner/repo.git"); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); // ACT var result = await sut.RunAsync(new ApplicationChangeLog()); @@ -700,6 +767,11 @@ public async Task Run_creates_client_through_client_factory(string hostName) public async Task Task_fails_if_GitHub_client_throws_an_ApiException() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", $"http://github.com/owner/repo.git"); @@ -707,7 +779,7 @@ public async Task Task_fails_if_GitHub_client_throws_an_ApiException() .SetupGet() .ThrowsAsync(new ApiException()); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -736,6 +808,11 @@ public async Task Task_fails_if_GitHub_client_throws_an_ApiException() public async Task Run_adds_web_links_to_commit_references() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -743,7 +820,7 @@ public async Task Run_adds_web_links_to_commit_references() .Setup(x => x.Get("owner", "repo", It.IsAny())) .ReturnsTestCommit(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -783,6 +860,11 @@ public async Task Run_adds_web_links_to_commit_references() public async Task Run_ignores_commit_references_that_cannot_be_found() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitHub; + } + var repoMock = new Mock(MockBehavior.Strict); repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git"); @@ -790,7 +872,7 @@ public async Task Run_ignores_commit_references_that_cannot_be_found() .Setup(x => x.Get("owner", "repo", It.IsAny())) .ThrowsNotFound(); - var sut = new GitHubLinkTask(m_Logger, m_DefaultConfiguration, repoMock.Object, m_GitHubClientFactoryMock.Object); + var sut = new GitHubLinkTask(m_Logger, configuration, repoMock.Object, m_GitHubClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { diff --git a/src/ChangeLog.Test/Integrations/GitLab/GitLabLinkTaskTest.cs b/src/ChangeLog.Test/Integrations/GitLab/GitLabLinkTaskTest.cs index cd124cf9..db51379c 100644 --- a/src/ChangeLog.Test/Integrations/GitLab/GitLabLinkTaskTest.cs +++ b/src/ChangeLog.Test/Integrations/GitLab/GitLabLinkTaskTest.cs @@ -85,7 +85,6 @@ public void Serialize(IXunitSerializationInfo info) } private readonly ILogger m_Logger; - private readonly ChangeLogConfiguration m_DefaultConfiguration; private readonly Mock m_ClientFactoryMock; private readonly GitLabClientMock m_ClientMock; private readonly Mock m_RepositoryMock; @@ -94,7 +93,6 @@ public void Serialize(IXunitSerializationInfo info) public GitLabLinkTaskTest(ITestOutputHelper testOutputHelper) { m_Logger = new XunitLogger(testOutputHelper); - m_DefaultConfiguration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); m_ClientFactoryMock = new(MockBehavior.Strict); m_ClientMock = new(); m_ClientFactoryMock.Setup(x => x.CreateClient(It.IsAny())).Returns(m_ClientMock.Object); @@ -143,17 +141,54 @@ private bool AssertAction(Action actionToVerify, params Action[] assert return true; } + [Theory] + [InlineData(ChangeLogConfiguration.IntegrationProvider.None)] + [InlineData(ChangeLogConfiguration.IntegrationProvider.GitHub)] + public async Task Task_is_skipped_if_GitLab_integration_is_disabled(ChangeLogConfiguration.IntegrationProvider integrationProvider) + { + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = integrationProvider; + } + + var changeLog = new ApplicationChangeLog() + { + GetSingleVersionChangeLog( + version: "1.2.3", + entries: new [] + { + GetChangeLogEntry() + }) + }; + + + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + + // ACT + var result = await sut.RunAsync(changeLog); + + // ASSERT + Assert.Equal(ChangeLogTaskResult.Skipped, result); + m_RepositoryMock.Verify(x => x.Remotes, Times.Never); + m_ClientFactoryMock.Verify(x => x.CreateClient(It.IsAny()), Times.Never); + } [Fact] public async Task Run_does_nothing_if_repository_does_not_have_remotes() { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupEmptyRemotes(); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog(); - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -167,13 +202,18 @@ public async Task Run_does_nothing_if_repository_does_not_have_remotes() [InlineData("http://not-a-gitlab-url.com")] public async Task Run_does_nothing_if_remote_url_cannot_be_parsed(string url) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", url); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog(); - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -187,12 +227,17 @@ public async Task Run_does_nothing_if_remote_url_cannot_be_parsed(string url) [InlineData("example.com")] public async Task Run_creates_client_through_client_factory(string hostName) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", $"http://{hostName}/owner/repo.git"); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); - // ACT + // ACT var result = await sut.RunAsync(new ApplicationChangeLog()); // ASSERT @@ -208,7 +253,12 @@ public async Task Run_creates_client_through_client_factory(string hostName) [InlineData("anotherOwner/anotherRepo#42", 42, "anotherOwner/anotherRepo")] public async Task Run_adds_issue_links_to_footers(string footerText, int id, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -221,7 +271,7 @@ public async Task Run_adds_issue_links_to_footers(string footerText, int id, str new Issue() { WebUrl = $"https://example.com/{projectPath}/issues/{id}" } ); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -235,7 +285,7 @@ public async Task Run_adds_issue_links_to_footers(string footerText, int id, str ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -269,7 +319,12 @@ public async Task Run_adds_issue_links_to_footers(string footerText, int id, str [InlineData("anotherOwner/anotherRepo#42", 42, "anotherOwner/anotherRepo")] public async Task Run_does_not_add_link_if_issue_cannot_be_found(string footerText, int id, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -280,7 +335,7 @@ public async Task Run_does_not_add_link_if_issue_cannot_be_found(string footerTe .SetupGetAsync() .ThrowsNotFound(); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -294,7 +349,7 @@ public async Task Run_does_not_add_link_if_issue_cannot_be_found(string footerTe ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -321,7 +376,12 @@ public async Task Run_does_not_add_link_if_issue_cannot_be_found(string footerTe [InlineData("anotherOwner/anotherRepo!42", 42, "anotherOwner/anotherRepo")] public async Task Run_adds_merge_request_links_to_footers(string footerText, int id, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -334,7 +394,7 @@ public async Task Run_adds_merge_request_links_to_footers(string footerText, int new List() { new MergeRequest() { WebUrl = $"https://example.com/{projectPath}/merge_requests/{id}" } } ); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -348,7 +408,7 @@ public async Task Run_adds_merge_request_links_to_footers(string footerText, int ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -394,7 +454,12 @@ public async Task Run_adds_merge_request_links_to_footers(string footerText, int [InlineData("anotherOwner/anotherRepo!42", "anotherOwner/anotherRepo")] public async Task Run_does_not_add_link_if_merge_request_cannot_be_found(string footerText, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -405,7 +470,7 @@ public async Task Run_does_not_add_link_if_merge_request_cannot_be_found(string .Setup(x => x.GetAsync(It.IsAny(), It.IsAny>())) .ThrowsNotFound(); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -419,7 +484,7 @@ public async Task Run_does_not_add_link_if_merge_request_cannot_be_found(string ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -446,7 +511,12 @@ public async Task Run_does_not_add_link_if_merge_request_cannot_be_found(string [InlineData("anotherOwner/anotherRepo%42", 42, "anotherOwner/anotherRepo")] public async Task Run_adds_milestone_links_to_footers(string footerText, int id, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -459,7 +529,7 @@ public async Task Run_adds_milestone_links_to_footers(string footerText, int id, new List() { new() { WebUrl = $"https://example.com/{projectPath}/milestones/{id}" } } ); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -473,7 +543,7 @@ public async Task Run_adds_milestone_links_to_footers(string footerText, int id, ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -518,7 +588,12 @@ public async Task Run_adds_milestone_links_to_footers(string footerText, int id, [InlineData("anotherOwner/anotherRepo%42", "anotherOwner/anotherRepo")] public async Task Run_does_not_add_link_if_milestone_cannot_be_found(string footerText, string projectPath) { - // ARRANGE + // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/owner/repo.git"); m_ClientMock.Commits @@ -529,7 +604,7 @@ public async Task Run_does_not_add_link_if_milestone_cannot_be_found(string foot .SetupGetMilestonesAsync() .ThrowsNotFound(); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -543,7 +618,7 @@ public async Task Run_does_not_add_link_if_milestone_cannot_be_found(string foot ) }; - // ACT + // ACT var result = await sut.RunAsync(changeLog); // ASSERT @@ -812,10 +887,14 @@ public async Task Run_uses_the_expected_remote_url(GitLabProjectInfoTestCase tes }) }) }; - var config = ChangeLogConfigurationLoader.GetDefaultConfiguration(); - config.Integrations.GitLab = testCase.Configuration; - var sut = new GitLabLinkTask(m_Logger, config, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + configuration.Integrations.GitLab = testCase.Configuration; + } + + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); // // ACT @@ -838,13 +917,18 @@ public async Task Run_uses_the_expected_remote_url(GitLabProjectInfoTestCase tes public async Task Run_adds_web_links_to_commit_references() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/user/repo.git"); m_ClientMock.Commits .Setup(x => x.GetAsync(MatchProjectId("user/repo"), It.IsAny())) .ReturnsTestCommit(sha => $"https://example.com/commit/{sha}"); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { @@ -884,13 +968,18 @@ public async Task Run_adds_web_links_to_commit_references() public async Task Run_ignores_commit_references_that_cannot_be_found() { // ARRANGE + var configuration = ChangeLogConfigurationLoader.GetDefaultConfiguration(); + { + configuration.Integrations.Provider = ChangeLogConfiguration.IntegrationProvider.GitLab; + } + m_RepositoryMock.SetupRemotes("origin", "http://gitlab.com/user/repo.git"); m_ClientMock.Commits .Setup(x => x.GetAsync(MatchProjectId("user/repo"), It.IsAny())) .ThrowsNotFound(); - var sut = new GitLabLinkTask(m_Logger, m_DefaultConfiguration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); + var sut = new GitLabLinkTask(m_Logger, configuration, m_RepositoryMock.Object, m_ClientFactoryMock.Object); var changeLog = new ApplicationChangeLog() { diff --git a/src/ChangeLog.Test/Integrations/IntegrationsExtensionsTest.cs b/src/ChangeLog.Test/Integrations/IntegrationsExtensionsTest.cs index f6575ee5..d81a47ac 100644 --- a/src/ChangeLog.Test/Integrations/IntegrationsExtensionsTest.cs +++ b/src/ChangeLog.Test/Integrations/IntegrationsExtensionsTest.cs @@ -44,13 +44,11 @@ public void RegisterIntegrations_registers_expected_types() AutofacAssert.CanResolveType(container); } - [Theory] - [CombinatorialData] - public void AddIntegrationTasks_adds_expected_tasks(ChangeLogConfiguration.IntegrationProvider integrationProvider) + [Fact] + public void AddIntegrationTasks_adds_expected_tasks() { // ARRANGE var configuration = new ChangeLogConfiguration(); - configuration.Integrations.Provider = integrationProvider; using var container = BuildContainer(b => b.RegisterInstance(configuration)); @@ -63,16 +61,8 @@ public void AddIntegrationTasks_adds_expected_tasks(ChangeLogConfiguration.Integ pipelineBuilderMock.Object.AddIntegrationTasks(); // ASSERT - pipelineBuilderMock.Verify( - x => x.AddTask(), - integrationProvider == ChangeLogConfiguration.IntegrationProvider.GitHub ? Times.Once() : Times.Never() - ); - pipelineBuilderMock.Verify( - x => x.AddTask(), - integrationProvider == ChangeLogConfiguration.IntegrationProvider.GitLab ? Times.Once() : Times.Never() - ); + pipelineBuilderMock.Verify(x => x.AddTask(), Times.Once); + pipelineBuilderMock.Verify(x => x.AddTask(), Times.Once); } - - } } diff --git a/src/ChangeLog/Integrations/GitHub/GitHubLinkTask.cs b/src/ChangeLog/Integrations/GitHub/GitHubLinkTask.cs index 3131bb4b..bb448de6 100644 --- a/src/ChangeLog/Integrations/GitHub/GitHubLinkTask.cs +++ b/src/ChangeLog/Integrations/GitHub/GitHubLinkTask.cs @@ -18,7 +18,7 @@ namespace Grynwald.ChangeLog.Integrations.GitHub /// [BeforeTask(typeof(RenderTemplateTask))] [AfterTask(typeof(ParseCommitsTask))] - // AddCommitFooterTask must run before eGitHubLinkTask so a web link can be added to the "Commit" footer + // AddCommitFooterTask must run before GitHubLinkTask so a web link can be added to the "Commit" footer [AfterTask(typeof(AddCommitFooterTask))] internal sealed class GitHubLinkTask : IChangeLogTask { @@ -39,6 +39,9 @@ public GitHubLinkTask(ILogger logger, ChangeLogConfiguration con public async Task RunAsync(ApplicationChangeLog changeLog) { + if (m_Configuration.Integrations.Provider != ChangeLogConfiguration.IntegrationProvider.GitHub) + return ChangeLogTaskResult.Skipped; + var projectInfo = GetProjectInfo(); if (projectInfo != null) { diff --git a/src/ChangeLog/Integrations/GitLab/GitLabLinkTask.cs b/src/ChangeLog/Integrations/GitLab/GitLabLinkTask.cs index 78603084..6e0321fb 100644 --- a/src/ChangeLog/Integrations/GitLab/GitLabLinkTask.cs +++ b/src/ChangeLog/Integrations/GitLab/GitLabLinkTask.cs @@ -17,7 +17,7 @@ namespace Grynwald.ChangeLog.Integrations.GitLab { [BeforeTask(typeof(RenderTemplateTask))] [AfterTask(typeof(ParseCommitsTask))] - // AddCommitFooterTask must run before eGitHubLinkTask so a web link can be added to the "Commit" footer + // AddCommitFooterTask must run before GitHubLinkTask so a web link can be added to the "Commit" footer [AfterTask(typeof(AddCommitFooterTask))] internal sealed class GitLabLinkTask : IChangeLogTask { @@ -39,6 +39,9 @@ public GitLabLinkTask(ILogger logger, ChangeLogConfiguration con /// public async Task RunAsync(ApplicationChangeLog changeLog) { + if (m_Configuration.Integrations.Provider != ChangeLogConfiguration.IntegrationProvider.GitLab) + return ChangeLogTaskResult.Skipped; + var projectInfo = GetProjectInfo(); if (projectInfo != null) { diff --git a/src/ChangeLog/Integrations/IntegrationsExtensions.cs b/src/ChangeLog/Integrations/IntegrationsExtensions.cs index 1580baca..dce0a641 100644 --- a/src/ChangeLog/Integrations/IntegrationsExtensions.cs +++ b/src/ChangeLog/Integrations/IntegrationsExtensions.cs @@ -1,5 +1,4 @@ using Autofac; -using Grynwald.ChangeLog.Configuration; using Grynwald.ChangeLog.Integrations.GitHub; using Grynwald.ChangeLog.Integrations.GitLab; using Grynwald.ChangeLog.Pipeline; @@ -18,20 +17,9 @@ public static void RegisterIntegrations(this ContainerBuilder containerBuilder) } - public static IChangeLogPipelineBuilder AddIntegrationTasks(this IChangeLogPipelineBuilder pipelineBuilder) - { - var configuration = pipelineBuilder.Container.Resolve(); - - if (configuration.Integrations.Provider == ChangeLogConfiguration.IntegrationProvider.GitHub) - { - pipelineBuilder = pipelineBuilder.AddTask(); - } - else if (configuration.Integrations.Provider == ChangeLogConfiguration.IntegrationProvider.GitLab) - { - pipelineBuilder = pipelineBuilder.AddTask(); - } - - return pipelineBuilder; - } + public static IChangeLogPipelineBuilder AddIntegrationTasks(this IChangeLogPipelineBuilder pipelineBuilder) => + pipelineBuilder + .AddTask() + .AddTask(); } }