Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions chronicler/events/core/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) GrimoireLab Developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import logging
from typing import Any, Optional

from cloudevents.http import CloudEvent

from grimoirelab_toolkit.identities import generate_uuid

from ...eventizer import Eventizer, uuid


GITLAB_DATASOURCE = "gitlab"

GITLAB_EVENT_ISSUE = "org.grimoirelab.events.gitlab.issue"

GITLAB_EVENT_AUTHOR = "org.grimoirelab.events.gitlab.author"
GITLAB_EVENT_ASSIGNEE = "org.grimoirelab.events.gitlab.assignee"
GITLAB_EVENT_CLOSED_BY = "org.grimoirelab.events.gitlab.closed_by"

ISSUE_IDENTITY_TYPES = {
"author": GITLAB_EVENT_AUTHOR,
"assignees": GITLAB_EVENT_ASSIGNEE,
"closed_by": GITLAB_EVENT_CLOSED_BY,
}

logger = logging.getLogger(__name__)


class GitLabEventizer(Eventizer):
"""Eventize GitLab issues and merge requests.

The incoming `raw_item` is expected to be a chronicler item produced by
Perceval/driver for GitLab. Field names can vary, so the implementation is
defensive and tries common variants (for example: 'notes' or
'comments_data' for comments, 'iid' or 'number' for the issue number).
"""

def eventize_item(self, raw_item: dict[str, Any]) -> list[CloudEvent]:
events: list[CloudEvent] = []

item_uuid = raw_item.get("uuid")
if not item_uuid:
raise ValueError("'uuid' attribute not found on item.")
if raw_item["backend_name"].lower() != GITLAB_DATASOURCE:
raise ValueError(f"Item {item_uuid} is not a {GITLAB_DATASOURCE} item.")

category = raw_item.get("category")
if category == "issue":
event_type = GITLAB_EVENT_ISSUE
else:
raise ValueError(f"Unsupported category '{category}' for item '{item_uuid}'.")

attributes = {
"id": item_uuid,
"type": event_type,
"source": raw_item["origin"],
"time": raw_item["updated_on"],
}

cloud_event = CloudEvent(attributes, raw_item["data"])
events.append(cloud_event)

identities_events = self._eventize_identities(
cloud_event, raw_item["data"], ISSUE_IDENTITY_TYPES
)
events.extend(identities_events)

return events

def _eventize_identities(
self,
parent_event: CloudEvent,
raw_item: dict[str, Any],
identity_types: dict[str, Any],
) -> list[CloudEvent]:
"""Eventize issue identities from a GitLab issue item."""

events: list[CloudEvent] = []

for identity_type, event_type in identity_types.items():
identity_data = raw_item.get(identity_type)
if not identity_data:
continue

if isinstance(identity_data, list):
for item in identity_data:
user = self._process_identity(
parent_event=parent_event,
raw_identity=item,
event_type=event_type,
)
if user:
events.append(user)
else:
user = self._process_identity(
parent_event=parent_event,
raw_identity=identity_data,
event_type=event_type,
)
if user:
events.append(user)

return events

def _process_identity(
self,
parent_event: CloudEvent,
raw_identity: dict[str, Any],
event_type: str,
) -> Optional[CloudEvent]:
"""Obtain a CloudEvent for a given identity.

:param parent_event: Parent CloudEvent
:param raw_identity: Raw identity data from the item
:param event_type: Type of identity event

:returns: CloudEvent for the identity
"""
source = parent_event["source"]
time = parent_event["time"]
parent_id = parent_event["id"]

identity = {
"username": raw_identity.get("username"),
"email": raw_identity.get("public_email") or None,
"name": raw_identity.get("name") or None,
}

try:
identity_id = generate_uuid(
source=GITLAB_DATASOURCE,
email=identity["email"],
name=identity["name"],
username=identity["username"],
)
except ValueError as e:
logger.warning(
f"Cannot generate UUID for identity '{raw_identity}' "
f"in event '{parent_id}': {e}. Skipping."
)
return None

event_id = uuid(parent_id, event_type, identity_id)

data = {
"source": GITLAB_DATASOURCE,
"name": identity["name"],
"username": identity["username"],
"email": identity["email"],
"uuid": identity_id,
}
attributes = {
"id": event_id,
"linked_event": parent_id,
"type": event_type,
"source": source,
"time": time,
}

return CloudEvent(attributes, data)
8 changes: 8 additions & 0 deletions releases/unreleased/gitlab-eventizer-for-issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: GitLab Eventizer for Issues
category: added
author: Jose Javier Merchante <jjmerchante@bitergia.com>
issue: null
notes: >
Handle GitLab issue events and generate related events,
including comments and identities.
4 changes: 4 additions & 0 deletions tests/data/gitlab_issues.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"backend_name":"GitLab","backend_version":"1.0.0","category":"issue","classified_fields_filtered":null,"data":{"_links":{"award_emoji":"https://gitlab.com/api/v4/projects/77837443/issues/2/award_emoji","closed_as_duplicate_of":null,"notes":"https://gitlab.com/api/v4/projects/77837443/issues/2/notes","project":"https://gitlab.com/api/v4/projects/77837443","self":"https://gitlab.com/api/v4/projects/77837443/issues/2"},"assignee":null,"assignees":[],"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"blocking_issues_count":0,"closed_at":"2026-01-20T09:56:45.944Z","closed_by":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"confidential":false,"created_at":"2026-01-20T09:56:26.236Z","description":"Body issue 2","discussion_locked":null,"downvotes":0,"due_date":null,"has_tasks":true,"id":181476659,"iid":2,"imported":false,"imported_from":"none","issue_type":"issue","labels":[],"merge_requests_count":0,"milestone":null,"moved_to_id":null,"notes_data":[],"project_id":77837443,"references":{"full":"user_1/tmp#2","relative":"#2","short":"#2"},"service_desk_reply_to":null,"severity":"UNKNOWN","state":"closed","task_completion_status":{"completed_count":0,"count":0},"task_status":"0 of 0 checklist items completed","time_stats":{"human_time_estimate":null,"human_total_time_spent":null,"time_estimate":0,"total_time_spent":0},"title":"Example issue 2","type":"ISSUE","updated_at":"2026-01-20T09:56:46.324Z","upvotes":0,"user_notes_count":0,"web_url":"https://gitlab.com/user_1/tmp/-/issues/2"},"origin":"https://gitlab.com/user_1/tmp","perceval_version":"1.4.4","search_fields":{"groups":null,"iid":2,"item_id":"181476659","owner":"user_1","project":"tmp"},"tag":"https://gitlab.com/user_1/tmp","timestamp":1768904305.608975,"updated_on":1768903006.324,"uuid":"cbd19489e7812e32f5f26473e202af1a892c0a65"}
{"backend_name":"GitLab","backend_version":"1.0.0","category":"issue","classified_fields_filtered":null,"data":{"_links":{"award_emoji":"https://gitlab.com/api/v4/projects/77837443/issues/3/award_emoji","closed_as_duplicate_of":null,"notes":"https://gitlab.com/api/v4/projects/77837443/issues/3/notes","project":"https://gitlab.com/api/v4/projects/77837443","self":"https://gitlab.com/api/v4/projects/77837443/issues/3"},"assignee":null,"assignees":[],"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"blocking_issues_count":0,"closed_at":null,"closed_by":null,"confidential":false,"created_at":"2026-01-20T09:57:03.162Z","description":"Body incident","discussion_locked":null,"downvotes":0,"due_date":null,"has_tasks":true,"id":181476677,"iid":3,"imported":false,"imported_from":"none","issue_type":"incident","labels":[],"merge_requests_count":0,"milestone":null,"moved_to_id":null,"notes_data":[],"project_id":77837443,"references":{"full":"user_1/tmp#3","relative":"#3","short":"#3"},"service_desk_reply_to":null,"severity":"UNKNOWN","state":"opened","task_completion_status":{"completed_count":0,"count":0},"task_status":"0 of 0 checklist items completed","time_stats":{"human_time_estimate":null,"human_total_time_spent":null,"time_estimate":0,"total_time_spent":0},"title":"Example incident","type":"INCIDENT","updated_at":"2026-01-20T09:57:03.162Z","upvotes":0,"user_notes_count":0,"web_url":"https://gitlab.com/user_1/tmp/-/issues/3"},"origin":"https://gitlab.com/user_1/tmp","perceval_version":"1.4.4","search_fields":{"groups":null,"iid":3,"item_id":"181476677","owner":"user_1","project":"tmp"},"tag":"https://gitlab.com/user_1/tmp","timestamp":1768904306.188272,"updated_on":1768903023.162,"uuid":"8ea9a635f395f8dadafd932c59b1e42e754eefd0"}
{"backend_name":"GitLab","backend_version":"1.0.0","category":"issue","classified_fields_filtered":null,"data":{"_links":{"award_emoji":"https://gitlab.com/api/v4/projects/77837443/issues/4/award_emoji","closed_as_duplicate_of":null,"notes":"https://gitlab.com/api/v4/projects/77837443/issues/4/notes","project":"https://gitlab.com/api/v4/projects/77837443","self":"https://gitlab.com/api/v4/projects/77837443/issues/4"},"assignee":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"assignees":[{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"}],"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"blocking_issues_count":0,"closed_at":null,"closed_by":null,"confidential":false,"created_at":"2026-01-20T09:57:16.287Z","description":"Body task","discussion_locked":null,"downvotes":0,"due_date":null,"has_tasks":true,"id":181476687,"iid":4,"imported":false,"imported_from":"none","issue_type":"task","labels":[],"merge_requests_count":0,"milestone":null,"moved_to_id":null,"notes_data":[{"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"body":"assigned to @user_1","commands_changes":{},"confidential":false,"created_at":"2026-01-20T09:57:16.435Z","id":3019322655,"imported":false,"imported_from":"none","internal":false,"noteable_id":181476687,"noteable_iid":4,"noteable_type":"Issue","project_id":77837443,"resolvable":false,"system":true,"type":null,"updated_at":"2026-01-20T09:57:16.444Z"}],"project_id":77837443,"references":{"full":"user_1/tmp#4","relative":"#4","short":"#4"},"service_desk_reply_to":null,"severity":"UNKNOWN","state":"opened","task_completion_status":{"completed_count":0,"count":0},"task_status":"0 of 0 checklist items completed","time_stats":{"human_time_estimate":null,"human_total_time_spent":null,"time_estimate":0,"total_time_spent":0},"title":"Example task","type":"TASK","updated_at":"2026-01-20T09:57:16.287Z","upvotes":0,"user_notes_count":0,"web_url":"https://gitlab.com/user_1/tmp/-/work_items/4"},"origin":"https://gitlab.com/user_1/tmp","perceval_version":"1.4.4","search_fields":{"groups":null,"iid":4,"item_id":"181476687","owner":"user_1","project":"tmp"},"tag":"https://gitlab.com/user_1/tmp","timestamp":1768904307.043856,"updated_on":1768903036.287,"uuid":"9d47dbbd2df9579269d1d409548d04c51cf1f0b9"}
{"backend_name":"GitLab","backend_version":"1.0.0","category":"issue","classified_fields_filtered":null,"data":{"_links":{"award_emoji":"https://gitlab.com/api/v4/projects/77837443/issues/1/award_emoji","closed_as_duplicate_of":null,"notes":"https://gitlab.com/api/v4/projects/77837443/issues/1/notes","project":"https://gitlab.com/api/v4/projects/77837443","self":"https://gitlab.com/api/v4/projects/77837443/issues/1"},"assignee":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"assignees":[{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"}],"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"blocking_issues_count":0,"closed_at":null,"closed_by":null,"confidential":false,"created_at":"2026-01-20T09:55:57.188Z","description":"Body issue 1","discussion_locked":null,"downvotes":0,"due_date":null,"has_tasks":true,"id":181476619,"iid":1,"imported":false,"imported_from":"none","issue_type":"issue","labels":[],"merge_requests_count":0,"milestone":null,"moved_to_id":null,"notes_data":[{"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"body":"assigned to @user_1","commands_changes":{},"confidential":false,"created_at":"2026-01-20T09:55:57.529Z","id":3019317146,"imported":false,"imported_from":"none","internal":false,"noteable_id":181476619,"noteable_iid":1,"noteable_type":"Issue","project_id":77837443,"resolvable":false,"system":true,"type":null,"updated_at":"2026-01-20T09:55:57.536Z"},{"author":{"avatar_url":"https://secure.gravatar.com/avatar/xxxxxxxx?s=80&d=identicon","id":8276902,"locked":false,"name":"User Test","public_email":"","state":"active","username":"user_1","web_url":"https://gitlab.com/user_1"},"award_emoji_data":[],"body":"This is a comment","commands_changes":{},"confidential":false,"created_at":"2026-01-20T10:18:14.789Z","id":3019389027,"imported":false,"imported_from":"none","internal":false,"noteable_id":181476619,"noteable_iid":1,"noteable_type":"Issue","project_id":77837443,"resolvable":false,"system":false,"type":null,"updated_at":"2026-01-20T10:18:14.789Z"}],"project_id":77837443,"references":{"full":"user_1/tmp#1","relative":"#1","short":"#1"},"service_desk_reply_to":null,"severity":"UNKNOWN","state":"opened","task_completion_status":{"completed_count":0,"count":0},"task_status":"0 of 0 checklist items completed","time_stats":{"human_time_estimate":null,"human_total_time_spent":null,"time_estimate":0,"total_time_spent":0},"title":"Example issue 1","type":"ISSUE","updated_at":"2026-01-20T10:18:14.879Z","upvotes":0,"user_notes_count":1,"web_url":"https://gitlab.com/user_1/tmp/-/issues/1"},"origin":"https://gitlab.com/user_1/tmp","perceval_version":"1.4.4","search_fields":{"groups":null,"iid":1,"item_id":"181476619","owner":"user_1","project":"tmp"},"tag":"https://gitlab.com/user_1/tmp","timestamp":1768904308.924827,"updated_on":1768904294.879,"uuid":"753f641525292d18618695f3c4ea7d36a9253fce"}
Loading