Patchwork releasenotes: add custom admonitions support for release notes

login
register
mail settings
Submitter Rishabh Madan
Date July 7, 2017, 5:13 p.m.
Message ID <766932e4c2768577173f.1499447638@bunty>
Download mbox | patch
Permalink /patch/22103/
State Superseded
Headers show

Comments

Rishabh Madan - July 7, 2017, 5:13 p.m.
# HG changeset patch
# User Rishabh Madan <rishabhmadan96@gmail.com>
# Date 1499447586 -7200
#      Fri Jul 07 19:13:06 2017 +0200
# Node ID 766932e4c2768577173f1bfaa10e919b9df739ec
# Parent  e714159860fd0872ae0555bb07546aa7e9f700e0
releasenotes: add custom admonitions support for release notes

By default, the extension has default sections like fix, feature, perf etc.. This
patch allow user to add support for custom admonition. In order to add a custom
admonition, one needs to have a .hgadmonitions file inside the repository. All the
custom directive with name specified under the tag [releasenotes.sections] will be
usable by the extension. One important thing to keep in mind is if there exists any
custom admonitions then they will override the default ones.
Yuya Nishihara - July 9, 2017, 1:12 p.m.
On Fri, 07 Jul 2017 19:13:58 +0200, Rishabh Madan wrote:
> # HG changeset patch
> # User Rishabh Madan <rishabhmadan96@gmail.com>
> # Date 1499447586 -7200
> #      Fri Jul 07 19:13:06 2017 +0200
> # Node ID 766932e4c2768577173f1bfaa10e919b9df739ec
> # Parent  e714159860fd0872ae0555bb07546aa7e9f700e0
> releasenotes: add custom admonitions support for release notes

>  class releasenotessections(object):
> -    def __init__(self, ui):
> -        # TODO support defining custom sections from config.
> -        self._sections = list(DEFAULT_SECTIONS)
> +    def __init__(self, repo, revs, ui):
> +        custom_sections = self.getcustomadmonitions(repo, revs, ui)
> +        if custom_sections:
> +            self._sections = custom_sections
> +        else:
> +            self._sections = list(DEFAULT_SECTIONS) + custom_sections
                                                         ^^^^^^^^^^^^^^^

Here custom_sections is empty, right?

> +    def getcustomadmonitions(self, repo, revs, ui):

Nit: this can be a free function since it doesn't depend on self.

> +        custom_sections = list()
> +        for rev in revs:
> +            ctx = repo[rev]

What do you want to do here? Processing all revs or just taking the last rev?

> +        p = config.config()
> +        repo = ctx.repo()
> +
> +        def read(f, sections=None, remap=None):
> +            if f in ctx:
> +                try:
> +                    data = ctx[f].data()
> +
> +                except IOError as err:
> +                    if err.errno != errno.ENOENT:
> +                        raise
> +                    ui.warn(_("warning: .hgadmonitions file \'%s\' not found\n") %
> +                            repo.pathto(f))
> +                    return
> +                p.parse(f, data, sections, remap, read)
> +                sectiondict = p.__getitem__(sections)
                                 ^^^^^^^^^^^^^^^^^^^^^^^
p[sections]

BTW, this sections should not be the same as the sections passed to parse().
Perhaps, you would just need to parse the all sections in config file, and
pick the one you want.

  p.parse(src, data)
  return p.items('releasenotes.sections')

> +                sectionlist = list()
> +                for key, value in sectiondict.iteritems():
> +                    temp = (key, value)
> +                    sectionlist.append(temp)
> +            else:
> +                raise error.Abort(_(".hgadmonitions file \'%s\' not found") %
> +                                  repo.pathto(f))
> +            return sectionlist
> +        if '.hgadmonitions' in ctx:
> +            custom_sections = read('.hgadmonitions', 'releasenotes.sections')

.hgadmonitions sounds too obscure. .hgreleasenotes is probably better, and
the section name could be just [sections] or [admonitions].

> +        return custom_sections
> +
>  def parsenotesfromrevisions(repo, directives, revs):
>      notes = parsedreleasenotes()
>  
> @@ -396,9 +432,9 @@
>      that file. A particular use case for this is to tweak the wording of a
>      release note after it has been added to the release notes file.
>      """
> -    sections = releasenotessections(ui)
> +    revs = scmutil.revrange(repo, [rev or 'not public()'])
>  
> -    revs = scmutil.revrange(repo, [rev or 'not public()'])
> +    sections = releasenotessections(repo, revs, ui)

I doubt it's correct to read all .hgadmonitions files. Maybe we would simply
want to use the latest config for the revision you're generating a release
note?

> -
> +    print(incoming)

:)

And, can you write some tests?

Patch

diff -r e714159860fd -r 766932e4c276 hgext/releasenotes.py
--- a/hgext/releasenotes.py	Fri Jul 07 08:33:10 2017 +0200
+++ b/hgext/releasenotes.py	Fri Jul 07 19:13:06 2017 +0200
@@ -20,6 +20,7 @@ 
 
 from mercurial.i18n import _
 from mercurial import (
+    config,
     error,
     minirst,
     registrar,
@@ -111,9 +112,12 @@ 
                 self.addnontitleditem(section, paragraphs)
 
 class releasenotessections(object):
-    def __init__(self, ui):
-        # TODO support defining custom sections from config.
-        self._sections = list(DEFAULT_SECTIONS)
+    def __init__(self, repo, revs, ui):
+        custom_sections = self.getcustomadmonitions(repo, revs, ui)
+        if custom_sections:
+            self._sections = custom_sections
+        else:
+            self._sections = list(DEFAULT_SECTIONS) + custom_sections
 
     def __iter__(self):
         return iter(self._sections)
@@ -128,6 +132,38 @@ 
 
         return None
 
+    def getcustomadmonitions(self, repo, revs, ui):
+        custom_sections = list()
+        for rev in revs:
+            ctx = repo[rev]
+        p = config.config()
+        repo = ctx.repo()
+
+        def read(f, sections=None, remap=None):
+            if f in ctx:
+                try:
+                    data = ctx[f].data()
+
+                except IOError as err:
+                    if err.errno != errno.ENOENT:
+                        raise
+                    ui.warn(_("warning: .hgadmonitions file \'%s\' not found\n") %
+                            repo.pathto(f))
+                    return
+                p.parse(f, data, sections, remap, read)
+                sectiondict = p.__getitem__(sections)
+                sectionlist = list()
+                for key, value in sectiondict.iteritems():
+                    temp = (key, value)
+                    sectionlist.append(temp)
+            else:
+                raise error.Abort(_(".hgadmonitions file \'%s\' not found") %
+                                  repo.pathto(f))
+            return sectionlist
+        if '.hgadmonitions' in ctx:
+            custom_sections = read('.hgadmonitions', 'releasenotes.sections')
+        return custom_sections
+
 def parsenotesfromrevisions(repo, directives, revs):
     notes = parsedreleasenotes()
 
@@ -396,9 +432,9 @@ 
     that file. A particular use case for this is to tweak the wording of a
     release note after it has been added to the release notes file.
     """
-    sections = releasenotessections(ui)
+    revs = scmutil.revrange(repo, [rev or 'not public()'])
 
-    revs = scmutil.revrange(repo, [rev or 'not public()'])
+    sections = releasenotessections(repo, revs, ui)
     incoming = parsenotesfromrevisions(repo, sections.names(), revs)
 
     try:
@@ -411,7 +447,7 @@ 
         notes = parsedreleasenotes()
 
     notes.merge(ui, incoming)
-
+    print(incoming)
     with open(file_, 'wb') as fh:
         fh.write(serializenotes(sections, notes))