|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const _ = require('lodash') |
| 3 | +const execSync = require('child_process').execSync; |
4 | 4 | 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') |
9 | 6 |
|
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] |
13 | 18 |
|
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')) |
19 | 21 | } |
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 | | - } |
34 | 22 |
|
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) |
41 | 44 | } |
42 | 45 |
|
43 | 46 | module.exports = { |
| 47 | + editChangelog: true, |
| 48 | + parseFooterTags: true, |
| 49 | + updateVersion: 'update-version-file', |
| 50 | + |
44 | 51 | addEntryToChangelog: { |
45 | 52 | preset: 'prepend', |
46 | | - fromLine: 3 |
47 | | - }, |
48 | | - getChangelogDocumentedVersions: { |
49 | | - preset: 'changelog-headers', |
50 | | - clean: /^v/ |
| 53 | + fromLine: 6 |
51 | 54 | }, |
52 | 55 |
|
53 | | - includeCommitWhen: 'has-changelog-entry', |
| 56 | + includeCommitWhen: (commit) => { return true; }, |
54 | 57 | getIncrementLevelFromCommit: (commit) => { |
55 | 58 | return 'patch' |
56 | 59 | }, |
57 | 60 | incrementVersion: (currentVersion, incrementLevel) => { |
58 | | - if (isESR(currentVersion)) { |
59 | | - const [majorVersion, minorVersion, patchVersion] = currentVersion.split('.', 3); |
60 | | - return `${majorVersion}.${minorVersion}.${Number(patchVersion) + 1}` |
61 | | - } |
62 | 61 | 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) { |
64 | 64 | let revision = Number(String(parsedCurrentVersion.build).split('rev').pop()) |
65 | | - if (!_.isFinite(revision)) { |
| 65 | + console.log(`revision: ${revision}`) |
| 66 | + if (!Number.isFinite(revision)) { |
66 | 67 | throw new Error(`Could not extract revision number from ${currentVersion}`) |
67 | 68 | } |
68 | 69 | return `${parsedCurrentVersion.version}+rev${revision + 1}` |
69 | 70 | } |
70 | 71 | return `${parsedCurrentVersion.version}` |
71 | 72 | }, |
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 | | - } |
77 | 73 |
|
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 | + }); |
89 | 82 |
|
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; |
102 | 84 | }, |
103 | 85 |
|
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') |
105 | 97 | } |
0 commit comments