Skip to content

Commit e9ce35d

Browse files
authored
Use GitHub Releases API
1 parent 076e54c commit e9ce35d

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

js/helpinject.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* @fileoverview This file executes on load and is meant to check if the help
33
* page is the latest version and inject warnings into the page if not.
4-
* TODO: Need to use GitHub API to retrieve latest version published
4+
*
5+
* Changes:
6+
* - 2017/04/22: Use the GitHub Releases API to check if this is the lastest documentation
57
*/
68

79
'use strict';
@@ -33,10 +35,29 @@
3335
return null;
3436
};
3537

36-
var getLatestReleaseVersion = function() {
37-
// TODO: This is a hard-coded version string, need to use GH API instead
38-
var version = "0.9.13";
39-
return semverRegex().test(version) ? semverRegex().exec(version)[0] : null;
38+
var getLatestReleaseVersion = function(cb) {
39+
// On the basis Mu doesn't run on XP, IE6 support is irrelavent
40+
var xhr = new XMLHttpRequest();
41+
// We want the lastest release (doesn't include draft or preview releases)
42+
xhr.open('GET', 'https://api.github.com/repos/mu-editor/mu/releases/latest');
43+
xhr.onload = function() {
44+
// We got the data
45+
if (xhr.status === 200) {
46+
// IE6 & IE7 support is superfluous
47+
try {
48+
var lastest = JSON.parse(xhr.responseText);
49+
// Run the callback
50+
cb(semverRegex().test(lastest.tag_name) ? semverRegex().exec(lastest.tag_name)[0] : null);
51+
} catch (e) {
52+
if (console && console.error) console.error('Unable to parse release information', e);
53+
}
54+
} else {
55+
// Something went wrong. Fail quietly
56+
if (console && console.error) console.error('Unable to fetch release information');
57+
}
58+
};
59+
// Send the request
60+
xhr.send();
4061
};
4162

4263
var isVersionLowerThan = function(baseVersion, compareVersion) {
@@ -52,19 +73,20 @@
5273
var helpInjectDiv = document.getElementById('mu-help-header-inject');
5374
if (helpInjectDiv) {
5475
var urlVersion = getUrlVersion();
55-
var latestVersion = getLatestReleaseVersion();
56-
if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) {
57-
helpInjectDiv.innerHTML = [
58-
'<div class="alert alert-danger" role="alert">',
59-
' <h2><strong>Update Mu!</strong></h2>',
60-
' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version</a>.</p>',
61-
'</div>',
62-
'<div role="alert" class="alert alert-warning">',
63-
' <h2><strong>This is not the latest Documentation!</strong></h2>',
64-
' <p>This documentation corresponds to the Mu version you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>',
65-
' <p><a href="/help">View documentation for newest versions of Mu.</a></p>',
66-
'</div>'].join('\n');
67-
}
76+
getLatestReleaseVersion(function (latestVersion) {
77+
if (urlVersion && latestVersion && isVersionLowerThan(urlVersion, latestVersion)) {
78+
helpInjectDiv.innerHTML = [
79+
'<div class="alert alert-danger" role="alert">',
80+
' <h2><strong>Update Mu!</strong></h2>',
81+
' <p>You appear to be using an old version of Mu. Please <a href="https://codewith.mu" target="_blank">download the latest version (' + latestVersion + ')</a>.</p>',
82+
'</div>',
83+
'<div role="alert" class="alert alert-warning">',
84+
' <h2><strong>This is not the latest Documentation!</strong></h2>',
85+
' <p>This documentation corresponds to Mu version (' + urlVersion + ') which seems to be what you are using, however documentation for newer versions of Mu might contain more content and troubleshooting information.</p>',
86+
' <p><a href="/help">View documentation for latest version of Mu.</a></p>',
87+
'</div>'].join('\n');
88+
}
89+
});
6890
}
6991
});
7092
})();

0 commit comments

Comments
 (0)