Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Made pre-commit hook require dependency changes #2172

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/../bin/update-third-party-notices.sh"
./"$(dirname "$0")/../bin/update-third-party-notices.sh"

npx lint-staged
77 changes: 73 additions & 4 deletions bin/update-third-party-notices.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
#
# Checks all staged files for
# package.json. If it changes
Expand All @@ -8,15 +8,84 @@

STAGED_FILES=$(git diff-index --cached --name-only HEAD)

PKG_HAS_CHANGED=0
for FILE in $STAGED_FILES
do
if [ $FILE == "package.json" ]; then
RUN_THIRD_PARTY=1
if [ ${FILE} == "package.json" ]; then
PKG_HAS_CHANGED=1
break
fi
done

if [ -n "$RUN_THIRD_PARTY" ]; then
if [ ${PKG_HAS_CHANGED} -eq 0 ]; then
echo "package.json has not changed"
exit 0
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add an echo here so we now it's a no-op. same for at the end when it doesn't updating third party deps?


if ! [ -x "$(command -v jq)" ]; then
echo "Please install jq."
echo "https://jqlang.github.io/jq/"
exit 1
fi

MAIN_PKG=$(git show main:package.json | jq -rc)
HEAD_PKG=$(git cat-file blob :package.json | jq -rc)

MAIN_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.dependencies | length')
HEAD_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.dependencies | length')
MAIN_OPT_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.optionalDependencies | length')
HEAD_OPT_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.optionalDependencies | length')
MAIN_DEV_DEPS_LEN=$(echo "${MAIN_PKG}" | jq '.devDependencies | length')
HEAD_DEV_DEPS_LEN=$(echo "${HEAD_PKG}" | jq '.devDependencies | length')

RUN_THIRD_PARTY=0
if [ ${MAIN_DEPS_LEN} -ne ${HEAD_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
elif [ ${MAIN_OPT_DEPS_LEN} -ne ${HEAD_OPT_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
elif [ ${MAIN_DEV_DEPS_LEN} -ne ${HEAD_DEV_DEPS_LEN} ]; then
RUN_THIRD_PARTY=1
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.dependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.dependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.optionalDependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.optionalDependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 0 ]; then
for dep in $(echo "${MAIN_PKG}" | jq -rc '.devDependencies | to_entries | .[]'); do
NAME=$(echo ${dep} | jq -r '.key')
VERSION=$(echo ${dep} | jq -r '.value')
HEAD_VERSION=$(echo "${HEAD_PKG}" | jq -rc --arg pkg "${NAME}" '.devDependencies[$pkg]')
if [ "${VERSION}" != "${HEAD_VERSION}" ]; then
RUN_THIRD_PARTY=1
break
fi
done
fi

if [ ${RUN_THIRD_PARTY} -eq 1 ]; then
echo "package.json changed, running oss manifest and notices"
npm run third-party-updates
else
echo "package.json has not changed"
fi
Loading