|
| 1 | +name: Commit message format check |
| 2 | +on: |
| 3 | + # ATTENTION: See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ |
| 4 | + # re security implications of using this trigger; in particular, no code from PR branches must |
| 5 | + # be executed in any flows triggered by it |
| 6 | + pull_request_target: |
| 7 | + branches: |
| 8 | + - main |
| 9 | + - 1.* |
| 10 | + - 2.* |
| 11 | + - 3.* |
| 12 | + - 4.* |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + name: Commit message |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Commit messages in format debezium/dbz#xxx |
| 20 | + id: check |
| 21 | + env: |
| 22 | + pull_request_number: ${{ github.event.pull_request.number }} |
| 23 | + github_repository: ${{ github.repository }} |
| 24 | + run: | |
| 25 | + echo "PREFIX_COMMITS=false" >> $GITHUB_OUTPUT |
| 26 | +
|
| 27 | + make_api_call_with_retries() { |
| 28 | + local URL="$1" |
| 29 | + local MAX_RETRIES="${2:-3}" |
| 30 | + local DELAY="${3:-2}" |
| 31 | + local ATTEMPT=1 |
| 32 | +
|
| 33 | + if [[ -z "$URL" ]]; then |
| 34 | + echo "::error:: No URL provided to call_api_with_retries." >&2 |
| 35 | + return 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + while [ $ATTEMPT -le $MAX_RETRIES ]; do |
| 39 | + RESPONSE=$(curl --silent -w "%{http_code}" -X "GET" $URL) |
| 40 | + HTTP_CODE="${RESPONSE: -3}" |
| 41 | + BODY="${RESPONSE:: -3}" |
| 42 | +
|
| 43 | + if [ "$HTTP_CODE" -eq 200 ]; then |
| 44 | + echo "$BODY" |
| 45 | + return 0 |
| 46 | + else |
| 47 | + sleep "$DELAY" |
| 48 | + DELAY=$((DELAY * 2)) |
| 49 | + fi |
| 50 | +
|
| 51 | + ATTEMPT=$((ATTEMPT + 1)) |
| 52 | + done |
| 53 | +
|
| 54 | + echo "::error:: No luck getting a successful response after $MAX_RETRIES retries." >&2 |
| 55 | + return 1 |
| 56 | + } |
| 57 | +
|
| 58 | + if ! RESPONSE=$(make_api_call_with_retries "https://api.github.com/repos/$github_repository/pulls/$pull_request_number/commits"); then |
| 59 | + echo "::error:: No luck getting the commit messages." |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | +
|
| 63 | + MESSAGE=$(echo "$RESPONSE" | jq -r '.[].commit.message // empty') |
| 64 | + if [[ "$MESSAGE" == "API rate limit exceeded"* ]]; then |
| 65 | + echo "::error:: Github API rate limit exceeded. Skipping commit message check." |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + NON_PREFIX_COMMITS="" |
| 70 | + echo "$RESPONSE" | jq -r '.[] | .commit.message | split("\n")[0]' > COMMIT_MSGS.txt |
| 71 | +
|
| 72 | + while IFS= read -r line; |
| 73 | + do |
| 74 | + echo "-> checking: $line" |
| 75 | + if [[ ! $line =~ (^debezium/dbz#[[:digit:]]+)|(DBZ-[[:digit:]]+)|(\[release\])|(\[jenkins-jobs\])|(\[docs\])|(\[maven-release-plugin\])|(\[ci\]) ]]; then |
| 76 | + NON_PREFIX_COMMITS="${NON_PREFIX_COMMITS} -> ${line}\n" |
| 77 | + fi |
| 78 | + done < COMMIT_MSGS.txt |
| 79 | +
|
| 80 | + if [[ -n $NON_PREFIX_COMMITS ]]; then |
| 81 | + echo "========================================================================" |
| 82 | + echo " COMMIT MESSAGES WITH MISSING \"debezium/dbz\" PREFIX" |
| 83 | + echo "========================================================================" |
| 84 | + echo -e "$NON_PREFIX_COMMITS" |
| 85 | + echo "PREFIX_COMMITS=false" >> $GITHUB_OUTPUT |
| 86 | + else |
| 87 | + echo "All commit messages are properly prefixed." |
| 88 | + echo "PREFIX_COMMITS=true" >> $GITHUB_OUTPUT |
| 89 | + fi |
| 90 | +
|
| 91 | + - name: Create Comment |
| 92 | + if: ${{ steps.check.outputs.PREFIX_COMMITS == 'false' }} |
| 93 | + uses: peter-evans/create-or-update-comment@v5 |
| 94 | + with: |
| 95 | + issue-number: ${{ github.event.pull_request.number }} |
| 96 | + body: | |
| 97 | + Hi @${{ github.event.pull_request.user.login }}, thanks for your contribution. Please prefix the commit message(s) with the [debezium/dbz#xxx GitHub issue key](https://github.com/debezium/debezium/blob/main/CONTRIBUTING.md#making-changes). |
| 98 | +
|
| 99 | + - name: Check failure |
| 100 | + if: ${{ steps.check.outputs.PREFIX_COMMITS == 'false' }} |
| 101 | + uses: actions/github-script@v9 |
| 102 | + continue-on-error: false |
| 103 | + with: |
| 104 | + script: | |
| 105 | + throw new Error('Commit has no debezium/dbz#xxx prefix') |
0 commit comments