Add /version/history endpoint to API.

Retrieves Git tag annotations.
This commit is contained in:
D. Berge
2025-07-26 10:58:42 +02:00
parent 5487a3a49b
commit a58cce8565
5 changed files with 62 additions and 2 deletions

View File

@@ -83,7 +83,10 @@ app.map({
post: [ mw.user.logout ] post: [ mw.user.logout ]
}, },
'/version': { '/version': {
get: [ mw.auth.operations, mw.version.get ] get: [ mw.auth.operations, mw.version.get ],
'/history': {
get: [ /*mw.auth.user,*/ mw.version.history.get ],
}
}, },
'/': { '/': {
get: [ mw.openapi.get ] get: [ mw.openapi.get ]

View File

@@ -0,0 +1,15 @@
const version = require('../../../../lib/version');
module.exports = async function (req, res, next) {
try {
const v = await version.history(req.query.count, req.query.lines);
res.status(200).json(v);
} catch (err) {
next(err);
return;
}
next();
};

View File

@@ -0,0 +1,4 @@
module.exports = {
get: require('./get'),
}

View File

@@ -1,4 +1,5 @@
module.exports = { module.exports = {
get: require('./get') get: require('./get'),
history: require('./history'),
} }

View File

@@ -103,6 +103,42 @@ async function describe () {
}); });
} }
/** Return version history, from git tags
*
*/
async function history (count=5, lines=15) {
return new Promise( (resolve, reject) => {
const separator2 = String(Math.random()).substring(2)+String(Math.random()).substring(2);
const separator1 = separator2.repeat(2);
const cmd = `for tag in $(git tag --sort=-version:refname |head -n ${count}); do printf "\n${separator1}\n"; echo "$tag"; printf "\n${separator2}\n"; git --no-pager tag -n${lines} "$tag"; done`;
exec(cmd, {cwd: __dirname}, (error, stdout, stderr) => {
if (error) {
reject(error);
}
if (stdout) {
const result = stdout
.split(separator1) // Separate each tag
.map(i => i.trim()) // Trim spaces
.filter(i => i) // Filter empty lines
.map(i => i // Now for each tag…
.split(separator2) // …separate the tag from the description
.map(i => i.trim()) // trim spaces in both
.filter( i => i) // and remove empty lines
)
resolve(Object.fromEntries(result));
// resolve(result.map( ([k, v]) => ({tag: k, notes: v}) ));
} else {
// Most likely not installed from Git, use the
// version number in package.json.
resolve()
}
})
});
}
function version_old () { function version_old () {
return pkg.version; return pkg.version;
} }
@@ -132,5 +168,6 @@ async function version () {
version.compatible = compatible; version.compatible = compatible;
version.name = app_name; version.name = app_name;
version.describe = describe; version.describe = describe;
version.history = history;
module.exports = version; module.exports = version;