summaryrefslogtreecommitdiff
path: root/.github/workflows/check_commit_messages.yml
blob: 2a3e5c530231cd35e380cfd52d9d53eefdf7529e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: Check commit prefix

on:
  pull_request:
    types: [edited, opened, synchronize, reopened, ready_for_review]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  check-commit-prefix:
    # Only trigger on the main Django repository
    if: github.repository == 'django/django' && startsWith(github.event.pull_request.base.ref, 'stable/')
    runs-on: ubuntu-latest
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v5
        with:
          persist-credentials: false

      - name: Calculate commit prefix
        id: vars
        env:
          BASE: ${{ github.event.pull_request.base.ref }}
        run: |
          echo "BASE=$BASE" >> $GITHUB_ENV
          VERSION="${BASE#stable/}"
          echo "prefix=[$VERSION]" >> $GITHUB_OUTPUT

      - name: Check PR title prefix
        env:
          TITLE: ${{ github.event.pull_request.title }}
          PREFIX: ${{ steps.vars.outputs.prefix }}
        run: |
          if [[ "$TITLE" != "$PREFIX"* ]]; then
            echo "❌ PR title must start with the required prefix: $PREFIX"
            exit 1
          fi
          echo "✅ PR title has the required prefix."

      - name: Fetch relevant branches
        run: |
          git fetch origin $BASE:base
          git fetch origin pull/${{ github.event.pull_request.number }}/head:pr

      - name: Check commit messages prefix
        env:
          PREFIX: ${{ steps.vars.outputs.prefix }}
        run: |
          COMMITS=$(git rev-list base..pr)
          echo "Checking commit messages for required prefix: $PREFIX"
          FAIL=0
          for SHA in $COMMITS; do
            MSG=$(git log -1 --pretty=%s $SHA)
            echo "Checking commit $SHA: $MSG"
            if [[ "$MSG" != "$PREFIX"* ]]; then
              echo "❌ Commit $SHA must start with the required prefix: $PREFIX"
              FAIL=1
            fi
          done

          if [[ $FAIL -eq 1 ]]; then
            echo "One or more commit messages are missing the required prefix."
            exit 1
          fi

          echo "✅ All commits have the required prefix."

  check-commit-suffix:
    # Only trigger on the main Django repository
    if: github.repository == 'django/django'
    runs-on: ubuntu-latest
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Fetch relevant branches
        run: |
          git fetch origin $BASE:base
          git fetch origin pull/${{ github.event.pull_request.number }}/head:pr

      - name: Check commit messages suffix
        run: |
          COMMITS=$(git rev-list base..pr)
          echo "Checking commit messages for trailing period"
          FAIL=0
          for SHA in $COMMITS; do
            MSG=$(git log -1 --pretty=%s $SHA)
            echo "Checking commit $SHA: $MSG"
            if [[ "${MSG: -1}" != "." ]]; then
              echo "❌ Commit $SHA must end with a period."
              echo "Refer to the guidelines linked in the PR checklist for commit message format."
              echo "For how to rewrite commit messages, see https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/working-with-git/#editing-commit-messages"
              FAIL=1
            fi
          done

          if [[ $FAIL -eq 1 ]]; then
            echo "One or more commit messages are missing a trailing period."
            exit 1
          fi

          echo "✅ All commits have the required trailing period."