Step 5 (Optional): Add an n8n automation to Slack

We are going to create a simple workflow that will send a daily reminder of how many PRs are currently open without a review.

  1. Go to n8n on port 5678
  2. Anywhere on a workflow page, paste the JSON below, it will create the workflow:
{
  "nodes": [
    {
      "parameters": {
        "endpoint": "http://hasura:8080/v1/graphql",
        "allowUnauthorizedCerts": true,
        "requestFormat": "json",
        "query": "=query OpenPRWithoutReviews {\n  vcs_PullRequest(where: {stateCategory: {_eq: \"Open\"}, _not: {vcs_PullRequestReviews: {}}}) {\n    state\n    stateCategory\n    uid\n    vcs_Repository {\n      name\n    }\n  }\n}",
        "headerParametersUi": {
          "parameter": [
            {
              "name": "x-hasura-admin-secret",
              "value": "admin"
            }
          ]
        }
      },
      "name": "GraphQL",
      "type": "n8n-nodes-base.graphql",
      "typeVersion": 1,
      "position": [
        540,
        260
      ]
    },
    {
      "parameters": {
        "channel": "random",
        "text": "=There are {{$json[\"numberOfPRs\"]}} PRs waiting for your attention.",
        "jsonParameters": true,
        "attachments": [],
        "otherOptions": {}
      },
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 1,
      "position": [
        980,
        260
      ],
      "credentials": {
        "slackApi": {
          "id": "1",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\nfor (item of items) {\n  item.json.numberOfPRs = item.json.data.vcs_PullRequest.length\n}\n// You can write logs to the browser console\nconsole.log('Done!');\n\nreturn items;"
      },
      "name": "Summarize",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        760,
        260
      ]
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {}
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        320,
        260
      ]
    }
  ],
  "connections": {
    "GraphQL": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron": {
      "main": [
        [
          {
            "node": "GraphQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3452

🚧

This example assumes you haven't changed the Hasura password.

Otherwise, please update the corresponding header in the GraphQL node.

πŸ“˜

What does each node do in our example?

The Cron node simply triggers the workflow at a regular interval.
The GraphQL node queries Hasura for PRs without reviews.
The Function node (named Summarize) extracts how many PRs are waiting a review.
The Slack node sends that information via Slack.

  1. Double click on the Slack node and add your slack credentials

πŸ“˜

What scopes for the Slack token should you use?

We suggest using a user (not a bot) access token with the following scopes:
channels:read
chat:write
users.profile:read

  1. (Optional) Double click on the Cron node and select the desired frequency and time
  2. (Optional) Test the workflow by clicking Execute Workflow
  3. Click Save, and switch the workflow to Active

πŸ“˜

Get Oriented in n8n

n8n is a great open-source workflow engine. You can find its documentation here.
See also πŸ€– Workflows in n8n for how it can be used in Faros CE.

πŸ€– Workflows in n8n