From 8e678497e69fb66518589868048d3a0a5589c11c Mon Sep 17 00:00:00 2001 From: mabrouk Date: Sun, 11 Aug 2024 17:34:22 +0200 Subject: [PATCH] INFRA: Build Project --- .github/workflows/dotnet.yml | 32 ++++++ .../FlexiMail.Infrastructure.Build.csproj | 14 +++ FlexiMail.Infrastructure.Build/Program.cs | 106 ++++++++++++++++++ FlexiMail.sln | 6 + FlexiMail/Services/FlexiExchangeService.cs | 30 ++++- 5 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/dotnet.yml create mode 100644 FlexiMail.Infrastructure.Build/FlexiMail.Infrastructure.Build.csproj create mode 100644 FlexiMail.Infrastructure.Build/Program.cs diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..de54f12 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,32 @@ +name: FlexiMail Build +on: + push: + branches: + - main + pull_request: + branches: + - main +jobs: + build: + runs-on: windows-latest + steps: + - name: Pulling Code + uses: actions/checkout@v3 + - name: Installing .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 8.0.* + - name: Restoring Packages + run: dotnet restore + - name: Building Solution + run: dotnet build --no-restore + - name: Running Tests + run: dotnet test --no-build --verbosity normal + env: + Authority: ${{ secrets.AUTHORITY }} + ClientId: ${{ secrets.CLIENTID }} + ClientSecret: ${{ secrets.CLIENTSECRET }} + PrincipalName: ${{ secrets.PRINCIPALNAME }} + Sid: ${{ secrets.SID }} + SmtpAddress: ${{ secrets.SMTPADDRESS }} + TenantId: ${{ secrets.TENANTID }} diff --git a/FlexiMail.Infrastructure.Build/FlexiMail.Infrastructure.Build.csproj b/FlexiMail.Infrastructure.Build/FlexiMail.Infrastructure.Build.csproj new file mode 100644 index 0000000..bb1ce5e --- /dev/null +++ b/FlexiMail.Infrastructure.Build/FlexiMail.Infrastructure.Build.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + disable + disable + + + + + + + diff --git a/FlexiMail.Infrastructure.Build/Program.cs b/FlexiMail.Infrastructure.Build/Program.cs new file mode 100644 index 0000000..9c82797 --- /dev/null +++ b/FlexiMail.Infrastructure.Build/Program.cs @@ -0,0 +1,106 @@ +using System.Collections.Generic; +using System.IO; +using ADotNet.Clients; +using ADotNet.Models.Pipelines.GithubPipelines.DotNets; +using ADotNet.Models.Pipelines.GithubPipelines.DotNets.Tasks; +using ADotNet.Models.Pipelines.GithubPipelines.DotNets.Tasks.SetupDotNetTaskV3s; + +namespace FlexiMail.Infrastructure.Build +{ + internal class Program + { + private const string BuildScriptPath = "../../../../.github/workflows/dotnet.yml"; + + private static void Main(string[] args) + { + var adoNetClient = new ADotNetClient(); + + var githubPipeline = new GithubPipeline + { + Name = "FlexiMail Build", + + OnEvents = new Events + { + Push = new PushEvent + { + Branches = new string[] { "main" } + }, + + PullRequest = new PullRequestEvent + { + Branches = new string[] { "main" } + } + }, + + Jobs = new Dictionary + { + { + "build", + new Job + { + EnvironmentVariables = new Dictionary + { + { "Authority", "${{ secrets.AUTHORITY }}" }, + { "ClientId", "${{ secrets.CLIENTID }}" }, + { "ClientSecret", "${{ secrets.CLIENTSECRET }}" }, + { "PrincipalName", "${{ secrets.PRINCIPALNAME }}" }, + { "Sid", "${{ secrets.SID }}" }, + { "SmtpAddress", "${{ secrets.SMTPADDRESS }}" }, + { "TenantId", "${{ secrets.TENANTID }}" } + }, + + RunsOn = BuildMachines.WindowsLatest, + + Steps = + [ + new CheckoutTaskV3 + { + Name = "Pulling Code" + }, + + + new SetupDotNetTaskV3 + { + Name = "Installing .NET", + + With = new TargetDotNetVersionV3 + { + DotNetVersion = "8.0.*" + } + }, + + + new RestoreTask + { + Name = "Restoring Packages" + }, + + + new DotNetBuildTask + { + Name = "Building Solution" + }, + + + new TestTask + { + Name = "Running Tests" + } + ] + } + } + } + }; + + var directoryPath = Path.GetDirectoryName(BuildScriptPath); + if (!Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath!); + } + + adoNetClient.SerializeAndWriteToFile( + adoPipeline: githubPipeline, + path: BuildScriptPath); + } + } +} \ No newline at end of file diff --git a/FlexiMail.sln b/FlexiMail.sln index 698aed4..e4cde77 100644 --- a/FlexiMail.sln +++ b/FlexiMail.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlexiMail", "FlexiMail\Flex EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlexiMail.Tests.Unit", "FlexiMail.Tests.Unit\FlexiMail.Tests.Unit.csproj", "{E7E74297-E2B4-4A5F-9F10-13CFDB1DDF69}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlexiMail.Infrastructure.Build", "FlexiMail.Infrastructure.Build\FlexiMail.Infrastructure.Build.csproj", "{5DDAB7DA-8535-413C-BF4E-E76FC5BF24CC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {E7E74297-E2B4-4A5F-9F10-13CFDB1DDF69}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7E74297-E2B4-4A5F-9F10-13CFDB1DDF69}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7E74297-E2B4-4A5F-9F10-13CFDB1DDF69}.Release|Any CPU.Build.0 = Release|Any CPU + {5DDAB7DA-8535-413C-BF4E-E76FC5BF24CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5DDAB7DA-8535-413C-BF4E-E76FC5BF24CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5DDAB7DA-8535-413C-BF4E-E76FC5BF24CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5DDAB7DA-8535-413C-BF4E-E76FC5BF24CC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/FlexiMail/Services/FlexiExchangeService.cs b/FlexiMail/Services/FlexiExchangeService.cs index f6192ad..5c536ae 100644 --- a/FlexiMail/Services/FlexiExchangeService.cs +++ b/FlexiMail/Services/FlexiExchangeService.cs @@ -15,12 +15,13 @@ namespace FlexiMail.Services { - internal class FlexiExchangeService(ExchangeConfigurations configurations, IExchangeBroker exchangeBroker) + internal class FlexiExchangeService( + ExchangeConfigurations configurations, + IExchangeBroker exchangeBroker) : IFlexiExchangeService { private readonly IExchangeBroker exchangeBroker = exchangeBroker; - - + public async void SendAndSaveCopyAsync(FlexiMessage flexiMessage) { var exchangeService = await CreateExchangeServiceAsync(); @@ -58,20 +59,37 @@ public async void SendAndSaveCopyAsync(FlexiMessage flexiMessage) File.Delete(filePath); } } - private async ValueTask CreateExchangeServiceAsync() { var accessToken = await this.exchangeBroker.GetAccessTokenAsync(); + var idType = GetConnectingIdType(); var impersonatedUserId = new ImpersonatedUserId( - idType: ConnectingIdType.SmtpAddress, - id: configurations.SmtpAddress); + idType: idType, + id: GetUserId(idType)); return this.exchangeBroker.CreateExchangeService(ExchangeVersion.Exchange2013, accessToken, impersonatedUserId); } + private string GetUserId(ConnectingIdType idType) => idType switch + { + ConnectingIdType.PrincipalName => configurations.PrincipalName, + ConnectingIdType.SmtpAddress => configurations.SmtpAddress, + ConnectingIdType.SID => configurations.Sid, + _ => configurations.SmtpAddress + }; + private ConnectingIdType GetConnectingIdType() + { + if (!string.IsNullOrWhiteSpace(configurations.SmtpAddress)) + return ConnectingIdType.SmtpAddress; + if (!string.IsNullOrWhiteSpace(configurations.Sid)) + return ConnectingIdType.SID; + return !string.IsNullOrWhiteSpace(configurations.PrincipalName) + ? ConnectingIdType.PrincipalName + : ConnectingIdType.SmtpAddress; + } private static BodyType MapBodyType(BodyContentType bodyContentType) => bodyContentType switch { BodyContentType.Html => BodyType.HTML,