@@ -15,6 +15,22 @@ export const EMPTY_CHANGELOG = `# CodeQL Action Changelog
1515
1616${ NO_CHANGES_STR } `;
1717
18+ /**
19+ * Represents sections in a changelog.
20+ */
21+ export interface ChangelogSection {
22+ headerLine : string ;
23+ bodyLines : string [ ] ;
24+ }
25+
26+ /**
27+ * Represents a changelog.
28+ */
29+ export interface Changelog {
30+ preamble : string [ ] ;
31+ sections : ChangelogSection [ ] ;
32+ }
33+
1834/** Returns `date` formatted as `DD Mon YYYY`. */
1935export function getReleaseDateString ( today : Date = new Date ( ) ) : string {
2036 return today . toLocaleDateString ( "en-GB" , {
@@ -60,6 +76,75 @@ export function setVersionAndDate(
6076 return content . replace ( UNRELEASED_PLACEHOLDER , versionAndDate ) ;
6177}
6278
79+ /**
80+ * Parses `content` into a structured representation of a changelog.
81+ *
82+ * @param content The contents of the changelog file.
83+ */
84+ export function parseChangelog ( content : string ) : Changelog {
85+ const lines = content . split ( "\n" ) ;
86+ let i = 0 ;
87+
88+ const preamble : string [ ] = [ ] ;
89+ const sections : ChangelogSection [ ] = [ ] ;
90+ let currentSection : ChangelogSection | undefined = undefined ;
91+
92+ // Process all lines of the input file.
93+ while ( i < lines . length ) {
94+ const line = lines [ i ] ;
95+
96+ // Sections of the changelog start with `## `.
97+ if ( line . startsWith ( "## " ) ) {
98+ // We have discovered a new section. If `currentSection` is already defined,
99+ // then this marks the end of that section. Push it to the array of sections
100+ // in the changelog.
101+ if ( currentSection !== undefined ) {
102+ sections . push ( currentSection ) ;
103+ }
104+
105+ // Initialise the new section.
106+ currentSection = { headerLine : line , bodyLines : [ ] } ;
107+ } else if ( currentSection !== undefined ) {
108+ // Add lines between the section header and the next to the current section.
109+ currentSection . bodyLines . push ( line ) ;
110+ } else {
111+ // This is neither a section header nor are we in a section already,
112+ // so this line is part of the preamble.
113+ preamble . push ( line ) ;
114+ }
115+
116+ i ++ ;
117+ }
118+
119+ // Push the current section to the array of completed sections, if there is
120+ // still one unfinished.
121+ if ( currentSection !== undefined ) {
122+ sections . push ( currentSection ) ;
123+ }
124+
125+ return { preamble, sections } ;
126+ }
127+
128+ /**
129+ * Combines an array of lines into a single string by adding line breaks.
130+ */
131+ export function unlines ( lines : string [ ] ) : string {
132+ return `${ lines . join ( "\n" ) } ` ;
133+ }
134+
135+ /**
136+ * Renders a given changelog to a string.
137+ */
138+ export function renderChangelog ( changelog : Changelog ) : string {
139+ let result = unlines ( changelog . preamble ) ;
140+
141+ for ( const section of changelog . sections ) {
142+ result += `\n${ section . headerLine } \n${ unlines ( section . bodyLines ) } ` ;
143+ }
144+
145+ return result ;
146+ }
147+
63148/**
64149 * Processes changelog entries for a backport, converting version references
65150 * from the source major version to the target major version and filtering
0 commit comments