From 094d835811714afcf8cc9137b23e56f6deb846c2 Mon Sep 17 00:00:00 2001 From: mahdichtioui Date: Thu, 28 Nov 2024 12:52:14 +0100 Subject: [PATCH] ci: Implement conventional commit validation stage --- build/azure-pipelines.yml | 11 +++++-- build/stage-commit-validation.yaml | 7 ++++ build/templates/validate-commits.yaml | 46 +++++++++++++++++++++++++++ src/cli/CHANGELOG.md | 3 ++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 build/stage-commit-validation.yaml create mode 100644 build/templates/validate-commits.yaml diff --git a/build/azure-pipelines.yml b/build/azure-pipelines.yml index c3d77275..1ebda78b 100644 --- a/build/azure-pipelines.yml +++ b/build/azure-pipelines.yml @@ -30,8 +30,14 @@ variables: - template: variables.yml stages: +- stage: Commit_Validation + dependsOn: [] + jobs: + - template: stage-commit-validation.yaml + #-if false - stage: CopyTool_GeneratedApp + dependsOn: Commit_Validation jobs: - template: ../src/cli/.azuredevops/stage-test-cli.yaml @@ -67,10 +73,9 @@ stages: - template: ../src/cli/.azuredevops/stage-publish-cli.yml #-endif + - stage: Build_Staging -#-if false - dependsOn: [] # This removes the implicit dependency on previous stage and causes this to run in parallel. -#-endif + dependsOn: Commit_Validation jobs: - template: stage-build.yml parameters: diff --git a/build/stage-commit-validation.yaml b/build/stage-commit-validation.yaml new file mode 100644 index 00000000..7bba2f7e --- /dev/null +++ b/build/stage-commit-validation.yaml @@ -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 \ No newline at end of file diff --git a/build/templates/validate-commits.yaml b/build/templates/validate-commits.yaml new file mode 100644 index 00000000..c78fa16f --- /dev/null +++ b/build/templates/validate-commits.yaml @@ -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 .+)' + 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' \ No newline at end of file diff --git a/src/cli/CHANGELOG.md b/src/cli/CHANGELOG.md index a5ec128b..c99ef7b9 100644 --- a/src/cli/CHANGELOG.md +++ b/src/cli/CHANGELOG.md @@ -5,6 +5,9 @@ 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. +## 0.21.1 +- Added conventional commit validation stage `stage-build.yml` + ## 0.21.0 - Add bugsee sdk in Fluttter template - Update build stage in `steps-build-android.yml` and `steps-build-ios` providing bugsee token