Repository: packit/packit-service
Issue: GitLab events are not parsed correctly

<details>
<summary>Logs from prod</summary>

```
[2022-05-23 17:17:24,643: DEBUG/MainProcess] TaskPool: Apply <function fast_trace_task at 0x7ff4ff9bf250> (args:('task.steve_jobs.process_message', 'c2bb6cc1-27fd-473d-a761-3b6e4280469c', {'lang': 'py', 'task': 'task.steve_jobs.process_message', 'id': 'c2bb6cc1-27fd-473d-a761-3b6e4280469c', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': 'c2bb6cc1-27fd-473d-a761-3b6e4280469c', 'parent_id': None, 'argsrepr': '()', 'kwargsrepr': "{'event': {'object_kind': 'merge_request', 'event_type': 'merge_request', 'user': {'id': 375555, 'name': 'Matej Focko', 'username': 'mfocko', 'avatar_url': 'https://gitlab.com/uploads/-/system/user/avatar/375555/avatar.png', 'email': '[REDACTED]'}, 'project': {'id': 36413152, 'name': 'karel-the-robot', 'description': None, 'web_url': 'https://gitlab.com/mfocko/karel-the-robot', 'avatar_url': None, 'git_ssh_url': 'git@gitlab.com:mfocko/karel-the-robot.git', 'git_http_url': 'https://gitlab.com/mfocko...
[2022-05-23 17:17:24,644: INFO/ForkPoolWorker-1]
Package Versions:
* OGR 0.38.1
* Packit 0.51.1.dev2+g213c789
* Packit Service 0.1.dev1+g2fbea7c.d20220516
* SQL Alchemy 1.4.36
[2022-05-23 17:17:24,644: INFO/ForkPoolWorker-1] Source: url=https://gitlab.com/mfocko/karel-the-robot namespace=mfocko repo=karel-the-robot branch=test-packit.
[2022-05-23 17:17:24,644: INFO/ForkPoolWorker-1] Target: url=https://gitlab.com/mfocko/karel-the-robot namespace=mfocko repo=karel-the-robot branch=main.
[2022-05-23 17:17:24,644: INFO/ForkPoolWorker-1]
Package Versions:
* OGR 0.38.1
* Packit 0.51.1.dev2+g213c789
* Packit Service 0.1.dev1+g2fbea7c.d20220516
* SQL Alchemy 1.4.36
[2022-05-23 17:17:25,184: DEBUG/ForkPoolWorker-1] Getting package_config:
	project: GitlabProject(namespace="mfocko", repo="karel-the-robot")
	base_project: GitlabProject(namespace="mfocko", repo="karel-the-robot")
	reference: c41f0870ea1dbfcfa9f9d02c369a42359cf502a6
	pr_id: 1
[2022-05-23 17:17:26,280: DEBUG/ForkPoolWorker-1] Found a config file '.packit.yaml' on ref 'c41f0870ea1dbfcfa9f9d02c369a42359cf502a6' of the 'mfocko/karel-the-robot' repository.
[2022-05-23 17:17:26,283: WARNING/ForkPoolWorker-1] Spec file path is not specified in .packit.yaml.
[2022-05-23 17:17:26,492: DEBUG/ForkPoolWorker-1] Package config:
{
    "specfile_path": "karel.spec",
    "upstream_package_name": "karel-the-robot",
    "downstream_package_name": "libkarel",
    "srpm_build_deps": [],
    "jobs": [
        {
            "job": "copr_build",
            "trigger": "pull_request",
            "targets": [
                "fedora-all",
                "opensuse-tumbleweed",
                "opensuse-15.3"
            ]
        },
        {
            "job": "copr_build",
            "trigger": "commit",
            "branch": "main",
            "owner": "mfocko",
            "project": "karel",
            "targets": [
                "fedora-all",
                "opensuse-tumbleweed",
                "opensuse-15.3"
            ]
        }
    ]
}
[2022-05-23 17:17:26,631: INFO/ForkPoolWorker-1] mfocko is admin, you shall pass.
[2022-05-23 17:17:26,635: DEBUG/ForkPoolWorker-1] Removing contents of the PV.
[2022-05-23 17:17:26,734: ERROR/ForkPoolWorker-1] Task task.steve_jobs.process_message[c2bb6cc1-27fd-473d-a761-3b6e4280469c] raised unexpected: AttributeError("'EventData' object has no attribute 'action'")
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/sentry_sdk/integrations/celery.py", line 200, in _inner
    reraise(*exc_info)
  File "/usr/local/lib/python3.10/site-packages/sentry_sdk/_compat.py", line 54, in reraise
    raise value
  File "/usr/local/lib/python3.10/site-packages/sentry_sdk/integrations/celery.py", line 195, in _inner
    return f(*args, **kwargs)
  File "/usr/lib/python3.10/site-packages/celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/tasks.py", line 82, in process_message
    return SteveJobs().process_message(event=event)
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/jobs.py", line 136, in process_message
    return SteveJobs(event_object).process()
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/jobs.py", line 172, in process
    processing_results = self.process_jobs()
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/jobs.py", line 348, in process_jobs
    processing_results.extend(self.create_tasks(job_configs, handler_kls))
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/jobs.py", line 366, in create_tasks
    if self.should_task_be_created_for_job_config_and_handler(
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/jobs.py", line 410, in should_task_be_created_for_job_config_and_handler
    if not handler.pre_check():
  File "/usr/local/lib/python3.10/site-packages/packit_service/worker/handlers/copr.py", line 143, in pre_check
    and self.data.action == GitlabEventAction.closed.value
AttributeError: 'EventData' object has no attribute 'action'
```
</details>

We're apparently missing an attribute in the `EventData` for GitLab. Might be a bug introduced by refactoring.

**»This event hasn't been sent to Sentry.«**

Changed files:
tests/integration/test_handler.py, packit_service/worker/handlers/koji.py, packit_service/worker/handlers/testing_farm.py, packit_service/worker/handlers/copr.py, tests/unit/test_events.py

diff --git a/packit_service/worker/handlers/copr.py b/packit_service/worker/handlers/copr.py
index 699d74bc..8c105a8e 100644
--- a/packit_service/worker/handlers/copr.py
+++ b/packit_service/worker/handlers/copr.py
@@ -140,7 +140,7 @@ class CoprBuildHandler(JobHandler):
     def pre_check(self) -> bool:
         if (
             self.data.event_type == MergeRequestGitlabEvent.__name__
-            and self.data.action == GitlabEventAction.closed.value
+            and self.data.event_dict["action"] == GitlabEventAction.closed.value
         ):
             # Not interested in closed merge requests
             return False
diff --git a/packit_service/worker/handlers/koji.py b/packit_service/worker/handlers/koji.py
index 43625869..2ce9863f 100644
--- a/packit_service/worker/handlers/koji.py
+++ b/packit_service/worker/handlers/koji.py
@@ -107,7 +107,7 @@ class KojiBuildHandler(JobHandler):
     def pre_check(self) -> bool:
         if (
             self.data.event_type == MergeRequestGitlabEvent.__name__
-            and self.data.action == GitlabEventAction.closed.value
+            and self.data.event_dict["action"] == GitlabEventAction.closed.value
         ):
             # Not interested in closed merge requests
             return False
diff --git a/packit_service/worker/handlers/testing_farm.py b/packit_service/worker/handlers/testing_farm.py
index 19adca33..5338bd3d 100644
--- a/packit_service/worker/handlers/testing_farm.py
+++ b/packit_service/worker/handlers/testing_farm.py
@@ -120,7 +120,7 @@ class TestingFarmHandler(JobHandler):
     def pre_check(self) -> bool:
         if (
             self.data.event_type == MergeRequestGitlabEvent.__name__
-            and self.data.action == GitlabEventAction.closed.value
+            and self.data.event_dict["action"] == GitlabEventAction.closed.value
         ):
             # Not interested in closed merge requests
             return False
diff --git a/tests/integration/test_handler.py b/tests/integration/test_handler.py
index 8c50a34a..3dbe3722 100644
--- a/tests/integration/test_handler.py
+++ b/tests/integration/test_handler.py
@@ -93,6 +93,37 @@ def test_precheck(github_pr_event):
     assert copr_build_handler.pre_check()
 
 
+def test_precheck_gitlab(gitlab_mr_event):
+    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
+        pr_id=1,
+        namespace="testing/packit",
+        repo_name="hello-there",
+        project_url="https://gitlab.com/testing/packit/hello-there",
+    ).and_return(
+        flexmock(id=1, job_config_trigger_type=JobConfigTriggerType.pull_request)
+    )
+    copr_build_handler = CoprBuildHandler(
+        package_config=PackageConfig(
+            jobs=[
+                JobConfig(
+                    type=JobType.copr_build,
+                    trigger=JobConfigTriggerType.pull_request,
+                ),
+                JobConfig(
+                    type=JobType.tests,
+                    trigger=JobConfigTriggerType.pull_request,
+                ),
+            ]
+        ),
+        job_config=JobConfig(
+            type=JobType.copr_build,
+            trigger=JobConfigTriggerType.pull_request,
+        ),
+        event=gitlab_mr_event.get_dict(),
+    )
+    assert copr_build_handler.pre_check()
+
+
 def test_precheck_push(github_push_event):
     flexmock(GitBranchModel).should_receive("get_or_create").and_return(
         flexmock(id=1, job_config_trigger_type=JobConfigTriggerType.commit)
diff --git a/tests/unit/test_events.py b/tests/unit/test_events.py
index 6710ac9b..f1a6072a 100644
--- a/tests/unit/test_events.py
+++ b/tests/unit/test_events.py
@@ -1744,16 +1744,26 @@ class TestCentOSEventParser:
         )
         assert event_object.package_config
 
-
-def test_event_data_parse_pr(github_pr_event):
-    # A db_trigger would be created in the get_dict() call bellow.
-    # We don't need that in this test, so let's mock it out.
-    flexmock(PullRequestGithubEvent)
-    PullRequestGithubEvent.db_trigger = None
-    data = EventData.from_event_dict(github_pr_event.get_dict())
-    assert data.event_type == "PullRequestGithubEvent"
-    assert data.actor == "lbarcziova"
-    assert not data.git_ref
-    assert data.commit_sha == "528b803be6f93e19ca4130bf4976f2800a3004c4"
-    assert data.identifier == "342"
-    assert data.pr_id == 342
+    def test_event_data_parse_pr(self, github_pr_event):
+        # A db_trigger would be created in the get_dict() call bellow.
+        # We don't need that in this test, so let's mock it out.
+        flexmock(PullRequestGithubEvent)
+        PullRequestGithubEvent.db_trigger = None
+        data = EventData.from_event_dict(github_pr_event.get_dict())
+        assert data.event_type == "PullRequestGithubEvent"
+        assert data.actor == "lbarcziova"
+        assert not data.git_ref
+        assert data.commit_sha == "528b803be6f93e19ca4130bf4976f2800a3004c4"
+        assert data.identifier == "342"
+        assert data.pr_id == 342
+        assert data.event_dict["action"] == "opened"
+
+    def test_event_data_parse_gitlab_mr(self, gitlab_mr_event):
+        data = EventData.from_event_dict(gitlab_mr_event.get_dict())
+        assert data.event_type == "MergeRequestGitlabEvent"
+        assert data.actor == "shreyaspapi"
+        assert not data.git_ref
+        assert data.commit_sha == "1f6a716aa7a618a9ffe56970d77177d99d100022"
+        assert data.identifier == "1"
+        assert data.pr_id == 1
+        assert data.event_dict["action"] == "opened"