Skip to content

Commit

Permalink
Use the 'pushed_at' time for current repo
Browse files Browse the repository at this point in the history
instead of using the last 'committed_at' time. This is because we use
the 'pushed_time' of the fork to determine if the fork is newer than the
current repository.

This should address #13.j
  • Loading branch information
musically-ut committed Feb 21, 2016
1 parent f6e34d9 commit f67353c
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions app/data/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ function makeRemoteDataURL(user, repo) {
}

function makeSelfDataURL(user, repo) {
return 'https://api.github.com/repos/' +
user + '/' + repo + '/commits';
return 'https://api.github.com/repos/' + user + '/' + repo;
}

// From: http://crocodillon.com/blog/always-catch-localstorage-security-and-quota-exceeded-errors
Expand Down Expand Up @@ -242,28 +241,49 @@ function processWithData(user, repo, remoteDataStr, selfDataStr, isFreshData) {
var remoteUpdateTimeMs = mbStrToMs(mostStarredFork['pushed_at']);

/* Parse self data */
var allCommits = JSON.parse(selfDataStr);
/* This could either be the repo data (v1) or `all_commits` data (v2). */
var selfData = JSON.parse(selfDataStr);
var selfDataToSave = null;

var selfUpdateTimeMs = null;
if (selfData.hasOwnProperty('pushed_at')) {
/* This is not an array, but the repository data. */
selfUpdateTimeMs = mbStrToMs(selfData['pushed_at']);
selfDataToSave = selfData;
} else {
/* This is the old all-commits data. */
var allCommits = selfData;
if (!allCommits || allCommits.length < 1) {
if (DEBUG) {
console.log(_logName,
'Repository does not have any commits.');
}
return;
}

if (!allCommits || allCommits.length < 1) {
if (DEBUG) {
console.log(_logName,
'Repository does not have any commits.');
var latestCommit = allCommits[0]['commit'];
var committer = latestCommit['committer'];

if (!committer) {
if (DEBUG) {
console.error(_logName,
'Could not find the latest committer.');
}
return;
}
return;
}

var latestCommit = allCommits[0]['commit'];
var committer = latestCommit['committer'];
selfUpdateTimeMs = mbStrToMs(committer['date']);
selfDataToSave = [allCommits[0]];
}

if (!committer) {
if (!selfUpdateTimeMs) {
if (DEBUG) {
console.error(_logName,
'Could not find the latest committer.');
'Could not find the selfUpdateTimeMs.');
}
return;
}

var selfUpdateTimeMs = mbStrToMs(committer['date']);

/* Cache data, if necessary */
if (isFreshData) {
Expand All @@ -283,7 +303,7 @@ function processWithData(user, repo, remoteDataStr, selfDataStr, isFreshData) {
relevantRemoteDataStr);

// Only the latest commit is relevant
var relevantSelfDataStr = JSON.stringify([allCommits[0]]);
var relevantSelfDataStr = JSON.stringify(selfDataToSave);
localStorage.setItem(makeSelfDataKey(user, repo),
relevantSelfDataStr);
} catch(e) {
Expand Down

0 comments on commit f67353c

Please sign in to comment.