From 264818e517f087afdf186b0d931b9c3da8767e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 7 Oct 2020 18:21:23 -0400 Subject: [PATCH] fix: use proper api url no need to parse all tags since https://api.github.com/repos/NodeBB/NodeBB/releases/latest gives the latest non-prerelease, non-draft releases --- src/admin/versions.js | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/admin/versions.js b/src/admin/versions.js index 607315b75d..c1aa46d11a 100644 --- a/src/admin/versions.js +++ b/src/admin/versions.js @@ -1,6 +1,5 @@ 'use strict'; -const semver = require('semver'); const request = require('request'); const meta = require('../meta'); @@ -10,7 +9,7 @@ let versionCacheLastModified = ''; const isPrerelease = /^v?\d+\.\d+\.\d+-.+$/; -function getLatestRelease(page, callback) { +function getLatestVersion(callback) { const headers = { Accept: 'application/vnd.github.v3+json', 'User-Agent': encodeURIComponent('NodeBB Admin Control Panel/' + meta.config.title), @@ -19,11 +18,12 @@ function getLatestRelease(page, callback) { if (versionCacheLastModified) { headers['If-Modified-Since'] = versionCacheLastModified; } - request('https://api.github.com/repos/NodeBB/NodeBB/tags?page=' + page, { + + request('https://api.github.com/repos/NodeBB/NodeBB/releases/latest', { json: true, headers: headers, - timeout: 1000, - }, function (err, res, releases) { + timeout: 2000, + }, function (err, res, latestRelease) { if (err) { return callback(err); } @@ -36,28 +36,16 @@ function getLatestRelease(page, callback) { return callback(new Error(res.statusMessage)); } - releases = releases.filter(function (version) { - return !isPrerelease.test(version.name); // filter out automated prerelease versions - }).map(function (version) { - return version.name.replace(/^v/, ''); - }).sort(function (a, b) { - return semver.lt(a, b) ? 1 : -1; - }); - - if (!releases.length) { - return getLatestRelease(page + 1, callback); + if (!latestRelease || !latestRelease.tag_name) { + return callback(new Error('[[error:cant-get-latest-release]]')); } - versionCache = releases[0]; + const tagName = latestRelease.tag_name.replace(/^v/, ''); + versionCache = tagName; versionCacheLastModified = res.headers['last-modified']; - callback(null, versionCache); }); } -function getLatestVersion(callback) { - getLatestRelease(1, callback); -} - exports.getLatestVersion = getLatestVersion; exports.isPrerelease = isPrerelease;