Panoptikum

GitLab + Habitica + GCP function = Love!

Keeping your team alive during Covid19 home-office time can be hard. Read how we connected GitLab with Habitica for some daily gaming grind.

Some days ago, I wrote a Tweet about my team and that we connected our GitLab on-premises installation with Habitica and this Medium story is about how you can do it too! 🤗

Image for post
Image for post

Preface

The Tweet below got very positive responses and therefore I decided to write a Medium story about the background. Together with my team and a colleague from another team we are playing computer games for more than ten years. For us, playing cooperative games together is a team-building event. We play different games, with different settings — for example, for several years we played Dota2. Playing together in cooperative games strengthens your team members' awareness of each other. And, we play it together, often in the same room — which is currently not possible due to Covid19.

Playing together in cooperative games strengthens your team members’ awareness of each other.

Currently, the longer the Covid19 caused social distancing lasts, the more important is it, to find some mental distraction and topics to talk about inside your team — and some daily monster hunting with your team members can be fun!😂

And that's where Habitica comes in. Habitica, at its core, is a personal task manager like Wunderlist (now Microsoft To-Do) or Google Tasks. But Habitica has added gamification (RPG elements) to this core functionality! I know Habitica since around 2015 but, I never really used it — until now.

During one of my last weekends I thought about the question, what can I do to give my team and myself a goal to reach besides the work goals? Is there a way that we can make use of our work tasks to also work towards a common goal? Can we have some work gamification that is easy the reach and does not mean an additional burden? And then, Habitica came to my mind. 😋

Read on, to learn how easy it is to connect GitLab, or whatever like, with Habitica via a Google Cloud Function.

Implementation

Habitica is available for free but it does also support paid group tasks and many more features (which must be fun), but we do only use the free version.

At work, we have coded a central webhook service endpoint, which is able to process every data you send to it, as long as it is JSON data. This service uses gomplate and classic scripts to fulfill whatever task we need. But this is too complex for people out there to connect GitLab with Habitica. Therefore I searched for a simpler way, and here we go — with a Google Cloud Function. It will take you not more than five minutes to set these things up.

  • Habitica — here we will retrieve our API keys which are used by the Google Cloud function to score up your habit on Habitica
  • A Google Cloud Function — this will receive the webhook call from GitLab, make a simple transformation, and will trigger the Habitica API
  • GitLab — here we will need to configure the webhook endpoint, which is the Google Cloud Function

We will start with Habitica, then we will set up the Google Cloud Function, and finally, we connect GitLab to it.

Create a Habitica account if you do not have one. Next, create a new habit in Habitica. You can name it whatever you like! I called mine “GitLab Closed Issue!”.

Image for post
Image for post

Next, head over to your Habitica settings and use the API tab. Now you see your User ID and with a click on Show API Token. This information is sensitive!

Image for post
Image for post

Note down your User ID and your API Token, we need them for the Google Cloud function and we will use them now, to retrieve the technical id from your habit, in my case from the habit called “GitLab Closed Issue!”.

I use Visual Studio Code with the REST Client extension. Create a new file, for example, call.http and paste the request as shown in the screenshot. Replace the “X-Api-Key” and “X-Api-User” values with your values! Afterward, click the “Send Request” text above the URL and search the response on the left for your habit. In my case “GitLab Closed Issue!” and write down the value of the “id” field!

Image for post
Image for post

We use habits for this implementation because to-do’s are hard to handle because you have to create them one by one. Instead, a habit has a plus and minus functionality and therefore the count can score up or down without creating a to-do for it.

If you have a Google account, head over to the Google Cloud Functions console. You can follow the Python Quickstart Guide from Google to set up your function within a couple of seconds.

Use the following GitHub Gist from me for the inline code editor in your function. You have to replace the <your-task-id>, <your-x-api-key> and <your-x-api-user> placeholders with your values! The code will only trigger a Habitica habit score up when the issue is updated and the state of the issue changes to closed.

Creating the Google Cloud Function looks like this and it should be the same for you. Under “Entry point” you have to select the entry point function. The function name in this Python code is “gitlab_habitica_google_function”.

Image for post
Image for post

Besides this, you also have to edit the “requirements.txt” too and add the Python “requests” library.

Image for post
Image for post

If you have all set up, you can find the endpoint URL of your function under the “TRIGGER” tab.

Image for post
Image for post

Note down this URL, we will need it in the next step!

Now, we have configured Habitica and we have also installed our Google Cloud Function. The last thing that we need is, that we configure our GitLab project to call the Google Cloud Function endpoint, the URL. To do this, head over to GitLab and have a look under the project settings. There you can configure the GitLab Webhook. Put in your Google Cloud Function URL and select “Issue Events”.

Image for post
Image for post

That's it! If you move an issue from open to closed, it will trigger the Google Cloud function which will in-turn trigger the Habitica API and score up your habit! 💕

Image for post
Image for post

You can check the logs of your Google Cloud Function to see what happens in the background and check if everything is working! 🚀

Image for post
Image for post

Takeaways

Danger ahead! This implementation is not failure proven! Take care of your sensitive information! The Google Cloud Function is not authenticated! …

There are better ways to do the same in a more secure way, but this story is to give you a starting idea. It’s a basis to show how you can use this technology to implement more complex things. Therefore I decided to make it as simple as possible.

If you would like to test the Google Cloud Function locally, you can install the Python functions-framework and run your function locally.

That’s it for today! Happy monster hunting! 🤖😂

Last edited on 1st February 2021

Speaker, GitLab Hero, Docker Community Leader, Author, Cloud Architect — 𝗜𝗺𝗮𝗴𝗶𝗻𝗮𝘁𝗶𝗼𝗻 𝗶𝘀 𝗺𝗼𝗿𝗲 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝘁𝗵𝗮𝗻 𝗸𝗻𝗼𝘄𝗹𝗲𝗱𝗴𝗲.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store