fader_master/scripts/ci-publish-badges.sh

107 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
set -eu
usage() {
echo "usage: $0 ROOT OWNER PROJECT DEFAULT_BRANCH BADGE [BADGE ...]" >&2
exit 2
}
[ "$#" -ge 5 ] || usage
root=$1
owner=$2
project=$3
default_branch=$4
shift 4
case "$root" in
/*) ;;
*) echo "publication root must be absolute: $root" >&2; exit 2 ;;
esac
[ -d "$root" ] && [ ! -L "$root" ] && [ -w "$root" ] || {
echo "publication root must be an existing, writable, non-symlink directory: $root" >&2
exit 2
}
validate_component() {
component_name=$1
component_value=$2
case "$component_value" in
''|.|..|*[!A-Za-z0-9._-]*)
echo "$component_name is not a safe path component: $component_value" >&2
exit 2
;;
esac
}
validate_component owner "$owner"
validate_component project "$project"
validate_component default-branch "$default_branch"
seen=' '
for badge_name in "$@"; do
case "$badge_name" in
coverage|test|version) ;;
*) echo "unsupported badge name: $badge_name" >&2; exit 2 ;;
esac
case "$seen" in
*" $badge_name "*) echo "duplicate badge name: $badge_name" >&2; exit 2 ;;
esac
seen="${seen}${badge_name} "
source_file="coverage/${badge_name}.svg"
[ -f "$source_file" ] && [ ! -L "$source_file" ] && [ -s "$source_file" ] || {
echo "badge source must be a regular, nonempty, non-symlink file: $source_file" >&2
exit 2
}
done
ensure_directory() {
directory=$1
if [ -e "$directory" ] || [ -L "$directory" ]; then
[ -d "$directory" ] && [ ! -L "$directory" ] || {
echo "publication path component is not a real directory: $directory" >&2
exit 2
}
else
mkdir -m 0755 "$directory"
fi
chmod 0755 "$directory"
}
owner_dir="$root/$owner"
project_dir="$owner_dir/$project"
destination="$project_dir/$default_branch"
ensure_directory "$owner_dir"
ensure_directory "$project_dir"
if [ -L "$destination" ]; then
unlink "$destination"
elif [ -e "$destination" ] && [ ! -d "$destination" ]; then
echo "default-branch destination is not a directory: $destination" >&2
exit 2
fi
ensure_directory "$destination"
temporary_files=
cleanup() {
for temporary_file in $temporary_files; do
rm -f "$temporary_file"
done
}
trap cleanup EXIT
trap 'cleanup; exit 1' HUP INT TERM
sequence=0
for badge_name in "$@"; do
sequence=$((sequence + 1))
source_file="coverage/${badge_name}.svg"
temporary_file=$(mktemp "$destination/.${badge_name}.svg.tmp.${sequence}.XXXXXX")
temporary_files="$temporary_files $temporary_file"
cp "$source_file" "$temporary_file"
chmod 0644 "$temporary_file"
mv "$temporary_file" "$destination/${badge_name}.svg"
done
temporary_files=
trap - EXIT HUP INT TERM