GitHub: removing followers with the GitHub api

GitHub has a feature that allows other members to follow your account and receive alerts on actions you take in any repository.  Unfortunately, there is not a setting that allows you to disable this at a global level.

The only workaround is to temporarily block the user, which removes the relationship.  This can be done from the web UI manually, but takes effort as well as the discipline to do this periodically.

In this article, I will provide a script that uses basic curl commands to invoke the GitHub api and remove all followers.   Additionally, there is a Systemd service unit that can invoke this script once a day.

Create GitHub API personal access token

In order to invoke the GitHub API, you will need to authenticate.  The easiest way to do this is to create a personal access token as described in the official documentation here.

You will need to enable the “user” scopes as shown below.

Save the personal access token string, this will be used in the sections below.

GitHub API calls explained

Armed with the personal access token, we can use it to make calls to the GitHub API.  For example, here is the curl command that gets the currently logged in user info.

# personal access token
export github_pat=********

curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $github_pat" \
https://api.github.com/user

And here is the call that lists your current followers.

# your user id
export github_user=******

curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $github_pat" \
https://api.github.com/users/$github_user

We use a similar syntax to block and unblock users.

Download scripts

# download OS packages
sudo apt install curl git -y

# get github project code
git clone https://github.com/fabianlee/remove-github-followers.git
cd remove-github-followers

Run script manually

To run the script manually, just make sure the environment variable ‘github_pat’ representing the GitHub personal access token is defined first.

export github_pat=*****

# invoke script that calls github API and removes followers
./remove-github-followers.sh

Create Systemd timer that invokes script daily

If you would like to create a user-level Systemd timer that invokes this script on a daily basis.

export github_pat=*****

cd systemd-userlevel
./create-github-userservice.sh

# populate personal access token file in personal dir
vi ~/default/github

# log in personal dir
tail ~/log/github/github.log

The only caveat with user-level Systemd processes is they only run if the user is logged in.

If you would rather run a system level Systemd process then use the commands below.

export github_pat=*****

cd systemd-systemlevel
./create-github-service.sh

# populate personal access token file in global location
sudo vi /etc/default/github

# log in global location
sudo tail /var/log/github/github.log

 

REFERENCES

github, official doc describing how block removes user as a follower

stackoverflow, removing github follower by temporarily blocking

github, GitHub API reference

fabianlee.org, Systemd timer for running bash script periodically as user-level Systemd