Table of Contents
This is a Google Cloud Function that does the following:
- Parses an incoming HTTP POST request from a Marketo contact form Webhook,
- Extracts the
real_name
andemail
fields from the incoming request body, - Automatically invites the user to a given Slack workspace using the Slack Admin User Invite API with the following parameters:
- The workspace id:
SLACK_TEAM_ID
- The channels to add the user:
SLACK_CHANNEL_IDS
- The workspace id:
- After a successful invitation, sends a chat message to a Slack channel defined by
SLACK_MESSAGE_CHANNEL
.
flowchart TD
A[User wants to join our Slack Community] -->|opens browser| B(Goes to https://saucelabs.com/community/slack) -->|Fills out Marketo contact form| C(Webhook captures `first_name`, `last_name`, and `email`)
C -->|HTTP POST | D([fa:fa-server Google Cloud Function])
D -->|HTTP POST | E[Member added fa:fa-slack, message sent to slack fa:fa-grin]
- A GCP project with the Cloud Functions API enabled.
- A Slack Grid Enterprise account.
- Slack Org/Grid privileges to create an app with the following User Token Scopes:
admin.invites.read
admin.invites.write
admin.users.read
admin.users.write
,admin.users.invite
,chat.write
users.read
users.read.email
users.write
- Node.js and npm installed on your local machine:
node v18*
- Use
nvm
to set node to18
i.e.use nvm18
- Run
npm install
- Run
npm test
Unit Test are located in ./src/index.test.js
, and require the jest
testing framework. There are two parts to manually test the functionality:
Below is a Sample Slack API Post Request to the Slack API endpoint
curl -X POST \
-H "Authorization: Bearer $SLACK_USER_TOKEN" \
-H "Content-Type: application/json; charset=utf-8" \
-d '{
"channel_ids": "$SLACK_CHANNEL_IDS",
"email": "[email protected]",
"team_id": "$SLACK_TEAM_ID",
"email_password_policy_enabled": true,
"real_name": "Joe Shmo",
"set_active": false
}' \
https://slack.com/api/admin.users.invite
Below is HTTP Post Request to the Google Cloud Function URL
curl -m 70 -X POST \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d '{
"real_name": "Joe Shmo",
"email": "[email protected]"
}' \
https://<gcp-region>-<project-id>.cloudfunctions.net/slack-auto-invite
All GCP deployment resources are located in the
terraform
directory.
The .gitlab-ci.yml
config deploys to GCP and sets up a pipeline that includes several stages:
test
build
deploy
The stages are defined with corresponding jobs, each specifying the container image to be used, scripts to run, rules to apply, and dependencies.
There are several key variables
used in the pipeline, including:
GOOGLE_CREDENTIALS
: project service account keyGOOGLE_PROJECT_ID
: google project idSLACK_USER_TOKEN
: Slack user token with correct API scopesSLACK_MESSAGE_CHANNEL
: Channel to post notification messages for Slack adminsSLACK_CHANNEL_IDS
: Channels to add the new userSLACK_TEAM_ID
: The Slack Workspace ID
All of the variables above are crucial to the src/
code and also to validate the terraform configuration.
The test
stage includes two jobs:
run-unit-tests
: runs unit tests using the Jest framework vianpm test
.run-linter
: lints the code located insrc/
usingeslint
.
The build
stage has a single job called terraform_validate
, which validates the Terraform configuration.
The deploy
stage has two jobs:
terraform_plan
: job that creates a deployment plan for Terraform.terraform_apply
: job that deploys the changes.
The terraform_apply
job also has a dependency on the terraform_plan
job, and artifacts (the tfplan
file) are saved for debugging purporses in case of job failure.