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

[CI] Script to help know if localized pages need updating #4599

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from 1 commit
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
103 changes: 103 additions & 0 deletions scripts/lsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash
#
# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh

EXTRA_DIFF_ARGS="--numstat"
TARGET_PATHS=""

function _usage() {
cat <<EOS
Usage: $(basename "$0") [options] TARGET_PATH ...

For each localized page target, this script reports whether the English
language version of that page has changed since the localized file was
first written or last edited.

TARGET_PATH can be a single markdown file of a localized page, such as
'content/ja/_index.md', or a directory of localized pages, such as 'content/ja'.

-h Output this usage info.
-d Output diff details.
-v Verbose mode.
EOS
}

function usage() {
local status=${1:-0}
_usage 1>&2
exit $status
}

function process_CLI_args() {
while getopts ":hdv" opt; do
case $opt in
h)
usage
;;
d)
EXTRA_DIFF_ARGS=""
;;
v)
VERBOSE=1
;;
\?)
echo "ERROR: unrecognized flag: -$OPTARG"
usage 1
;;
esac
done

shift $((OPTIND-1))
if [ "$#" -lt 1 ]; then
echo "ERROR: target path argument is missing" >&2
usage 1
fi

TARGET_PATHS="$@"

if [[ -f "TARGET_PATHS" && ! -e "$TARGET_PATHS" ]] ; then
echo "Path not found: '$TARGET_PATHS'" >&2
exit 2
fi
}

function main() {
process_CLI_args "$@"

if [ -f "$TARGET_PATHS" ] ; then
TARGETS="$TARGET_PATHS"
else
TARGETS=$(find $TARGET_PATHS -name "*.md")
if [[ -z "$TARGETS" ]]; then
echo "ERROR: target directory contains no markdown files: '$TARGET_PATHS'" >&2
exit 1
fi
# if [[ -n $VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi
fi

SYNCED=1
for f in $TARGETS; do
# if [[ -n $VERBOSE ]]; then echo -e "Checking\t$f"; fi
EN_VERSION=$(echo "$f" | sed "s/content\/.\{2,5\}\//content\/en\//g")
if [[ ! -e "$EN_VERSION" ]]; then
echo "Base file renamed or removed: $EN_VERSION"
SYNCED=0
continue
fi

LASTCOMMIT=$(git log -n 1 --pretty=format:%h -- "$f")
git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION"
if [ $? -ne 0 ] ; then
SYNCED=0
elif [[ -n $VERBOSE ]]; then
echo -e "File is in sync\t$f"
fi
done
if [ $SYNCED -ne 1 ]; then
exit 1
fi

echo "$TARGET_PATHS is still in sync"
}

main "$@"