| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright © 2025 Collabora Ltd. |
| 3 |
# Authors: |
| 4 |
# Sergi Blanch Torne <[email protected]> |
| 5 |
# |
| 6 |
# SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
from dataclasses import dataclass |
| 9 |
from datetime import timedelta |
| 10 |
|
| 11 |
from unittest import TestCase |
| 12 |
from typing import Iterable |
| 13 |
|
| 14 |
from marge_queue import MargeMergeRequest, MargeQueue, get_merge_queue |
| 15 |
|
| 16 |
|
| 17 |
DATA = { |
| 18 |
37564: { |
| 19 |
# sample of a Merge Request rejected by marge and later reassigned to marge |
| 20 |
"updated_at": "2025-09-25T10:59:27.144Z", |
| 21 |
"title": "etnaviv: Fix util_blitter_save_so_targets(..) call", |
| 22 |
"notes": [ |
| 23 |
# Those notes must be listed in reverse order |
| 24 |
# because the list is requested sort="desc" |
| 25 |
{ |
| 26 |
"body": "assigned to @marge-bot and unassigned @austriancoder", |
| 27 |
"created_at": "2025-09-25T11:44:01.672Z", |
| 28 |
}, |
| 29 |
{ |
| 30 |
"body": "added 1 commit", |
| 31 |
"created_at": "2025-09-25T10:43:43.490Z", |
| 32 |
}, |
| 33 |
{ |
| 34 |
"body": "This branch couldn't be merged:", |
| 35 |
"created_at": "2025-09-25T10:42:27.817Z", |
| 36 |
}, |
| 37 |
{ |
| 38 |
"body": "assigned to @austriancoder and unassigned @marge-bot", |
| 39 |
"created_at": "2025-09-25T10:42:15.404Z", |
| 40 |
}, |
| 41 |
{ |
| 42 |
"body": "added 3 commits", |
| 43 |
"created_at": "2025-09-25T10:38:57.332Z", |
| 44 |
}, |
| 45 |
{ |
| 46 |
"body": "assigned to @marge-bot", |
| 47 |
"created_at": "2025-09-25T10:38:03.993Z", |
| 48 |
}, |
| 49 |
], |
| 50 |
}, |
| 51 |
37560: { |
| 52 |
# sample of a Merge Request assigned to marge between the two |
| 53 |
# assignments of the previous sample. This is meant to show that |
| 54 |
# this goes first in the queue, and it doesn't confuses the sequence. |
| 55 |
"updated_at": "2025-09-25T11:38:08.420Z", |
| 56 |
"title": "tu: limit query pool types logged into RMV", |
| 57 |
"notes": [ |
| 58 |
{ |
| 59 |
"body": "assigned to @marge-bot", |
| 60 |
"created_at": "2025-09-25T10:47:40.040Z", |
| 61 |
}, |
| 62 |
{ |
| 63 |
"body": "added 1 commit", |
| 64 |
"created_at": "2025-09-25T10:46:42.113Z", |
| 65 |
}, |
| 66 |
{ |
| 67 |
"body": "mentioned in issue #13970", |
| 68 |
"created_at": "2025-09-25T10:45:35.566Z", |
| 69 |
}, |
| 70 |
{ |
| 71 |
"body": "Rb", |
| 72 |
"created_at": "2025-09-25T08:25:51.527Z", |
| 73 |
}, |
| 74 |
], |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
@dataclass |
| 80 |
class ProjectMergeRequestNote: |
| 81 |
body: str |
| 82 |
created_at: str |
| 83 |
|
| 84 |
|
| 85 |
@dataclass |
| 86 |
class ProjectMergeRequestNoteManager: |
| 87 |
""" |
| 88 |
Mock the gitlab.v4.objects.ProjectMergeRequestNoteManager |
| 89 |
""" |
| 90 |
iid: int |
| 91 |
|
| 92 |
def list(self, *args, **kwargs) -> Iterable: |
| 93 |
for note in DATA[self.iid]["notes"]: |
| 94 |
yield ProjectMergeRequestNote(**note) |
| 95 |
|
| 96 |
|
| 97 |
@dataclass |
| 98 |
class ProjectMergeRequest: |
| 99 |
""" |
| 100 |
Mock the gitlab.v4.objects.ProjectMergeRequest |
| 101 |
""" |
| 102 |
iid: int |
| 103 |
|
| 104 |
@property |
| 105 |
def updated_at(self) -> str: |
| 106 |
return DATA[self.iid]["updated_at"] |
| 107 |
|
| 108 |
@property |
| 109 |
def web_url(self) -> str: |
| 110 |
return f"https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/{self.iid}" |
| 111 |
|
| 112 |
@property |
| 113 |
def title(self) -> str: |
| 114 |
return DATA[self.iid]["title"] |
| 115 |
|
| 116 |
@property |
| 117 |
def notes(self) -> ProjectMergeRequestNoteManager: |
| 118 |
return ProjectMergeRequestNoteManager(self.iid) |
| 119 |
|
| 120 |
|
| 121 |
@dataclass |
| 122 |
class ProjectMergeRequestManager: |
| 123 |
""" |
| 124 |
Mock the gitlab.v4.objects.ProjectMergeRequestManager |
| 125 |
""" |
| 126 |
def list(self, *args, **kwargs) -> Iterable: |
| 127 |
for mr in DATA.keys(): |
| 128 |
yield ProjectMergeRequest(mr) |
| 129 |
|
| 130 |
|
| 131 |
@dataclass |
| 132 |
class Project: |
| 133 |
""" |
| 134 |
Mock the gitlab.v4.objects.Project |
| 135 |
""" |
| 136 |
@property |
| 137 |
def mergerequests(self) -> ProjectMergeRequestManager: |
| 138 |
return ProjectMergeRequestManager() |
| 139 |
|
| 140 |
|
| 141 |
class MargeMergeTestCase(TestCase): |
| 142 |
def test_queue(self) -> None: |
| 143 |
queue = MargeQueue() |
| 144 |
for mr_id in DATA.keys(): |
| 145 |
gl_mr_mock_obj = ProjectMergeRequest(mr_id) |
| 146 |
marge_mr = MargeMergeRequest(gl_mr_mock_obj) |
| 147 |
|
| 148 |
# for each element in the data sample, test the MargeMergeRequest construction |
| 149 |
self.assertEqual(marge_mr.title, gl_mr_mock_obj.title) |
| 150 |
self.assertEqual(marge_mr.web_url, gl_mr_mock_obj.web_url) |
| 151 |
self.assertIsInstance(marge_mr.time_enqueued, timedelta) |
| 152 |
|
| 153 |
queue.append(marge_mr) |
| 154 |
|
| 155 |
self.assertEqual(queue.n_merge_requests_enqueued, 2) |
| 156 |
|
| 157 |
|
| 158 |
def test_get_merge_queue(self) -> None: |
| 159 |
project = Project() |
| 160 |
queue = get_merge_queue(project) |
| 161 |
|
| 162 |
self.assertEqual(queue.n_merge_requests_enqueued, 2) |
| 163 |
self.assertEqual(len(queue.sorted_queue), 2) |
| 164 |
# They are in reverse order because of the reassignment to marge |
| 165 |
self.assertEqual(queue.sorted_queue[0].id, list(DATA.keys())[1]) |
| 166 |
self.assertEqual(queue.sorted_queue[1].id, list(DATA.keys())[0]) |
| 167 |
self.assertEqual(len(queue.undetermined), 0) |
| 168 |
|
| 169 |
for marge_mr in queue.sorted_queue: |
| 170 |
mr_id = marge_mr.id |
| 171 |
self.assertEqual(marge_mr.title, DATA[mr_id]["title"]) |
| 172 |
self.assertEqual( |
| 173 |
marge_mr.web_url, |
| 174 |
f"https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/{mr_id}" |
| 175 |
) |
| 176 |
self.assertIsInstance(marge_mr.time_enqueued, timedelta) |