Skip to content

Commit

Permalink
INFRA: Build Project
Browse files Browse the repository at this point in the history
  • Loading branch information
mabroukmahdhi committed Aug 11, 2024
1 parent 4e2b8f7 commit 8e67849
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 6 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ADotNet" Version="3.0.3" />
</ItemGroup>

</Project>
106 changes: 106 additions & 0 deletions FlexiMail.Infrastructure.Build/Program.cs
Original file line number Diff line number Diff line change
@@ -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<string, Job>
{
{
"build",
new Job
{
EnvironmentVariables = new Dictionary<string, string>
{
{ "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);
}
}
}
6 changes: 6 additions & 0 deletions FlexiMail.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
30 changes: 24 additions & 6 deletions FlexiMail/Services/FlexiExchangeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -58,20 +59,37 @@ public async void SendAndSaveCopyAsync(FlexiMessage flexiMessage)
File.Delete(filePath);
}
}

private async ValueTask<ExchangeService> 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,
Expand Down

0 comments on commit 8e67849

Please sign in to comment.