Skip to content

Instantly share code, notes, and snippets.

@electrickite
Last active November 13, 2022 02:00
Show Gist options
  • Save electrickite/ba7e734752ee90f04587a24eb6d58b04 to your computer and use it in GitHub Desktop.
Save electrickite/ba7e734752ee90f04587a24eb6d58b04 to your computer and use it in GitHub Desktop.
Shell script to check for new Github releases
#!/bin/bash
# Send email notification of new Github project releases
usage () {
cat <<EOF
Usage: release-check.sh [OPTION]... PROJECT...
Check for new releases on Github PROJECTs
Options:
-h Print this help text
-t ADDRESS Send email notifications to ADDRESS
-f ADDRESS Optional From email ADDRESS
-l PATH Path to last release file
Default: last_release
EOF
}
last_release_path="last_release"
from=""
to=""
while getopts ":ht:f:l:" opt; do
case $opt in
h)
usage
exit 0
;;
t)
to="$OPTARG"
;;
f)
from="$OPTARG"
;;
l)
last_release_path="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
echo "No projects specified!" >&2
usage >&2
exit 1
fi
if [ ! -f "$last_release_path" ]; then
touch "$last_release_path"
fi
for project
do
last_release="$(grep -w "$project" "$last_release_path" | cut -f 2)"
url="https://github.com/$project/releases.atom"
feed="$(curl --silent --fail "$url")"
if [ $? -ne 0 ]; then
echo "Error fetching feed!" >&2
continue
fi
latest_release="$(echo $feed | xmllint --xpath "//*[local-name()='entry'][1]/*[local-name()='title']/text()" -)"
if [ $? -ne 0 ]; then
echo "Error parsing feed!" >&2
continue
fi
if [ "$latest_release" != "$last_release" ]; then
echo "New release found for $project"
echo "New release: $latest_release"
echo "Previous release: $last_release"
if [ -n "$last_release" ]; then
sed -i '' "s|$project .*|$project $latest_release|" "$last_release_path"
else
printf '%s\t%s\n' "$project" "$latest_release" >> "$last_release_path"
fi
if [ -n "$to" ]; then
from_arg=""
if [ -n "$from" ]; then
from_arg="-r $from"
fi
mailx -s "New release for $project" $from_arg "$to" <<EOF
There is a new release available for $project
The latest release is $latest_release
EOF
if [ $? -ne 0 ]; then
echo "Error sending notification email!" >&2
fi
fi
else
echo "No new releases for $project"
echo "Current release: $last_release"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment