0

Follower Sync

The follower-sync action automates GitHub follower management, saving time and effort.

GitHub Follower Management

GitHub Actions provide a powerful way to automate tasks within your repositories. One such task is managing followers, which can be streamlined using custom actions. Let's explore follower-sync, a GitHub Action I created to automate follower management on GitHub.

Understanding the Action

The follower-sync action is designed to handle two main operations

  1. following users who are following you but you're not following back.
  2. unfollow users whom you're following but aren't follow you back.

Let's break down the key components of this action.

Dependencies

The action utilizes several dependencies:

  • @actions/core For interacting with GitHub Actions core library.
  • @octokit/rest Enables interaction with the GitHub API.

Configuration Inputs

The action accepts the following inputs:

  • personal-access-token GitHub personal access token for authentication.
  • each-operation-delay Optional delay between each operation.
  • should-unfollow Boolean flag to enable/disable unfollow user.
  • should-follow Boolean flag to enable/disable follow user.
  • safe-list List of users to exclude from unfollow.
example.ts
const access = core.getInput("personal-access-token", { required: true });
const delay = parseNumber(core.getInput("each-operation-delay"));
const shouldUnfollow = core.getBooleanInput("should-unfollow");
const shouldFollow = core.getBooleanInput("should-follow");
const safeList = parseLine(core.getMultilineInput("safe-list"));

Query Users

These functions retrieve the list of followers and followings of the current user from the GitHub API. queryFollowers function retrieves the followers, while queryFollowing retrieves the followings.

example.ts
const followers = await queryFollowers();
const followings = await queryFollowing();

Identifying Users to Follow and Unfollow

example.ts
const unfollowUsers = followings.filter(
  user =>
    !safeList.includes(user.login) &&
    !followers.some(follower => follower.login === user.login)
);

This line of code filters out users who need to be unfollowed. It compares the followings array with the followers array and ensures that the user's login is not in the safeList or not among the followers, preparing a list of users to unfollow.

example.ts
const usersNeedToFollow = followers.filter(
  user => !followings.some(following => following.login === user.login)
);

This line of code filters out users who need to be followed by comparing the followers array with the followings array to find users not already being followed.

Follow and Unfollow method

example.ts
async function unfollowUsers(users: CurrentUserFollowing[]) {}
 
async function followUsers(users: CurrentUserFollower[]) {
  for (const user of users) {
    await octokit.users.follow({ username: user.login });
    core.debug(`Followed ${user.login}.`);
    await wait(delay);
  }
}
  • These functions handle the unfollowing and following operations respectively.
  • unfollowUsers unfollows each user in the list, while followUsers follows each user.
  • It then waits for a specified delay using the wait function before moving on to the next user.

Following and Unfollowing Users

The following code segment manages the process of following and unfollowing users based on the provided configurations:

example.ts
if (shouldUnfollow) {
  await unfollowUsers(usersNeedToUnfollow);
}
 
if (shouldFollow) {
  await followUsers(usersNeedToFollow);
}
  • Unfollowing Users
    • It checks if there are users to unfollow and if unfollowing is enabled. It proceeds to unfollow the users and logs the number of unfollowed users.
  • Following Users
    • Similarly, it checks if there are users to follow and if following is enabled. It follows the users and logs the number of followed users.

Conclusion

The follower-sync action simplifies the process of managing GitHub followers by automating the task based on configurable inputs. By leveraging the GitHub Actions ecosystem, you can easily integrate this functionality into your workflow, saving time and effort in follower management.