Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 #1142

Merged
merged 11 commits into from
Oct 18, 2017
Merged

Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 #1142

merged 11 commits into from
Oct 18, 2017

Conversation

paulmelnikow
Copy link
Member

@paulmelnikow paulmelnikow commented Oct 9, 2017

- Rename data -> queryParams for readability
- Factor out duplicated code
- Remove spurious return
- Better encapsulate analytics
@paulmelnikow paulmelnikow added the core Server, BaseService, GitHub auth, Shared helpers label Oct 9, 2017
@paulmelnikow paulmelnikow changed the title Test caching request + minor cleanup Add test for caching request + minor cleanup Oct 9, 2017
@paulmelnikow paulmelnikow changed the title Add test for caching request + minor cleanup Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 Oct 10, 2017
@paulmelnikow
Copy link
Member Author

@RedSparr0w I would really like to get this merged. Would you be up for reviewing it? Feel free to ask if any of it is unclear or if you have questions.

@RedSparr0w
Copy link
Member

Had a look over it, only noticed 1 thing (will add a note), other than that all looks good to me.

@@ -109,7 +99,8 @@ function handleRequest (vendorRequestHandler) {
}
options.headers = options.headers || {};
options.headers['User-Agent'] = options.headers['User-Agent'] || 'Shields.io';
return request(options, function(err, res, body) {

request(options, (err, res, body) => {
Copy link
Member

@RedSparr0w RedSparr0w Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need to either keep the return or update the parts in server.js where it is required.
image

Edit: Looks like it would be better updating server.js as it only appears on Lines 2422 & 2467

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

  request(apiUrl, function(err, res, body) {
    if (err != null) {
      badgeData.text[1] = 'invalid';
      sendBadge(format, badgeData);
      return;
    }
    try {
      ...
      sendBadge(format, badgeData);
    } catch(e) {
      badgeData.text[1] = 'malformed';
      sendBadge(format, badgeData);
    }
  }).on('error', function(e) {
    badgeData.text[1] = 'inaccessible';
    sendBadge(format, badgeData);
  });

It seems to me the .on('error', ... blocks for request's streaming API should simply be removed. request also provides response errors to the callback, and the callbacks are handling them.

@@ -14,16 +14,32 @@ if (process.env.REDISTOGO_URL) {
}

let analytics = {};
let autosaveIntervalId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Call this autoSaveIntervalId (capital S) for consistency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed everything to lowercase. AFAIC autosave is one word. Changed autoLoad to load which seems to describe it better.

lib/analytics.js Outdated
setInterval(function analyticsAutoSave() {

function performAutosave() {
const contents = JSON.stringify(analytics);
if (useRedis) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should use the adapter pattern rather than hard-coding Redis vs plain text file. That could definitely be refactored separately though!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, agreed. We're not even using Redis in production so I don't know if this code works. We could remove it, though it would make more sense to get some tests around it, since it makes our hosting portable to Heroku for people who want to self-host. Definitely agreed it would be nice to refactor it.

}, analyticsAutoSavePeriod);
}

function scheduleAutosaving() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be consistent about autosave (lowercase S) vs autoSave (capital S) in this file.

lib/analytics.js Outdated
}

function setRoutes (server) {
server.ajax.on('analytics/v1', function(json, end) { end(analytics); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrow function?

server.ajax.on('analytics/v1', (json, end) => { end(analytics); });

@@ -109,7 +99,8 @@ function handleRequest (vendorRequestHandler) {
}
options.headers = options.headers || {};
options.headers['User-Agent'] = options.headers['User-Agent'] || 'Shields.io';
return request(options, function(err, res, body) {

request(options, (err, res, body) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

'/testing/123.svg?foo=2'
).then(() => { assert.equal(handlerCallCount, 1); });
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like this.

@Daniel15
Copy link
Member

Looks pretty nice. Just left some minor comments inline.

@paulmelnikow paulmelnikow merged commit dc44ba7 into badges:master Oct 18, 2017
@paulmelnikow paulmelnikow deleted the test-caching-request branch October 18, 2017 02:01
@paulmelnikow
Copy link
Member Author

Thanks /Dani[ae]l/!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants