Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ csharp_style_expression_bodied_accessors = true:error
csharp_style_expression_bodied_lambdas = true:error
csharp_style_expression_bodied_local_functions = true:error

# S1186: Methods should not be empty
dotnet_diagnostic.S1186.severity = warning

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none

[*.{cs,vb}]
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
Expand Down
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "daily"
66 changes: 66 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Build

on:
workflow_dispatch:
push:
branches:
- main

# Pull requests with specific types will trigger the build action
pull_request:
branches:
- main
types:
- opened
- synchronize
- reopened
- ready_for_review

env:
DOTNET_VERSION: "8.x"

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: ['8.x']
os: [ubuntu-latest]
configuration: [Release]

steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Cache NuGet packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-

- name: Restore
run: dotnet restore LegalAssistant.AppService.sln

- name: Build
run: dotnet build LegalAssistant.AppService.sln --configuration ${{ matrix.configuration }} --no-restore

- name: Test
run: dotnet test LegalAssistant.AppService.sln --configuration ${{ matrix.configuration }} --no-restore --no-build --verbosity normal --collect:"XPlat Code Coverage"

- name: Code Analysis
run: dotnet build LegalAssistant.AppService.sln --configuration ${{ matrix.configuration }} --verbosity normal /p:TreatWarningsAsErrors=true

- name: Publish Web API
run: dotnet publish src/Web.Api/Web.Api.csproj --configuration ${{ matrix.configuration }} --no-restore --no-build --output ./publish

- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: published-app
path: ./publish
289 changes: 289 additions & 0 deletions BUILD_SCRIPTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
# 🏗️ Build Scripts - Legal Assistant App

Hướng dẫn build project với SonarAnalyzer để check lỗi code quality như trên CI/CD.

## 📋 Mục lục
- [Build All Projects (Solution)](#-build-all-projects-solution)
- [Build Individual Projects](#-build-individual-projects)
- [Quick Commands](#-quick-commands)
- [PowerShell Scripts](#-powershell-scripts)
- [Troubleshooting](#-troubleshooting)

---

## 🎯 Build All Projects (Solution)

### Debug Build (Development)
```bash
# Build nhanh cho development
dotnet build LegalAssistant.AppService.sln --configuration Debug
```

### Release Build (Production/CI-like)
```bash
# Build giống CI/CD với SonarAnalyzer enabled
dotnet build LegalAssistant.AppService.sln --configuration Release --verbosity normal /p:TreatWarningsAsErrors=true
```

### Full CI/CD Simulation
```bash
# Restore packages
dotnet restore LegalAssistant.AppService.sln

# Build with strict analysis
dotnet build LegalAssistant.AppService.sln --configuration Release --no-restore --verbosity normal /p:TreatWarningsAsErrors=true

# Run tests
dotnet test LegalAssistant.AppService.sln --configuration Release --no-restore --no-build --verbosity normal

# Code Analysis (same as CI/CD)
dotnet build LegalAssistant.AppService.sln --configuration Release --verbosity normal /p:TreatWarningsAsErrors=true
```

---

## 🔧 Build Individual Projects

### Domain Layer
```bash
# Check Domain layer issues
dotnet build src/Domain/Domain.csproj --configuration Release /p:TreatWarningsAsErrors=true
```

### Application Layer
```bash
# Check Application layer issues
dotnet build src/Application/Application.csproj --configuration Release /p:TreatWarningsAsErrors=true
```

### Infrastructure Layer
```bash
# Check Infrastructure layer issues
dotnet build src/Infrastructure/Infrastructure.csproj --configuration Release /p:TreatWarningsAsErrors=true
```

### Web API
```bash
# Check Web API issues
dotnet build src/Web.Api/Web.Api.csproj --configuration Release /p:TreatWarningsAsErrors=true
```

---

## ⚡ Quick Commands

### Check All SonarAnalyzer Issues
```bash
dotnet build --configuration Release /p:TreatWarningsAsErrors=true
```

### Build & Test Pipeline
```bash
dotnet restore && dotnet build --configuration Release /p:TreatWarningsAsErrors=true && dotnet test --no-build --configuration Release
```

### Clean & Full Rebuild
```bash
dotnet clean && dotnet restore && dotnet build --configuration Release /p:TreatWarningsAsErrors=true
```

---

## 📜 PowerShell Scripts

### Build Script (`build.ps1`)
```powershell
# Save this as build.ps1
param(
[string]$Configuration = "Release",
[switch]$SkipTests,
[switch]$Verbose
)

Write-Host "🏗️ Building Legal Assistant App..." -ForegroundColor Green

# Set verbosity
$verbosity = if ($Verbose) { "normal" } else { "minimal" }

try {
# Restore packages
Write-Host "📦 Restoring packages..." -ForegroundColor Yellow
dotnet restore LegalAssistant.AppService.sln

if ($LASTEXITCODE -ne 0) { throw "Restore failed" }

# Build solution
Write-Host "🔨 Building solution..." -ForegroundColor Yellow
dotnet build LegalAssistant.AppService.sln --configuration $Configuration --no-restore --verbosity $verbosity /p:TreatWarningsAsErrors=true

if ($LASTEXITCODE -ne 0) { throw "Build failed" }

# Run tests (optional)
if (-not $SkipTests) {
Write-Host "🧪 Running tests..." -ForegroundColor Yellow
dotnet test LegalAssistant.AppService.sln --configuration $Configuration --no-restore --no-build --verbosity $verbosity

if ($LASTEXITCODE -ne 0) { throw "Tests failed" }
}

Write-Host "✅ Build completed successfully!" -ForegroundColor Green
}
catch {
Write-Host "❌ Build failed: $_" -ForegroundColor Red
exit 1
}
```

### Quick Check Script (`check.ps1`)
```powershell
# Save this as check.ps1
Write-Host "🔍 Quick SonarAnalyzer Check..." -ForegroundColor Cyan

dotnet build --configuration Release /p:TreatWarningsAsErrors=true

if ($LASTEXITCODE -eq 0) {
Write-Host "✅ No issues found!" -ForegroundColor Green
} else {
Write-Host "❌ Issues detected. Fix them before CI/CD." -ForegroundColor Red
}
```

### Project-specific Script (`build-project.ps1`)
```powershell
# Save this as build-project.ps1
param(
[Parameter(Mandatory=$true)]
[ValidateSet("Domain", "Application", "Infrastructure", "Web.Api")]
[string]$Project
)

$projectPath = "src/$Project/$Project.csproj"
Write-Host "🏗️ Building $Project..." -ForegroundColor Green

dotnet build $projectPath --configuration Release /p:TreatWarningsAsErrors=true

if ($LASTEXITCODE -eq 0) {
Write-Host "✅ $Project build successful!" -ForegroundColor Green
} else {
Write-Host "❌ $Project build failed!" -ForegroundColor Red
}
```

---

## 🐛 Troubleshooting

### Common SonarAnalyzer Issues

#### S1186: Empty Methods
```csharp
// ❌ BAD
public void ProcessData() { }

// ✅ GOOD
public void ProcessData()
{
// Intentionally empty - placeholder for future implementation
}
```

#### S1481: Unused Variables
```csharp
// ❌ BAD
var result = GetData();

// ✅ GOOD
var result = GetData();
Console.WriteLine($"Result: {result}");
```

#### S3400: Methods Returning Constants
```csharp
// ❌ BAD
private bool IsEnabled() => false;

// ✅ GOOD
private const bool DefaultEnabledStatus = false;
private bool IsEnabled() => DefaultEnabledStatus;
```

### Disable Specific Rules Temporarily
```csharp
#pragma warning disable S1186 // Empty methods
public void PlaceholderMethod() { }
#pragma warning restore S1186
```

### Suppress Rules in .editorconfig
```ini
# Add to .editorconfig for project-wide suppression
dotnet_diagnostic.S1186.severity = none
dotnet_diagnostic.S1481.severity = none
```

---

## 📊 CI/CD Integration

### GitHub Actions Equivalent
```yaml
# This is what runs on CI/CD
- name: Build
run: dotnet build LegalAssistant.AppService.sln --configuration Release --no-restore

- name: Code Analysis
run: dotnet build LegalAssistant.AppService.sln --configuration Release --verbosity normal /p:TreatWarningsAsErrors=true
```

### Local Testing Before Push
```bash
# Run this before committing to ensure CI/CD will pass
dotnet restore LegalAssistant.AppService.sln
dotnet build LegalAssistant.AppService.sln --configuration Release --verbosity normal /p:TreatWarningsAsErrors=true
dotnet test LegalAssistant.AppService.sln --configuration Release --no-restore --no-build
```

---

## 🎯 Usage Examples

### Daily Development
```bash
# Quick check during development
./check.ps1

# Or using dotnet directly
dotnet build --configuration Release /p:TreatWarningsAsErrors=true
```

### Before Committing
```bash
# Full validation
./build.ps1 -Verbose

# Or manual steps
dotnet clean
dotnet restore
dotnet build --configuration Release /p:TreatWarningsAsErrors=true
dotnet test --configuration Release --no-build
```

### Project-specific Issues
```bash
# Check specific layer
./build-project.ps1 -Project Domain
./build-project.ps1 -Project Application
./build-project.ps1 -Project Infrastructure
./build-project.ps1 -Project Web.Api
```

---

## 📝 Notes

- **Release Configuration**: Bắt buộc để enable tất cả SonarAnalyzer rules như CI/CD
- **TreatWarningsAsErrors**: Biến warnings thành errors để match CI/CD behavior
- **Verbosity**: Dùng `normal` để thấy chi tiết lỗi, `minimal` cho output ngắn gọn
- **No-restore/No-build**: Tối ưu performance khi chạy nhiều commands liên tiếp

💡 **Tip**: Bookmark file này và chạy `check.ps1` thường xuyên để tránh surprise trên CI/CD!
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
<!-- <ItemGroup Condition="'$(MSBuildProjectExtension)' != '.dcproj'">
<PackageReference Include="SonarAnalyzer.CSharp">
<ItemGroup Condition="'$(MSBuildProjectExtension)' != '.dcproj'">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> -->
</ItemGroup>
</Project>
Loading