Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add API versioning to Docker build and Swagger documentation #43

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

italopessoa
Copy link
Member

No description provided.

Copy link

coderabbitai bot commented Nov 20, 2024

Walkthrough

The changes made in this pull request involve updates to the GitHub Actions workflow, Dockerfile, and the Program.cs file of the Bmb.Payment.Api project. A new build argument API_VERSION has been introduced to the Docker image build process, enabling dynamic versioning. The workflow now includes modifications for the execution of the dotnet-coverage step and a fallback mechanism for version retrieval. Additionally, the Dockerfile has been updated to utilize the new version argument during the publish stage, and the Program.cs file has been enhanced to reflect the assembly version in the Swagger documentation.

Changes

File Change Summary
.github/workflows/build-and-deploy.yaml Added API_VERSION to Docker build step; modified dotnet-coverage step to always run; updated semantic-release job for version fallback; included API_IMAGE_TAG in create-app job.
src/Bmb.Payment.Api/Dockerfile Introduced ARG API_VERSION for dynamic versioning in the dotnet publish command.
src/Bmb.Payment.Api/Program.cs Added version variable for assembly version; updated Swagger documentation to reflect dynamic versioning; refactored Swagger setup methods.
src/Bmb.Payment.Api/Bmb.Payment.Api.csproj Added project version; updated package references including Swashbuckle.AspNetCore.
src/Bmb.Payment.Api/Controllers/PaymentsController.cs Changed authentication namespace from Bmb.Auth to Bmb.Tools.Auth.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant GitHub Actions
    participant Docker
    participant API

    Developer->>GitHub Actions: Push changes
    GitHub Actions->>Docker: Build and push Docker image with API_VERSION
    Docker->>API: Publish API with dynamic version
    API->>GitHub Actions: Notify build completion
    GitHub Actions->>Developer: Notify success
Loading

Warning

Rate limit exceeded

@italopessoa has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 23 minutes and 30 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between db73285 and 9f5f20c.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between db73285 and 9f5f20c.

📒 Files selected for processing (1)
  • src/Bmb.Payment.Api/Bmb.Payment.Api.csproj (3 hunks)
🔇 Additional comments (3)
src/Bmb.Payment.Api/Bmb.Payment.Api.csproj (3)

8-9: Verify version consistency across configuration files

The version and product properties look good, but let's ensure the version matches across Docker configurations and workflow files.

✅ Verification successful

Version configuration is properly integrated across the build system

The version consistency is well-maintained:

  • Project file version (1.0.0) serves as the default version
  • Dockerfile correctly accepts API_VERSION as build argument and passes it to the dotnet publish command
  • CI/CD workflow properly manages versioning through semantic release and propagates it to Docker build
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check version consistency across Docker and workflow files
# Look for version references in Dockerfile and workflow files

echo "Checking version references in Dockerfile:"
rg -A 2 'ARG API_VERSION' Dockerfile

echo "Checking version references in workflow:"
rg -A 2 'API_VERSION' .github/workflows/

Length of output: 576


Script:

#!/bin/bash
# Check for Dockerfile in the correct location and its content
echo "Checking for Dockerfile in src/Bmb.Payment.Api:"
fd Dockerfile src/Bmb.Payment.Api

echo "Content of Dockerfile if found:"
cat src/Bmb.Payment.Api/Dockerfile 2>/dev/null

echo "Checking version in workflow file:"
cat .github/workflows/build-and-deploy.yaml

Length of output: 9758


23-23: Verify authentication functionality after package replacement

The replacement of Bmb.Auth with Bmb.Tools might impact authentication. Please ensure all auth functionality is properly migrated.

✅ Verification successful

Let me gather more information about authentication-related code and the new Bmb.Tools package usage.


Authentication functionality is properly migrated to Bmb.Tools

The codebase shows that the authentication functionality has been successfully migrated from Bmb.Auth to Bmb.Tools.Auth. Key evidence:

  • JWT configuration is properly set up using Bmb.Tools.Auth in Program.cs
  • Role-based authorization is implemented using BmbRoles from Bmb.Tools.Auth
  • Custom authentication for MercadoPago webhooks remains intact and independent of the package change
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for authentication-related code that might need updates

echo "Checking for auth-related code:"
rg -l 'using.*Bmb.Auth' 

echo "Checking for potential auth implementations:"
ast-grep --pattern 'class $_ : $AuthHandler { $$$ }'

Length of output: 252


Script:

#!/bin/bash
# Check for any authentication/authorization related code and Bmb.Tools usage
echo "Checking for authentication/authorization related code:"
rg -i "auth|authenticate|authorize" -t cs -A 3

echo -e "\nChecking for Bmb.Tools usage:"
rg "using.*Bmb.Tools" -t cs -A 1

echo -e "\nChecking Program.cs and Startup.cs for auth configuration:"
fd -e cs -E "*Test*" "Program.cs|Startup.cs" --exec cat {}

Length of output: 23880


34-35: Review Swashbuckle breaking changes

The major version update of Swashbuckle.AspNetCore (6.4.0 -> 7.0.0) may introduce breaking changes. The addition of Filters package is good for API versioning support.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
src/Bmb.Payment.Api/Dockerfile (1)

18-18: Consider adding documentation and default value for API_VERSION argument

While the argument is correctly placed, it would be beneficial to:

  1. Add a comment explaining its purpose
  2. Provide a default value to prevent build failures when not specified
-ARG API_VERSION
+# Version to be embedded in the published assembly
+ARG API_VERSION=1.0.0
src/Bmb.Payment.Api/Program.cs (2)

26-26: Consider extracting version retrieval to avoid duplication

The assembly version is retrieved twice (here and at line 114). Consider extracting this into a single variable at the start of the method to improve maintainability and handle potential null values.

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);
    ILogger<Program>? logger = null;
    try
    {
+       var version = Assembly.GetExecutingAssembly().GetName().Version 
+           ?? throw new InvalidOperationException("Assembly version is not set");
-       var version = Assembly.GetExecutingAssembly().GetName().Version;

Line range hint 26-116: Architecture Review: Changes align with Clean Architecture principles

The versioning implementation:

  1. Maintains separation of concerns
  2. Follows single responsibility principle
  3. Properly integrates with the existing configuration setup
  4. Provides good error handling and logging

The changes successfully achieve the PR objective of adding API versioning while maintaining the architectural integrity of the application.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8902566 and e3abeb0.

📒 Files selected for processing (3)
  • .github/workflows/build-and-deploy.yaml (1 hunks)
  • src/Bmb.Payment.Api/Dockerfile (1 hunks)
  • src/Bmb.Payment.Api/Program.cs (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/Bmb.Payment.Api/Program.cs (1)

Pattern **/*.cs: Validate all files and structures against Domain Driven Design, SOLID and Clean Architecture principles.

🔇 Additional comments (4)
src/Bmb.Payment.Api/Dockerfile (1)

19-19: Improve command readability and verify version propagation

The publish command is becoming lengthy. Consider improving readability by splitting parameters across multiple lines.

-RUN dotnet publish "src/Bmb.Payment.Api/Bmb.Payment.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false /p:Version=$API_VERSION
+RUN dotnet publish "src/Bmb.Payment.Api/Bmb.Payment.Api.csproj" \
+    -c $BUILD_CONFIGURATION \
+    -o /app/publish \
+    /p:UseAppHost=false \
+    /p:Version=$API_VERSION

Let's verify how the version parameter affects the assemblies:

✅ Verification successful

Command readability improvement is valid, version parameter is properly propagated

The suggested command readability improvement is beneficial. Regarding version propagation:

  • The API project references three other projects (Masstransit, Controllers, and DI)
  • No AssemblyInfo files were found in the referenced projects that would override versioning
  • No explicit version-related properties were found in the referenced projects' csproj files
  • The /p:Version parameter in the publish command will properly cascade to all referenced projects

The original suggestion to improve command readability stands, and the version parameter will work as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the project references other projects that might need version synchronization

# Look for project references in the API project
rg -A 5 "ProjectReference" "src/Bmb.Payment.Api/Bmb.Payment.Api.csproj"

# Check if there are any AssemblyInfo files that might override versioning
fd -e cs "AssemblyInfo.cs" src/

Length of output: 528


Script:

#!/bin/bash
# Check the content of AssemblyInfo.cs to see if it overrides version
cat "src/Bmb.Payment.Api/Properties/AssemblyInfo.cs"

# Check if referenced projects have their own AssemblyInfo files
fd -e cs "AssemblyInfo.cs" "src/Bmb.Payment.Masstransit" "src/Bmb.Payment.Controllers" "src/Bmb.Payment.DI"

# Check if referenced projects have version-related properties in their csproj files
rg -g "*.csproj" "Version|AssemblyVersion|FileVersion|InformationalVersion" "src/Bmb.Payment.Masstransit" "src/Bmb.Payment.Controllers" "src/Bmb.Payment.DI"

Length of output: 1126

src/Bmb.Payment.Api/Program.cs (1)

59-61: Verify Swagger endpoint format and version display

The configuration looks good, but there are two points to consider:

  1. The version format follows semantic versioning (major.minor.build) which is good practice.
  2. The endpoint at line 115 uses .yaml extension - verify if this is intentional as .json is more commonly used.
.github/workflows/build-and-deploy.yaml (2)

Line range hint 192-205: LGTM! Environment variables are properly configured.

The API_IMAGE_TAG is correctly referenced from the build-docker-image job output and properly used to set the api_docker_image Terraform variable.


170-171: LGTM! Verify version propagation.

The addition of the API_VERSION build argument is well-implemented and aligns with the PR objective of adding API versioning to the Docker build.

Let's verify that the version is correctly propagated through the workflow:

✅ Verification successful

Version propagation verified successfully

The Dockerfile correctly accepts and utilizes the API_VERSION build argument:

  • ARG API_VERSION is properly declared
  • The version is correctly passed to the dotnet publish command via /p:Version=$API_VERSION
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the Dockerfile accepts and uses the API_VERSION build arg

# Check if Dockerfile accepts the build arg
rg -A 5 'ARG API_VERSION' src/Bmb.Payment.Api/Dockerfile

# Check if the version is used in the dotnet publish command
rg -A 5 '/p:Version=\$API_VERSION' src/Bmb.Payment.Api/Dockerfile

Length of output: 541

Comment on lines 112 to 116
app.UseSwaggerUI(options =>
{
var version = Assembly.GetExecutingAssembly().GetName().Version.Major;
options.SwaggerEndpoint($"/swagger/v{version}/swagger.yaml", $"v{version}");
});
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor Swagger UI configuration to reuse version

The Swagger UI configuration can be improved:

  1. Reuse the version variable from line 26 instead of retrieving it again.
  2. Consider using .json format for consistency unless YAML is specifically required.
-                app.UseSwaggerUI(options =>
-                {
-                    var version = Assembly.GetExecutingAssembly().GetName().Version.Major;
-                    options.SwaggerEndpoint($"/swagger/v{version}/swagger.yaml", $"v{version}");
-                });
+                app.UseSwaggerUI(options =>
+                {
+                    options.SwaggerEndpoint($"/swagger/v{version.Major}/swagger.json", $"v{version.Major}");
+                });

Committable suggestion skipped: line range outside the PR's diff.

Copy link

sonarcloud bot commented Nov 22, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant