Skip to content

Commit

Permalink
Merge pull request #29 from marcduiker/develop
Browse files Browse the repository at this point in the history
Added logic to MessageBuilder to handle messages with length > 255
  • Loading branch information
marcduiker committed Sep 18, 2019
2 parents d6a629b + 1c2399b commit 1298a4f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AzureFunctionsUpdates.Builders;
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
using FluentAssertions;
using Xunit;

namespace AzureFunctionsUpdates.UnitTests.Builders
{
public class MessageBuilderTests
{
[Fact]
public void GivenMessageContentIsTooLong_WhenUpdateMessageIsCreated_ThenTheMessageIsShortened()
{
// Arrange
var release = RepositoryReleaseBuilder.BuildOneWithLongRepoAndReleaseName();

// Act
var message = MessageBuilder.BuildForRelease(release);

// Assert
var messageLengthWithoutUrlPlusFixedShortenedUrl =
message.Content.Length - release.HtmlUrl.Length + MessageBuilder.TwitterShortenedUrlCharacterCount;
messageLengthWithoutUrlPlusFixedShortenedUrl.Should().BeLessOrEqualTo(MessageBuilder.MaxTwitterCharacterCount);
}

[Fact]
public void GivenMessageContentIsNotTooLong_WhenUpdateMessageIsCreated_ThenTheMessageIsNotShortened()
{
// Arrange
var release = RepositoryReleaseBuilder.BuildOneWithShortRepoAndReleaseNameOnAspecificDate();

// Act
var message = MessageBuilder.BuildForRelease(release);

// Assert
var messageLengthWithoutUrlPlusFixedShortenedUrl =
message.Content.Length - release.HtmlUrl.Length + MessageBuilder.TwitterShortenedUrlCharacterCount;
messageLengthWithoutUrlPlusFixedShortenedUrl.Should().BeLessOrEqualTo(MessageBuilder.MaxTwitterCharacterCount);
}

[Fact]
public void GivenARepositoryRelease_WhenUpdateMessageIsCreated_ThenTheMessageContainsTheCorrespondingReleaseInfo()
{
// Arrange
var release = RepositoryReleaseBuilder.BuildOneWithShortRepoAndReleaseNameOnAspecificDate();

// Act
var message = MessageBuilder.BuildForRelease(release);

// Assert
message.Content.Should().Be(@"A new azure-functions-host release, Azure Functions Runtime 2.0.12477 (tagged v2.0.12477), is available on GitHub since Monday, January 1, 2018.
See https://github.com/Azure/azure-functions-host/releases/tag/v2.0.12477 for more information.
#AzureFunctions #Serverless");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ public static RepositoryRelease BuildOneWithReleaseIdAndDate(string repositoryNa
.With(r => r.ReleaseCreatedAt, releaseDate)
.Create();
}

public static RepositoryRelease BuildOneWithLongRepoAndReleaseName()
{
return _fixture.Build<RepositoryRelease>()
.With(r => r.RepositoryName, "azure-functions-powershell-worker")
.With(r => r.ReleaseName, "v0.1.174 Release of PowerShell worker for Azure Functions")
.With(r => r.TagName, " v0.1.174-preview")
.With( r=> r.HtmlUrl, "https://github.com/Azure/azure-functions-powershell-worker/releases/tag/v0.1.174-preview")
.With(r => r.HashTags, "#AzureFunctions #Serverless #PowerShell")
.Create();
}

public static RepositoryRelease BuildOneWithShortRepoAndReleaseNameOnAspecificDate()
{
return _fixture.Build<RepositoryRelease>()
.With(r => r.RepositoryName, "azure-functions-host")
.With(r => r.ReleaseName, "Azure Functions Runtime 2.0.12477")
.With(r => r.ReleaseCreatedAt, new DateTime(2018, 1, 1))
.With(r => r.TagName, "v2.0.12477")
.With(r=> r.HtmlUrl, "https://github.com/Azure/azure-functions-host/releases/tag/v2.0.12477")
.With(r => r.HashTags, "#AzureFunctions #Serverless")
.Create();
}

public static RepositoryRelease BuildNullRelease(string repositoryName)
{
Expand Down
43 changes: 32 additions & 11 deletions src/AzureFunctionsUpdates/Builders/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,51 @@ public static class MessageBuilder
{
public static UpdateMessage BuildForRelease(RepositoryRelease release)
{
string moreInfoAndHashTagContent = $"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"See {release.HtmlUrl} for more information." +
$"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"{release.HashTags}";

int effectiveMoreInfoAndHashTagContentLength = moreInfoAndHashTagContent.Length - release.HtmlUrl.Length + TwitterShortenedUrlCharacterCount;
int maxReleaseDescriptionLength = MaxTwitterCharacterCount - effectiveMoreInfoAndHashTagContentLength;

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")}.";
firstLine = GetReleaseDescriptionWithoutReleaseName(release);
}
else
{
firstLine = $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), " +
$"is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
firstLine = GetReleaseDescriptionWithReleaseName(release);
if (firstLine.Length > maxReleaseDescriptionLength)
{
firstLine = GetReleaseDescriptionWithoutReleaseName(release);
}
}

var topic = $"{nameof(RepositoryRelease)}|{release.RepositoryName}";
var content = firstLine +
$"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"See {release.HtmlUrl} for more information." +
$"{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"{release.HashTags}";
var content = firstLine + moreInfoAndHashTagContent;

return new UpdateMessage(topic, content);

string GetReleaseDescriptionWithoutReleaseName(RepositoryRelease repositoryRelease)
{
return $"A new {repositoryRelease.RepositoryName} release, tagged {repositoryRelease.TagName}, " +
$"is available on GitHub since {repositoryRelease.ReleaseCreatedAt:D}.";
}

string GetReleaseDescriptionWithReleaseName(RepositoryRelease repositoryRelease)
{
return $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), " +
$"is available on GitHub since {release.ReleaseCreatedAt:D}.";
}
}

public const int TwitterShortenedUrlCharacterCount = 28; // Urls are shortened to 28 characters by Twitter.
public const int MaxTwitterCharacterCount = 255; // Urls are shortened to 28 characters by Twitter.

public static UpdateMessage BuildForPublication(Publication publication)
{
var topic = $"{nameof(Publication)}|{publication.Title}";
Expand Down

0 comments on commit 1298a4f

Please sign in to comment.