Skip to content

Commit 7ac06ba

Browse files
committed
feat: added pom check hooks
1 parent ca73db3 commit 7ac06ba

6 files changed

Lines changed: 49 additions & 4 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
A Pull Request should be used
66
entry: git-hooks/prevent-commit-on-default
77
language: script
8+
pass_filenames: false
9+
always_run: true
810
stages: [ pre-commit ]
911
minimum_pre_commit_version: 3.2.0
1012
- id: prevent-out-of-sync-commit
@@ -14,6 +16,8 @@
1416
A pull should be done first
1517
entry: git-hooks/prevent-out-of-sync-commit
1618
language: script
19+
pass_filenames: false
20+
always_run: true
1721
stages: [ pre-commit ]
1822
minimum_pre_commit_version: 3.2.0
1923
- id: git-fetch
@@ -25,3 +29,12 @@
2529
pass_filenames: false
2630
always_run: true
2731
stages: [ pre-commit ]
32+
- id: check-pom-xml
33+
name: check maven pom
34+
description: |
35+
Check a maven pom file
36+
entry: git-hooks/check-pom-xml
37+
language: script
38+
pass_filenames: false
39+
files: 'pom\.xml'
40+
stages: [ pre-commit ]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ In your scripts, you can use the following env:
212212

213213
The `pre-commit`:
214214

215-
* config is part of the template at [.pre-commit-config.yaml](copier-template/.pre-commit-config.yaml)
215+
* config is part of the template at [.pre-commit-config.yaml](copier-template/.pre-commit-config.yaml.jinja2)
216216
* setup is performed with direnv via [.envrc](copier-template/.envrc.jinja2)
217217
* extra `git hooks` are available at [git-hooks](git-hooks)
218218

copier-template/.github/dependabot.yml.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ updates:
66
- package-ecosystem: "maven"
77
directory: "/"
88
schedule:
9-
interval: daily
9+
interval: weekly
1010
ignore:
1111
- dependency-name: "org.slf4j:*"
1212
- dependency-name: "org.apache.logging.log4j:*"

copier-template/.pre-commit-config.yaml renamed to copier-template/.pre-commit-config.yaml.jinja2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ repos:
3636
additional_dependencies: [ '@commitlint/config-conventional' ]
3737
args: [ "--verbose", "--config", ".config/commitlint.config.js" ]
3838
- repo: https://github.com/combostrap/devfiles
39-
rev: v0.1.1
39+
rev: v0.1.2 # devfiles
4040
hooks:
4141
- id: prevent-out-of-sync-commit
4242
name: prevent out-of-sync branch commit
43+
{%- if package_manager == 'maven' %}
44+
- id: check-pom-xml
45+
name: check pom.xml file
46+
{%- endif %}
4347
- repo: https://github.com/editorconfig-checker/editorconfig-checker
4448
rev: c9d5ed0 # v3.4.1
4549
hooks:

git-hooks/check-pom-xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
# A hook to check for any error in a pom.xml file
3+
4+
set -TCEeuo pipefail
5+
6+
# validate the project is correct and all necessary information is available
7+
# https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
8+
if ! ./mvnw validate -Dorg.slf4j.simpleLogger.defaultLogLevel=warn; then
9+
echo "mvn validate command failed"
10+
exit 1
11+
fi
12+
# Unfortunately, missing version is a warning, not an error
13+
EFFECTIVE_POM=/tmp/effective-pom.xml
14+
# No warning, no error
15+
if ! LOG=$(./mvnw help:effective-pom -Dorg.slf4j.simpleLogger.defaultLogLevel=warn -Doutput="$EFFECTIVE_POM"); then
16+
printf 'Error mvn effective command failed\n%s' "$LOG"
17+
exit 1
18+
fi
19+
if [ "$LOG" != "" ]; then
20+
printf 'Error or warning seen in pom.xml file:\n%s' "$LOG"
21+
exit 1
22+
fi

release

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export GIT_CLIFF__BUMP__INITIAL_TAG=v0.1.0 # by default it's 0.1.0
1313
next_version=$(git cliff --unreleased --bump "$bump_type" --context --tag-pattern "v[0-9]+\\.[0-9]+\\.[0-9]+" | jq -r '.[0].version' | tr -d "v")
1414

1515
# Update in-place the version
16-
yq -i "(.repos[] | select(.repo | match(\".*$ORGANISATION_PATH_NAME/devfiles$\")) | .rev) = \"v$next_version\"" copier-template/.pre-commit-config.yaml
16+
# Backup: Bad in place update will empty the file
17+
cp copier-template/.pre-commit-config.yaml.jinja2 /tmp/.pre-commit-config.yaml.jinja2.bak
18+
if ! sed -i "s/rev: .*# devfiles/rev: v$next_version #devfiles/; t; Q1" copier-template/.pre-commit-config.yaml.jinja2; then
19+
echo "Substitution has not substituted anything"
20+
cp /tmp/.pre-commit-config.yaml.jinja2.bak copier-template/.pre-commit-config.yaml.jinja2
21+
exit 1
22+
fi
1723
git-commit "chore: release $next_version"
1824

1925
echo "Releasing to the version $next_version"

0 commit comments

Comments
 (0)