Compare commits
1 Commits
7a7e47d987
...
chore/pre-
| Author | SHA1 | Date | |
|---|---|---|---|
| 925f1a092d |
@@ -10,21 +10,50 @@ jobs:
|
|||||||
runs-on: [self-hosted, linux]
|
runs-on: [self-hosted, linux]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Prepare git repository
|
||||||
uses: actions/checkout@v4
|
env:
|
||||||
with:
|
SERVER_URL: ${{ github.server_url }}
|
||||||
fetch-depth: 0
|
REPOSITORY: ${{ github.repository }}
|
||||||
|
GITHUB_TOKEN: ${{ github.token }}
|
||||||
|
BEFORE_SHA: ${{ github.event.before }}
|
||||||
|
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
|
||||||
|
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||||
|
HEAD_SHA: ${{ github.sha }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
- name: Setup Node.js
|
if [ ! -d .git ]; then
|
||||||
uses: actions/setup-node@v4
|
git init
|
||||||
with:
|
fi
|
||||||
node-version: 22
|
|
||||||
cache: npm
|
|
||||||
|
|
||||||
- name: Install dependencies
|
if git remote get-url origin >/dev/null 2>&1; then
|
||||||
run: npm ci --ignore-scripts
|
git remote set-url origin "${SERVER_URL}/${REPOSITORY}.git"
|
||||||
|
else
|
||||||
|
git remote add origin "${SERVER_URL}/${REPOSITORY}.git"
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Validate commit messages with commitlint
|
FETCH_CMD=(git fetch --force --prune --no-tags origin '+refs/heads/*:refs/remotes/origin/*')
|
||||||
|
if [ -n "${GITHUB_TOKEN:-}" ]; then
|
||||||
|
FETCH_CMD=(git -c "http.extraHeader=Authorization: Bearer ${GITHUB_TOKEN}" fetch --force --prune --no-tags origin '+refs/heads/*:refs/remotes/origin/*')
|
||||||
|
fi
|
||||||
|
|
||||||
|
"${FETCH_CMD[@]}"
|
||||||
|
|
||||||
|
for SHA in "${HEAD_SHA:-}" "${PR_BASE_SHA:-}" "${PR_HEAD_SHA:-}" "${BEFORE_SHA:-}"; do
|
||||||
|
if [ -n "${SHA}" ] && [ "${SHA}" != "0000000000000000000000000000000000000000" ]; then
|
||||||
|
if [ -n "${GITHUB_TOKEN:-}" ]; then
|
||||||
|
git -c "http.extraHeader=Authorization: Bearer ${GITHUB_TOKEN}" fetch --no-tags origin "${SHA}" || true
|
||||||
|
else
|
||||||
|
git fetch --no-tags origin "${SHA}" || true
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -n "${HEAD_SHA:-}" ] && git cat-file -e "${HEAD_SHA}^{commit}" 2>/dev/null; then
|
||||||
|
git checkout --detach "${HEAD_SHA}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Validate commit messages
|
||||||
env:
|
env:
|
||||||
EVENT_NAME: ${{ github.event_name }}
|
EVENT_NAME: ${{ github.event_name }}
|
||||||
BEFORE_SHA: ${{ github.event.before }}
|
BEFORE_SHA: ${{ github.event.before }}
|
||||||
@@ -36,16 +65,25 @@ jobs:
|
|||||||
|
|
||||||
BASE_SHA=""
|
BASE_SHA=""
|
||||||
TARGET_SHA="${HEAD_SHA}"
|
TARGET_SHA="${HEAD_SHA}"
|
||||||
|
BASE_HINT_SHA="${PR_BASE_SHA:-}"
|
||||||
|
|
||||||
if [ "${EVENT_NAME}" = "pull_request" ] && [ -n "${PR_HEAD_SHA:-}" ]; then
|
if [ "${EVENT_NAME}" = "pull_request" ]; then
|
||||||
TARGET_SHA="${PR_HEAD_SHA}"
|
if [ -n "${PR_HEAD_SHA:-}" ]; then
|
||||||
|
TARGET_SHA="${PR_HEAD_SHA}"
|
||||||
|
elif git rev-parse "${HEAD_SHA}^2" >/dev/null 2>&1; then
|
||||||
|
TARGET_SHA="$(git rev-parse "${HEAD_SHA}^2")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${BASE_HINT_SHA}" ] && git rev-parse "${HEAD_SHA}^1" >/dev/null 2>&1; then
|
||||||
|
BASE_HINT_SHA="$(git rev-parse "${HEAD_SHA}^1")"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${EVENT_NAME}" = "pull_request" ] && [ -n "${PR_BASE_SHA:-}" ] && [ -n "${TARGET_SHA:-}" ]; then
|
if [ "${EVENT_NAME}" = "pull_request" ] && [ -n "${BASE_HINT_SHA}" ] && [ -n "${TARGET_SHA:-}" ]; then
|
||||||
BASE_SHA="$(git merge-base "${PR_BASE_SHA}" "${TARGET_SHA}" || true)"
|
BASE_SHA="$(git merge-base "${BASE_HINT_SHA}" "${TARGET_SHA}" || true)"
|
||||||
|
|
||||||
if [ -z "${BASE_SHA}" ]; then
|
if [ -z "${BASE_SHA}" ]; then
|
||||||
BASE_SHA="${PR_BASE_SHA}"
|
BASE_SHA="${BASE_HINT_SHA}"
|
||||||
fi
|
fi
|
||||||
elif [ -n "${BEFORE_SHA:-}" ] && [ "${BEFORE_SHA}" != "0000000000000000000000000000000000000000" ]; then
|
elif [ -n "${BEFORE_SHA:-}" ] && [ "${BEFORE_SHA}" != "0000000000000000000000000000000000000000" ]; then
|
||||||
BASE_SHA="${BEFORE_SHA}"
|
BASE_SHA="${BEFORE_SHA}"
|
||||||
@@ -54,7 +92,38 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "${BASE_SHA}" ] && [ "${BASE_SHA}" != "${TARGET_SHA}" ]; then
|
if [ -n "${BASE_SHA}" ] && [ "${BASE_SHA}" != "${TARGET_SHA}" ]; then
|
||||||
npx --no -- commitlint --from "${BASE_SHA}" --to "${TARGET_SHA}" --verbose
|
MESSAGES="$(git log --format=%s "${BASE_SHA}..${TARGET_SHA}")"
|
||||||
else
|
else
|
||||||
git log -1 --format=%s "${TARGET_SHA}" | npx --no -- commitlint --verbose
|
MESSAGES="$(git log -1 --format=%s "${TARGET_SHA}")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${MESSAGES}" ]; then
|
||||||
|
MESSAGES="$(git log -1 --format=%s "${TARGET_SHA}")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
PATTERN='^(build|chore|ci|docs|feat|fix|perf|refactor|style|test)(\([a-z0-9._/-]+\))?(!)?: .+'
|
||||||
|
FAILED=0
|
||||||
|
|
||||||
|
while IFS= read -r SUBJECT; do
|
||||||
|
[ -z "${SUBJECT}" ] && continue
|
||||||
|
|
||||||
|
if [[ "${SUBJECT}" =~ ^Merge[[:space:]] ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${SUBJECT}" =~ ^Revert[[:space:]] ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! "${SUBJECT}" =~ ${PATTERN} ]]; then
|
||||||
|
echo "Invalid commit message: ${SUBJECT}"
|
||||||
|
FAILED=1
|
||||||
|
else
|
||||||
|
echo "OK: ${SUBJECT}"
|
||||||
|
fi
|
||||||
|
done <<< "${MESSAGES}"
|
||||||
|
|
||||||
|
if [ "${FAILED}" -ne 0 ]; then
|
||||||
|
echo "Conventional Commit check failed."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user