Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: validate PR titles and commits, and autoassign PRs #1227

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/scripts/colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Colors
export YLW='\033[1;33m'
export RED='\033[0;31m'
export GRN='\033[0;32m'
export BLU='\033[0;34m'
export BLD='\033[1m'
export RST='\033[0m'

# Clear line
export CLR='\033[2K'
4 changes: 4 additions & 0 deletions .github/scripts/commit_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

source .github/scripts/parse_commits.sh
parse_commits "$@"
30 changes: 30 additions & 0 deletions .github/scripts/parse_commits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

# The output of this script is as follows:
# 1. One line "checking commits between: <start_commit> <end_commit>"
# 2. One line for each commit message that is not well-formed

set -euo pipefail

source .github/scripts/colors.sh

parse_commits() {

BASE_BRANCH=${BASE_BRANCH:-master}

start_commit=${1:-origin/${BASE_BRANCH}}
end_commit=${2:-HEAD}
exit_code=0

echo -e "${GRN}Checking commits between:${RST} $start_commit $end_commit"
# Run the loop in the current shell using process substitution
while IFS= read -r message || [ -n "$message" ]; do
# Check if commit message follows conventional commits format
if [[ ! $message =~ ^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?:.*$ ]]; then
echo -e "${YLW}Commit message is ill-formed:${RST} $message"
exit_code=1
fi
done < <(git log --format=%s "$start_commit".."$end_commit")

exit ${exit_code}
}
12 changes: 12 additions & 0 deletions .github/workflows/auto_assign_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Auto Assign PR to Creator

on:
pull_request:
types:
- opened

jobs:
assign_creator:
runs-on: ubuntu-latest
steps:
- uses: toshimaru/[email protected]
77 changes: 77 additions & 0 deletions .github/workflows/commit_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: "Conventional Commits"

on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
jobs:
main:
name: Validate commit messages
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}

- name: Check commit message
id: check_commit_message
if: always()
run: |
set +e

base_sha=${{ github.event.pull_request.base.sha }}
head_sha=${{ github.event.pull_request.head.sha }}

output=$(.github/scripts/commit_check.sh "${base_sha}" "${head_sha}" 2>&1)
exit_code=$?

echo "${output}" | sed '$d'
echo "exit_code=${exit_code}" >> $GITHUB_OUTPUT

invalid_commit_messages=$(echo "${output}" | sed '1d;$d')
invalid_commit_messages=$(echo "${output}" | sed '1d;$d')
invalid_commit_messages=$(echo "${invalid_commit_messages}" | sed 's/\x1b\[[0-9;]*m//g') # Remove color codes
invalid_commit_messages=$(echo "${invalid_commit_messages}" | sed 's/^Commit message is ill-formed: //') # Remove prefix

if [[ $exit_code -ne 0 ]]; then
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "error_message<<$EOF" >> "$GITHUB_ENV"
echo "${invalid_commit_messages}" >> "$GITHUB_ENV"
echo "$EOF" >> "$GITHUB_ENV"
fi

- name: "Publish failed commit messages"
uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always() && (steps.check_commit_message.outputs.exit_code != 0)
with:
header: commit-message-lint-error
message: |
Commits must follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/)
Please fix these commit messages:
```
${{ env.error_message }}
```

# Delete a previous comment when the issue has been resolved
- name: "Delete previous comment"
if: ${{ steps.check_commit_message.outputs.exit_code == 0 }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: commit-message-lint-error
delete: true

- name: "Mark as failed"
if: steps.check_commit_message.outputs.exit_code != 0
uses: actions/github-script@v7
with:
script: |
core.setFailed("Some commit messages are ill-formed")
35 changes: 35 additions & 0 deletions .github/workflows/pr_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "Conventional Commits"

on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
jobs:
main:
name: Validate PR title
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: amannn/action-semantic-pull-request@v5
id: lint_pr_title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always() && (steps.lint_pr_title.outputs.error_message != null)
with:
header: pr-title-lint-error
message: |
Pull requests titles must follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/)

# Delete a previous comment when the issue has been resolved
- if: ${{ steps.lint_pr_title.outputs.error_message == null }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr-title-lint-error
delete: true
Loading