Skip to content

Commit 1298a4f

Browse files
authored
Merge pull request #29 from marcduiker/develop
Added logic to MessageBuilder to handle messages with length > 255
2 parents d6a629b + 1c2399b commit 1298a4f

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using AzureFunctionsUpdates.Builders;
2+
using AzureFunctionsUpdates.UnitTests.TestObjectBuilders;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace AzureFunctionsUpdates.UnitTests.Builders
7+
{
8+
public class MessageBuilderTests
9+
{
10+
[Fact]
11+
public void GivenMessageContentIsTooLong_WhenUpdateMessageIsCreated_ThenTheMessageIsShortened()
12+
{
13+
// Arrange
14+
var release = RepositoryReleaseBuilder.BuildOneWithLongRepoAndReleaseName();
15+
16+
// Act
17+
var message = MessageBuilder.BuildForRelease(release);
18+
19+
// Assert
20+
var messageLengthWithoutUrlPlusFixedShortenedUrl =
21+
message.Content.Length - release.HtmlUrl.Length + MessageBuilder.TwitterShortenedUrlCharacterCount;
22+
messageLengthWithoutUrlPlusFixedShortenedUrl.Should().BeLessOrEqualTo(MessageBuilder.MaxTwitterCharacterCount);
23+
}
24+
25+
[Fact]
26+
public void GivenMessageContentIsNotTooLong_WhenUpdateMessageIsCreated_ThenTheMessageIsNotShortened()
27+
{
28+
// Arrange
29+
var release = RepositoryReleaseBuilder.BuildOneWithShortRepoAndReleaseNameOnAspecificDate();
30+
31+
// Act
32+
var message = MessageBuilder.BuildForRelease(release);
33+
34+
// Assert
35+
var messageLengthWithoutUrlPlusFixedShortenedUrl =
36+
message.Content.Length - release.HtmlUrl.Length + MessageBuilder.TwitterShortenedUrlCharacterCount;
37+
messageLengthWithoutUrlPlusFixedShortenedUrl.Should().BeLessOrEqualTo(MessageBuilder.MaxTwitterCharacterCount);
38+
}
39+
40+
[Fact]
41+
public void GivenARepositoryRelease_WhenUpdateMessageIsCreated_ThenTheMessageContainsTheCorrespondingReleaseInfo()
42+
{
43+
// Arrange
44+
var release = RepositoryReleaseBuilder.BuildOneWithShortRepoAndReleaseNameOnAspecificDate();
45+
46+
// Act
47+
var message = MessageBuilder.BuildForRelease(release);
48+
49+
// Assert
50+
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.
51+
52+
See https://github.com/Azure/azure-functions-host/releases/tag/v2.0.12477 for more information.
53+
54+
#AzureFunctions #Serverless");
55+
}
56+
}
57+
}

src/AzureFunctionsUpdates.UnitTests/TestObjectBuilders/RepositoryReleaseBuilder.cs

+23
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ public static RepositoryRelease BuildOneWithReleaseIdAndDate(string repositoryNa
3434
.With(r => r.ReleaseCreatedAt, releaseDate)
3535
.Create();
3636
}
37+
38+
public static RepositoryRelease BuildOneWithLongRepoAndReleaseName()
39+
{
40+
return _fixture.Build<RepositoryRelease>()
41+
.With(r => r.RepositoryName, "azure-functions-powershell-worker")
42+
.With(r => r.ReleaseName, "v0.1.174 Release of PowerShell worker for Azure Functions")
43+
.With(r => r.TagName, " v0.1.174-preview")
44+
.With( r=> r.HtmlUrl, "https://github.com/Azure/azure-functions-powershell-worker/releases/tag/v0.1.174-preview")
45+
.With(r => r.HashTags, "#AzureFunctions #Serverless #PowerShell")
46+
.Create();
47+
}
48+
49+
public static RepositoryRelease BuildOneWithShortRepoAndReleaseNameOnAspecificDate()
50+
{
51+
return _fixture.Build<RepositoryRelease>()
52+
.With(r => r.RepositoryName, "azure-functions-host")
53+
.With(r => r.ReleaseName, "Azure Functions Runtime 2.0.12477")
54+
.With(r => r.ReleaseCreatedAt, new DateTime(2018, 1, 1))
55+
.With(r => r.TagName, "v2.0.12477")
56+
.With(r=> r.HtmlUrl, "https://github.com/Azure/azure-functions-host/releases/tag/v2.0.12477")
57+
.With(r => r.HashTags, "#AzureFunctions #Serverless")
58+
.Create();
59+
}
3760

3861
public static RepositoryRelease BuildNullRelease(string repositoryName)
3962
{

src/AzureFunctionsUpdates/Builders/MessageBuilder.cs

+32-11
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,51 @@ public static class MessageBuilder
99
{
1010
public static UpdateMessage BuildForRelease(RepositoryRelease release)
1111
{
12+
string moreInfoAndHashTagContent = $"{Environment.NewLine}" +
13+
$"{Environment.NewLine}" +
14+
$"See {release.HtmlUrl} for more information." +
15+
$"{Environment.NewLine}" +
16+
$"{Environment.NewLine}" +
17+
$"{release.HashTags}";
18+
19+
int effectiveMoreInfoAndHashTagContentLength = moreInfoAndHashTagContent.Length - release.HtmlUrl.Length + TwitterShortenedUrlCharacterCount;
20+
int maxReleaseDescriptionLength = MaxTwitterCharacterCount - effectiveMoreInfoAndHashTagContentLength;
21+
1222
string firstLine;
1323
if (string.IsNullOrEmpty(release.ReleaseName) || release.ReleaseName == release.TagName)
1424
{
15-
firstLine = $"A new {release.RepositoryName} release, tagged {release.TagName}, " +
16-
$"is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
25+
firstLine = GetReleaseDescriptionWithoutReleaseName(release);
1726
}
1827
else
1928
{
20-
firstLine = $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), " +
21-
$"is available on GitHub since {release.ReleaseCreatedAt.ToString("D")}.";
29+
firstLine = GetReleaseDescriptionWithReleaseName(release);
30+
if (firstLine.Length > maxReleaseDescriptionLength)
31+
{
32+
firstLine = GetReleaseDescriptionWithoutReleaseName(release);
33+
}
2234
}
2335

2436
var topic = $"{nameof(RepositoryRelease)}|{release.RepositoryName}";
25-
var content = firstLine +
26-
$"{Environment.NewLine}" +
27-
$"{Environment.NewLine}" +
28-
$"See {release.HtmlUrl} for more information." +
29-
$"{Environment.NewLine}" +
30-
$"{Environment.NewLine}" +
31-
$"{release.HashTags}";
37+
var content = firstLine + moreInfoAndHashTagContent;
3238

3339
return new UpdateMessage(topic, content);
40+
41+
string GetReleaseDescriptionWithoutReleaseName(RepositoryRelease repositoryRelease)
42+
{
43+
return $"A new {repositoryRelease.RepositoryName} release, tagged {repositoryRelease.TagName}, " +
44+
$"is available on GitHub since {repositoryRelease.ReleaseCreatedAt:D}.";
45+
}
46+
47+
string GetReleaseDescriptionWithReleaseName(RepositoryRelease repositoryRelease)
48+
{
49+
return $"A new {release.RepositoryName} release, {release.ReleaseName} (tagged {release.TagName}), " +
50+
$"is available on GitHub since {release.ReleaseCreatedAt:D}.";
51+
}
3452
}
3553

54+
public const int TwitterShortenedUrlCharacterCount = 28; // Urls are shortened to 28 characters by Twitter.
55+
public const int MaxTwitterCharacterCount = 255; // Urls are shortened to 28 characters by Twitter.
56+
3657
public static UpdateMessage BuildForPublication(Publication publication)
3758
{
3859
var topic = $"{nameof(Publication)}|{publication.Title}";

0 commit comments

Comments
 (0)