Skip to content

Commit

Permalink
Always add GitHubLinkTask and GitLabLinkTask to the pipeline
Browse files Browse the repository at this point in the history
Instead of conditionally adding the GitHubLinkTask and GitLabLinkTask tasks based on the configuration, always add the tasks but skip the tasks if the corresponding integration is not enabled in the configuration.
  • Loading branch information
ap0llo committed Jun 6, 2022
1 parent 7876280 commit e1d6719
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 83 deletions.
116 changes: 99 additions & 17 deletions src/ChangeLog.Test/Integrations/GitHub/GitHubLinkTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,14 @@ public void Serialize(IXunitSerializationInfo info)
}

private readonly ILogger<GitHubLinkTask> m_Logger;
private readonly ChangeLogConfiguration m_DefaultConfiguration;

private readonly GitHubClientMock m_GithubClientMock;
private readonly Mock<IGitHubClientFactory> m_GitHubClientFactoryMock;

public GitHubLinkTaskTest(ITestOutputHelper testOutputHelper)
{
m_Logger = new XunitLogger<GitHubLinkTask>(testOutputHelper);

m_DefaultConfiguration = ChangeLogConfigurationLoader.GetDefaultConfiguration();

m_GithubClientMock = new();

m_GitHubClientFactoryMock = new(MockBehavior.Strict);
Expand All @@ -105,7 +103,7 @@ public void Logger_must_not_be_null()
// ARRANGE

// ACT
var ex = Record.Exception(() => new GitHubLinkTask(null!, m_DefaultConfiguration, Mock.Of<IGitRepository>(MockBehavior.Strict), Mock.Of<IGitHubClientFactory>(MockBehavior.Strict)));
var ex = Record.Exception(() => new GitHubLinkTask(null!, new ChangeLogConfiguration(), Mock.Of<IGitRepository>(MockBehavior.Strict), Mock.Of<IGitHubClientFactory>(MockBehavior.Strict)));

// ASSERT
var argumentNullException = Assert.IsType<ArgumentNullException>(ex);
Expand All @@ -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<IGitHubClientFactory>(MockBehavior.Strict)));
var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, new ChangeLogConfiguration(), null!, Mock.Of<IGitHubClientFactory>(MockBehavior.Strict)));

// ASSERT
var argumentNullException = Assert.IsType<ArgumentNullException>(ex);
Expand All @@ -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<IGitRepository>(MockBehavior.Strict), null!));
var ex = Record.Exception(() => new GitHubLinkTask(m_Logger, new ChangeLogConfiguration(), Mock.Of<IGitRepository>(MockBehavior.Strict), null!));

// ASSERT
var argumentNullException = Assert.IsType<ArgumentNullException>(ex);
Expand All @@ -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<IGitRepository>(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
Expand All @@ -169,6 +171,39 @@ public async Task Run_does_nothing_if_repository_does_not_have_remotes()
m_GitHubClientFactoryMock.Verify(x => x.CreateClient(It.IsAny<string>()), 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<IGitRepository>(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<string>()), Times.Never);
}

[Theory]
[InlineData("origin", "not-a-url")]
[InlineData("origin", "http://not-a-github-url.com")]
Expand All @@ -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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes(remoteName, url);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

Expand All @@ -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()
{
Expand Down Expand Up @@ -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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

Expand All @@ -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()
{
Expand Down Expand Up @@ -578,14 +630,19 @@ 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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

m_GithubClientMock.Repository.Commit
.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()
{
Expand Down Expand Up @@ -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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

Expand All @@ -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()
{
Expand Down Expand Up @@ -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<IGitRepository>(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());
Expand All @@ -700,14 +767,19 @@ 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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", $"http://github.com/owner/repo.git");

m_GithubClientMock.Repository.Commit
.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()
{
Expand Down Expand Up @@ -736,14 +808,19 @@ 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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

m_GithubClientMock.Repository.Commit
.Setup(x => x.Get("owner", "repo", It.IsAny<string>()))
.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()
{
Expand Down Expand Up @@ -783,14 +860,19 @@ 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<IGitRepository>(MockBehavior.Strict);
repoMock.SetupRemotes("origin", "http://github.com/owner/repo.git");

m_GithubClientMock.Repository.Commit
.Setup(x => x.Get("owner", "repo", It.IsAny<string>()))
.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()
{
Expand Down
Loading

0 comments on commit e1d6719

Please sign in to comment.