GitLab

A connector for GitLab.

Note that this connector emits only upports sending messages back to the event (Issue, Merge Request), if you need to create more elaborate workflow, please use the GitLab API in your skill.

Requirements

Configuration

connectors:
  gitlab:
    # Optional but recommended
    webhook-token: "my-very-secret-webhook-secret"
    # Required if you want opsdroid to reply to issues/MRs
    token: "<personal access token>"

Setup Webhook

You need to expose Opsdroid to the internet, when you have your url, you can go to your repository, click Settings and then select Webhook, you can then add the url with the connector/<connector name> enpoint. For example, assume that you are using example.com as your base url and using this connector with the default name, you can use the following url https://example.com/connector/gitlab to receive events from your repository.

You can then choose a secret token - this will be your webhook-token in the Opsdroid configuration. It’s highly recommended that you choose a strong token to validate the requests coming into the https://example.com/connector/gitlab endpoint.

Finally, you can choose which events you want Gitlab to send you and choose if you want to turn off SSL verification. It’s recommended that you use SSL at all times.

Examples

The GitLab connector allows you to send back a message when an event is triggered. This is a great way to automate some workflows. For example, let’s assume that you want to thank every user that opens a new issue in your repository.

Here’s a small example of how the opsdroid configuration looks like:

connectors:
  gitlab:
  webhook-token: "my-secret-token"
  token: "my-personal-token"
skills:
  thankyou:
    path: "<skill pwd>"

Then you can create a new folder for your skill and write the following:

from opsdroid.skill import Skill
from opsdorid.matchers import match_event
from opsdroid.connector.gitlab.events import GitlabIssueCreated
from opsdroid.events import Message

class ThankUser(Skill):
    def __init__(self, opsdroid, config, *args, **kwargs):
        super().__init__(opsdroid, config, *args, **kwargs)
        self.opsdroid = opsdroid

    @match_event(GitlabIssueCreated)
    async def say_thank_you(self, event):
        """Send message to issue, thanking user."""
        await event.respond(
            Message(
                text="Thank you for opening this issue, a team member will be with you shortly!",
                target=event.target
            )
        )

Now let’s assume that you want to notify a slack channel every time a label was changed in an MR. This can be useful if you want to alert your team that an MR is ready for code review.

A more robust opsdroid configuration:

connectors:
  gitlab:
    webhook-token: "/secret-t0k3n|"
    token: "<your secret token>"
  slack:
    bot-token: "<your token>"
    socket-mode: false
    bot-name: "Review-Bot"
    default-room: "#code-reviews"
skills:
  alert-team:
    path: "<skill pwd>"

The skill will look somewhat similar to the previous example with a few changes:

from opsdroid.skill import Skill
from opsdorid.matchers import match_event
from opsdroid.connector.gitlab.events import GitlabIssueCreated
from opsdroid.events import Message

class AlertTeam(Skill):
    def __init__(self, opsdroid, config, *args, **kwargs):
        super().__init__(opsdroid, config, *args, **kwargs)
        self.opsdroid = opsdroid

    @match_event(MRLabeled)
    async def mr_labeled(self, event):
        """Send message slack if MR was labeled as code-review."""
        slack = self.opsdroid.get_connector("slack")
        if "code-review" in event.labels:
            await slack.send(
                Message(
                    text=f"Hey folks, the MR ({event.url}) was just marked as ready for review!",
                )
            )

This skill will trigger every time an MR gets labeled with something but only sends the message to the Slack channel #code-reviews when the MR is labeled with the code-review label.

Events Available

Currently, the GitLab connector handles Merge Requests and Issues events, any other event such as pipeline events, for example, will be retured as GenericGitlabEvent.

… py:class:: GenericGitlabEvent(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when an unhandled event is sent.

… py:class:: GitlabIssueCreated(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a new issue is created.

… py:class:: GitlabIssueClosed(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when an issue is closed.

… py:class:: GitlabIssueEdited(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when an issue is edited.

… py:class:: GitlabIssueLabeled(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when an issue is labeled.

… py:class:: GenericIssueEvent(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when any other issue action happen.

… py:class:: MRCreated(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a MR is created.

… py:class:: MRMerged(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a MR is merged.

… py:class:: MRClosed(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a MR is closed.

… py:class:: MRLabeled(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a MR label is updated.

… py:class:: MRApproved(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a MR is approved.

… py:class:: GenericMREvent(project, user, title, description, labels, url, *args, **kwargs)

module:

opsdroid.connector.gitlab.events

Event class that triggers when a Generic MR events.Event happens.

Reference

This dataclass is used internally within the Gitlab connector, but is worth noting a few things here.

… py:class:: GitlabPayload(attributes, changes, labels, project_name, url, target, description, title, username, action, raw_payload)

module:

opsdroid.connector.gitlab

canonical:

opsdroid.connector.gitlab.connector.GitlabPayload

Generate Gitlab Payload object.

This dataclass will be used to handle the gitlab payload and to build the object from the payload returned by Gitlab webhooks. The main reason for that is because Gitlab might return different structures depending on the event that was sent.

… note::

  • url is refered to the event url (for example the MR url)

  • target is the API endpoint to send a reply by using ConnectorGitlab.send_message.

… py:method:: GitlabPayload.from_dict(payload)

module:

opsdroid.connector.gitlab

classmethod:

Create the GitlabPayload object from a dictionary.

This is the Connector reference

… py:class:: ConnectorGitlab(config, opsdroid=None)

module:

opsdroid.connector.gitlab

canonical:

opsdroid.connector.gitlab.connector.ConnectorGitlab

A connector for Gitlab.

… py:method:: ConnectorGitlab.connect()

module:

opsdroid.connector.gitlab

async:

Connect method for Gitlab.

This connector is using webhook events only. This method is used only to create the routes for the webhooks.

… py:method:: ConnectorGitlab.gitlab_webhook_handler(request)

module:

opsdroid.connector.gitlab

async:

Handle event from Gitlab webhooks.

rtype:

:sphinx_autodoc_typehints_type:\:py\:class\:\~aiohttp.web_response.Response``

… py:method:: ConnectorGitlab.handle_issue_event(payload)

module:

opsdroid.connector.gitlab

async:

Handle issues events.

rtype: