commit:     530d4faa9f61242138c67f634967f981e5c5a5c1
Author:     Lucio Sauer <watermanpaint <AT> posteo <DOT> net>
AuthorDate: Sat Jul 20 14:44:22 2024 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Sat Jul 20 15:37:19 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgdev.git/commit/?id=530d4faa

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 <AT> posteo.net>
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgdev/scripts/pkgdev_commit.py | 51 +++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/pkgdev/scripts/pkgdev_commit.py 
b/src/pkgdev/scripts/pkgdev_commit.py
index dc4d46f..77a21f0 100644
--- a/src/pkgdev/scripts/pkgdev_commit.py
+++ b/src/pkgdev/scripts/pkgdev_commit.py
@@ -10,6 +10,7 @@ import tempfile
 import textwrap
 from collections import defaultdict, deque, UserDict
 from dataclasses import dataclass
+from enum import Enum
 from functools import partial
 from itertools import chain
 
@@ -57,13 +58,17 @@ class ArgumentParser(cli.ArgumentParser):
 class BugTag(argparse.Action):
     """Register bug-related tag to inject into the commit message footer."""
 
-    def __call__(self, parser, namespace, value, option_string=None):
+    def parse_url(self, value):
         try:
             url = f"https://bugs.gentoo.org/{int(value)}"
         except ValueError:
             url = value
             if not url.startswith(("https://";, "http://";)):
                 raise argparse.ArgumentError(self, f"invalid URL: {url}")
+        return url
+
+    def __call__(self, parser, namespace, value, option_string=None):
+        url = self.parse_url(value)
         namespace.footer.add((self.dest.capitalize(), url))
 
 
@@ -80,6 +85,39 @@ class CommitTag(argparse.Action):
         namespace.footer.add((name.capitalize(), val))
 
 
+class BugzillaAwareBugTag(BugTag):
+    """Register bug-related tag and resolution to inject into the commit 
message footer."""
+
+    class Resolution(Enum):
+        FIXED = "fixed"
+        OBSOLETE = "obsolete"
+        PKGREMOVED = "pkgremoved"
+
+    def __call__(self, parser, namespace, values, option_string=None):
+        has_resolution = False
+        try:
+            bug, val = values.rsplit(":", 1)
+            if not bug:
+                raise argparse.ArgumentError(self, f"invalid commit tag: 
{values!r}")
+            if not val.startswith("//"):
+                has_resolution = True
+                res = self.Resolution(val.lower()) if val else 
self.Resolution.FIXED
+        except ValueError:
+            if has_resolution:
+                err = f"{val!r} should be one of: {', '.join([m.value for m in 
self.Resolution])}"
+                raise argparse.ArgumentError(self, err)
+
+        if not has_resolution:
+            super().__call__(parser, namespace, values, option_string)
+            return
+        url = self.parse_url(bug)
+        is_bgo = "bugs.gentoo.org" in url
+        if is_bgo and not res is self.Resolution.FIXED:
+            url = f"{url} ({res.value})"
+
+        namespace.footer.add((self.dest.capitalize(), url))
+
+
 commit = ArgumentParser(
     prog="pkgdev commit",
     description="create git commit",
@@ -92,7 +130,16 @@ commit_opts.add_argument(
     "-b", "--bug", action=BugTag, help="add Bug tag for a given Gentoo or 
upstream bug"
 )
 commit_opts.add_argument(
-    "-c", "--closes", action=BugTag, help="add Closes tag for a given Gentoo 
bug or upstream PR URL"
+    "-c",
+    "--closes",
+    action=BugzillaAwareBugTag,
+    metavar="CLOSES[:RESOLUTION]",
+    help="add Closes tag and optionally a resolution for a given Gentoo bug or 
upstream PR URL",
+    docs="""
+        Indicate that a bug or PR may be closed. The optional resolution string
+        for Gentoo's Bugzilla describes what happened to a bug. It is
+        case-insensitive and must be one of FIXED, OBSOLETE or PKGREMOVED.
+    """,
 )
 commit_opts.add_argument(
     "-T", "--tag", action=CommitTag, metavar="NAME:VALUE", help="add commit 
tag"

Reply via email to