-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_labels.py
executable file
·802 lines (741 loc) · 25.9 KB
/
check_labels.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright (C) 2020 Martine Lenders <[email protected]>
#
# Distributed under terms of the MIT license.
import argparse
import fnmatch
import re
import os
import sys
import github
try:
import pytest
except ImportError: # pragma: no cover
pytest = None
def cs_string_list(value): # pylint: disable=too-many-branches
"""
Parses a comma separated strings into a python list
# >>> cs_string_list('')
# []
# >>> cs_string_list("''")
# ['']
# >>> cs_string_list('""')
# ['']
# >>> cs_string_list('test')
# ['test']
# >>> cs_string_list(' test ')
# ['test']
# >>> cs_string_list(' te"st" ')
# ['te"st"']
# >>> cs_string_list('foo bar')
# ['foo bar']
# >>> cs_string_list("test,' foo bar'")
# ['test', ' foo bar']
>>> cs_string_list('"foo bar " , test ')
['foo bar ', 'test']
>>> cs_string_list('test, "' "'foo bar'" '"')
['test', "'foo bar'"]
>>> cs_string_list("test, " '"bar, foo"' ", 'foo bar' ")
['test', 'bar, foo', 'foo bar']
>>> cs_string_list("test,,,")
['test', '', '', '']
>>> cs_string_list("test,,, ")
['test', '', '', '']
"""
res = [""]
in_str = False
strip_str = True
open_char = None
last_char = None
for char in value:
if in_str: # we are in a string
if open_char: # we are in a quoted string quoted with open_char
if char == open_char: # quoted string is closed
in_str = False
open_char = None
else:
res[-1] += char
elif char == ",": # next string to process
in_str = False
# we only land here for unquoted strings as char == open_char
# branch sets in_str == False
res[-1] = res[-1].strip()
res.append("") # append next state
else:
res[-1] += char
elif char == " ": # skip non-quoted white-spaces
continue
elif char == ",": # next string to process
in_str = False
if strip_str: # was not a quoted string => strip
res[-1] = res[-1].strip()
else:
strip_str = True # else reset strip_str for next
res.append("") # append next state
else:
in_str = True # we found a string!
if char in ['"', "'"]: # and it is quoted
open_char = char
strip_str = False
else:
res[-1] += char
last_char = char
if open_char:
raise ValueError(f"Unterminated string '{res[-1]}'")
if not res[-1] and last_char not in [",", "'", '"']:
del res[-1]
if res and strip_str: # strip remaining unquoted string
res[-1] = res[-1].strip()
return res
def cs_2tuple_list(value): # pylint: disable=too-many-branches
"""
Parses a comma separated 2-tuple strings into a python list of tuples
>>> cs_2tuple_list('')
[]
>>> cs_2tuple_list('(foobar, "test")')
[('foobar', 'test')]
>>> cs_2tuple_list('(foobar, "test"), ('"'barfoo', "' lalala) ')
[('foobar', 'test'), ('barfoo', 'lalala')]
>>> cs_2tuple_list('(foobar, "test"), ("(barfoo", "lalala)")')
[('foobar', 'test'), ('(barfoo', 'lalala)')]
"""
res = [""]
in_tuple = False
quote_char = None
for char in value:
if in_tuple:
if not quote_char and char in ["'", '"']:
quote_char = char
elif char == quote_char:
quote_char = None
elif not quote_char and char == ")":
res[-1] = tuple(cs_string_list(res[-1]))
in_tuple = False
else:
res[-1] += char
elif char == " ":
continue
elif char == ",":
res.append("")
elif char == "(":
in_tuple = True
else:
raise ValueError(f"Unexpected character '{char}' after '{res}'")
if in_tuple or quote_char:
raise ValueError(f"Unterminated tuple {res[-1]}")
# remove empty string stored as state
if not isinstance(res[-1], tuple):
del res[-1]
if any(not isinstance(e, tuple) or len(e) != 2 for e in res):
raise ValueError(f"Unexpected value in {res}")
return res
def get_pull_no(ref):
"""
Get pull request from a git given ref
>>> get_pull_no('refs/pull/12345/head')
12345
>>> get_pull_no('refs/pull/6789/merge')
6789
"""
match = re.search("refs/pull/([0-9]+)/", ref)
if match:
return int(match[1])
print(os.environ)
if os.environ.get("INPUT_PULL_REQUEST"):
return int(os.environ["INPUT_PULL_REQUEST"])
raise ValueError(f"Unable to get pull request number from ref {ref}")
def parse_condition(condition):
"""
Parses a condition for cond_labels
>>> parse_condition("review.approval > 3")
['review.approval', '3']
"""
elems = condition.split(">")
if len(elems) != 2:
raise ValueError("Unable to parse ")
return [e.strip() for e in elems]
def check_review_approvals(pull, condition, missing_approvals_label):
condition[1] = int(condition[1])
approving_reviewers = set()
for review in pull.get_reviews():
if review.state == "APPROVED":
approving_reviewers.add(review.user.login)
if len(approving_reviewers) > condition[1]:
print(
f"PR#{pull} has {len(approving_reviewers)}/{condition[1] + 1}"
f" approving reviewers, removing label '{missing_approvals_label}'"
)
if missing_approvals_label:
try:
pull.remove_from_labels(missing_approvals_label)
except github.GithubException as exc:
# may happen if label was already removed => log but don't error
print(
f"Error on removing {missing_approvals_label}, "
f"{exc.status}: {exc.data['message']}"
)
return True
if missing_approvals_label and condition[1] > 0:
print(
f"PR#{pull} has only {len(approving_reviewers)}/{condition[1] + 1}"
f" approving reviewers, setting label '{missing_approvals_label}'"
)
pull.add_to_labels(missing_approvals_label)
return False
VALID_CONDITIONS = {"review.approvals": check_review_approvals}
def check_condition(pull, condition, missing_approvals_label):
elems = parse_condition(condition)
try:
return VALID_CONDITIONS[elems[0]](pull, elems, missing_approvals_label)
except KeyError:
# We don't want the original traceback here
# pylint: disable=raise-missing-from
raise ValueError(f"Unrecognized condition {condition}")
def check_labels(set_labels, unset_labels, cond_labels, missing_approvals_label, pull):
pull_labels = [label.name for label in pull.get_labels()]
set_labels_check = [False for label in set_labels]
for pull_label in pull_labels:
for i, set_label in enumerate(set_labels):
if fnmatch.fnmatch(pull_label, set_label):
set_labels_check[i] = True
for unset_label in unset_labels:
if fnmatch.fnmatch(pull_label, unset_label):
print(f"{', '.join(unset_labels)} are expected not to be set")
return 1
if not all(set_labels_check):
print(f"{', '.join(set_labels)} are expected to be set")
return 1
res = 0
for cond_label, condition in cond_labels:
for pull_label in pull_labels:
if fnmatch.fnmatch(pull_label, cond_label) and not check_condition(
pull, condition, missing_approvals_label
):
print(f"Condition {condition} for label {pull_label} not fulfilled")
# favor listing all failed conditions over early exit
res = 1
return res
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"set_labels",
default="",
type=cs_string_list,
help="Comma-separated list of labels required to be set. default: ''",
)
parser.add_argument(
"unset_labels",
default="",
type=cs_string_list,
help="Comma-separated list of labels required not to be set. default: ''",
)
parser.add_argument(
"cond_labels",
default="",
type=cs_2tuple_list,
help=(
"Comma-separated list of (label, condition) for labels "
"introducing a conditions. "
"default: ''. "
"Supported conditions: 'review.approvals>x' "
"where x is a positive number"
),
)
parser.add_argument(
"missing_approvals_label",
default="",
type=str,
help="Name of label reflecting the approval status,"
"will be set while approvals are missing"
"default: '' (no label is managed). ",
)
args = parser.parse_args()
repo_name = os.environ["GITHUB_REPOSITORY"]
repo = github.Github(os.environ.get("INPUT_ACCESS_TOKEN", None)).get_repo(repo_name)
if args.missing_approvals_label:
# turn label string into label object
try:
missing_approvals_label = repo.get_label(args.missing_approvals_label)
except github.GithubException:
print(
f"Error getting label '{args.missing_approvals_label}'"
" from github. Does it exist?"
)
return 1
else:
missing_approvals_label = None
return check_labels(
args.set_labels,
args.unset_labels,
args.cond_labels,
missing_approvals_label,
repo.get_pull(get_pull_no(os.environ["GITHUB_REF"])),
)
if __name__ == "__main__":
sys.exit(main()) # pragma: no cover
if pytest: # noqa: C901
@pytest.mark.parametrize(
"value, exp",
[("", [])],
)
def test_cs_string_list(value, exp):
res = cs_string_list(value)
assert res == exp
@pytest.mark.parametrize(
"value",
['"', "'", "test,' foo bar", 'test," foo bar', '"test', "'test"],
)
def test_cs_string_list_value_error(value):
with pytest.raises(ValueError):
cs_string_list(value)
@pytest.mark.parametrize(
"value",
[")", '"', "'", "(test", '(test, foobar, "foo bar")'],
)
def test_cs_2tuple_list_value_error(value):
with pytest.raises(ValueError):
cs_2tuple_list(value)
def test_get_pull_no_invalid():
with pytest.raises(ValueError):
get_pull_no("foobar")
def test_get_pull_from_env(monkeypatch):
monkeypatch.setitem(os.environ, "INPUT_PULL_REQUEST", "59706")
assert get_pull_no("refs/pull/12345/head") == 12345
assert get_pull_no("foobar") == 59706
@pytest.mark.parametrize(
"value",
["foobar", "foobar>3>test"],
)
def test_parse_condition_invalid(value):
with pytest.raises(ValueError):
parse_condition(value)
# pylint: disable=too-few-public-methods
class MockLabel:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
# pylint: disable=too-few-public-methods
class MockReview:
def __init__(self, state, reviewer):
self._state = state
class MockUser:
def __init__(self, login):
self.login = login
self.user = MockUser(reviewer)
@property
def state(self):
return self._state
class MockPull:
def __init__(self, labels=None, reviews=None):
self.labels = labels
self.reviews = reviews
def add_to_labels(self, label):
if self.labels:
self.labels.append(label)
else:
self.labels = [label]
def remove_from_labels(self, label):
if self.labels is not None:
try:
self.labels.remove(label)
except ValueError as exc:
raise github.GithubException(
status=404, data={"message": str(exc)}
) from exc
def get_labels(self):
yield from self.labels
def get_reviews(self):
yield from self.reviews
@pytest.mark.parametrize(
"value, labels, reviews, missing_approvals_label, exp",
[
(
["review.approvals", 2],
None,
[("APPROVED", "a"), ("APPROVED", "b"), ("APPROVED", "c")],
"",
True,
),
(
["review.approvals", 2],
None,
[("APPROVED", "a"), ("APPROVED", "b")],
"",
False,
),
(
["review.approvals", 1],
None,
[("APPROVED", "a"), ("APPROVED", "b")],
"",
True,
),
(
["review.approvals", 1],
None,
[("COMMENT", "a"), ("APPROVED", "b")],
"",
False,
),
(["review.approvals", 1], None, [("COMMENT", "a")], "", False),
(["review.approvals", 1], None, [("APPROVED", "a")], "DON'T MERGE", False),
(
["review.approvals", 1],
["FOOBAR"],
[("APPROVED", "a")],
"DON'T MERGE",
False,
),
(
["review.approvals", 1],
None,
[("APPROVED", "a"), ("APPROVED", "b")],
"DON'T MERGE",
True,
),
(
["review.approvals", 1],
["FOOBAR"],
[("APPROVED", "a"), ("APPROVED", "b")],
"DON'T MERGE",
True,
),
(
["review.approvals", 1],
["DON'T MERGE", "FOOBAR"],
[("APPROVED", "a"), ("APPROVED", "b")],
"DON'T MERGE",
True,
),
(
["review.approvals", 2],
None,
[("APPROVED", "a"), ("APPROVED", "a")],
"DON'T MERGE",
False,
),
],
)
def test_check_review_approvals(
value, labels, reviews, missing_approvals_label, exp
):
pull = MockPull(
labels=labels,
reviews=[MockReview(state, reviewer) for (state, reviewer) in reviews],
)
assert check_review_approvals(pull, value, missing_approvals_label) == exp
if not exp and missing_approvals_label:
assert missing_approvals_label in pull.labels
if exp and missing_approvals_label:
assert not pull.labels or missing_approvals_label not in pull.labels
def test_check_condition(monkeypatch):
pull = MockPull()
monkeypatch.setattr(
sys.modules[__name__], "parse_condition", lambda x: ["test", 1]
)
monkeypatch.setitem(VALID_CONDITIONS, "test", lambda x, y, z: True)
assert check_condition(pull, "test > 1", "")
def test_check_condition_invalid(monkeypatch):
pull = MockPull()
monkeypatch.setattr(
sys.modules[__name__], "parse_condition", lambda x: ["test", 1]
)
if "test" in VALID_CONDITIONS:
monkeypatch.delitem(VALID_CONDITIONS, "test", lambda x, y: True)
with pytest.raises(ValueError):
check_condition(pull, "test > 1", "")
@pytest.mark.parametrize(
"set_labels, unset_labels, cond_labels, cond_res, pull_labels, "
"missing_approvals_label, exp",
[
([], [], [], False, [], "", 0),
([], [], [], False, ["lalala"], "", 0),
([], [], [], False, ["foobar"], "", 0),
([], [], [], True, [], "", 0),
([], [], [], True, ["lalala", "yes"], "", 0),
([], [], [], True, ["foobar"], "", 0),
([], [], [("foobar", "test>1")], False, [], "", 0),
([], [], [("foobar", "test>1")], False, ["lalala"], "", 0),
([], [], [("foobar", "test > 1")], False, ["lalala", "foobar"], "", 1),
([], [], [("foobar", "test>1")], True, [], "", 0),
([], [], [("foobar", "test>1")], True, ["lalala"], "", 0),
([], [], [("foobar", "test>1")], True, ["lalala", "foobar"], "", 0),
([], ["don't merge"], [], False, [], "", 0),
([], ["don't merge"], [], False, ["lalala"], "", 0),
([], ["don't merge"], [], False, ["lalala", "don't merge"], "", 1),
([], ["don't merge"], [], True, [], "", 0),
([], ["don't merge"], [], True, ["lalala"], "", 0),
([], ["don't merge"], [], True, ["lalala", "don't merge"], "", 1),
([], ["don't merge"], [("foobar", "test>1")], False, [], "", 0),
([], ["don't merge"], [("foobar", "test>1")], False, ["lalala"], "", 0),
(
[],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar"],
"",
1,
),
(
[],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar", "don't merge"],
"",
1,
),
([], ["don't merge"], [("foobar", "test>1")], True, [], "", 0),
([], ["don't merge"], [("foobar", "test>1")], True, ["lalala"], "", 0),
(
[],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar"],
"",
0,
),
(
[],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar", "don't merge"],
"",
1,
),
(["yes"], [], [], False, [], "", 1),
(["yes"], [], [], False, ["lalala"], "", 1),
(["yes"], [], [], False, ["lalala", "don't merge"], "", 1),
(["yes*"], [], [], False, ["lalala", "don't merge", "yes"], "", 0),
(["yes"], [], [], True, [], "", 1),
(["yes"], [], [], True, ["lalala"], "", 1),
(["yes"], [], [], True, ["lalala", "don't merge"], "", 1),
(["yes"], [], [], True, ["lalala", "don't merge", "yes"], "", 0),
(["yes"], [], [("foobar", "test>1")], False, [], "", 1),
(["yes"], [], [("foobar", "test>1")], False, ["lalala"], "", 1),
(["yes"], [], [("foobar", "test>1")], False, ["lalala", "foobar"], "", 1),
(["yes"], [], [("foobar", "test>1")], False, ["lalala", "yes"], "", 0),
(
["yes"],
[],
[("foobar", "test>1")],
False,
["lalala", "foobar", "don't merge"],
"",
1,
),
(
["yes"],
[],
[("foobar", "test>1")],
False,
["lalala", "foobar", "don't merge", "yes"],
"",
1,
),
(["yes"], [], [("foobar", "test>1")], True, [], "", 1),
(["yes"], [], [("foobar", "test>1")], True, ["lalala"], "", 1),
(["yes"], [], [("foobar", "test>1")], True, ["lalala", "foobar"], "", 1),
(
["yes"],
[],
[("foobar", "test>1")],
True,
["lalala", "foobar", "don't merge"],
"",
1,
),
(
["yes"],
[],
[("foobar", "test>1")],
True,
["lalala", "foobar", "don't merge", "yes"],
"",
0,
),
(["yes"], ["don't merge"], [], False, [], "", 1),
(["yes"], ["don't merge"], [], False, ["lalala"], "", 1),
(["yes"], ["don't merge"], [], False, ["lalala", "don't merge"], "", 1),
(
["yes"],
["don't *ge"],
[],
False,
["lalala", "don't merge", "yes"],
"",
1,
),
(["yes"], ["don't merge"], [], True, [], "", 1),
(["yes"], ["don't merge"], [], True, ["lalala"], "", 1),
(["yes"], ["don't *rge"], [], True, ["lalala", "don't merge"], "", 1),
(["yes"], ["don't merge"], [], True, ["lalala", "yes"], "", 0),
(
["yes"],
["don't merge"],
[],
True,
["lalala", "don't merge", "yes"],
"",
1,
),
(["ye*"], ["don't merge"], [("foobar", "test>1")], False, [], "", 1),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar", "don't merge"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar", "don't merge", "yes"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
False,
["lalala", "foobar", "yes"],
"",
1,
),
(["yes"], ["don't merge"], [("foobar", "test>1")], True, [], "", 1),
(["yes"], ["don't merge"], [("foobar", "test>1")], True, ["lalala"], "", 1),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar", "don't merge"],
"",
1,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar", "yes"],
"",
0,
),
(
["yes"],
["don't merge"],
[("foobar", "test>1")],
True,
["lalala", "foobar", "don't merge", "yes"],
"",
1,
),
],
)
# pylint: disable=too-many-arguments
def test_check_labels(
monkeypatch,
set_labels,
unset_labels,
cond_labels,
cond_res,
pull_labels,
missing_approvals_label,
exp,
):
pull = MockPull(labels=[MockLabel(name) for name in pull_labels])
monkeypatch.setattr(
sys.modules[__name__], "check_condition", lambda x, y, z: cond_res
)
assert (
check_labels(
set_labels, unset_labels, cond_labels, missing_approvals_label, pull
)
== exp
)
@pytest.mark.parametrize(
"missing_approvals_label, get_label_errors, exp",
[
pytest.param("", False, 0, id="no missing_approvals_label"),
pytest.param("DON'T MERGE", False, 0, id="with missing_approvals_label"),
pytest.param("DON'T MERGE", True, 1, id="with missing_approvals_label"),
pytest.param("DON'T MERGE", False, 0, id="with missing_approvals_label"),
],
)
def test_main(monkeypatch, missing_approvals_label, get_label_errors, exp):
class MockArgs:
set_labels = ["yes"]
unset_labels = []
cond_labels = []
missing_approvals_label = ""
class MockRepo:
def get_pull(self, pull_no):
return pull_no
def get_label(self, label):
raise github.GithubException(status=404, data="")
class MockGithub:
def __init__(self, *args, **kwargs):
pass
# pylint: disable=unused-argument
def get_repo(self, name):
return MockRepo()
MockArgs.missing_approvals_label = missing_approvals_label
if not get_label_errors:
MockRepo.get_label = lambda self, label: label
monkeypatch.setattr(
argparse.ArgumentParser, "add_argument", lambda *args, **kwargs: None
)
monkeypatch.setattr(
argparse.ArgumentParser, "parse_args", lambda *args, **kwargs: MockArgs()
)
monkeypatch.setattr(github, "Github", lambda *args, **kwargs: MockGithub())
monkeypatch.setattr(
sys.modules[__name__], "check_labels", lambda *args, **kwargs: 0
)
monkeypatch.setattr(
sys.modules[__name__], "get_pull_no", lambda *args, **kwargs: 12345
)
monkeypatch.setenv("GITHUB_REPOSITORY", "test")
monkeypatch.setenv("GITHUB_REF", "foobar")
assert main() == exp