Skip to content

Commit

Permalink
Merge branch 'master' into bellatrix-visual-regression-tracker-integr…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
MiriamKyoseva committed Jul 9, 2024
2 parents 63ead42 + c719550 commit 6e0c0f7
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 0 deletions.
35 changes: 35 additions & 0 deletions BellatrixTestFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.Playwright.Gettin
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.VisualRegressionTracker", "src\Bellatrix.VisualRegressionTracker\Bellatrix.VisualRegressionTracker.csproj", "{D4FFE66D-72FB-4B39-AB87-DF7DEB8CB093}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bellatrix.SMS", "src\Bellatrix.SMS\Bellatrix.SMS.csproj", "{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bellatrix.Plugins.Jira.Zephyr", "src\Bellatrix.Plugins.Jira.Zephyr\Bellatrix.Plugins.Jira.Zephyr.csproj", "{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bellatrix.Playwright.Tests", "src\Bellatrix.Playwright.Tests\Bellatrix.Playwright.Tests.csproj", "{FC40FB5D-EABC-4A20-9695-3D52C0093FBC}"
Expand Down Expand Up @@ -1204,6 +1206,38 @@ Global
{D4FFE66D-72FB-4B39-AB87-DF7DEB8CB093}.STAGE|x64.Build.0 = Debug|Any CPU
{D4FFE66D-72FB-4B39-AB87-DF7DEB8CB093}.STAGE|x86.ActiveCfg = Debug|Any CPU
{D4FFE66D-72FB-4B39-AB87-DF7DEB8CB093}.STAGE|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.DEBUG|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.QA|x86.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|Any CPU.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|ARM.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|ARM.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x64.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x64.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x86.ActiveCfg = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.Release|x86.Build.0 = Release|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|Any CPU.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|Any CPU.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|ARM.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|ARM.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x64.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x64.Build.0 = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x86.ActiveCfg = Debug|Any CPU
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C}.STAGE|x86.Build.0 = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|Any CPU.ActiveCfg = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|Any CPU.Build.0 = Debug|Any CPU
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB}.DEBUG|ARM.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -1308,6 +1342,7 @@ Global
{08D46A7C-4BCC-4813-B20F-C7A9039621C9} = {1DF0F472-E4CA-4EFD-9459-F5B90E2E8E67}
{EACDDFC5-A313-4981-9FFB-F41639713BB1} = {74747D87-4B09-479B-B2FC-63F2CCAC4201}
{D4FFE66D-72FB-4B39-AB87-DF7DEB8CB093} = {E8AA67CB-09C6-4E4B-AFF1-0D5875903C34}
{2E7AA3E4-36F3-4C11-9C3F-C767DF29814C} = {E8AA67CB-09C6-4E4B-AFF1-0D5875903C34}
{1F8317BA-2A68-4D64-AEBA-5FB42FCEB8FB} = {1DF0F472-E4CA-4EFD-9459-F5B90E2E8E67}
{FC40FB5D-EABC-4A20-9695-3D52C0093FBC} = {A48D5A62-96E5-4D44-818C-18748101CCC5}
EndGlobalSection
Expand Down
17 changes: 17 additions & 0 deletions src/Bellatrix.SMS/Bellatrix.SMS.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Twilio" Version="7.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bellatrix.Core\Bellatrix.Core.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/Bellatrix.SMS/SmsEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <copyright file="TwilioMessageEventArgs.cs" company="Automate The Planet Ltd.">
// Copyright 2024 Automate The Planet Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Miriam Kyoseva</author>
// <site>https://bellatrix.solutions/</site>

using Twilio.Rest.Api.V2010.Account;

namespace Bellatrix.SMS;

public class SmsEventArgs : EventArgs
{
public SmsListener SmsListener { get; }
public MessageResource Message { get; }

public SmsEventArgs(SmsListener smsListener, MessageResource message)
{
SmsListener = smsListener;
Message = message;
}
}
82 changes: 82 additions & 0 deletions src/Bellatrix.SMS/SmsListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// <copyright file="SmsListener.cs" company="Automate The Planet Ltd.">
// Copyright 2024 Automate The Planet Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Miriam Kyoseva</author>
// <site>https://bellatrix.solutions/</site>

using Twilio.Rest.Api.V2010.Account;

namespace Bellatrix.SMS;

public class SmsListener
{
public static event EventHandler<SmsEventArgs> MessageReceived;

Check warning on line 21 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable event 'MessageReceived' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

Check warning on line 21 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable event 'MessageReceived' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

private readonly string phoneNumber;
private readonly List<MessageResource> messages = new List<MessageResource>();
private CancellationTokenSource cancellationTokenSource;
private DateTime start;

public SmsListener(string phoneNumber = null)

Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'cancellationTokenSource' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 28 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'cancellationTokenSource' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
this.phoneNumber = phoneNumber;
}

public List<MessageResource> GetMessages()
{
return new List<MessageResource>(messages);
}

public MessageResource GetLastMessage()
{
return messages.LastOrDefault();

Check warning on line 40 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 40 in src/Bellatrix.SMS/SmsListener.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

public void Listen()
{
start = DateTime.UtcNow;
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() => CheckForMessages(cancellationTokenSource.Token));
}

public void StopListening()
{
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
cancellationTokenSource.Dispose();
}
}

private async Task CheckForMessages(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var messageReader = MessageResource.ReadAsync(
dateSentAfter: start,
from: phoneNumber,
limit: 1
);

var foundMessages = await messageReader;

if (foundMessages.Any())
{
var message = foundMessages.First();
messages.Add(message);
MessageReceived?.Invoke(this, new SmsEventArgs(this, message));
start = DateTime.UtcNow;
}

await Task.Delay(TimeSpan.FromMilliseconds(500), cancellationToken);
}
}
}
44 changes: 44 additions & 0 deletions src/Bellatrix.SMS/SmsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Twilio;
using Twilio.Rest.Api.V2010.Account;

namespace Bellatrix.SMS;

public class SmsService
{
private static readonly TwilioSettings settings;

static SmsService()
{
settings = ConfigurationService.GetSection<TwilioSettings>();
TwilioClient.Init(settings.AccountSID, settings.AuthToken);
}

public static SmsListener ListenForSms(string fromNumber)
{
var smsListener = new SmsListener(fromNumber);
smsListener.Listen();
return smsListener;
}

public static SmsListener ListenForSms()
{
var smsListener = new SmsListener();
smsListener.Listen();
return smsListener;
}

public static void StopListeningForSms(SmsListener smsListener)
{
smsListener.StopListening();
}

public static List<MessageResource> GetMessages(SmsListener smsListener)
{
return smsListener.GetMessages();
}

public static MessageResource GetLastMessage(SmsListener smsListener)
{
return smsListener.GetLastMessage();
}
}
22 changes: 22 additions & 0 deletions src/Bellatrix.SMS/TwilioSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="TwilioSettings.cs" company="Automate The Planet Ltd.">
// Copyright 2024 Automate The Planet Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Miriam Kyoseva</author>
// <site>https://bellatrix.solutions/</site>

namespace Bellatrix.SMS;

public class TwilioSettings
{
public string? AccountSID { get; set; }
public string? AuthToken { get; set; }
public string? PhoneNumber { get; set; }
}

0 comments on commit 6e0c0f7

Please sign in to comment.