Skip to content

Commit 2c8780e

Browse files
committed
Implement changelog writer
1 parent 955d702 commit 2c8780e

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

__tests__/changelog.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as io from '@actions/io'
2+
import * as path from 'path'
3+
import * as fs from 'fs'
4+
import {Settings} from '../src/settings'
5+
import {Changelog} from '../src/changelog'
6+
import {GitCommit} from '../src/git'
7+
import {TEST_DIR} from './common'
8+
9+
describe('Changelog', () => {
10+
afterAll(() => {
11+
io.rmRF(TEST_DIR)
12+
})
13+
14+
it('Verifies changelog is saved to file', async () => {
15+
const settings = {} as Settings
16+
settings.changelogFilePath = path.join(TEST_DIR, 'CHANGELOG.md')
17+
18+
// Verifies path doesn't exists
19+
expect(fs.existsSync(TEST_DIR)).toBe(false)
20+
21+
const gitLog: GitCommit[] = [
22+
new GitCommit('295b513', 'Third commit'),
23+
new GitCommit('0dad212', 'Second commit'),
24+
new GitCommit('80913ba', 'First commit')
25+
]
26+
const changelog = new Changelog(settings)
27+
changelog.write(gitLog)
28+
29+
// Read created CHANGELOG.md
30+
const fileContent: string = fs.readFileSync(settings.changelogFilePath).toString()
31+
32+
const expectedFileContent: string = [
33+
'# Changelog',
34+
'295b513 Third commit',
35+
'0dad212 Second commit',
36+
'80913ba First commit'
37+
].join('\n')
38+
39+
// Verifies file is created and contains expected data
40+
expect(fs.existsSync(settings.changelogFilePath)).toBe(true)
41+
expect(fileContent).toEqual(expectedFileContent)
42+
})
43+
})

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ module.exports = {
77
transform: {
88
'^.+\\.ts$': 'ts-jest'
99
},
10-
// maxConcurrency: 1,
1110
verbose: true
1211
}

src/changelog.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import * as io from '@actions/io'
4+
import {Settings} from './settings'
5+
import {GitCommit} from './git'
6+
7+
export class Changelog {
8+
private settings: Settings
9+
10+
constructor(settings: Settings) {
11+
this.settings = settings
12+
}
13+
14+
write(log: GitCommit[]) {
15+
let lines: string[] = ['# Changelog']
16+
17+
log.forEach(c => {
18+
lines.push(`${c.hash} ${c.message}`)
19+
})
20+
21+
const changelogDir: string = path.parse(this.settings.changelogFilePath).dir
22+
if (!fs.existsSync(changelogDir)) {
23+
io.mkdirP(changelogDir)
24+
}
25+
26+
fs.writeFileSync(this.settings.changelogFilePath, lines.join('\n'))
27+
}
28+
}

src/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Git {
6868
res.stdout.forEach(commit => {
6969
const split = commit.split(' ')
7070
const hash = split[0]
71-
const message = split.slice(1).join(' ')
71+
const message = split.slice(1).join(' ').trim()
7272

7373
if (this.settings.filterRegex && RegExp(this.settings.filterRegex).test(message)) {
7474
return
@@ -109,7 +109,7 @@ class GitOutput {
109109
exitCode = 0
110110
}
111111

112-
class GitCommit {
112+
export class GitCommit {
113113
hash: string = ''
114114
message: string = ''
115115

src/settings.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as core from '@actions/core'
2+
import * as io from '@actions/io'
3+
14
export interface Settings {
25
// TODO: DOCS
36
gitPath: string
@@ -6,3 +9,13 @@ export interface Settings {
69
filterRegex: string | RegExp
710
changelogFilePath: string
811
}
12+
13+
export async function initSettings(): Promise<Settings> {
14+
const settings = {} as Settings
15+
settings.gitPath = await io.which('git', true)
16+
settings.repoPath = core.getInput('path') || '.'
17+
settings.tagRegex = core.getInput('tag-regex') || ''
18+
settings.filterRegex = core.getInput('filter-regex') || ''
19+
settings.changelogFilePath = core.getInput('changelog-file-path') || 'CHANGELOG.md'
20+
return settings
21+
}

0 commit comments

Comments
 (0)