How to Use Cloud Functions to Automate GitHub Moderation

Idris Olubisi💡 - Dec 5 '23 - - Dev Community

In today's world, automated tools enhance productivity and ensure task consistency while maintaining accuracy. When managing repositories on GitHub, moderation can become tedious, especially as the number of issues open each day increases.

What if there were a way to bring automation to this process, streamlining the workflow and ensuring timely responses? Appwrite Functions — a potent tool to supercharge our GitHub moderation tasks — is all we need.

In this tutorial, we‘ll learn how to automate GitHub moderation using Appwrite Functions and the "GitHub issue bot" template. We‘ll also cover customizing the comments the bot posts when creating an issue and tailoring it to our preferred response. Finally, we’ll discuss how to monitor the performance of the Appwrite function.

Prerequisites

We’ll need a few things to fully understand the concepts presented in this tutorial:

  • A basic understanding of JavaScript
  • A GitHub account
  • An Appwrite account

Getting started

First, let’s create a GitHub repository; this will serve as the workspace for our Appwrite Function to perform and await new issues. Here's how:

  • Log into GitHub.
  • Click the New Repository button.
  • Name it github_moderation.
  • Click Create Repository.

Create a GitHub repository

Setting up a webhook

With the repo set up, let’s establish the webhook. Webhooks are automated systems that allow external services to track changes and send data within a system. Here, we’ll set up a webhook to monitor actions within the repository to listen to events closely.

To do this, navigate to the Settings tab of the repository and click Webhooks as shown below.

Navigate to the webhook section

Next, we need to set the webhook with the following details:

  • Click Add webhook.
  • For the Payload URL, enter https://sample.com/.
  • Choose application/json for Content type.
  • Type in myTopSecret for the Secret.

Add webhook

Input webhook details

Note: The https://sample.com URL is a placeholder; we will replace it with our Appwrite Function domain later. It's also important to note the secret we entered, as it will be used when configuring the Function.

Next, change the event option to Let me select individual events, select Issues, and Add webhook.

Select individual event type

Select issues from the option and save

Create an access token

We must create an access token for our Function to connect to GitHub and securely monitor activities via the Webhook. To do this, click the Settings menu under the profile drawer and navigate to the Developer settings section.

Click settings on GitHub profile

GitHub Developer Settings

Navigate to the Fine-grained tokens tab and click the Generate new token button.

Generate a New Token

Input github_moderation as the name, and change the Repository access to All Repository.

Input token name and give access

Scroll to the Repository permissions section in the Issues category, change the permission to Read and Write, and then click the Generate token button to create it.

Update permission in the issues category

After completing these steps, GitHub will generate an access token for us. Store this token securely; we will need it when creating our Function.

Generate Access Token

All set with GitHub! Let’s create a cloud function on Appwrite in the following step.

Create a Cloud Function on Appwrite

To make the process seamless, Appwrite offers two options for creating functions. We can create a function from scratch, allowing for extensive customization, or we can choose from a set of templates with support for various runtimes, making it more convenient. In our case, we will create from a template.

To do this, we need to log into our Appwrite console, click the Create project button, input github_moderation as the name, and then Create.

Create project on Appwrite

Next, navigate to the Functions tab, select the Templates tab, locate the GitHub issue bot, and click Create function.

Appwrite dashboard

Input GitHub issue bot as the function name, select Node.js - 16.0 as the runtime, and click Next.

Add configuration to Appwrite cloud function

Input the GitHub token and secret we generated earlier and continue.

Add the required secret

Select Add to existing repository, connect to GitHub, select the github_moderation repository, and continue.

Connect function to an existing GitHub repository

Connect GitHub

Select Repository

Accept the default branch and directory and then Create.

Create Appwrite Function

That’s it! Appwrite will automatically scaffold a function, deploy it, and push the corresponding source code to the specified repository on creation, as shown below.

Appwrite Function Deployment

Update the webhook payload URL and issue comment

With our function deployed, we can get the domain on which it will run and use it as our Payload URL. To do this, navigate to the Domain tab and copy the URL as shown below.

Copy Function URL

Next, we need to update our Webhook URL on GitHub with the copied URL.

Update Function URL on GitHub

Finally, to customize the comment that the bot will post when an issue is created, navigate to the Code tab and modify the src/main.js as shown below:

Manage webhook

Next, update the following code snippet to the main.js file.

import GithubService from './github.js';
import { throwIfMissing } from './utils.js';

export default async ({ res, req, log, error }) => {
     throwIfMissing(process.env, ['GITHUB_WEBHOOK_SECRET', 'GITHUB_TOKEN']);

 const github = new GithubService();

 if (!(await github.verifyWebhook(req))) {
      error('Invalid signature');
      return res.json({ ok: false, error: 'Invalid signature' }, 401);
 }

 if (!github.isIssueOpenedEvent(req)) {
      log('Received non-issue event - ignoring');
      return res.json({ ok: true });
 }

 await github.postComment(
    req.body.repository,
    req.body.issue,
    `Thank you for helping us improve our project, someone from our team will respond soon!` //UPDATE THE COMMENT HERE
 );

  return res.json({ ok: true });
};
Enter fullscreen mode Exit fullscreen mode

Update main.js

Now that it's set up, we can try it out by opening an Issue in the **github_moderation** repository.

Additionally, we can check the Appwrite function's performance and the GitHub webhook's operation, as shown below.

Execution on Appwrite

Webhook deliveries on GitHub

As mentioned above, Appwrite allows the flexibility to create custom functions from scratch and offers numerous templates. These templates are a game-changer, providing developers with flexibility and a head start and enabling them to rapidly develop and customize functionalities in new and existing applications using Appwrite's functions.

Additionally, the range of templates available supports multiple programming languages, giving developers a broad spectrum of options within the Appwrite ecosystem.

Conclusion

In this tutorial, we provided a step-by-step guide on how to use Appwrite Cloud Functions for automating GitHub moderation. We also demonstrated how to customize the comments the bot posts when creating an issue, tailoring it to our preferred response. Additionally, we discussed how to monitor the Appwrite function's performance and the GitHub webhook's operation via the dashboard.

These resources might also be helpful:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .