Skip to content

Commit

Permalink
feat(workflows): spellcheck commit messages & misc (#20)
Browse files Browse the repository at this point in the history
* fix(workflows): set write permission on release

* feat(workflows): spellcheck commit messages

* misc: update workflow docs

* misc: rename cspell.config to .cspell.config

* fix(workflows): missing vars

* fix(workflows): missing vars

* docs: scripts

* misc(workflows): bump actions
  • Loading branch information
tcodes0 authored Sep 8, 2024
1 parent 15108e2 commit 8454958
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 64 deletions.
File renamed without changes.
29 changes: 16 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
shell: ${{ steps.list-changed-files.outputs.shell_all_changed_files }}
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7

- name: Changed files
id: list-changed-files
uses: tj-actions/changed-files@v44
uses: tj-actions/changed-files@v45.0.1
with:
# since_last_remote_commit: true # use only for testing, it's easy to produce no changed files
files_yaml_from_source_file: .changed-files.yml
Expand All @@ -47,7 +47,7 @@ jobs:
BASH_ENV: ./lib.sh
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0
submodules: true
Expand All @@ -56,16 +56,19 @@ jobs:
run: echo '{}' > package-lock.json

- name: Setup Node.js environment
uses: actions/[email protected].2
uses: actions/[email protected].3
with:
node-version: lts/iron
cache: npm
- run: npm --global install @commitlint/config-conventional
- run: |
npm --global install @commitlint/config-conventional
npm install --global @commitlint/[email protected]
npm install --global [email protected]
- name: Run commitlint
- name: Lint commits with commitlint and spellcheck
env:
CONFIG_PATH: .commitlintrc.yml
VERSION: 19.3.0
COMMITLINT_CONFIG_PATH: .commitlintrc.yml
CSPELL_CONFIG_PATH: .cspell.config.yml
# will not be set when running on push/merge to main
BASE_REF: ${{ github.event.pull_request.base.ref }}
run: ./workflows/main/commit_lint.sh
Expand All @@ -77,7 +80,7 @@ jobs:
if: needs.changed-files.outputs.shell
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7

- name: Lint and format
uses: luizm/[email protected]
Expand All @@ -99,7 +102,7 @@ jobs:
FILES: ${{ needs.changed-files.outputs.config }}
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7

- name: Prettier
uses: actionsx/prettier@v3
Expand All @@ -114,11 +117,11 @@ jobs:
if: (needs.changed-files.outputs.shell || needs.changed-files.outputs.doc) && github.event.local != true
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7

- name: Cspell
uses: streetsidesoftware/cspell-action@v6.4.0
uses: streetsidesoftware/cspell-action@v6.8.1
with:
files: "."
incremental_files_only: true
config: ./cspell.config.yml
config: ./.cspell.config.yml
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ on:
pull_request:
branches: ["main"]

permissions:
contents: write

jobs:
release:
name: release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7
with:
submodules: true

Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/release_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ on:
workflow_dispatch:
inputs:
url:
description: Github repository URL to point commit links at
description: Github repository URL to point links at, prefixed 'https://github.com/'
required: true
type: string
title:
description: Release title; new version and date will be added
type: string
tag_prefix:
description: Prefix to be concatenated to semver tag, i.e ${PREFIX}v1.0.0
tag_prefixes:
description: Comma separated prefixes to find tags, i.e ${PREFIX}v1.0.0
type: string

jobs:
Expand All @@ -26,14 +26,14 @@ jobs:
echo expected main, got: ${{ github.ref }} && exit 1
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7
with:
fetch-tags: true
fetch-depth: 0
submodules: true

- name: Set up Go
uses: actions/setup-go@v5
uses: actions/setup-go@v5.0.2
with:
go-version: 1.23
- run: go install github.com/tcodes0/go/cmd/changelog@latest
Expand All @@ -45,10 +45,10 @@ jobs:
BASH_ENV: ./lib.sh
TAGS_FILE: tags.txt
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./workflows/release/changelog.sh "${{ inputs.url }}" "${{ inputs.title }}" "${{ inputs.tag_prefix }}"
run: ./workflows/release/changelog.sh "${{ inputs.url }}" "${{ inputs.title }}" "${{ inputs.tag_prefixes }}"

- name: Open PR
uses: peter-evans/create-pull-request@v6.1.0
uses: peter-evans/create-pull-request@v7.0.1
if: github.event.local != true
with:
commit-message: "chore: release ${{ inputs.title }}"
Expand Down
10 changes: 4 additions & 6 deletions migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOF
}

# Description: Validates user input
# Globals : MIGRATIONS_DIR (github workflow)
# Globals : MIGRATIONS_DIR (github workflow or .env)
# Args : $@
# STDERR : Might print errors
# Returns : 1 if fail
Expand All @@ -31,19 +31,17 @@ validate() {
local name=${1-}

if [ ! "${MIGRATIONS_DIR:-}" ]; then
err $LINENO "missing MIGRATIONS_DIR env variable"
return 1
fatal $LINENO "missing MIGRATIONS_DIR env variable"
fi

if [ ! "$name" ]; then
err $LINENO "missing migration name"
usage
return 1
fatal $LINENO "missing migration name"
fi
}

# Description: Create a new timestamped migration file
# Globals : MIGRATIONS_DIR (github workflow)
# Globals : MIGRATIONS_DIR (github workflow or .env)
# Args : 1=(up | down) 2=name
# STDOUT : Path to the new file
# Sideeffects: Creates a new file
Expand Down
105 changes: 68 additions & 37 deletions workflows/main/commit_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@ trap 'err $LINENO' ERR

CONVENTIONAL_COMMITS_URL="See https://www.conventionalcommits.org/en/v1.0.0/"

# Description: Validates user input
# Globals : BASE_REF (github workflow)
# Sideeffects: Might exit the script with an error, might mutate BASE_REF
# Example : validate_input "$@"
validate() {
if [ ! "${BASE_REF:-}" ]; then
BASE_REF=main
fi

if ! command -v commitlint >/dev/null; then
fatal $LINENO "commitlint not found"
fi

if ! command -v cspell >/dev/null; then
fatal $LINENO "cspell not found"
fi
}

# Description: Parses a git log and checks if the commits are conventional
# Globals : CONFIG_PATH (lib.sh)
# Globals : COMMITLINT_CONFIG_PATH (github workflow)
# Args : 1=git log
# STDOUT : Problems found
# Example : lint_commits "$log"
lint_commits() {
local log="$1" problems="" out

while read -r commit; do
out="$(commitlint --config="$CONFIG_PATH" <<<"$commit" || true)"
out="$(commitlint --config="$COMMITLINT_CONFIG_PATH" <<<"$commit" || true)"

if [ "$out" ]; then
problems+=("$out")
Expand All @@ -35,22 +53,8 @@ lint_commits() {
printf %s "${problems[*]}"
}

# Description: Validates user input
# Globals : BASE_REF, VERSION (github workflow)
# Sideeffects: Might install commitlint, might mutate BASE_REF
# Example : validate_input "$@"
validate() {
if [ ! "${BASE_REF:-}" ]; then
BASE_REF=main
fi

if ! command -v commitlint >/dev/null; then
npm install --global @commitlint/cli@"$VERSION" >/dev/null
fi
}

# Description: Check if PR title is a conventional commit
# Globals : PR_TITLE (github workflow), CONFIG_PATH (lib.sh), CONVENTIONAL_COMMITS_URL (script)
# Globals : PR_TITLE, COMMITLINT_CONFIG_PATH (github workflow), CONVENTIONAL_COMMITS_URL (script)
# STDOUT : Messages
# STDERR : Might print errors and logs
# Returns : 1 if fail
Expand All @@ -62,28 +66,25 @@ lint_title() {

log $LINENO "$PR_TITLE"

if ! commitlint --config="$CONFIG_PATH" <<<"$PR_TITLE"; then
err $LINENO "PR title must be a conventional commit, got: $PR_TITLE"
err $LINENO "$CONVENTIONAL_COMMITS_URL"
return 1
if ! commitlint --config="$COMMITLINT_CONFIG_PATH" <<<"$PR_TITLE"; then
fatal $LINENO "PR title must be a conventional commit, got: $PR_TITLE. See $CONVENTIONAL_COMMITS_URL"
fi

msgln PR title ok
}

# Description: Check if commit messages are conventional commits
# Globals : BASE_REF (github workflow), CONVENTIONAL_COMMITS_URL (script)
# STDOUT : Diagnostic report
# Args : 1=git log
# STDOUT : Ok message
# STDERR : Might print errors and logs
# Returns : 1 if fail
# Example : lint_log
# Sideeffects: Might exit the script with an error
# Example : lint_log "$git_log"
lint_log() {
local revision=refs/remotes/origin/"$BASE_REF"..HEAD total_commits bad_commits git_log issues
local revision="refs/remotes/origin/${BASE_REF}..HEAD" total_commits bad_commits git_log="$1" issues

log $LINENO git log "$revision"

git_log=$(git log --format=%s "$revision" --)

if [ ! "$git_log" ]; then
log $LINENO empty git log
return
Expand All @@ -94,28 +95,54 @@ lint_log() {
issues=$(lint_commits "$git_log")

if [ ! "$issues" ]; then
msgln "commits ok"
msgln "conventional commits ok"
return
fi

total_commits=$(wc -l <<<"$git_log")
bad_commits=$(grep -Eie input -c <<<"$issues")

command cat <<-EOF
fatal $LINENO "
commits:
"$git_log"
$git_log
linter\ output:
"$issues"
$issues
Commit messages not formatted properly: $bad_commits out of $total_commits commits
$CONVENTIONAL_COMMITS_URL
To fix all, try 'git rebase -i $revision', change bad commits to 'reword', fix messages and 'git push --force'
"
}

# Description: Check if commit messages are spelled properly
# Globals : CSPELL_CONFIG_PATH, BASE_REF (github workflow)
# Args : 1=git log
# STDOUT : Ok message
# STDERR : Might print errors and logs
# Sideeffects: Might exit the script with an error
# Example : spellcheck_log "$git_log"
spellcheck_log() {
# shellcheck disable=SC2155
local log="$1" issues=$(cspell --config="$CSPELL_CONFIG_PATH" stdin <<<"$git_log")
local revision="refs/remotes/origin/${BASE_REF}..HEAD"

if [ ! "$issues" ]; then
msgln "spellcheck commits ok"
return
fi

fatal $LINENO "
commits:
$git_log
"Commit messages not formatted properly: $bad_commits out of $total_commits commits"
"$CONVENTIONAL_COMMITS_URL"
"To fix all, try 'git rebase -i $revision', change bad commits to 'reword', fix messages and 'git push --force'"
EOF
linter\ output:
return 1
$issues
To fix all, try 'git rebase -i $revision', change bad commits to 'reword', fix messages and 'git push --force'
"
}

##############
Expand All @@ -130,4 +157,8 @@ fi

validate
lint_title
lint_log

git_log=$(git log --format=%s "refs/remotes/origin/${BASE_REF}..HEAD" --)

lint_log "$git_log"
spellcheck_log "$git_log"

0 comments on commit 8454958

Please sign in to comment.