Skip to content

Commit

Permalink
[DT-142] ih-pre-commit integration (#40)
Browse files Browse the repository at this point in the history
This adds a new, custom pre-commit handler (ih-pre-commit) which resides
in the image-builder repository. This change updates the git step to
remove its installation of the standard pre-commit hook, and updates the
toolrepos step to install the ih-pre-commit step as a template and to
update all existing IH repositories.

This also adds a couple useful tools as dependency, yq and ripgrep.
  • Loading branch information
SteveRuble authored Apr 3, 2023
1 parent c33cb79 commit db65553
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 41 deletions.
10 changes: 10 additions & 0 deletions .ih-pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
languages:
- markdown
- pem
- plist
- shell
- text
- yaml
ignored_directories:
- formula/
- Casks/
49 changes: 35 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-executables-have-shebangs
- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 2.1.5
hooks:
- id: shellcheck
- id: script-must-have-extension
- id: script-must-not-have-extension
- id: shfmt
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-xml
- id: no-commit-to-branch
args:
- --branch
- master
- --branch
- main
- --branch
- trunk
- --branch
- develop
- id: detect-private-key
- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 2.1.5
hooks:
- id: shellcheck
- id: script-must-have-extension
- id: script-must-not-have-extension
- id: shfmt
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.20
hooks:
- id: shellcheck
types:
- shell
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.25
0.1.26
6 changes: 4 additions & 2 deletions formula/ih-core.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class IhCore < Formula
VERSION="0.1.25"
VERSION="0.1.26"
desc "Brew formula for installing core tools used at Included Health engineering."
homepage "https://github.com/ConsultingMD/homebrew-ih-public"
license "CC BY-NC-ND 4.0"
Expand All @@ -13,6 +13,7 @@ class IhCore < Formula
depends_on "git"
depends_on "gnu-getopt"
depends_on "jq"
depends_on "yq"
depends_on "go-jira"
depends_on "virtualenv"
depends_on "envconsul"
Expand All @@ -21,6 +22,7 @@ class IhCore < Formula
depends_on "yamllint"
depends_on "wget"
depends_on "rancher-cli"
depends_on "ripgrep"

def install
lib.install Dir["lib/*"]
Expand All @@ -30,7 +32,7 @@ def install

def caveat

"Run `ih-setup install` to install IH compontents"
"Run `ih-setup install` to install IH components"

end

Expand Down
13 changes: 1 addition & 12 deletions lib/core/git/step.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function ih::setup::core.git::help() {
This will not overwrite any existing settings that you have set.
- Create $GR_HOME if it doesn't exist.
- Create a default global .gitignore if one doesn't exist.
- Set up pre-commit to install automatically when repos are cloned."
- Set up linting pre-commit hook to be installed automatically when repos are cloned."

}

Expand Down Expand Up @@ -53,17 +53,6 @@ function ih::setup::core.git::install() {
# make git use ssh for everything
git config --global [email protected]:ConsultingMD/.insteadOf https://github.com/ConsultingMD/

# set up pre-commit to automatically be set up for all cloned repos,
# if the user doesn't have a templateDir already
if git config --global init.templateDir; then
ih::log::warn "Detected existing templateDir, not setting up pre-commit auto-enable."
else
local GIT_TEMPLATE_DIR="${IH_DIR}/git-template"
mkdir "$GIT_TEMPLATE_DIR" 2>/dev/null || :
git config --global init.templateDir "$GIT_TEMPLATE_DIR"
pre-commit init-templatedir "$GIT_TEMPLATE_DIR"
fi

# Make sure the desired src directory exists if GR_HOME is declared
[[ -n ${GR_HOME} ]] && mkdir -p "${GR_HOME}"

Expand Down
46 changes: 34 additions & 12 deletions lib/core/toolrepos/step.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ function ih::setup::core.toolrepos::help() {
This repo contains tools for
- Clone the ConsultingMD/image-builder repo
This repo contains tools for building images
and for setting up development enviroments"
and for setting up development environments"
}

function ih::setup::core.toolrepos::test() {
ih::setup::core.toolrepos::test-or-install "test"
}

function ih::setup::core.toolrepos::deps() {
# echo "other steps"
echo "core.github"
}

Expand All @@ -27,7 +26,6 @@ function ih::setup::core.toolrepos::install() {
# If $1 is "test", this will check if install is needed and return 1 if it is.
# Otherwise, this will install the repos.
function ih::setup::core.toolrepos::test-or-install() {

if [ "$1" == "install" ]; then
mkdir -p "${GR_HOME}"
fi
Expand All @@ -37,15 +35,15 @@ function ih::setup::core.toolrepos::test-or-install() {
return 1
fi
ih::log::info "Cloning engineering repo..."
git clone [email protected]:ConsultingMD/engineering.git --filter=blob:limit=1m --depth=5 "${GR_HOME}/engineering" || return
git clone [email protected]:ConsultingMD/engineering.git --filter=blob:limit=1m "${GR_HOME}/engineering" || return
fi

if [ ! -d "${GR_HOME}/image-builder" ]; then
if [ "$1" == "test" ]; then
return 1
fi
ih::log::info "Cloning image-builder repo.."
git clone [email protected]:ConsultingMD/image-builder.git --filter=blob:limit=1m --depth=5 "${GR_HOME}/image-builder" || return
git clone [email protected]:ConsultingMD/image-builder.git --filter=blob:limit=1m "${GR_HOME}/image-builder" || return
fi

if [ ! -d "${GR_HOME}/kore" ]; then
Expand All @@ -67,14 +65,37 @@ function ih::setup::core.toolrepos::test-or-install() {
ih::setup::core.toolrepos::set-auto-update-repositories-job
fi

if [ "$1" = "test" ]; then
ih::file::check-file-in-sync "$toolsrepo_src_path" "$toolsrepo_tgt_path"
return
if ! ih::file::check-file-in-sync "$toolsrepo_src_path" "$toolsrepo_tgt_path"; then
if [ "$1" = "test" ]; then
return 1
fi
cp -f "$toolsrepo_src_path" "$toolsrepo_tgt_path"
export IH_WANT_RE_SOURCE=1
fi

export IH_WANT_RE_SOURCE=1
PRE_COMMIT_HOOK_DST="${IH_DIR}/git-template/hooks/pre-commit"
if [ ! -f "$PRE_COMMIT_HOOK_DST" ]; then
if [ "$1" == "test" ]; then
return 1
fi

cp -f "$toolsrepo_src_path" "$toolsrepo_tgt_path"
export IMAGE_BUILDER_ROOT="${IH_HOME}/image-builder"
(
cd "$IMAGE_BUILDER_ROOT" || exit
CURRENT_BRANCH=$(git branch --show-current)
if [ "$CURRENT_BRANCH" != "master" ]; then
ih::log::warn "You have a non-master branch of image-builder checked out.
You will need to check out and pull the master branch and run
'ih-pre-commit install --global'
manually in order to have pre-commit configured correctly."
return 1
else
git pull
ih::log::info "Installing ih-pre-commit hook everywhere..."
"$IMAGE_BUILDER_ROOT/bin/ih-pre-commit" install --global
fi
)
fi
}

function ih::setup::core.toolrepos::set-auto-update-repositories-job() {
Expand All @@ -84,11 +105,12 @@ function ih::setup::core.toolrepos::set-auto-update-repositories-job() {
PLIST_FILE="com.includedhealth.auto-update-repositories"
LAUNCH_AGENTS_PATH="${HOME}/Library/LaunchAgents/${PLIST_FILE}.plist"

# shellcheck disable=SC2001
GR_HOME_ESC=$(echo "$GR_HOME" | sed 's_/_\\/_g')

sed "s/\$IH_HOME/${GR_HOME_ESC}/g" "${THIS_DIR}/${PLIST_FILE}.plist" > "${LAUNCH_AGENTS_PATH}"
sed "s/\$IH_HOME/${GR_HOME_ESC}/g" "${THIS_DIR}/${PLIST_FILE}.plist" >"${LAUNCH_AGENTS_PATH}"

if launchctl list | grep -q ${PLIST_FILE} ; then
if launchctl list | grep -q ${PLIST_FILE}; then
launchctl unload "${LAUNCH_AGENTS_PATH}"
fi

Expand Down
5 changes: 5 additions & 0 deletions lib/utils/file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ function ih::file::check-file-in-sync() {
return 1
fi

ih::log::debug "Checking for differences between $SRC and $DST ..."

if ! diff -q "$DST" "$SRC" >/dev/null; then
ih::log::debug "File $DST does not match source"
return 1
fi

ih::log::debug "Files match"

}

# Returns 0 if the directory at $2 has all
Expand Down

0 comments on commit db65553

Please sign in to comment.