Skip to content

Commit 075e78a

Browse files
committed
Compare release/build dates during update check
Switch GitHub API libraries so that short SHAs can be used to get commit dates. It adds two GitHub API calls so rate-limiting will occur more quickly, but hardcoding the build date would only cut out one call.
1 parent b38ca13 commit 075e78a

File tree

6 files changed

+77
-44
lines changed

6 files changed

+77
-44
lines changed

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"Jed": "SlexAxton/Jed#1.1.1",
3434
"jed-gettext-parser": "1.0.0",
3535
"marked": "0.3.6",
36-
"lodash": "4.17.4",
37-
"octokat": "0.8.0"
36+
"lodash": "4.17.4"
3837
}
3938
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"decompress": "^4.0.0",
1717
"decompress-unzip": "^4.0.1",
1818
"fs-extra": "^3.0.1",
19+
"github-api": "^3.0.0",
1920
"polymer-analyzer": "^2.1.0",
2021
"replace": "^0.3.0",
2122
"request": "^2.79.0"

src/gui/html/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ <h2>First-Time Tips</h2>
468468
<script src="../../../bower_components/Jed/jed.js"></script>
469469
<script src="../../../bower_components/jed-gettext-parser/jedGettextParser.js"></script>
470470
<script src="../../../bower_components/lodash/dist/lodash.core.min.js"></script>
471-
<script src="../../../bower_components/octokat/dist/octokat.js"></script>
471+
<script src="../../../node_modules/github-api/dist/GitHub.bundle.min.js"></script>
472472
<script src="js/dialog.js"></script>
473473
<script src="js/dom.js"></script>
474474
<script src="js/events.js"></script>

src/gui/html/js/updateExists.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
(function exportModule(root, factory) {
33
if (typeof define === 'function' && define.amd) {
44
// AMD. Register as an anonymous module.
5-
define(['bower_components/octokat/dist/octokat.js'], factory);
5+
define(['node_modules/github-api/dist/GitHub.bundle.min.js'], factory);
66
} else {
77
// Browser globals
88
root.loot = root.loot || {};
9-
root.loot.updateExists = factory(root.Octokat);
9+
root.loot.updateExists = factory(root.GitHub);
1010
}
11-
}(this, (Octokat) => {
11+
}(this, (GitHub) => {
1212
const versionRegex = /^(\d+)\.(\d+)\.(\d+)$/;
1313

1414
function compare(lhs, rhs) {
@@ -40,20 +40,31 @@
4040
return Promise.reject(new Error('Invalid arguments, both version and build must be given'));
4141
}
4242

43-
const repo = (new Octokat()).repos('loot', 'loot');
43+
const repo = (new GitHub()).getRepo('loot', 'loot');
4444

45-
return repo.releases.latest.fetch().then((latestRelease) => {
46-
const comparison = compare(currentVersion, latestRelease.tagName);
45+
return repo.getRelease('latest').then((response) => {
46+
const latestReleaseTagName = response.data.tag_name;
47+
const comparison = compare(currentVersion, latestReleaseTagName);
4748
if (comparison === -1) {
4849
return true;
4950
} else if (comparison === 1) {
5051
return false;
5152
}
5253

53-
return repo.tags.fetch().then((tags) => {
54-
const tag = tags.items.find((element) => element.name === latestRelease.tagName);
54+
return repo.listTags().then((tagsResponse) => (
55+
tagsResponse.data.find((element) => element.name === latestReleaseTagName)
56+
)).then((tag) => {
57+
if (tag.commit.sha.startsWith(currentBuild)) {
58+
return false;
59+
}
5560

56-
return !tag.commit.sha.startsWith(currentBuild);
61+
return repo.getCommit(tag.commit.sha)
62+
.then((commitResponse) => Date.parse(commitResponse.data.committer.date))
63+
.then((tagDate) => (
64+
repo.getSingleCommit(currentBuild)
65+
.then((commitResponse) => Date.parse(commitResponse.data.commit.committer.date))
66+
.then((buildCommitDate) => tagDate > buildCommitDate)
67+
));
5768
});
5869
}).catch((error) => {
5970
if (!error.message) {
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
'use strict';
22

33
/* Mock the octokat methods used. */
4-
function Octokat() { // eslint-disable-line no-unused-vars
5-
this.repos = (/* owner, repo */) => ({
6-
releases: {
7-
latest: {
8-
fetch() {
9-
return Promise.resolve({
10-
tagName: '0.9.2',
11-
});
4+
function GitHub() { // eslint-disable-line no-unused-vars
5+
this.getRepo = (/* owner, repo */) => ({
6+
getRelease: (/* latest */) => (
7+
Promise.resolve({
8+
data: {
9+
tag_name: '0.9.2',
1210
},
13-
},
14-
},
15-
tags: {
16-
fetch() {
17-
return Promise.resolve({
18-
items: [{
19-
name: '0.9.2',
20-
commit: {
21-
sha: '6b58f92a5d41f5d7f149a1263dac78687a065ff5',
22-
},
23-
}, {
24-
name: '0.9.1',
25-
commit: {
26-
sha: 'dc24e10a4774903ede4e94165e7d6fa806466e4a',
27-
},
28-
}, {
29-
name: '0.9.0',
30-
commit: {
31-
sha: '44a0d8505d5402dd24cf0fda9540da9557866c80',
11+
})
12+
),
13+
listTags: () => (
14+
Promise.resolve({
15+
data: [{
16+
name: '0.9.2',
17+
commit: {
18+
sha: '6b58f92a5d41f5d7f149a1263dac78687a065ff5',
19+
},
20+
}, {
21+
name: '0.9.1',
22+
commit: {
23+
sha: 'dc24e10a4774903ede4e94165e7d6fa806466e4a',
24+
},
25+
}, {
26+
name: '0.9.0',
27+
commit: {
28+
sha: '44a0d8505d5402dd24cf0fda9540da9557866c80',
29+
},
30+
}],
31+
})
32+
),
33+
getCommit: (/* sha */) => (
34+
Promise.resolve({
35+
data: {
36+
committer: {
37+
date: '2011-04-14T16:00:49Z',
38+
},
39+
},
40+
})
41+
),
42+
getSingleCommit: (ref) => (
43+
Promise.resolve({
44+
data: {
45+
commit: {
46+
committer: {
47+
date: ref === 'deadbeef' ? '2011-04-14T16:00:00Z' : '2011-04-14T16:00:49Z',
3248
},
33-
}],
34-
});
35-
},
36-
},
49+
},
50+
},
51+
})
52+
),
3753
});
3854
}

src/tests/gui/html/js/test_updateExists.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ describe('updateExists()', () => {
4343
)
4444
);
4545

46-
it('should resolve to true if the given version equals the latest version but the short build SHAs are unequal', () =>
46+
it('should resolve to true if the given version equals the latest version but the short build SHAs are unequal and the latest version commit is newer', () =>
4747
loot.updateExists('0.9.2', 'deadbeef').then((result) =>
4848
result.should.be.true
4949
)
5050
);
5151

52+
it('should resolve to false if the given version equals the latest version but the short build SHAs are unequal and the build commits dates are equal', () =>
53+
loot.updateExists('0.9.2', 'feedbac').then((result) =>
54+
result.should.be.false
55+
)
56+
);
57+
5258
it('should resolve to false if the given version equals the latest version and the short build SHAs are equal', () =>
5359
loot.updateExists('0.9.2', '6b58f92').then((result) =>
5460
result.should.be.false

0 commit comments

Comments
 (0)