Patchwork [1,of,3,v4] releasenotes: add custom admonitions support for release notes

login
register
mail settings
Submitter Rishabh Madan
Date July 18, 2017, 12:40 p.m.
Message ID <d8997d12840aa113cde7.1500381618@bunty>
Download mbox | patch
Permalink /patch/22478/
State Superseded
Headers show

Comments

Rishabh Madan - July 18, 2017, 12:40 p.m.
# HG changeset patch
# User Rishabh Madan <rishabhmadan96@gmail.com>
# Date 1500371554 -19800
#      Tue Jul 18 15:22:34 2017 +0530
# Node ID d8997d12840aa113cde7ffa0ea7af8d62cf1add7
# Parent  4672db164c986da4442bd864cd044512d975c3f2
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 .hgreleasenotes file inside the repository. All the
custom directive with name specified under the tag [sections] will be
usable by the extension. One important thing to keep in mind is if there exists any
custom admonitions with same key as default then they will override the default ones.
Yuya Nishihara - July 18, 2017, 1:55 p.m.
On Tue, 18 Jul 2017 18:10:18 +0530, Rishabh Madan wrote:
> # HG changeset patch
> # User Rishabh Madan <rishabhmadan96@gmail.com>
> # Date 1500371554 -19800
> #      Tue Jul 18 15:22:34 2017 +0530
> # Node ID d8997d12840aa113cde7ffa0ea7af8d62cf1add7
> # Parent  4672db164c986da4442bd864cd044512d975c3f2
> 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, ui, repo=None):
> +        if repo:
> +            sections = dict(DEFAULT_SECTIONS)
> +            custom_sections = getcustomadmonitions(repo)
> +            if custom_sections:
> +                sections.update(custom_sections)
> +            self._sections = list(sections.iteritems())

The test fails at PATCH 3. It appears the order of sections is important
so you'll have to use util.sortdict() instead.

> +def getcustomadmonitions(repo):
> +    custom_sections = list()
> +    ctx = repo['.']
> +    p = config.config()
> +    repo = ctx.repo()

Meaningless re-assignment of repo.

> +    def read(f, sections=None, remap=None):
> +        if f in ctx:
> +            data = ctx[f].data()
> +            p.parse(f, data, sections, remap, read)
> +            return p

No need to return p.

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

This can be return p['sections'].

Patch

diff -r 4672db164c98 -r d8997d12840a hgext/releasenotes.py
--- a/hgext/releasenotes.py	Sat Jun 24 15:29:42 2017 -0700
+++ b/hgext/releasenotes.py	Tue Jul 18 15:22:34 2017 +0530
@@ -20,6 +20,7 @@ 
 
 from mercurial.i18n import _
 from mercurial import (
+    config,
     error,
     minirst,
     registrar,
@@ -111,9 +112,15 @@ 
                 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, ui, repo=None):
+        if repo:
+            sections = dict(DEFAULT_SECTIONS)
+            custom_sections = getcustomadmonitions(repo)
+            if custom_sections:
+                sections.update(custom_sections)
+            self._sections = list(sections.iteritems())
+        else:
+            self._sections = list(DEFAULT_SECTIONS)
 
     def __iter__(self):
         return iter(self._sections)
@@ -128,6 +135,29 @@ 
 
         return None
 
+def getcustomadmonitions(repo):
+    custom_sections = list()
+    ctx = repo['.']
+    p = config.config()
+    repo = ctx.repo()
+
+    def read(f, sections=None, remap=None):
+        if f in ctx:
+            data = ctx[f].data()
+            p.parse(f, data, sections, remap, read)
+            return p
+        else:
+            raise error.Abort(_(".hgreleasenotes file \'%s\' not found") %
+                              repo.pathto(f))
+
+    if '.hgreleasenotes' in ctx:
+        configdict = read('.hgreleasenotes')
+        sectiondict = configdict['sections']
+        for key, value in sectiondict.iteritems():
+            temp = (key, value)
+            custom_sections.append(temp)
+        return custom_sections
+
 def parsenotesfromrevisions(repo, directives, revs):
     notes = parsedreleasenotes()
 
@@ -396,7 +426,7 @@ 
     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)
+    sections = releasenotessections(ui, repo)
 
     revs = scmutil.revrange(repo, [rev or 'not public()'])
     incoming = parsenotesfromrevisions(repo, sections.names(), revs)
@@ -416,7 +446,7 @@ 
         fh.write(serializenotes(sections, notes))
 
 @command('debugparsereleasenotes', norepo=True)
-def debugparsereleasenotes(ui, path):
+def debugparsereleasenotes(ui, path, repo=None):
     """parse release notes and print resulting data structure"""
     if path == '-':
         text = sys.stdin.read()
@@ -424,7 +454,7 @@ 
         with open(path, 'rb') as fh:
             text = fh.read()
 
-    sections = releasenotessections(ui)
+    sections = releasenotessections(ui, repo)
 
     notes = parsereleasenotesfile(sections, text)
 
diff -r 4672db164c98 -r d8997d12840a tests/test-releasenotes-formatting.t
--- a/tests/test-releasenotes-formatting.t	Sat Jun 24 15:29:42 2017 -0700
+++ b/tests/test-releasenotes-formatting.t	Tue Jul 18 15:22:34 2017 +0530
@@ -255,6 +255,8 @@ 
   
   * Short summary of fix 3
 
+  $ cd ..
+
 Multiple 'Other Changes' sub-sections for every section
 
   $ hg init multiple-otherchanges
@@ -324,3 +326,52 @@ 
   
   * Short summary of fix 2
 
+  $ cd ..
+
+Using custom sections in notes
+
+  $ hg init custom-section
+  $ cd custom-section
+  $ cat >> .hgreleasenotes << EOF
+  > [sections]
+  > testsection=Name of Section
+  > EOF
+
+  $ touch a
+  $ hg -q commit -A -l - << EOF
+  > commit 1
+  > 
+  > .. testsection::
+  > 
+  >    First paragraph under this admonition.
+  > EOF
+
+  $ hg releasenotes -r . $TESTTMP/relnotes-custom-section
+  $ cat $TESTTMP/relnotes-custom-section
+  Name of Section
+  ===============
+  
+  * First paragraph under this admonition.
+
+Overriding default sections (For eg. by default feature = New Features)
+
+  $ cat >> .hgreleasenotes << EOF
+  > [sections]
+  > feature=Feature Additions
+  > EOF
+
+  $ touch b
+  $ hg -q commit -A -l - << EOF
+  > commit 2
+  > 
+  > .. feature::
+  > 
+  >    Adds a new feature.
+  > EOF
+
+  $ hg releasenotes -r . $TESTTMP/relnotes-override-section
+  $ cat $TESTTMP/relnotes-override-section
+  Feature Additions
+  =================
+  
+  * Adds a new feature.