|
| 1 | +# This workflow validates commit messages in a pull request to ensure they follow a specific pattern. |
| 2 | +# The pattern is defined as follows: |
| 3 | +# - The commit message must contain the string "[cherry-pick/XXX]" where XXX is a valid label. |
| 4 | +# - The commit message must also contain "(cherry picked from commit <commit_hash>)" where <commit_hash> is a 40-character SHA-1 hash. |
| 5 | +# - Merge commits are ignored. |
| 6 | +# |
| 7 | +# Copyright (c) Microsoft Corporation. |
| 8 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 9 | + |
| 10 | +name: Cherry-Pick Workflow |
| 11 | + |
| 12 | +on: |
| 13 | + pull_request: |
| 14 | + types: |
| 15 | + - edited |
| 16 | + - opened |
| 17 | + - reopened |
| 18 | + - synchronize |
| 19 | + workflow_dispatch: |
| 20 | + |
| 21 | +jobs: |
| 22 | + cherry-pick-job: |
| 23 | + if: contains(github.event.pull_request.title, '[cherry pick]') || contains(github.event.pull_request.title, '[cherry-pick]') |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@v2 |
| 29 | + with: |
| 30 | + fetch-depth: 0 # Fetch the full history to ensure all commits are available |
| 31 | + |
| 32 | + - name: Validate commit messages |
| 33 | + run: | |
| 34 | + base_branch="${{ github.event.pull_request.base.ref }}" |
| 35 | +
|
| 36 | + # Ensure the base branch is fetched |
| 37 | + git fetch origin $base_branch |
| 38 | + |
| 39 | + # Get the commits included in the PR |
| 40 | + commits=$(git log --pretty=format:%H origin/$base_branch..HEAD) |
| 41 | +
|
| 42 | + echo "Number of commits being reviewed: $(echo "$commits" | wc -w)" |
| 43 | +
|
| 44 | + for commit in $commits; do |
| 45 | + commit_message=$(git log --format=%B -n 1 "$commit") |
| 46 | +
|
| 47 | + if [[ "$commit_message" =~ ^Merge ]]; then |
| 48 | + echo "Warning: Commit $commit is a merge commit and will be ignored." |
| 49 | + continue |
| 50 | + fi |
| 51 | +
|
| 52 | + if [[ ! $(echo "$commit_message" | tr '[:upper:]' '[:lower:]') =~ \[cherry-pick\/.+\] ]] || [[ ! "$commit_message" =~ \(cherry\ picked\ from\ commit\ [0-9a-f]{40}\) ]]; then |
| 53 | + echo "Commit $commit does not meet the required pattern." |
| 54 | + echo "Commit message: $commit_message" |
| 55 | + echo "Please use 'git cherry-pick -x <commit_hash>' to cherry-pick the commit." |
| 56 | + exit 1 |
| 57 | + else |
| 58 | + echo "Commit $commit meets the required pattern." |
| 59 | + fi |
| 60 | + done |
| 61 | +
|
| 62 | + echo "All commit messages meet the required pattern."; |
0 commit comments