Get Started
7 Webhook Subscription

Step 7 - Webhook Subscriptions

So far, we have created some basic demonstrations of the platform. Now, let's build a useful integration between two apps.

Enable the GitHub repository webhook

In src/index.js, uncomment the import of ./githubToSlack.

import "./helloWorld";
import "./helloSlack";
import "./basicWebhook";
import "./githubToSlack";

Take a look at the webhook definition

Look at src/githubToSlack.js. The first thing we do is import the GitHub connector.

import { GitHub } from "@runlightyear/github";
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
 
const githubWebhook = GitHub.defineWebhook({
  name: "githubWebhook",
  title: "GitHub Webhook",
  variables: ["owner", "repo"],
  subscribeProps: ({ variables }) => {
    return {
      owner: variables.owner,
      repo: variables.repo,
      events: ["push"],
    };
  },
});
...

Next, we define a GitHub webhook. This webhook will be more sophisticated than the previous one we defined in that it will have a subscription to events in a GitHub repository.

You'll notice that instead of using the defineWebhook function that we used in the previous step, we actually call a function on the GitHub connector. This will be typical of connectors that support subscriptions and will take care of most of the work of subscribing and unsubscribing.

import { GitHub } from "@runlightyear/github";
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
 
const githubWebhook = GitHub.defineWebhook({
  name: "githubWebhook",
  title: "GitHub Webhook",
  variables: ["owner", "repo"],
  subscribeProps: ({ variables }) => {
    return {
      owner: variables.owner,
      repo: variables.repo,
      events: ["push"],
    };
  },
});
...

We can use some variables to configure which owner/repo we want to connect to.

import { GitHub } from "@runlightyear/github";
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
 
const githubWebhook = GitHub.defineWebhook({
  name: "githubWebhook",
  title: "GitHub Webhook",
  variables: ["owner", "repo"],
  subscribeProps: ({ variables }) => {
    return {
      owner: variables.owner,
      repo: variables.repo,
      events: ["push"],
    };
  },
});
...

And finally we set up subscribeProps. This describes the properties of the subscription that we want to set up. You can take a look at the API docs for a full set of properties once we have the docs generating properly.

import { GitHub } from "@runlightyear/github";
import { defineAction } from "@runlightyear/lightyear";
import { Slack } from "@runlightyear/slack";
 
const githubWebhook = GitHub.defineWebhook({
  name: "githubWebhook",
  title: "GitHub Webhook",
  variables: ["owner", "repo"],
  subscribeProps: ({ variables }) => {
    return {
      owner: variables.owner,
      repo: variables.repo,
      events: ["push"],
    };
  },
});
...

Now look at the action definition

Here we leverage the optional trigger property to let the platform know we want this to run every time a particular webhook is called.

...
defineAction({
  name: "githubToSlack",
  title: "GitHub to Slack",
  trigger: {
    webhook: githubWebhook,
  },
  apps: ["slack"],
  variables: ["channel"],
  run: async ({ data, auths, variables }) => {
    const pushPayload = GitHub.asPushPayload(data);
 
    if (pushPayload) {
      console.info("Got a push payload");
 
      const slack = new Slack({ auth: auths.slack });
 
      await slack.postMessage({
        channel: variables.channel,
        text: `Got push event on repo: ${pushPayload.repository.fullName}`,
      });
 
      console.info("Posted message to Slack");
    }
  },
});

We'll use another variable to specify what channel to write the Slack message.

...
defineAction({
  name: "githubToSlack",
  title: "GitHub to Slack",
  trigger: {
    webhook: githubWebhook,
  },
  apps: ["slack"],
  variables: ["channel"],
  run: async ({ data, auths, variables }) => {
    const pushPayload = GitHub.asPushPayload(data);
 
    if (pushPayload) {
      console.info("Got a push payload");
 
      const slack = new Slack({ auth: auths.slack });
 
      await slack.postMessage({
        channel: variables.channel,
        text: `Got push event on repo: ${pushPayload.repository.fullName}`,
      });
 
      console.info("Posted message to Slack");
    }
  },
});

Now that we've reviewed both the webhook and the action, let's take a look at the terminal and see what happened on deploy.

VM: [INFO]: Starting deploy
VM: [INFO]: Deploying helloWorld, basicWebhook, helloSlack, githubWebhook, githubToSlack
VM: [INFO]: Deploy succeeded
CLI: [INFO]: Skipping subscribe for webhooks that need configuration: githubWebhook

Looks like we need to do some configuration!

Configure the webhook

Go to the Webhooks menu (opens in a new tab) and click on the new GitHub Webhook

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Webhooks
Basic Webhook
basicWebhook
No actions
GitHub Webhook
githubWebhook
GitHub to Slack
GitHub Icon

Notice that the GitHub app needs to be authorized. Click on the GitHub app...

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubWebhook
GitHub Webhook
Needs setup
https://app.runlightyear.com/api/v1/endpoints/fa8e5a76-74aa-4b3d-adf9-3b15000db25a
Config
Subscription
Deliveries
Config
Apps
GitHub Icon
GitHub
Unauthorized
Variables
Name
Value
owner *
repo *
Secrets
No secrets

...and then click Authorize to begin the OAuth2 flow.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
GitHub Icon
GitHub
github
Type
OAUTH2
Status
BETA
Authorization
github
Actions
No actions
Webhooks
GitHub Webhook

When you are back, you should see that GitHub is authorized and that you can share the auth with the production environment. Go ahead and do that.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
GitHub Icon
GitHub
github
Type
OAUTH2
Status
BETA
Authorization
github
Shared with production
Actions
No actions
Webhooks
GitHub Webhook

Now go back to the webhook.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
GitHub Icon
GitHub
github
Type
OAUTH2
Status
BETA
Authorization
github
Shared with production
Actions
No actions
Webhooks
GitHub Webhook

To finish configuring the webhook, we need to enter an owner and repo name. You should choose an existing repo that you don't mind pushing a commit to.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubWebhook
GitHub Webhook
Needs setup
https://app.runlightyear.com/api/v1/endpoints/fa8e5a76-74aa-4b3d-adf9-3b15000db25a
Config
Subscription
Deliveries
Config
Apps
GitHub Icon
GitHub
Authorized
Variables
Name
Value
owner *
repo *
Secrets
No secrets

Once you have filled in a valid owner/repo, take a look at the Subscription tab...

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubWebhook
GitHub Webhook
Ready
https://app.runlightyear.com/api/v1/endpoints/fa8e5a76-74aa-4b3d-adf9-3b15000db25a
Config
Subscription
Deliveries
Config
Apps
GitHub Icon
GitHub
Authorized
Variables
Name
Value
owner *
repo *
Secrets
No secrets

...and see if the subscription worked.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubWebhook
GitHub Webhook
Ready
https://app.runlightyear.com/api/v1/endpoints/fa8e5a76-74aa-4b3d-adf9-3b15000db25a
Config
Subscription
Deliveries
Subscription Activities  
Subscribed successfully at
Mar 29, 2023 5:48 AM
aed11c73-536d-413f-b54c-45ad64daabae
Received at  
Mar 29, 2023 5:48 AM
aed11c73-536d-413f-b54c-45ad64daabae
 [INFO]:
Subscribed to GitHub webhook

Configure the action

Now go to the Actions menu and click on the GitHub to Slack action.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
Actions
githubToSlack
manual
GitHub to Slack
Needs setup
GitHub Icon
Slack Icon
helloSlack
manual
Hello Slack
Ready
Slack Icon
helloWorld
manual
Hello World
Ready

You can see that Slack is already authorized since we did that earlier in this tutorial. So you just need to enter a Slack channel you want to post messages to.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubToSlack
GitHub to Slack
Ready
Config
Runs
Config
Apps
Slack Icon
Slack
Authorized
Variables
Name
Value
channel *
Secrets
No secrets

Push to the repo and see the result

And now, the moment of truth. Drumroll please....

Make a change to your repo, commit the change, and push it. Then, let's go to the Runs tab to see what happened.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubToSlack
GitHub to Slack
Ready
Config
Runs
Config
Apps
Slack Icon
Slack
Authorized
Variables
Name
Value
channel *
Secrets
No secrets

If everything went smoothly, you should see a single run like this.

Lightyear Logo
Environment
Development
Apps
Actions
Webhooks
Runs
Deploys
Account
Docs
githubToSlack
GitHub to Slack
Ready
Config
Runs
Runs
Ran successfully at
Mar 29, 2023 5:48 AM
93ede729-d6b1-40b6-b186-ccd53d427986
githubToSlack
Mar 29, 2023 5:48 AM
 [INFO]:
Running action githubToSlack
 [INFO]:
Got a push payload
 [INFO]:
Posted message to Slack