-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangelog_entries.ts
More file actions
111 lines (93 loc) · 3.29 KB
/
changelog_entries.ts
File metadata and controls
111 lines (93 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as core from '@actions/core'
export type ChangelogEntry = {
text: string,
type: string
}
enum Themes {
ADDED = 'Added',
CHANGED = 'Changed',
DEPRECATED = 'Deprecated',
REMOVED = 'Removed',
FIXED = 'Fixed',
SECURITY = 'Security'
}
/**
* Extract changelog entries from markdown text
*
* @param markdown
*/
export function extractEntriesFromMarkdown(markdown : string) : ChangelogEntry[] {
core.info('Extracting entries from markdown')
core.debug('Searching through:')
core.debug(markdown)
const patternStr = new RegExp("###(.*(?:(?:\\n|\\r\\n).+)*(?:\\n|\\r\\n)?)", "mg")
const themesContent = markdown.match(patternStr);
if (!Array.isArray(themesContent) || Array.isArray(themesContent) && themesContent.length === 0) {
return []
}
core.info(`Found ${themesContent.length} themes.`)
themesContent.forEach((themesContent) => {
core.debug('Theme content is:')
core.debug(themesContent)
})
let changeLogEntries : ChangelogEntry[] = []
const themes = Object.values(Themes)
themes.forEach((section) => {
let regex = new RegExp('##\\s{1}' + section)
let themeContent = themesContent.find((themeContent) => {
return regex.test(themeContent)
})
if (typeof themeContent !== "string") {
return
}
core.info(`Section: ${section} found. `)
core.debug(`Value: ${themeContent}`)
// Split the content based on newlines, then check if we are dealing with a list.
themeContent
// Replace all "\r\n" with \n
.replace(/\\r\\n/g, '\n')
// After several tests I found out that this regex is the most reliable
// It splits on both \r\n and \n. The Github PR body that I tested contained \r\n line breaks
.split(/(\r\n|\n)/)
.filter((line) => {
// See https://regex101.com/r/4XYvpR/1
return /^\s?[-*]\s.*$/.test(line)
})
.forEach((filteredLine) => {
changeLogEntries.push({
text: filteredLine.trim().replace(/(^[-*]\s|\\r\\n)/g, ''),
type: section.toLowerCase()
})
})
})
core.info(`Found ${changeLogEntries.length} changelog entries:`)
changeLogEntries.forEach((changelogEntry : ChangelogEntry) => {
core.info(`Type: ${changelogEntry.type}. Text: ${changelogEntry.text}`)
})
return changeLogEntries
}
/**
* Extract changelog entries from PR body. The format should look like this:
* ## Changelog
* ... text
*
* @param body
*/
export function extractChangelogSection(body: string) : string {
core.debug('PR body:')
core.debug(JSON.stringify(body))
if (typeof body !== "string") {
throw new Error('Pull request has no body')
}
// @ts-ignore
const matches = body.match(/#{2,3}\s[Cc]hangelog[\S\s]+/g);
if (!Array.isArray(matches) || matches.length !== 1) {
core.info('Cannot extract CHANGELOG from body')
}
const changelogSection = matches[0]
if (typeof changelogSection !== 'string') {
throw new Error('Changelog missing in pull request body!')
}
core.debug(`Matching section: ${changelogSection}`);
return changelogSection
}