What is GitLab bot?
Imagine you need to automate the process to react on new issues created in your task management system like Jira or GitLab. What kind of automation? Automatically add tag depends on issue name, fix the milestone, add assignee or just comment. Here I will show you how to do it using simple bot based on Swifty.
I will also show you how to automate the release process using git repo and Swifty synchronization feature. We will use GitLab as a repo and an issue tracker.
Setting up the environment
First, we need to create a GitLab project. If you do not have a GitLab account please create it here. Now you need to create a new public project. You can fork my project https://gitlab.com/bbelky/gitbotswf by opening it and click on Fork in a top right corner.
Or next step we need to copy gitbotswf project to our local machine to be able to edit it. Actually, we could do it online on gitlab, but let’s be closer to real life. Open your terminal and run:
git clone <link to your repo>
You can find a link in top right corner of your project: “Clone” > “Clone with HTTPS”. Copy and save this link – we will need it later again. Next, go to your bot folder:
cd gitbotswf/ ls README.md main.go
Here you see main.go file. This is our bot. Our bot is simple. It gets the webhook from GitLab in case of new issue and add the comment (GitLab calls it “note”) to the new issue. Main.go file has few comments that help you to understand how it works.
Stay in this folder, we will need to change main.go file later.
Now you need to get your Gitlab API token to be able to use Gitlab API and add the note.
- Go to your Gitlab profile > Settings > Access Tokens.
- Create new token with api scope.
- Copy and save it as GITLABTOKEN.
Now we need to create a function with our bot code. If you do not have Swifty account still please create it here. First we need to add our Gitlab repo to your Swifty account.
- Login to Swifty and click Repositories.
- Click Attach Repo, select type Git URL and past your Gitlab URL.
- Select Mirror repository automatically.
- Click Done.
Since this moment Swifty will automatically pull (get the updated version of) your repo every 30 minutes. Time to create a bot function:
- Click Functions > New Function. Switch to From repo (Templates) tab and select your Gitlab repository https://gitlab.com/bbelky/gitbotswf.git You will see files from the selected repo.
- Please select main.go file, click Next.
- Select Sync with repository option, enter the name for your function
gitbotswfand click Create.
- Click Triggers > Add Trigger > REST API (URL). Copy the newly created URL and save it as BOT_URL.
Now we have a bot function and Swifty will automatically update this function according to your project update on Gitlab.
Now we need to add our Gitlab token to your Swifty Accounts. Account is the secure and encrypted way to store sensitive data.
- Go to Swifty > Accounts and Create Account.
- Select Generic type.
- Add name GITLABTOKEN and add your GITLABTOKEN.
- Click Save.
Next, you need to add your account to bot function.
- Go to
gitbotswffunction > Access and click Add.
- Select Accounts type, GITLABTOKEN and click Add.
Now your function has access to your Gitlab token securely stored in Account.
The last step
On the last setup step we need to add Gitlab webhook to inform our function about newly created issues. Go to Gitlab UI.
- Select your Gitlab project > Settings > Integrations.
- Add BOT_URL in URL field.
- Select Issues events.
- Click Add webhook.
Gitlab will automatically trigger our bot function every time you create new issue in your project.
Time to test
Go to your Gitlab project > Issues and create New Issue. Check the comments. Ha! Do you see “Thanks for reporting new issue!” comment? If yes, it works. If no, ask us in Slack and we will help you to troubleshoot the issue.
Update your bot
What if you need to update your bot? With git integration available in Swifty you just need to push your new code to git (or Gitlab) in this case and Swifty will automatically update your function with new code.
Just update your code, change few comments for instance, and push your changes to git:
git add *
git commit -m "changes"
git push origin master