lusipad/Azure-Commitlog-Check

Repository files navigation

English | 中文

A tool for checking whether commit messages in Azure DevOps (TFS) Pull Requests comply with standards, especially suitable for PRs using squash commit method.

  • Connect to Azure DevOps (TFS) services
  • Retrieve detailed information about specific PRs
  • Check if PR titles conform to commit message standards
  • Support custom commit message validation with regex patterns
  • Automatically detect the PR of the current branch without manual PR ID specification
  • .NET 8.0
  • Azure DevOps API
  • System.CommandLine (command-line parsing)
  1. Clone the repository

  2. Build the project

    cd Azure-Commitlog-Check
    dotnet build
  3. Run the check (two options):

    a. With auto-detection (recommended):

    dotnet run -- --url "https://dev.azure.com/yourorganization" --token "your-pat-token" --project "your-project" --auto-detect

    b. With manual PR ID:

    dotnet run -- --url "https://dev.azure.com/yourorganization" --token "your-pat-token" --project "your-project" --pr-id 12345
ParameterDescriptionRequired
--urlAzure DevOps server URLYes
--tokenPersonal Access Token (PAT)Yes
--projectAzure DevOps project nameYes
--pr-idPull Request ID (optional if --auto-detect used)No
--repositoryRepository name (for auto-detection)No
--auto-detectAuto-detect PR of current branchNo
--patternRegex pattern for validationNo
--quietOnly output final resultNo

The tool uses the following regex pattern by default:

^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?: .{1,50}

This pattern requires commit messages to follow this format:

  • Must start with a type: feat, fix, docs, style, refactor, perf, test, chore
  • Optional scope in parentheses, like feat(login)
  • Colon and space followed by 1-50 characters description

You can customize the regex pattern using the --pattern parameter:

dotnet run -- --url "https://dev.azure.com/yourorganization" --token "your-pat-token" --project "your-project" --pr-id 12345 --pattern "^(feat|fix|custom)(\(.+\))?: .+"

If server auto-generated merge messages (like "Merged PR 123: Title content") don't comply with your commit standards, you can use a more permissive regex pattern:

# Allow standard format or auto-merge PR format
dotnet run -- --auto-detect --pattern "^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?: .+|^Merged\s+PR\s+\d+:.*"

Or exclude specific PR checks (use only in special cases):

# Skip check if PR title matches auto-merge format
if ($prTitle -match "^Merged\s+PR\s+\d+:") { exit 0 } else { azcommitcheck --auto-detect }
CodeMeaningTypical Scenario
0SuccessAll commits meet standards
1Invalid Commit MessageCommit message format is incorrect
2PR Not FoundSpecified PR ID doesn't exist
3No CommitsPR has no commit records
4API ErrorNetwork issues/token expired
5Git Not FoundMissing Git environment variables
6No PR For BranchBranch not associated with any PR

This tool can also be used as an Azure DevOps Pipeline extension, allowing you to automatically check commit messages in your CI/CD workflows.

  1. Install the "Azure Commitlog Check" extension from the Azure DevOps Marketplace

    • Search for "Azure Commitlog Check"
    • Click the "Install" button to add the extension to your organization
  2. Grant the necessary permissions for your pipelines

    • Make sure to enable the "Allow scripts to access OAuth token" option in your pipeline settings

If you want to modify or customize this extension, follow these steps:

  1. Build the project and copy the executable to the extension task folder

    # Build the project
    dotnet publish -c Debug -r win-x64 --self-contained false
    
    # Copy the executable to the extension task folder
    copy Azure-Commitlog-Check\bin\Debug\net8.0\win-x64\Azure-Commitlog-Check.exe Azure-Commitlog-Check\extension\task\
  2. Ensure there's an extension icon in the images directory

    # Create images directory if it doesn't exist
    mkdir -p Azure-Commitlog-Check\extension\images
    
    # Add an extension icon
    # Example: copy your-icon.png Azure-Commitlog-Check\extension\images\extension-icon.png
  3. Package the extension

    # Install the TFS Cross Platform Command Line Interface if you don't have it
    npm install -g tfx-cli
    
    # Package the extension
    cd Azure-Commitlog-Check\extension
    tfx extension create --manifest-globs vss-extension.json
  4. Upload the generated .vsix file to the Azure DevOps Marketplace

# Example azure-pipelines.yml
trigger:
- main
- feature/*

pool:
  vmImage: 'windows-latest'

steps:
- checkout: self
  fetchDepth: 0  # Required for PR auto-detection to work correctly
  
# Make sure to enable OAuth token access for this task
- task: AzureCommitlogCheck@1
  inputs:
    autoDetect: true  # Auto-detect PR from current branch
    # repository: 'MyRepository'  # Optional: specify if auto-detection has issues
    pattern: '^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?: .{1,50}'  # Optional: customize pattern
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)  # Required for API access
Input ParameterDescriptionDefault
autoDetectAuto-detect PR of current branchtrue
pullRequestIdManual PR ID (ignored if autoDetect=true)
repositoryRepository name for auto-detectionAuto-detected from env
patternRegex pattern for validationStandard pattern

For the task to work correctly, you need to:

  1. Enable "Allow scripts to access the OAuth token" in the pipeline settings
  2. Grant sufficient permissions to the build service account to access Pull Request information

OAuth Settings

This task is ideal for use in build validation policies. To set up:

  1. Go to your repository settings
  2. Navigate to Policies > Branch Policies
  3. Add a build validation policy and select your pipeline
  4. Configure the policy to run on PR creation/updates

This project uses Flow and automated releases through Actions:

  • Continuous Integration: Automatically runs build and tests when Pull Requests are submitted to the master branch
  • Automated Release: When version tags (e.g., v1.0.0) are pushed, Actions will automatically:
    • Build the project
    • Create release packages for Windows, Linux, and macOS
    • Publish a new Release with the packaged files

This project is licensed under the MIT License

About

A tool for checking whether commit messages in Azure DevOps (TFS) Pull Requests comply with standards, especially suitable for PRs using squash commit method.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published