• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1name: lint-commits
2
3on:
4  pull_request:
5
6concurrency:
7  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
8  cancel-in-progress: true
9
10jobs:
11  checkCommits:
12    name: check commits
13    runs-on: ubuntu-latest
14    steps:
15      - name: Checkout repository
16        uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
17        with:
18          fetch-depth: 0
19
20      - name: Get list of commits
21        run: |
22          read PR_BASE PR_TIP < <(curl -L \
23            -H "Accept: application/vnd.github.v3+json" \
24            -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
25            -H "X-GitHub-Api-Version: 2022-11-28" \
26            ${{ github.event.pull_request.url }} | \
27            tee /dev/stderr | \
28            jq -r '"\(.base.sha) \(.head.sha)"' | \
29            tee /dev/stderr)
30            echo "PR_TIP=${PR_TIP}" >> "${GITHUB_ENV}"
31            echo "PR_BASE=${PR_BASE}" >> "${GITHUB_ENV}"
32
33      - name: Check commit prefixes
34        run: |
35          if [[ -z "${PR_TIP}" || -z "${PR_BASE}" ]]; then
36              echo "::error title=invalid base and head:: Failed to retrieve base and head for Pull Request"
37              # Fail workflow
38              false
39          fi
40          START_MARKER='sync: Update libbpf submodule'
41          END_MARKER='sync: Pull latest bpftool changes from kernel'
42          declare -a PREFIXES=(
43              "ci"
44              "mirror"
45          )
46
47          misformed=0
48          syncing=0
49          while read commit ; do
50              valid=1
51              sha="${commit%% *}"
52              object="${commit#* }"
53              case "${object}" in
54                  "${START_MARKER}")
55                      syncing=1
56                      ;;
57                  "${END_MARKER}")
58                      syncing=0
59                      ;;
60                  *)
61                      if [[ "${syncing}" == 0 ]]; then
62                          valid=0
63                          for prefix in "${PREFIXES[@]}"; do
64                              if [[ "${object}" =~ ^"${prefix}: " ]]; then
65                                  valid=1
66                                  break
67                              fi
68                          done
69                      fi
70                      ;;
71              esac
72              if [[ "${valid}" = 1 ]]; then
73                  echo "::notice title=valid prefix::${sha} (\"${object}\") has a valid prefix"
74              else
75                  echo "::error title=invalid prefix::${sha} (\"${object}\") does not have a valid prefix"
76                  misformed=$((misformed+1))
77              fi
78          done < <(git log --format='%h %s' --reverse ${{ env.PR_BASE }}..${{ env.PR_TIP }})
79
80          echo "::notice ::Found ${misformed} invalid commit object(s)"
81          if [[ "${misformed}" != 0 ]]; then
82              echo "Please ensure all commits not part of kernel sync are prefixed with one of:"
83              echo "    ${PREFIXES[@]/%/:}"
84              # Fail workflow
85              false
86          fi
87