Skip to content

Instantly share code, notes, and snippets.

@leoh0
Last active April 25, 2019 11:15
Show Gist options
  • Save leoh0/06f763c23b46e1f07c05a4df7c9abdc2 to your computer and use it in GitHub Desktop.
Save leoh0/06f763c23b46e1f07c05a4df7c9abdc2 to your computer and use it in GitHub Desktop.
github contribution as a calendar

Setup

# COMMIT_USERNAME is for your commit user name
export COMMIT_USERNAME="Eohyung Lee"

# COMMIT_USERNAME is for your commit email
export [email protected]

# GITHUB_USERNAME is for github user name(or organization name)
export GITHUB_USERNAME=leoh0

# GITHUB_REPOSITORY is for pushed dummy datas which included commits for github contribution calendar.
# Attention!: This repository is always *DELETED* before it is created because of cleansing github contribution calendar. So please use disposable repository.
export GITHUB_REPOSITORY=d

# GITHUB_APITOKEN is for github API
# This token MUST include follow permissions. `delete_repo`, `repo`
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
export GITHUB_APITOKEN=*******************************************

Run

curl https://gist.githubusercontent.com/leoh0/06f763c23b46e1f07c05a4df7c9abdc2/raw/fd90af927cbd264c233998ca3ee4483fa78aa6ff/github-contribution-as-a-calendar.sh | bash
#!/bin/bash
set -e
set -o pipefail
USEPRIVATE=${USEPRIVATE:-false}
if [[ -z "$COMMIT_USERNAME" \
|| -z "$COMMIT_USEREMAIL" \
|| -z "$GITHUB_USERNAME" \
|| -z "$GITHUB_REPOSITORY" \
|| -z "$GITHUB_APITOKEN" ]]; then
echo "You need set below variables before run this script"
echo
echo "# COMMIT_USERNAME is for your commit user name"
echo "export COMMIT_USERNAME="
echo
echo "# COMMIT_USEREMAIL is for your commit email"
echo "export COMMIT_USEREMAIL="
echo
echo "# GITHUB_USERNAME is for github user name"
echo "export GITHUB_USERNAME="
echo
echo "# GITHUB_REPOSITORY is for pushed dummy datas which included commits for github contribution calendar."
echo "# Attention!: This repository is always *DELETED* before it is created because of cleansing github contribution calendar. So please use disposable repository."
echo "export GITHUB_REPOSITORY="
echo
echo "# GITHUB_APITOKEN is for github API"
echo "# This token MUST include follow permissions. delete_repo, repo"
echo "# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line"
echo "export GITHUB_APITOKEN="
exit 1
fi
# setup date for using timezone
if [ ! -d "/usr/share/zoneinfo" ]; then
export DEBIAN_FRONTEND=noninteractive
ERROR="ERROR: Installing timezone package is failed so you need to install "
ERROR="$ERROR timezone package by yourself before run this script."
sudo apt update
sudo apt install -y tzdata || ( echo "$ERROR" ; exit 1 )
fi
# setup github-contribution bin
mkdir -p "$HOME/bin"
if [[ "$USEPRIVATE" == true ]]; then
PRIVATE=", \\\"private\\\": true"
fi
cat << EOF > "$HOME/bin/update-github-contribution.sh"
#!/bin/bash
set -e
set -o pipefail
if [[ "\$(env TZ=Asia/Seoul date +%H)" != "00" ]]; then
echo "Only working at KST(Asia/Seoul) midnight"
exit 0
fi
curl -s -S -n -XDELETE \\
"https://api.github.com/repos/$GITHUB_USERNAME/$GITHUB_REPOSITORY"
curl -s -S -f -n -XPOST \\
-d "{\"name\": \"$GITHUB_REPOSITORY\"$PRIVATE}" \\
"https://api.github.com/user/repos"
docker run \\
--rm \\
--name github-spray \\
-i \\
-e "USERNAME=$COMMIT_USERNAME" \\
-e "USEREMAIL=$COMMIT_USEREMAIL" \\
-v $HOME/.netrc:/root/.netrc \\
leoh0/github-spray \\
-t "\$(env TZ=Asia/Seoul date '+%x')" \\
--multiplier 10 \\
--push \\
--origin https://github.com/$GITHUB_USERNAME/$GITHUB_REPOSITORY.git
EOF
chmod +x "$HOME/bin/update-github-contribution.sh"
# setup netrc
cat << EOF > "$HOME/.netrc"
machine github.com
login $GITHUB_USERNAME
password $GITHUB_APITOKEN
machine api.github.com
login $GITHUB_USERNAME
password $GITHUB_APITOKEN
EOF
# setup cron
if ! crontab -l | grep -q update-github-contribution.sh; then
# ensure exist
sudo touch "/var/spool/cron/crontabs/$USER"
sudo chmod 600 "/var/spool/cron/crontabs/$USER"
sudo chown "$USER".crontab "/var/spool/cron/crontabs/$USER"
# add to cron
crontab -l > tempcron
# work every our for supporting different timezone
echo "0 * * * * $HOME/bin/update-github-contribution.sh 2>&1 | \
/usr/bin/logger -t github-contribution" >> tempcron
crontab tempcron
rm -f tempcron
fi
# setup docker
if ! command -v docker >/dev/null ; then
curl https://releases.rancher.com/install-docker/18.09.sh | sh
sudo usermod -aG docker "$USER"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment