Skip to content

Commit 053d319

Browse files
committed
feat: use commitlint subaction instead of duplicating code
1 parent 2b62007 commit 053d319

File tree

2 files changed

+95
-85
lines changed

2 files changed

+95
-85
lines changed

action.yaml

+91-84
Original file line numberDiff line numberDiff line change
@@ -73,91 +73,98 @@ runs:
7373
- name: Install tools
7474
# This will skip the (slow) pyenv install if we triggered the above
7575
uses: open-turo/action-setup-tools@v1
76-
- name: Install @commitlint/cli
77-
shell: bash
78-
run: |
79-
npm install --no-save --no-package-lock --no-audit --no-fund \
80-
--prefix "$RUNNER_TEMP" "@commitlint/cli"
81-
echo "$RUNNER_TEMP/node_modules/.bin" >> $GITHUB_PATH
82-
echo "NODE_PATH=$RUNNER_TEMP/node_modules" >> $GITHUB_ENV
83-
- name: Use default conventional commit config
84-
if: inputs.turo-conventional-commit == 'true'
85-
shell: bash
86-
run: |
87-
# Write default commitlint config in the workspace.
88-
echo "extends: [\"@open-turo/commitlint-config-conventional\"]" \
89-
> "${{ inputs.config-file }}"
90-
# We have to list @commitlint/cli here otherwise `npm install` will try
91-
# to remove it from the above step. There is no way to prevent this
92-
# behavior that I'm aware of.
93-
npm install --no-save --no-package-lock --no-audit --no-fund \
94-
--prefix "$RUNNER_TEMP" \
95-
"@open-turo/commitlint-config-conventional" \
96-
"@commitlint/cli"
9776
- name: Commitlint
98-
if: hashFiles(inputs.config-file) != ''
99-
shell: bash
100-
run: |
101-
# Commitlint
102-
# Look for the base ref to compare against the current head in the
103-
# pull_request event metadata.
104-
if [[ -z "$FROM" ]]; then
105-
FROM="${{ github.event.pull_request.base.ref }}"
106-
if [[ -n "$FROM" ]]; then
107-
echo "::notice::Parsing base ref '$FROM'"
108-
# If we do have the base ref, it can be a branch name, so we want to
109-
# parse it. This will be a pass through for a SHA
110-
# via the event context, and we should try to find the default
111-
# branch instead (see below)
112-
if ! git show-ref --hash "$FROM" &>/dev/null; then
113-
# No such ref, so we try the next step
114-
FROM=""
115-
else
116-
FROM="$(git show-ref --hash "$FROM" | tail -n1)"
117-
fi
118-
fi
119-
fi
120-
# If we don't have a base ref, we try to find the history to the default
121-
# branch, e.g. main, which should be a sane check.
122-
if [[ -z "$FROM" ]]; then
123-
# This finds the default branch name from the remote information, e.g. "main", and then parses it to a SHA
124-
FROM="$(git show-ref --hash "$(git remote show $(git remote -v | grep push | awk '{print $2}') | grep 'HEAD branch' | awk '{print $3}')" | tail -n1)"
125-
echo "::notice::Could not find base ref, trying to use default branch"
126-
if ! git merge-base --is-ancestor "$FROM" HEAD; then
127-
echo "::warning::Default branch is not an ancestor of HEAD"
128-
FROM=""
129-
fi
130-
fi
131-
# If the pull_request event metadata doesn't exist, try to grab the
132-
# "before" commit from the push event metadata. This will usually only
133-
# give us a single commit to check, but it'll be the latest one.
134-
if [[ -z "$FROM" ]]; then
135-
# This is a SHA so no parsing needed
136-
FROM="${{ github.event.before }}"
137-
echo "::warning::Could not find ancestor ref, falling back"
138-
if ! git merge-base --is-ancestor "$FROM" HEAD; then
139-
echo "::warning::Before commit $FROM is not an ancestor of HEAD"
140-
else
141-
echo "::notice::Checking commits since $FROM (usually the latest commit)."
142-
fi
143-
fi
144-
# Default to looking at the last 20 commits otherwise.
145-
if [[ -z "$FROM" ]]; then
146-
FROM="HEAD~20"
147-
echo "::warning::Could not find commit range for commitlint"
148-
echo "::notice::Checking 20 most recent commit messages."
149-
fi
150-
CONFIG_FILE="${{ inputs.config-file }}"
151-
if [[ -n "$CONFIG_FILE" ]]; then
152-
CONFIG_FILE="--config $CONFIG_FILE"
153-
fi
154-
echo "::notice::Running commitlint"
155-
commitlint \
156-
$CONFIG_FILE \
157-
--verbose \
158-
--color \
159-
--from "$FROM" \
160-
--to HEAD
77+
# TODO: use a released version of this action
78+
uses: open-turo/action-pre-commit/commitlint@conditionalize-config
79+
with:
80+
restore-config: "false"
81+
config-file: ${{ inputs.config-file }}
82+
turo-conventional-commit: ${{ inputs.turo-conventional-commit }}
83+
# - name: Install @commitlint/cli
84+
# shell: bash
85+
# run: |
86+
# npm install --no-save --no-package-lock --no-audit --no-fund \
87+
# --prefix "$RUNNER_TEMP" "@commitlint/cli"
88+
# echo "$RUNNER_TEMP/node_modules/.bin" >> $GITHUB_PATH
89+
# echo "NODE_PATH=$RUNNER_TEMP/node_modules" >> $GITHUB_ENV
90+
# - name: Use default conventional commit config
91+
# if: inputs.turo-conventional-commit == 'true'
92+
# shell: bash
93+
# run: |
94+
# # Write default commitlint config in the workspace.
95+
# echo "extends: [\"@open-turo/commitlint-config-conventional\"]" \
96+
# > "${{ inputs.config-file }}"
97+
# # We have to list @commitlint/cli here otherwise `npm install` will try
98+
# # to remove it from the above step. There is no way to prevent this
99+
# # behavior that I'm aware of.
100+
# npm install --no-save --no-package-lock --no-audit --no-fund \
101+
# --prefix "$RUNNER_TEMP" \
102+
# "@open-turo/commitlint-config-conventional" \
103+
# "@commitlint/cli"
104+
# - name: Commitlint
105+
# if: hashFiles(inputs.config-file) != ''
106+
# shell: bash
107+
# run: |
108+
# # Commitlint
109+
# # Look for the base ref to compare against the current head in the
110+
# # pull_request event metadata.
111+
# if [[ -z "$FROM" ]]; then
112+
# FROM="${{ github.event.pull_request.base.ref }}"
113+
# if [[ -n "$FROM" ]]; then
114+
# echo "::notice::Parsing base ref '$FROM'"
115+
# # If we do have the base ref, it can be a branch name, so we want to
116+
# # parse it. This will be a pass through for a SHA
117+
# # via the event context, and we should try to find the default
118+
# # branch instead (see below)
119+
# if ! git show-ref --hash "$FROM" &>/dev/null; then
120+
# # No such ref, so we try the next step
121+
# FROM=""
122+
# else
123+
# FROM="$(git show-ref --hash "$FROM" | tail -n1)"
124+
# fi
125+
# fi
126+
# fi
127+
# # If we don't have a base ref, we try to find the history to the default
128+
# # branch, e.g. main, which should be a sane check.
129+
# if [[ -z "$FROM" ]]; then
130+
# # This finds the default branch name from the remote information, e.g. "main", and then parses it to a SHA
131+
# FROM="$(git show-ref --hash "$(git remote show $(git remote -v | grep push | awk '{print $2}') | grep 'HEAD branch' | awk '{print $3}')" | tail -n1)"
132+
# echo "::notice::Could not find base ref, trying to use default branch"
133+
# if ! git merge-base --is-ancestor "$FROM" HEAD; then
134+
# echo "::warning::Default branch is not an ancestor of HEAD"
135+
# FROM=""
136+
# fi
137+
# fi
138+
# # If the pull_request event metadata doesn't exist, try to grab the
139+
# # "before" commit from the push event metadata. This will usually only
140+
# # give us a single commit to check, but it'll be the latest one.
141+
# if [[ -z "$FROM" ]]; then
142+
# # This is a SHA so no parsing needed
143+
# FROM="${{ github.event.before }}"
144+
# echo "::warning::Could not find ancestor ref, falling back"
145+
# if ! git merge-base --is-ancestor "$FROM" HEAD; then
146+
# echo "::warning::Before commit $FROM is not an ancestor of HEAD"
147+
# else
148+
# echo "::notice::Checking commits since $FROM (usually the latest commit)."
149+
# fi
150+
# fi
151+
# # Default to looking at the last 20 commits otherwise.
152+
# if [[ -z "$FROM" ]]; then
153+
# FROM="HEAD~20"
154+
# echo "::warning::Could not find commit range for commitlint"
155+
# echo "::notice::Checking 20 most recent commit messages."
156+
# fi
157+
# CONFIG_FILE="${{ inputs.config-file }}"
158+
# if [[ -n "$CONFIG_FILE" ]]; then
159+
# CONFIG_FILE="--config $CONFIG_FILE"
160+
# fi
161+
# echo "::notice::Running commitlint"
162+
# commitlint \
163+
# $CONFIG_FILE \
164+
# --verbose \
165+
# --color \
166+
# --from "$FROM" \
167+
# --to HEAD
161168
- name: Get changed files
162169
uses: tj-actions/changed-files@v40
163170
if: inputs.only-changed == 'true'

commitlint/action.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ inputs:
99
required: true
1010
description: Set this to "false" to customize conventional commit config
1111
default: "true"
12+
restore-config:
13+
description: Set to "false" to skip the configuration restore step
14+
default: "true"
1215

1316
runs:
1417
using: composite
@@ -99,7 +102,7 @@ runs:
99102
--from "$FROM" \
100103
--to HEAD
101104
- name: Restore commitlint config
102-
if: always() && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true'
105+
if: always() && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true' && inputs.restore-config == 'true'
103106
shell: bash
104107
run: |
105108
# Restore the commitlint config file if we wrote it

0 commit comments

Comments
 (0)