Skip to content

Commit f2f43a8

Browse files
committed
Update versionist.conf.js
Signed-off-by: Kyle Harding <[email protected]>
1 parent cd0e06f commit f2f43a8

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

versionist.conf.js

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,97 @@
11
'use strict';
22

3-
const _ = require('lodash')
3+
const execSync = require('child_process').execSync;
44
const fs = require('fs')
5-
const path = require('path')
6-
const semver = require('balena-semver')
7-
const shell = require('shelljs')
8-
const yaml = require('js-yaml');
5+
const semver = require('versionist/node_modules/balena-semver')
96

10-
const isESR = (version) => {
11-
return /^\d{4}\.(01|1|04|4|07|7|10)\.\d+$/.test(version)
12-
}
7+
const getAuthor = (commitHash) => {
8+
return execSync(`git show --quiet --format="%an" ${commitHash}`, {
9+
encoding: 'utf8'
10+
}).replace('\n', '');
11+
};
12+
13+
const getRev = (documentedVersions, history, callback) => {
14+
15+
// Extract ARG DNSCRYPT_PROXY_VERSION from Dockerfile
16+
const dockerfile = fs.readFileSync('Dockerfile', 'utf8')
17+
const argVersion = dockerfile.match(/ARG DNSCRYPT_PROXY_VERSION=(\d+\.\d+\.\d+)/)[1]
1318

14-
const getMetaResinFromSubmodule = (documentedVersions, history, callback) => {
15-
const latestDocumented = _.trim(_.last(documentedVersions.sort(semver.compare)))
16-
// ESR releases do not update meta-balena versions
17-
if (isESR(latestDocumented)) {
18-
return callback(null, latestDocumented)
19+
if (!argVersion) {
20+
return callback(new Error('Could not determine version from Dockerfile'))
1921
}
20-
// This is a hack because git does not update all the relevant files when moving a
21-
// submodule. Because of this, older repos will still have references to meta-resin
22-
// and new ones will refer to meta-balena
23-
const metaName = fs.existsSync('.git/modules/layers/meta-resin', fs.constants.R_OK)
24-
? 'meta-resin'
25-
: 'meta-balena'
26-
shell.exec(`git --git-dir .git/modules/layers/${metaName} describe --tags --exact-match`, (code, stdout, stderr) => {
27-
if (code != 0) {
28-
return callback(new Error(`Could not find ${metaName} submodule`))
29-
}
30-
const metaVersion = stdout.replace(/\s/g,'').replace(/^v/g, '')
31-
if (!metaVersion) {
32-
return callback(new Error(`Could not determine ${metaName} version from version ${stdout}`))
33-
}
3422

35-
const latestDocumentedRevision = latestDocumented.includes('rev')? latestDocumented : `${semver.parse(latestDocumented).version}+rev0`
36-
// semver.gt will ignore the revision numbers but still compare the version
37-
// If metaVersion <= latestDocumented then the latestDocumented version is a revision of the current metaVersion
38-
const latestVersion = semver.gt(metaVersion, latestDocumentedRevision) ? metaVersion : latestDocumentedRevision
39-
return callback(null, latestVersion)
40-
})
23+
// Get the latest git tag matching semver
24+
const gitVersion = execSync(`git tag --sort=-v:refname | grep -E '^v?[0-9]+\\.[0-9]+\\.[0-9]+(\\+rev[0-9]+)?$' | head -n1`, {
25+
encoding: 'utf8'
26+
}).replace('\n', '').replace(/^v/, '')
27+
28+
// When the version does not include a revision number, +rev0 is implied and must be added here
29+
// in order for the increment to work correctly
30+
const latestDocumented = gitVersion.includes('rev') ? gitVersion : `${gitVersion}+rev0`
31+
32+
console.log(`argVersion: ${argVersion}`)
33+
console.log(`gitVersion: ${gitVersion}`)
34+
console.log(`latestDocumented: ${latestDocumented}`)
35+
36+
// process.exit(0)
37+
38+
// semver.gt will ignore the revision numbers but still compare the version
39+
// If argVersion <= latestDocumented then the latestDocumented version is a revision of the current argVersion
40+
const latestVersion = semver.gt(argVersion, latestDocumented) ? argVersion : latestDocumented
41+
42+
console.log(`latestVersion: ${latestVersion}`)
43+
return callback(null, latestVersion)
4144
}
4245

4346
module.exports = {
47+
editChangelog: true,
48+
parseFooterTags: true,
49+
updateVersion: 'update-version-file',
50+
4451
addEntryToChangelog: {
4552
preset: 'prepend',
46-
fromLine: 3
47-
},
48-
getChangelogDocumentedVersions: {
49-
preset: 'changelog-headers',
50-
clean: /^v/
53+
fromLine: 6
5154
},
5255

53-
includeCommitWhen: 'has-changelog-entry',
56+
includeCommitWhen: (commit) => { return true; },
5457
getIncrementLevelFromCommit: (commit) => {
5558
return 'patch'
5659
},
5760
incrementVersion: (currentVersion, incrementLevel) => {
58-
if (isESR(currentVersion)) {
59-
const [majorVersion, minorVersion, patchVersion] = currentVersion.split('.', 3);
60-
return `${majorVersion}.${minorVersion}.${Number(patchVersion) + 1}`
61-
}
6261
const parsedCurrentVersion = semver.parse(currentVersion)
63-
if ( ! _.isEmpty(parsedCurrentVersion.build) ) {
62+
console.log(`parsedCurrentVersion: ${JSON.stringify(parsedCurrentVersion)}`)
63+
if (parsedCurrentVersion.build != null && parsedCurrentVersion.build.length > 0) {
6464
let revision = Number(String(parsedCurrentVersion.build).split('rev').pop())
65-
if (!_.isFinite(revision)) {
65+
console.log(`revision: ${revision}`)
66+
if (!Number.isFinite(revision)) {
6667
throw new Error(`Could not extract revision number from ${currentVersion}`)
6768
}
6869
return `${parsedCurrentVersion.version}+rev${revision + 1}`
6970
}
7071
return `${parsedCurrentVersion.version}`
7172
},
72-
updateContract: (cwd, version, callback) => {
73-
if (/^\d+\.\d+\.\d+$/.test(version) == false &&
74-
/^\d+\.\d+\.\d+\+rev\d+$/.test(version) == false) {
75-
return callback(new Error(`Invalid version ${version}`));
76-
}
7773

78-
const contract = path.join(cwd, 'balena.yml');
79-
if (!fs.existsSync(contract)) {
80-
return callback(null, version);
81-
}
82-
83-
const content = yaml.load(fs.readFileSync(contract, 'utf8'));
84-
content.version = version;
85-
fs.writeFile(contract, yaml.dump(content), callback);
86-
},
87-
getCurrentBaseVersion: getMetaResinFromSubmodule,
88-
updateVersion: 'update-version-file',
74+
getCurrentBaseVersion: getRev,
75+
// If a 'changelog-entry' tag is found, use this as the subject rather than the
76+
// first line of the commit.
77+
transformTemplateData: (data) => {
78+
data.commits.forEach((commit) => {
79+
commit.subject = commit.footer['changelog-entry'] || commit.subject;
80+
commit.author = getAuthor(commit.hash);
81+
});
8982

90-
transformTemplateDataAsync: {
91-
preset: 'nested-changelogs',
92-
upstream: [
93-
{{#upstream}}
94-
{
95-
pattern: '{{{pattern}}}',
96-
repo: '{{repo}}',
97-
owner: '{{owner}}',
98-
ref: '{{ref}}'
99-
},
100-
{{/upstream}}
101-
]
83+
return data;
10284
},
10385

104-
template: 'default'
86+
template: [
87+
'## v{{version}} - {{moment date "Y-MM-DD"}}',
88+
'',
89+
'{{#each commits}}',
90+
'{{#if this.author}}',
91+
'* {{capitalize this.subject}} [{{this.author}}]',
92+
'{{else}}',
93+
'* {{capitalize this.subject}}',
94+
'{{/if}}',
95+
'{{/each}}'
96+
].join('\n')
10597
}

0 commit comments

Comments
 (0)