Skip to content

Commit 530d4fa

Browse files
gonsoosarthurzam
authored andcommitted
pkgdev commit: add optional support for Closes tags with resolution
Support resolutions FIXED, OBSOLETE and PKGREMOVED, provided that the bug belongs to b.g.o. The default and fallback resolution is FIXED. Together with server-side changes to https://gitweb.gentoo.org/infra/githooks.git/, this patch allows Gentoo contributors to generate commit tags that cause bugs to be closed automatically with the indicated resolution. Currently, only the string after the last colon in the argument is significant for the resolution. Therefore, one could theoretically craft invalid tags such as 'Closes: https://bugs.gentoo.org/123:foo (obsolete)' Signed-off-by: Lucio Sauer <watermanpaint@posteo.net> Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent 557aefc commit 530d4fa

1 file changed

Lines changed: 49 additions & 2 deletions

File tree

src/pkgdev/scripts/pkgdev_commit.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import textwrap
1111
from collections import defaultdict, deque, UserDict
1212
from dataclasses import dataclass
13+
from enum import Enum
1314
from functools import partial
1415
from itertools import chain
1516

@@ -57,13 +58,17 @@ def parse_known_args(self, args=None, namespace=None):
5758
class BugTag(argparse.Action):
5859
"""Register bug-related tag to inject into the commit message footer."""
5960

60-
def __call__(self, parser, namespace, value, option_string=None):
61+
def parse_url(self, value):
6162
try:
6263
url = f"https://bugs.gentoo.org/{int(value)}"
6364
except ValueError:
6465
url = value
6566
if not url.startswith(("https://", "http://")):
6667
raise argparse.ArgumentError(self, f"invalid URL: {url}")
68+
return url
69+
70+
def __call__(self, parser, namespace, value, option_string=None):
71+
url = self.parse_url(value)
6772
namespace.footer.add((self.dest.capitalize(), url))
6873

6974

@@ -80,6 +85,39 @@ def __call__(self, parser, namespace, value, option_string=None):
8085
namespace.footer.add((name.capitalize(), val))
8186

8287

88+
class BugzillaAwareBugTag(BugTag):
89+
"""Register bug-related tag and resolution to inject into the commit message footer."""
90+
91+
class Resolution(Enum):
92+
FIXED = "fixed"
93+
OBSOLETE = "obsolete"
94+
PKGREMOVED = "pkgremoved"
95+
96+
def __call__(self, parser, namespace, values, option_string=None):
97+
has_resolution = False
98+
try:
99+
bug, val = values.rsplit(":", 1)
100+
if not bug:
101+
raise argparse.ArgumentError(self, f"invalid commit tag: {values!r}")
102+
if not val.startswith("//"):
103+
has_resolution = True
104+
res = self.Resolution(val.lower()) if val else self.Resolution.FIXED
105+
except ValueError:
106+
if has_resolution:
107+
err = f"{val!r} should be one of: {', '.join([m.value for m in self.Resolution])}"
108+
raise argparse.ArgumentError(self, err)
109+
110+
if not has_resolution:
111+
super().__call__(parser, namespace, values, option_string)
112+
return
113+
url = self.parse_url(bug)
114+
is_bgo = "bugs.gentoo.org" in url
115+
if is_bgo and not res is self.Resolution.FIXED:
116+
url = f"{url} ({res.value})"
117+
118+
namespace.footer.add((self.dest.capitalize(), url))
119+
120+
83121
commit = ArgumentParser(
84122
prog="pkgdev commit",
85123
description="create git commit",
@@ -92,7 +130,16 @@ def __call__(self, parser, namespace, value, option_string=None):
92130
"-b", "--bug", action=BugTag, help="add Bug tag for a given Gentoo or upstream bug"
93131
)
94132
commit_opts.add_argument(
95-
"-c", "--closes", action=BugTag, help="add Closes tag for a given Gentoo bug or upstream PR URL"
133+
"-c",
134+
"--closes",
135+
action=BugzillaAwareBugTag,
136+
metavar="CLOSES[:RESOLUTION]",
137+
help="add Closes tag and optionally a resolution for a given Gentoo bug or upstream PR URL",
138+
docs="""
139+
Indicate that a bug or PR may be closed. The optional resolution string
140+
for Gentoo's Bugzilla describes what happened to a bug. It is
141+
case-insensitive and must be one of FIXED, OBSOLETE or PKGREMOVED.
142+
""",
96143
)
97144
commit_opts.add_argument(
98145
"-T", "--tag", action=CommitTag, metavar="NAME:VALUE", help="add commit tag"

0 commit comments

Comments
 (0)