|
#!/bin/bash |
|
|
|
#set -ex |
|
set -o pipefail |
|
|
|
UPSTREAM_MASTER_BRANCH=master |
|
UPSTREAM_MASTER_USER=default |
|
UPSTREAM_MASTER_REPO=default |
|
|
|
FORK_USER=test |
|
FORK_BRANCH=master |
|
FORK_COUNT=0 |
|
|
|
CURRENT_REPO="$(get_fork_user ${n}):$(get_fork_branch ${n})" |
|
|
|
API_BASE_URL=https://api.github.com |
|
|
|
# Take user input. This could be smoothed out. |
|
echo "Input the user/repo combination you would like to check:" |
|
read -p "User: " UPSTREAM_MASTER_USER |
|
read -p "Repo: " UPSTREAM_MASTER_REPO |
|
echo "Checking $UPSTREAM_MASTER_USER:$UPSTREAM_MASTER_REPO:$UPSTREAM_MASTER_BRANCH..." |
|
|
|
# Gets some environment tests out of the way |
|
# Check for curl |
|
if type -P curl 2>/dev/null; then |
|
echo "curl appears to be installed" |
|
else |
|
echo "curl not installed, aborting for now." |
|
exit |
|
fi |
|
|
|
# Check for `jq`. Should be present. Will handle failure later |
|
if type -P jq 2>/dev/null; then |
|
echo "jq appears to be installed" |
|
else |
|
echo "jq not installed, aborting for now." |
|
exit |
|
fi |
|
|
|
# Can we reach the GitHub API? |
|
if $(curl --silent --fail https://api.github.com 1>/dev/null); then |
|
echo "Successfully reached GitHub API" |
|
else |
|
echo "Could not reach GitHub API. Are you connected to the internet?" |
|
exit |
|
fi |
|
|
|
# Given a specific fork, return the name of the user |
|
get_fork_user () { |
|
curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/forks | jq -r ".["$1"].owner.login" |
|
} |
|
# Given a specific fork, return the the default branch |
|
get_fork_branch () { |
|
curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/forks | jq -r ".["$1"].default_branch" |
|
} |
|
|
|
# Get a list of forks of a given repo and whether its behind or ahead |
|
fork_status () { |
|
FORK_COUNT=$(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/forks | jq -r ". | length") |
|
for n in $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/forks | jq -r ".|keys[]") |
|
do |
|
CURRENT_REPO="$(get_fork_user ${n}):$(get_fork_branch ${n})" |
|
echo "$(get_fork_user ${n}):$(get_fork_branch ${n})" |
|
if [[ $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".status") = "ahead" ]]; then |
|
echo "is ahead by $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".ahead_by")" |
|
elif [[ $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".status") = "behind" ]]; then |
|
echo "is behind by $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".behind_by")" |
|
else |
|
echo "$(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".status"): \ |
|
behind: $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".behind_by"), \ |
|
ahead: $(curl --silent --fail $API_BASE_URL/repos/$UPSTREAM_MASTER_USER/$UPSTREAM_MASTER_REPO/compare/$UPSTREAM_MASTER_BRANCH...$CURRENT_REPO | jq -r ".ahead_by")" |
|
fi |
|
done |
|
} |
|
|
|
fork_status |
|
|
|
echo "Forks: ${FORK_COUNT}. Done." |