mirror of
https://github.com/captainark/github2email.git
synced 2024-11-14 13:37:39 +01:00
67 lines
2.2 KiB
Bash
Executable File
67 lines
2.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Function to clean temporary file
|
|
ExitCleanup() {
|
|
if [[ ! -z ${TempFile} ]]; then
|
|
[[ -f ${TempFile} ]] && rm ${TempFile}
|
|
fi
|
|
}
|
|
|
|
trap ExitCleanup EXIT
|
|
|
|
# shellcheck source=config
|
|
source "$(dirname $0)/config"
|
|
|
|
# Checking if UserName has been defined
|
|
if [[ -z ${UserName} ]] || [[ $UserName == "changemeplease" ]]; then
|
|
echo "GitHub username is incorrect of undefined in config file. Exiting..."
|
|
exit 2
|
|
fi
|
|
|
|
GitHubApi="https://api.github.com/users/${UserName}/starred"
|
|
LastPage="$(curl -sI ${GitHubApi} | grep -Eo 'page=[[:digit:]]+' | tail -n1)"
|
|
TempFile=$(mktemp -t github2email.sh.XXXXXXXXXX)
|
|
|
|
# Checking for dependencies
|
|
for Dependencies in curl jq r2e; do
|
|
if [[ ! -x $(which ${Dependencies}) ]]; then
|
|
echo "${Dependencies} is required to run to script. Exiting..."
|
|
exit 2
|
|
fi
|
|
done
|
|
|
|
for StarPages in $(seq 1 ${LastPage#*=}); do
|
|
curl -s "${GitHubApi}?page=${StarPages}" | jq -r '.[] | .name + "," + .html_url' | sed 's#$#/releases.atom#' >> ${TempFile}
|
|
done
|
|
|
|
# Checking for API request success
|
|
if [[ ! -s ${TempFile} ]]; then
|
|
echo "Something went wrong while fetching the project starred list for ${UserName}. Exiting..."
|
|
exit 2
|
|
fi
|
|
|
|
# Doing our magic stuff that does cool shit
|
|
for Star in $(cat ${TempFile}); do
|
|
ProjectName=${Star%,*}
|
|
ProjectUrl=${Star##*,}
|
|
# Only add a project to r2e config if it does not already exist
|
|
IsDeclaredProject="$(r2e list | grep ${ProjectUrl})"
|
|
if [[ -z ${IsDeclaredProject} ]]; then
|
|
r2e add g2e_${ProjectName} ${ProjectUrl}
|
|
# We only want to be informed of future releases
|
|
r2e run --no-send g2e_${ProjectName}
|
|
echo "Project ${ProjectName} has been added to rss2email configuration !"
|
|
fi
|
|
done
|
|
|
|
# Delete projects that were added to rss2email by this script but are no longer starred on GitHub
|
|
AddedProjects="$(r2e list | grep g2e_ | grep -Eo "https://github.com/.*/releases.atom")"
|
|
for Projects in ${AddedProjects}; do
|
|
StillStaredProject="$(grep ${Projects} ${TempFile})"
|
|
if [[ -z ${StillStaredProject} ]]; then
|
|
ProjectToRemove="$(echo ${Projects} | cut -d'/' -f5)"
|
|
r2e delete g2e_${ProjectToRemove}
|
|
echo "Project ${ProjectToRemove} has been removed from rss2email configuration !"
|
|
fi
|
|
done
|