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: Implement conventional commit validation #418

Merged
merged 2 commits into from
Dec 9, 2024
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
10 changes: 7 additions & 3 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ variables:
- template: build/variables.yml

stages:
- stage: Commit_Validation
mahdichtioui marked this conversation as resolved.
Show resolved Hide resolved
dependsOn: []
jobs:
- template: build/stage-commit-validation.yaml

#-if false
# This special if is used to remove those Dotnet_New stages for generated application.
- stage: Dotnet_New_GeneratedApp
dependsOn: Commit_Validation
jobs:
- template: .template.config/build/stage-donetnew.yaml

Expand Down Expand Up @@ -62,9 +68,7 @@ stages:

#-endif
- stage: Build_Staging
#-if false
dependsOn: []
#-endif
dependsOn: Commit_Validation
jobs:
- template: build/stage-build.yml
parameters:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

Prefix your items with `(Template)` if the change is about the template and not the resulting application.

## 3.6.X
- Added conventional commit validation stage `stage-commit-validation.yml`

## 3.5.X
- Bump Uno packages to 5.2.121 to fix a crash on iOS.
Expand Down
7 changes: 7 additions & 0 deletions build/stage-commit-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This stage is responsible for running the template to validate the commits of the PR
jobs:
- job: OnWindows_ValidateCommits
pool:
vmImage : $(windowsHostedAgentImage)
steps:
- template: templates/validate-commits.yaml
46 changes: 46 additions & 0 deletions build/templates/validate-commits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This template is used to validate that the commit messages follow the Conventional Commits specification (https://www.conventionalcommits.org/en/v1.0.0/).
# Consider placing this at the beginning of the build pipeline to ensure that the commits are valid before proceeding with longer build steps.
steps:
- task: PowerShell@2
condition: eq(variables['Build.Reason'], 'PullRequest')
inputs:
targetType: 'inline'
script: |
# Pre-Validation Logging
Write-Host "Starting PR Validation..."
Write-Host "Source Branch: $(System.PullRequest.SourceBranch)"
Write-Host "Target Branch: $(System.PullRequest.TargetBranch)"
Write-Host "Pull Request ID: $(System.PullRequest.PullRequestId)"
write-Host "Repository: $(Build.Repository.Name)"
Write-Host "Build.SourceBranch: $(Build.SourceBranch)"

# Fetch commit range
Write-Host "Retrieving commits..."
git fetch origin
Write-Host "Commit Range: origin/$(System.PullRequest.TargetBranch)..origin/$(System.PullRequest.SourceBranch)"
$commits = git log origin/$(System.PullRequest.TargetBranch)..origin/$(System.PullRequest.SourceBranch) --pretty=format:"%H %B"
$commitCount = ($commits | Measure-Object).Count
Write-Host "Commits found: $commitCount"

# Regex pattern for Conventional Commits
$pattern = '^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\.\-]+\))?(!)?: ([\w ])+([\s\S]*)|^(Merged PR \d+: .+)|^(Merge pull request #/d+ from .+)|^(Merge branch .+)'
Write-Host "Regular Expression: $pattern"

# Validate each commit message
$invalidCommits = @()
foreach ($commit in $commits -split "`n") {
$commitMessage = $commit.Substring($commit.IndexOf(" ") + 1)
Write-Host "Validating commit: $commitMessage"

if ($commitMessage -notmatch $pattern) {
$invalidCommits += $commitMessage
}
}

if($invalidCommits.count -gt 0) {
Write-Error "The following commit messages do no follow the Conventional Commits standard: `n$($invalidCommits -join "`n")"
exit 1
} else {
Write-Host "All commit messages are valid."
}
displayName: 'Validate Commit Messages'
Loading