How to add Github Pull Requests as records in an Airtable base
Tags: webdev, tutorial, lowcode, github
Published: 2022-09-06 09:29:39

Do you ever want to keep track of Pull Requests (PRs) made in your repo in a spreadsheet format? In this tutorial, I'll show you how to set up a workflow to achieve this using [Buildable](https://www.buildable.dev/). 

With this workflow, whenever a new PR is created in your Github repo, Buildable listens to that event and instantly takes action to add it as a new record in your Airtable database.


**Prerequisites**
Before we jump in, let’s take a look at some prerequisites you'll need for this tutorial:

- A Buildable account (Create your free account [here](https://app.buildable.dev/sign-in))
- A Github Repo
- An Airtable base


Now, let's get started!

## Create a connection

Start by adding a Github Repo connection in your Buildable IDE. Navigate to ***Connections*** and then click "***New***". 

From there, select "***Github Repos***" as your connection source.


![Select Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar64pplyqjf7ysatkh6i.png)

Complete the account connection prompt by filling the details:


![Complete your connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/disztv870pnwe0p79ph0.png)

> *Always give your connection an appropriate name e.g "Portfolio-Connection"*


Not sure how to get your Github Personal Access Token? [See docs](https://docs.buildable.dev/connections/source-catalog/github-repos#finding-your-credentials-in-github) 


Test your connection and once successful, **connect**. 
&nbsp;

## Subscribe to events

Next, select the Github repo events you want Buildable to subscribe to. Subscribing to these events allow Buildable to detect when these events occur and carry out the required action. 

For this tutorial, we’d want to subscribe to the Pull Request event.


![Subscribe to events](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmswnj3ui5wlfi3p2ags.png)

&nbsp;

## Build a workflow

Now that we’ve connected our event source, we need to build out this workflow. 

Go to ***Workflows*** and select ”***New.***”


![New workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aylitap1pq7pek0k5ysu.png)

You'll be prompted to give your workflow a suitable name and description.

Next, attach the pull request event for Buildable to listen to. This is what will trigger our workflow.

To do this, select your Github repo, the connection and then the event. In this case, ***github-repos*** > ***portfolio-connection*** > ***pull_request***.


![Attach event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6175ra0xcfolfsdbwvca.gif)

&nbsp;

## Add action

The action is what happens when Buildable detects a pull request in our repo. We want that action to send a record to our Airtable database.

From our IDE, click the "***Add action***" button.

From the list, select ***Airtable*** > ***Create Records***

&nbsp;

## Connect Airtable

If this is the first time you're connecting Airtable to Buildable, then you'll need to fill in the required credentials to connect Airtable to Buildable.

Not sure how to find the API key and Base ID? **See below**


![How to get your Airtable API Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqilpedsyz3fucux2typ.gif)

How to get your Airtable API key


![How to get your Airtable Base ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26vod50x8epm8znlufpn.gif)

How to get your Airtable Base ID

You'll notice the "***Create Records***" node is called `createdRecords`. Since this is a pull request record, let’s rename this correctly as `pullRequestRecord`.

&nbsp;

## Create Airtable base 

Now, we need to create the Airtable table where we'll be inserting pull request records. Let's call this table "Pull Requests.".

Next, create the fields you need to record in the table. For the purpose of this tutorial, we'll be creating these 6 fields:

- Title
- Description
- Author
- Number
- Created
- URL


![Airtable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzrtrlcflerlii7oysug.png)

> *Asides the Number field, make sure the remaining fields are “Single line text” or “Long text” fields.*
> 

&nbsp;

## Modify Input Node

Back in Buildable, you'll notice this template came with pre-filled data. We would need to modify this data for our scenario.

We have our API KEY, BASE ID, and endpoint already pre-filled, so we'd leave them as they are.


![Buildable IDE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4sz8x5h6m01rt8oqfee.png)

But, you need to change the `tableName` to the name of your table in Airtable. Remember, the table is called `Pull Request`

Finally, map the value of these fields from the Pull Request event to match exactly what we have in Airtable.

> *`$body` represents the entire payload coming into your node.*
> 

- Title -> `$body.pull_request.title`
- Description -> `$body.pull_request.body`
- Author -> `$body.sender.login`
- Number -> `$body.pull_request.number`
- Date created -> `$body.pull_request.created_at`
- URL -> `$body.pull_request.url`


![Buildable field records](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jx8kpx80usyoyp1e1vbj.png)

For ease, copy and paste this code into your IDE.

```jsx
const nodeInput = ({ $body, $headers, $env, $data }) => {
  return {
    BUILDABLE_AIRTABLE_API_KEY: $env.BUILDABLE_AIRTABLE_API_KEY, // Required
    BUILDABLE_AIRTABLE_BASE_ID: $env.BUILDABLE_AIRTABLE_BASE_ID, // Required
    endpoint: "https://api.airtable.com/v0", // Required
    tableName: "Pull Requests", // Required

    // Required - Array of objects to insert
    records: [
      {
        fields: {
          Title: $body.pull_request.title,
          Description: $body.pull_request.body,
          Author: $body.sender.login,
          Number: $body.pull_request.number,
          Created: $body.pull_request.created_at,
          URL: $body.pull_request.url,
        },
      },
    ],
  };
};
```

**Save and run your connection!**

&nbsp;
## Explore this workflow

Congrats! You've just completed this workflow. Go ahead and trigger a Pull request in your Github repo. You would see this data populated in Airtable 🎉

If you wish to extend the logic of your workflows, add in custom JavaScript functions from Buildable's action library.

Have questions? Drop them in the comments below. Join the Buildable [Discord community](https://discord.com/invite/47AJ42Wzys), or follow on [Twitter](https://twitter.com/BuildableHQ) to learn more about updates.