gitlab-unfurly
gitlab-unfurly copied to clipboard
Serverless Slack bot for unfurling GitLab URLs.
GitLab Unfurly
Serverless Slack bot for unfurling GitLab URLs.
Example
Installation
Deploy on AWS using Serverless framework.
Create Slack App
- Go to https://api.slack.com/apps?new_app=1 and log in to your workspace
- Name and create your app for your workspace
- Scroll down to App Credentials
- Copy the Client Id and Client Secret, or just remember that they are right here for later use
- Add features and functionality
- Event Subscriptions
- App unfurl domains
- Add your GitLab domain
- You'll come back here and fill in your Request URL once you've deployed the app using serverless
- Go back to Settings > Basic Information
- Add features and functionality
- Permissions
- Scopes > Add an OAuth scope
- links:write
- Reinstall the app as requested
- Copy the OAuth Access Token. That is your SLACK_TOKEN referenced later.
Get AWS Credentials
You will need AWS Access keys. If you do not know your keys or don't have them, you can retrieve your personal access keys from the following url: https://console.aws.amazon.com/iam/home?region=us-west-2#/security_credentials
For more information about creating an AWS account, or using an appropriately scoped service account, follow these directions from serverless
Get GitLab Token
- In GitLab, go to your user settings, which are found in the upper right corner of the Website.
- Open the access tokens section found in the left menu
- Add a personal Access Token with api scope
- The token presented is your GITLAB_TOKEN referenced later
Deploy Using GitLab-CI
The easiest way to deploy this lambda function is to use the provided .gitlab-ci.yml. This requires that you have a shared GitLab runner configured.
Setup:
- Go into your gitlab instance and create a project named gitlab-unfurly
- Copy the clone link from the project repo page
- Add the repo as a remote for this git repo
git remote add gitlab {your gitlab clone link}.git
- In GitLab, go to Settings > CI/CD
- Expand the Variables section
- Add the following variables:
-
GITLAB_URL
- The base url to your GitLab instance -
GITLAB_TOKEN
- Your personal Access Token generated earlier -
SLACK_TOKEN
- OAuth Access Token generated earlier -
SLACK_CLIENT_ID
-
SLACK_CLIENT_SECRET
-
AWS_REGION
- The AWS region you will deploy to.us-west-2
is 100% renewable energy -
AWS_ACCESS_KEY_ID
-
AWS_SECRET_ACCESS_KEY
-
- Save
- Push this repo to the gitlab project
git push -u gitlab master
- Wait for the deploy job to run to completion in GitLab Pipelines
- Open the job output to find the POST endpoint; it will be near the bottom of the build output.
- Go back to the slack app website you were using earlier
- Go to Features > Event Subscriptions
- Paste the POST endpoint url into the Request Url field
- Try sharing a gitlab link with yourself in slack to see if it works
Deploy Using Your Machine
If for some reason you do not want to use GitLab CI to deploy this lambda, then you can deploy it using the serverless cli on your own machine.
Deployment environment requirements:
- Linux:
- Python 3.8
- set dockerizePip to false in the serverless.yml.
- Windows
- Docker
- node.js
- serverless
npm install -g serverless
- serverless-python-requirements
serverless plugin install -n serverless-python-requirements
Set the following environment variables:
-
GITLAB_URL
- The base url to your GitLab instance -
GITLAB_TOKEN
- Your personal Access Token generated earlier -
SLACK_TOKEN
- OAuth Access Token generated earlier -
SLACK_CLIENT_ID
-
SLACK_CLIENT_SECRET
-
AWS_REGION
- The AWS region you will deploy to.us-west-2
is 100% renewable energy -
AWS_ACCESS_KEY_ID
-
AWS_SECRET_ACCESS_KEY
After that's all taken care of, you just run the following command: serverless deploy
Troubleshooting
The first step to troubleshooting is to open the AWS Console and navigate to the CloudWatch logs. You will find the logs for this lambda in the log group named something like gitlab-unfurly-dev-unfurl.
Common Issues
- SSL failure while contacting your GitLab instance
- This is likely caused by your GitLab instance using an untrusted Certificate Authority (CA)
- Use this StackOverflow answer to resolve the issue
- auth_failure while contacting slack
- This is likely caused by you skipping step #6 in the Create Slack App section above
Contributing
Bug reports and fixes are always welcome!
Tests are run with pytest. Install into virtual environment
requirements.txt
and test-requirements.txt
and run in shell command pytest
Code is formatted by Black.
License
FAQ
-
How to make unfurly ignore my urls (= not create previews)?
Append?no_unfurl
to the url. -
Is there a way to disable unfurly for the whole post?
Unfortunately no. Unfurly receives links from slack 1-by-1. Therefore it has no knowledge about links being part of the same post.