summaryrefslogtreecommitdiff
path: root/tools/ci/test_issue_labeler.py
blob: 6286cb487b6b8d97282cd517013c01630776968c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
#
# Copyright (c) 2026, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#

import unittest

import issue_labeler


def make_event(body, labels=(), action="opened"):
    return {
        "action": action,
        "repository": {"full_name": "RT-Thread/rt-thread"},
        "issue": {
            "number": 42,
            "body": body,
            "labels": [{"name": label} for label in labels],
        },
    }


class FakeClient:
    def __init__(self, available_labels, current_issue=None):
        self.available_labels = [{"name": label} for label in available_labels]
        self.current_issue = current_issue
        self.calls = []

    def list_labels(self, repository):
        self.calls.append(("list_labels", repository))
        return self.available_labels

    def add_labels(self, repository, issue_number, labels):
        self.calls.append(("add_labels", repository, issue_number, tuple(labels)))

    def get_issue(self, repository, issue_number):
        self.calls.append(("get_issue", repository, issue_number))
        return self.current_issue

    def remove_label(self, repository, issue_number, label):
        self.calls.append(("remove_label", repository, issue_number, label))


class FormParsingTests(unittest.TestCase):
    def test_maps_only_structured_allowlisted_values(self):
        body = """### Affected area

Device drivers

### Hardware/BSP vendor

STM32

### Architecture

ARM / AArch64

### Describe the bug

Mentions GD32 and RISC-V here, but free text must not add labels.
"""
        self.assertEqual(
            issue_labeler.desired_labels(body),
            (
                "Component",
                "component: drivers",
                "BSP",
                "BSP: STM32",
                "Arch: ARM/AArch64",
            ),
        )

    def test_unknown_and_no_response_values_are_ignored(self):
        body = """### Affected area

Other / Not sure

### Hardware/BSP vendor

_No response_

### Architecture

x86
"""
        self.assertEqual(issue_labeler.desired_labels(body), ())

    def test_headings_and_values_are_case_insensitive(self):
        body = "### AFFECTED AREA\n\nnetworking\n\n### Architecture\n\nrisc-v\n"
        self.assertEqual(
            issue_labeler.desired_labels(body),
            ("Component", "component: net", "Arch: RISC-V"),
        )


class EventTests(unittest.TestCase):
    def test_accepts_supported_issue_actions(self):
        context = issue_labeler.event_context(make_event("", action="edited"))
        self.assertEqual(context.issue_number, 42)

    def test_rejects_pull_requests_and_other_actions(self):
        pull_request = make_event("")
        pull_request["issue"]["pull_request"] = {"url": "https://example.test"}
        self.assertIsNone(issue_labeler.event_context(pull_request))
        self.assertIsNone(
            issue_labeler.event_context(make_event("", action="closed"))
        )


class LabelApplicationTests(unittest.TestCase):
    def test_adds_only_existing_labels_and_preserves_current_labels(self):
        body = """### Affected area

Kernel

### Hardware/BSP vendor

GD32
"""
        context = issue_labeler.event_context(
            make_event(body, labels=("Kernel", "bug"))
        )
        client = FakeClient(("Kernel", "BSP", "bug"))

        result = issue_labeler.apply_labels(client, context)

        self.assertEqual(result["added"], ["BSP"])
        self.assertEqual(result["missing"], ["BSP: GD32"])
        self.assertIn(
            ("add_labels", "RT-Thread/rt-thread", 42, ("BSP",)), client.calls
        )

    def test_replaces_stale_vendor_and_architecture_labels(self):
        body = """### Hardware/BSP vendor

NXP

### Architecture

RISC-V
"""
        context = issue_labeler.event_context(
            make_event(
                body,
                labels=("bug", "BSP", "BSP: STM32", "Arch: ARM/AArch64"),
            )
        )
        client = FakeClient(
            (
                "bug",
                "BSP",
                "BSP: STM32",
                "BSP: NXP",
                "Arch: ARM/AArch64",
                "Arch: RISC-V",
            ),
            current_issue=context.issue,
        )

        result = issue_labeler.apply_labels(client, context)

        self.assertEqual(result["added"], ["BSP: NXP", "Arch: RISC-V"])
        self.assertEqual(result["removed"], ["BSP: STM32", "Arch: ARM/AArch64"])
        self.assertIn(
            ("remove_label", "RT-Thread/rt-thread", 42, "BSP: STM32"),
            client.calls,
        )
        self.assertIn(
            ("remove_label", "RT-Thread/rt-thread", 42, "Arch: ARM/AArch64"),
            client.calls,
        )

    def test_dry_run_reports_without_mutating(self):
        body = "### Affected area\n\nDocumentation\n"
        context = issue_labeler.event_context(make_event(body))
        client = FakeClient(("Doc",))

        result = issue_labeler.apply_labels(client, context, dry_run=True)

        self.assertEqual(result["added"], ["Doc"])
        self.assertFalse(any(call[0] == "add_labels" for call in client.calls))


if __name__ == "__main__":
    unittest.main()