Skip to content

EXPOSERS: Fluent GitHub Pipeline Builder#145

Closed
mabroukmahdhi wants to merge 15 commits intomainfrom
users/mabroukmahdhi/exposures-fluent-pipeline-builder
Closed

EXPOSERS: Fluent GitHub Pipeline Builder#145
mabroukmahdhi wants to merge 15 commits intomainfrom
users/mabroukmahdhi/exposures-fluent-pipeline-builder

Conversation

@mabroukmahdhi
Copy link
Copy Markdown
Contributor

@mabroukmahdhi mabroukmahdhi commented Jan 24, 2025

Summary

This pull request introduces a comprehensive refactor and feature set for the GitHubPipelineBuilder class, making it more extensible, testable, and user-friendly. The key features include:

  • Dependency Injection Support: Replaced the direct dependency on ADotNetClient with an interface IADotNetClient, enabling easier testing and extensibility.
  • Fluent API Enhancements:
    • Added support for defining environment variables in pipeline jobs.
    • Added a generic task builder method to allow custom tasks.
  • Interface Implementation: Updated the ADotNetClient class to implement the IADotNetClient interface.
  • Code Refactor: Refactored methods to improve readability and maintainability.
  • Improved Testing: Updated unit tests to mock the IADotNetClient interface and validate behavior.

Features

Fluent API Enhancements

  1. Environment Variables Support:

    • Added methods to configure environment variables for jobs:
      job.AddEnvironmentVariable("AzureClientId", "${{ secrets.AZURECLIENTID }}")
         .AddEnvironmentVariables(new Dictionary<string, string>
         {
             { "AzureClientId", "${{ secrets.AZURECLIENTID }}" },
             { "AzureTenantId", "${{ secrets.AZURETENANTID }}" }
         });
  2. Generic Task Builder:

    • Added support for custom tasks using AddGenericStep:
      job.AddGenericStep(
          name: "Provision",
          runCommand: "dotnet run --project ./Project.csproj");
  3. Improved Pipeline Definition:

    • Simplified the creation of pipelines with triggers, jobs, and tasks:
      GitHubPipelineBuilder.CreateNewPipeline()
          .SetName("Github")
          .OnPush("master")
          .OnPullRequest("master")
          .AddJob("build", job => job
              .WithName("Build")
              .RunsOn(BuildMachines.WindowsLatest)
              .AddEnvironmentVariables(new Dictionary<string, string>
              {
                  { "AzureClientId", "${{ secrets.AZURECLIENTID }}" },
                  { "AzureTenantId", "${{ secrets.AZURETENANTID }}" },
                  { "AzureClientSecret", "${{ secrets.AZURECLIENTSECRET }}" },
                  { "AzureAdminName", "${{ secrets.AZUREADMINNAME }}" },
                  { "AzureAdminAccess", "${{ secrets.AZUREADMINACCESS }}" }
              })
              .AddCheckoutStep("Check Out")
              .AddSetupDotNetStep(
                  version: "6.0.101",
                  includePrerelease: true)
              .AddRestoreStep()
              .AddBuildStep()
              .AddGenericStep(
                  name: "Provision",
                  runCommand: "dotnet run --project .\OtripleS.Api.Infrastructure.Provision\OtripleS.Web.Api.Infrastructure.Provision.csproj"))
          .SaveToFile("github-pipelines-2.yaml");

Interface Implementation

  • Used IADotNetClient interface to abstract the ADotNetClient class.
  • Updated ADotNetClient to implement this interface without changing existing functionality.

Dependency Injection

  • Refactored GitHubPipelineBuilder to accept IADotNetClient via dependency injection, enabling mocking in unit tests and better modularity.

@mabroukmahdhi
Copy link
Copy Markdown
Contributor Author

This PR is Code-Completed. Pending #144

@mabroukmahdhi mabroukmahdhi changed the title EXPOSURES: Fluent GitHub Pipeline Builder EXPOSERS: Fluent GitHub Pipeline Builder Jan 24, 2025
@github-actions github-actions bot added the EXPOSERS The exposers category label Jan 24, 2025
OnEvents = new Events(),
Jobs = new Dictionary<string, Job>()
};
this.aDotNetClient = aDotNetClient;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line above as it is following a multi liner.

public static GitHubPipelineBuilder CreateNewPipeline()
{
var aDotNetClient = new ADotNetClient();
return new GitHubPipelineBuilder(aDotNetClient);
Copy link
Copy Markdown
Collaborator

@cjdutoit cjdutoit Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line before return, fix others as well

Comment on lines +66 to +71
public void SaveToFile(string path)
{
this.aDotNetClient.SerializeAndWriteToFile(
this.githubPipeline,
path);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void SaveToFile(string path)
{
this.aDotNetClient.SerializeAndWriteToFile(
this.githubPipeline,
path);
}
public void SaveToFile(string path) =>
this.aDotNetClient.SerializeAndWriteToFile(this.githubPipeline, path);

public JobBuilder WithName(string name)
{
this.job.Name = name;
return this;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line before a return, fix others on file as well

Comment on lines +41 to +45
this.job.EnvironmentVariables ??=
new Dictionary<string, string>();

this.job.EnvironmentVariables[key] = value;
return this;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.job.EnvironmentVariables ??=
new Dictionary<string, string>();
this.job.EnvironmentVariables[key] = value;
return this;
this.job.EnvironmentVariables ??= new Dictionary<string, string>();
this.job.EnvironmentVariables[key] = value;
return this;

.AddEnvironmentVariable("AzureClientSecret", "${{ secrets.AZURECLIENTSECRET }}")
.AddEnvironmentVariable("AzureAdminName", "${{ secrets.AZUREADMINNAME }}")
.AddEnvironmentVariable("AzureAdminAccess", "${{ secrets.AZUREADMINACCESS }}")
//.AddEnvironmentVariables(new Dictionary<string, string>
Copy link
Copy Markdown
Collaborator

@cjdutoit cjdutoit Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented section. You can demo this in the readme.md documentation file.

Maybe also add a separate markup file to explain the flient config or extend the existing one with the existing demo in fluent syntax?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I am saving to an other file: .SaveToFile("github-pipelines-2.yaml");

actualPipeline.OnEvents.Push.Should().NotBeNull();
actualPipeline.OnEvents.Push.Branches.Should().BeEquivalentTo(inputBranches);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what to remove exactly ?


this.aDotNetClientMock.VerifyNoOtherCalls();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what to remove exactly ?

@mabroukmahdhi
Copy link
Copy Markdown
Contributor Author

Please check the PR #147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

EXPOSERS The exposers category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants