Skip to content

Commit

Permalink
Merge pull request #11 from marcduiker/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcduiker committed Mar 20, 2019
2 parents 79509c0 + b17a7a1 commit 6a54fd7
Show file tree
Hide file tree
Showing 36 changed files with 648 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Models.Releases;
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
using FluentAssertions;
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Orchestrations;
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace AzureFunctionsUpdates.UnitTests.Orchestrations
{
public class PublicationUpdateOrchestrationTests
{
[Fact]
public async Task GivenHistoryIsEmptyAndNewPublicationIsAvailable_WhenOrchcestrationIsRun_ThenSaveAndUpdateShouldBeCalled()
{
// Arrange
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
var mockContext = PublicationUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndWithNewWebPublication();
var logger = new Mock<ILogger>();
var publicationUpdateOrchestration = new PublicationUpdateOrchestration();

// Act
await publicationUpdateOrchestration.Run(mockContext.Object, logger.Object);

// Assert
mockContext.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using AzureFunctionsUpdates.Activities;
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Orchestrations;
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -16,7 +15,8 @@ public class ReleaseUpdateOrchestrationTests
public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleasesAreRetrieved_WhenOrchestrationIsRunForTwoRepos_ThenSaveAndPostShouldBeCalled()
{
// Arrange
var mockContext = OrchestrationContextBuilder.BuildWithoutHistoryAndWithGitHubRelease();
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndWithGitHubRelease();
var logger = new Mock<ILogger>();
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();

Expand All @@ -31,7 +31,8 @@ public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleasesAreRet
public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleaseReturnsNullRelease_WhenOrchestrationIsRunForTwoRepos_ThenSaveAndPostShouldBeCalledForTheReleaseWhichWasReturnedFromGitHub()
{
// Arrange
var mockContext = OrchestrationContextBuilder.BuildWithoutHistoryAndGitHubReturnsNullRelease();
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithoutHistoryAndGitHubReturnsNullRelease();
var logger = new Mock<ILogger>();
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();

Expand All @@ -47,7 +48,7 @@ public async Task GivenNoReleasesAreAvailableInHistoryAndNewGithubReleaseReturns
public async Task GivenReleasesAreAvailableInHistoryAndNewGithubReleasesAreTheSame_WhenOrchestrationIsRun_ThenSaveAndPostShouldNotBeCalled()
{
// Arrange
var mockContext = OrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithEqualReleases();
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithEqualReleases();
var logger = new Mock<ILogger>();
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();

Expand All @@ -62,7 +63,8 @@ public async Task GivenReleasesAreAvailableInHistoryAndNewGithubReleasesAreTheSa
public async Task GivenReleasesAreAvailableInHistoryAndOneGithubReleaseIsEqualAndOneIsDifferent_WhenOrchestrationIsRun_ThenSaveAndPostShouldBeCalledOnce()
{
// Arrange
var mockContext = OrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithOneEqualAndOneDifferentRelease();
Environment.SetEnvironmentVariable(Toggles.DoPostUpdateVariableName, "true");
var mockContext = ReleaseUpdateOrchestrationContextBuilder.BuildWithHistoryAndWithGitHubWithOneEqualAndOneDifferentRelease();
var logger = new Mock<ILogger>();
var releaseUpdateOrchestration = new ReleaseUpdateOrchestration();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using AutoFixture;
using AzureFunctionsUpdates.Models.Publications;
using System;

namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
{
public static class PublicationBuilder
{
private static Fixture _fixture = new Fixture();

public static Publication BuildNullPublication(string publicationSourceName)
{
return new NullPublication(publicationSourceName);
}

public static Publication BuildPublicationFromWeb(string publicationSourceName)
{
return _fixture.Build<Publication>()
.With(p => p.PublicationSourceName, publicationSourceName)
.With(p => p.PublicationDate, DateTimeOffset.Now)
.Create();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AutoFixture;
using AzureFunctionsUpdates.Models.Publications;
using System.Collections.Generic;

namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
{
public static class PublicationConfigurationBuilder
{
private static Fixture _fixture = new Fixture();

private static PublicationConfiguration BuildOne(string publicationSourceName)
{
return _fixture.Build<PublicationConfiguration>()
.With(p => p.PublicationSourceName, publicationSourceName)
.Create();
}

public static IReadOnlyList<PublicationConfiguration> BuildListWithOne(string publicationSourceName)
{
return new List<PublicationConfiguration> { BuildOne(publicationSourceName) };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AzureFunctionsUpdates.Activities;
using AzureFunctionsUpdates.Activities.Publications;
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Models.Publications;
using Microsoft.Azure.WebJobs;
using Moq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
{
public static class PublicationUpdateOrchestrationContextBuilder
{
public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithNewWebPublication()
{
var mockContext = new Mock<DurableOrchestrationContextBase>(MockBehavior.Strict);
const string publicationSourceName = "Azure Service updates";

mockContext
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<PublicationConfiguration>>(
nameof(GetPublicationConfigurations),
It.IsAny<RetryOptions>(),
null))
.ReturnsAsync(PublicationConfigurationBuilder.BuildListWithOne(publicationSourceName));

mockContext
.Setup(c => c.CallActivityWithRetryAsync<Publication>(
nameof(GetLatestPublicationFromHistory),
It.IsAny<RetryOptions>(),
It.IsAny<PublicationConfiguration>()))
.ReturnsAsync(PublicationBuilder.BuildNullPublication(publicationSourceName));

mockContext
.Setup(c => c.CallActivityWithRetryAsync<Publication>(
nameof(GetLatestPublicationFromWeb),
It.IsAny<RetryOptions>(),
It.IsAny<PublicationConfiguration>()))
.ReturnsAsync(PublicationBuilder.BuildPublicationFromWeb(publicationSourceName));

mockContext
.Setup(c => c.CallActivityWithRetryAsync(
nameof(SaveLatestPublication),
It.IsAny<RetryOptions>(),
It.IsAny<Publication>()))
.Returns(Task.CompletedTask);

mockContext
.Setup(c => c.CallActivityWithRetryAsync(
nameof(PostUpdate),
It.IsAny<RetryOptions>(),
It.IsAny<UpdateMessage>()))
.Returns(Task.CompletedTask);

return mockContext;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using AzureFunctionsUpdates.Models.Releases;
using AzureFunctionsUpdates.Activities.Releases;

namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
{
public static class OrchestrationContextBuilder
public static class ReleaseUpdateOrchestrationContextBuilder
{
public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGitHubRelease()
{
Expand All @@ -23,7 +25,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGi
// Setup GetRepositoryConfigurations
mockContext
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
nameof(GetConfigurations),
nameof(GetRepositoryConfigurations),
It.IsAny<RetryOptions>(),
null))
.ReturnsAsync(repoConfigurations);
Expand Down Expand Up @@ -78,14 +80,14 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndWithGi
.Setup(c => c.CallActivityWithRetryAsync(
nameof(PostUpdate),
It.IsAny<RetryOptions>(),
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository1Name))))
It.Is<UpdateMessage>(message => message.Topic.Contains(repository1Name))))
.Returns(Task.CompletedTask);

mockContext
.Setup(c => c.CallActivityWithRetryAsync(
nameof(PostUpdate),
It.IsAny<RetryOptions>(),
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository2Name))))
It.Is<UpdateMessage>(message => message.Topic.Contains(repository2Name))))
.Returns(Task.CompletedTask);

return mockContext;
Expand All @@ -104,7 +106,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndGitHub
// Setup GetRepositoryConfigurations
mockContext
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
nameof(GetConfigurations),
nameof(GetRepositoryConfigurations),
It.IsAny<RetryOptions>(),
null))
.ReturnsAsync(repoConfigurations);
Expand Down Expand Up @@ -154,7 +156,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithoutHistoryAndGitHub
.Setup(c => c.CallActivityWithRetryAsync(
nameof(PostUpdate),
It.IsAny<RetryOptions>(),
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository1Name))))
It.Is<UpdateMessage>(message => message.Topic.Contains(repository1Name))))
.Returns(Task.CompletedTask);

return mockContext;
Expand All @@ -175,7 +177,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
// Setup GetRepositoryConfigurations
mockContext
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
nameof(GetConfigurations),
nameof(GetRepositoryConfigurations),
It.IsAny<RetryOptions>(),
null))
.ReturnsAsync(repoConfigurations);
Expand Down Expand Up @@ -230,7 +232,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
// Setup GetRepositoryConfigurations
mockContext
.Setup(c => c.CallActivityWithRetryAsync<IReadOnlyList<RepositoryConfiguration>>(
nameof(GetConfigurations),
nameof(GetRepositoryConfigurations),
It.IsAny<RetryOptions>(),
null))
.ReturnsAsync(repoConfigurations);
Expand Down Expand Up @@ -278,7 +280,7 @@ public static Mock<DurableOrchestrationContextBase> BuildWithHistoryAndWithGitHu
.Setup(c => c.CallActivityWithRetryAsync(
nameof(PostUpdate),
It.IsAny<RetryOptions>(),
It.Is<RepositoryRelease>(r => r.RepositoryName.Equals(repository2Name))))
It.Is<UpdateMessage>(message => message.Topic.Contains(repository2Name))))
.Returns(Task.CompletedTask);

return mockContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoFixture;
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Models.Releases;
using System;
using System.Collections.Generic;

namespace AzureFunctionsUpdates.UnitTests.TestObjectBuilders
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using AutoFixture;
using AzureFunctionsUpdates.Models;
using AzureFunctionsUpdates.Models.Releases;
using System;
using System.Collections.Generic;

Expand All @@ -13,13 +13,15 @@ public static RepositoryRelease BuildOne(string repositoryName)
{
return _fixture.Build<RepositoryRelease>()
.With(r => r.RepositoryName, repositoryName)
.With(r => r.ReleaseCreatedAt, DateTimeOffset.Now)
.Create();
}

public static RepositoryRelease BuildOneWithReleaseId(string repositoryName, int id)
{
return _fixture.Build<RepositoryRelease>()
.With(r => r.RepositoryName, repositoryName)
.With(r => r.ReleaseCreatedAt, DateTimeOffset.Now)
.With(r => r.ReleaseId, id)
.Create();
}
Expand Down
34 changes: 6 additions & 28 deletions src/AzureFunctionsUpdates/Activities/PostUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using AzureFunctionsUpdates.Models;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
using Tweetinvi;
using Tweetinvi.Models;
using UpdateMessage = AzureFunctionsUpdates.Models.UpdateMessage;

namespace AzureFunctionsUpdates.Activities
{
Expand All @@ -16,39 +16,17 @@ public class PostUpdate

[FunctionName(nameof(PostUpdate))]
public void Run(
[ActivityTrigger] RepositoryRelease newRelease,
[ActivityTrigger] UpdateMessage message,
ILogger logger)
{
logger.LogInformation($"Started {nameof(PostUpdate)} for { newRelease.RepositoryName} { newRelease.ReleaseName}.");
logger.LogInformation($"Started {nameof(PostUpdate)} for { message.Topic}.");

var creds = new TwitterCredentials(consumerApiKey, consumerApiSecret, accessToken, accessTokenSecret);
var tweetMessage = CreateMessage(newRelease);


var tweet = Auth.ExecuteOperationWithCredentials(creds, () =>
{
return Tweet.PublishTweet(tweetMessage);
return Tweet.PublishTweet(message.Content);
});
}

private static string CreateMessage(RepositoryRelease release)
{
string firstLine;
if (string.IsNullOrEmpty(release.ReleaseName) || release.ReleaseName == release.TagName)
{
firstLine = $"A new {release.RepositoryName} release, tagged {release.TagName}, is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
}
else
{
firstLine = $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
}

return firstLine +
$"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"See {release.HtmlUrl} for more information." +
$"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"{release.HashTags}";
}
}
}

0 comments on commit 6a54fd7

Please sign in to comment.