11import collections
22import logging
33import os
4- import rstcloth .rstcloth as rstcloth
54import yaml
65from jira import JIRA
6+ from rstcloth import RstCloth
77
88logger = logging .getLogger ('generatechangelogs.py' )
99
@@ -123,7 +123,7 @@ def get_issue_structure(config, issues, version):
123123 return headings
124124
125125
126- def generate_changelog_rst (config , headings , fixVersion ):
126+ def generate_changelog_rst (config , headings , fixVersion , outputFile ):
127127 """
128128 Generate the changelog rst from the groupings established in
129129 get_issue_structure()
@@ -135,92 +135,88 @@ def generate_changelog_rst(config, headings, fixVersion):
135135 for component in sub_headings :
136136 nested [component ] = enclosing_level
137137
138- # build the changelog content itself.
139- r = rstcloth .RstCloth ()
140- level = 3
138+ with open (outputFile , 'w' ) as f :
139+ # build the changelog content itself.
140+ r = RstCloth (f )
141+ level = 3
141142
142- r .ref_target ("{0}-changelog" .format (fixVersion ))
143- r .newline ()
144- r .heading (text = "{0} Changelog" .format (fixVersion ), char = '-' )
145- r .newline ()
143+ r .ref_target ("{0}-changelog" .format (fixVersion ))
144+ r .newline ()
145+ r .heading (text = "{0} Changelog" .format (fixVersion ), char = '-' )
146+ r .newline ()
146147
147- # process all of the issues by group.
148- for heading , issues in headings .items ():
149- if heading in nested :
150- # we deal with nested headings when we do their parent. skip here.
151- continue
152- else :
153- if heading in config .get ('nesting' ) and len (issues ) == 0 :
154- # if a heading has subheadings, and all are empty, then we should skip it entirely.
155- empty_sub_headings = 0
156- for sub in config .get ('nesting' ).get (heading ):
157- if len (headings [sub ]) == 0 :
158- empty_sub_headings += 1
159- if empty_sub_headings == len (config .get ('nesting' ).get (heading )):
160- continue
161- elif len (issues ) == 0 :
162- # skip empty headings.
148+ # process all of the issues by group.
149+ for heading , issues in headings .items ():
150+ if heading in nested :
151+ # we deal with nested headings when we do their parent. skip here.
163152 continue
164-
165- # format the heading.
166- r .heading (text = heading , indent = 0 ,
167- char = '~' )
168- r .newline ()
169-
170- if len (issues ) == 1 :
171- r .content ("{1} {0}" .format (issues [0 ][1 ], r .role (
172- "issue" , issues [0 ][0 ])))
173153 else :
174- for issue in issues :
175- r .li ("{1} {0}" .format (issue [1 ], r .role (
176- "issue" , issue [0 ])))
177- r .newline ()
178-
179- # repeat the above formatting with minor variations to do the nesting.
180- if heading in config .get ('nesting' ):
181- for sub in config .get ('nesting' ).get (heading ):
182- if len (headings [sub ]) == 0 :
154+ if heading in config .get ('nesting' ) and len (issues ) == 0 :
155+ # if a heading has subheadings, and all are empty, then we should skip it entirely.
156+ empty_sub_headings = 0
157+ for sub in config .get ('nesting' ).get (heading ):
158+ if len (headings [sub ]) == 0 :
159+ empty_sub_headings += 1
160+ if empty_sub_headings == len (config .get ('nesting' ).get (heading )):
183161 continue
162+ elif len (issues ) == 0 :
163+ # skip empty headings.
164+ continue
184165
185- r .heading (text = sub , indent = 0 ,
186- # char=giza.content.helper.character_levels[level+1])
187- char = '`' )
188- r .newline ()
189-
190- sub_issues = headings [sub ]
191- if len (sub_issues ) == 0 :
192- r .content ("{1} {0}" .format (sub_issues [0 ][1 ].strip (), r .role (
193- "issue" , sub_issues [0 ][0 ])))
194- else :
195- for issue in sub_issues :
196- r .li ("{1} {0}" .format (issue [1 ].strip (), r .role (
197- "issue" , issue [0 ])))
198- r .newline ()
199-
200- return r
166+ # format the heading.
167+ r .heading (text = heading , indent = 0 ,
168+ char = '~' )
169+ r .newline ()
170+
171+ if len (issues ) == 1 :
172+ r .content ("{1} {0}" .format (issues [0 ][1 ], r .role (
173+ "issue" , issues [0 ][0 ])))
174+ else :
175+ for issue in issues :
176+ r .li ("{1} {0}" .format (issue [1 ], r .role (
177+ "issue" , issue [0 ])))
178+ r .newline ()
179+
180+ # repeat the above formatting with minor variations to do the nesting.
181+ if heading in config .get ('nesting' ):
182+ for sub in config .get ('nesting' ).get (heading ):
183+ if len (headings [sub ]) == 0 :
184+ continue
185+
186+ r .heading (text = sub , indent = 0 ,
187+ # char=giza.content.helper.character_levels[level+1])
188+ char = '`' )
189+ r .newline ()
190+
191+ sub_issues = headings [sub ]
192+ if len (sub_issues ) == 0 :
193+ r .content ("{1} {0}" .format (sub_issues [0 ][1 ].strip (), r .role (
194+ "issue" , sub_issues [0 ][0 ])))
195+ else :
196+ for issue in sub_issues :
197+ r .li ("{1} {0}" .format (issue [1 ].strip (), r .role (
198+ "issue" , issue [0 ])))
199+ r .newline ()
201200
201+ print (
202+ "wrote changelog '{0}'. Commit this file independently." .format (outputFile ))
202203
203- def write_changelog_file (rst , fixVersion ):
204204
205- # Output the rst to source/includes/changelogs/releases
205+ def generate_output_filePath ( fixVersion ):
206206 sourceDir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' ))
207207 fn = fixVersion + ".rst"
208208 outputDir = os .path .join (
209209 sourceDir , "source/includes/changelogs/releases" , fn )
210210
211- with open (outputDir , 'w' ) as f :
212- f .write (str (rst ))
213-
214- logger .info (
215- "wrote changelog '{0}'. Commit this file independently." .format (outputDir ))
216-
217- print (
218- "wrote changelog '{0}'. Commit this file independently." .format (outputDir ))
211+ return outputDir
219212
220213def main ():
221214 # Prompt user for the version to generate the changelog for:
222215 fixVersion = input ("Enter changelog version: " )
223216
217+ # Generate the outputfile
218+ outputFile = generate_output_filePath (fixVersion )
219+
224220 # Get list of JIRA issues to include in changelog
225221 issues = get_jira_issues (fixVersion )
226222
@@ -231,10 +227,10 @@ def main():
231227 issue_headings = get_issue_structure (config , issues , fixVersion )
232228
233229 # Convert the issue headings into rst
234- changelog_rst = generate_changelog_rst (config , issue_headings , fixVersion )
230+ changelog_rst = generate_changelog_rst (config , issue_headings , fixVersion , outputFile )
235231
236232 # Write the changelog to source/includes/changelogs/releases
237- write_changelog_file (changelog_rst , fixVersion )
233+ # write_changelog_file(changelog_rst, fixVersion)
238234
239235if __name__ == "__main__" :
240236 main ()
0 commit comments