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

[GitHub] Fix undefined badges #1816

Merged
merged 10 commits into from
Aug 8, 2018
Merged

[GitHub] Fix undefined badges #1816

merged 10 commits into from
Aug 8, 2018

Conversation

PyvesB
Copy link
Member

@PyvesB PyvesB commented Jul 30, 2018

This pull request addresses issue #1768.

I noticed that many other GitHub badges were suffering from this same problem, but for now I've only fixed it for the "issues" badge and will wait for some initial feedback before tackling the other ones. 😉

@PyvesB PyvesB added bug Bugs in badges and the frontend service-badge New or updated service badge labels Jul 30, 2018
@shields-ci
Copy link

shields-ci commented Jul 30, 2018

Messages
📖

✨ Thanks for your contribution to Shields, @PyvesB!

Generated by 🚫 dangerJS

Copy link
Member

@RedSparr0w RedSparr0w left a comment

Choose a reason for hiding this comment

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

Changes look good!
👍

@RedSparr0w
Copy link
Member

this is a rather strange way for GitHub to report when a repo is not found:

{
  "message": "Validation Failed",
  "errors": [
    {
      "message": "The listed users and repositories cannot be searched either because the resources do not exist or you do not have permission to view them.",
      "resource": "Search",
      "field": "q",
      "code": "invalid"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/search/"
}

Do you think it would be worth checking the message field matches and isn't trying to report a different error?

@PyvesB
Copy link
Member Author

PyvesB commented Jul 31, 2018

I get your point and I did think about that alternative as well, but I felt slightly less enthusiastic about it:

  • to avoid parsing the response body several times, we would probably have to parse it before we check the response codes and pass it as a parameter to githubCheckErrorResponse, which seems to me as the wrong ordering of operations. Also, would this lead us to more trouble when we migrate the GitHub services to the new refactored service structure?
  • It seems quite clumsy, we would have to iterate through all the elements inside the errors array and look for a message that is more likely to change than a status code.

Does this make sense?

@RedSparr0w
Copy link
Member

No worries, I think it should be fine as is 👍

@PyvesB
Copy link
Member Author

PyvesB commented Jul 31, 2018

I'll have a go at fixing the other badges in the coming days, we can think about it some more in the meantime and see how it looks once everything is done. 😉

@PyvesB PyvesB changed the title [GitHub] Fix undefined badges [GitHub] Fix undefined badges - WIP Jul 31, 2018
@PyvesB PyvesB force-pushed the undefined_github branch from e8725df to 5fb061a Compare August 5, 2018 10:16
@@ -74,7 +74,7 @@ t.create('License - API rate limit exceeded')
message: "API rate limit exceeded for 123.123.123.123. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
documentation_url: "https://developer.github.com/v3/#rate-limiting"
}))
.expectJSON({ name: 'license', value: 'inaccessible', colorB: colorsB.lightgrey });
.expectJSON({ name: 'license', value: 'invalid', colorB: colorsB.lightgrey });
Copy link
Member Author

@PyvesB PyvesB Aug 5, 2018

Choose a reason for hiding this comment

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

This change is due to the default error-helper.js handling. Wouldn't inaccessible be more appropriate for 403 responses for all badges?

Copy link
Member

Choose a reason for hiding this comment

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

It could be worth updating the error-helper to something similar to this:

defaultErrorMessages = {
  404: 'not found',
  418: "I'm a teapot",
};

const checkErrorResponse = function(badgeData, err, res, errorMessage = 'not found') {
  if (typeof errorMessage === 'string')
    errorMessage = {...defaultErrorMessages, ...{404: errorMessage}}
  else
    errorMessage = {...defaultErrorMessages, ...errorMessage};
  if (err != null) {
    badgeData.text[1] = 'inaccessible';
    badgeData.colorscheme = 'red';
    return true;
  } else if (errorMessage[res.statusCode] !== undefined) {
    badgeData.text[1] = errorMessage[res.statusCode];
    badgeData.colorscheme = 'lightgrey';
    return true;
  } else if (res.statusCode !== 200) {
    badgeData.text[1] = 'invalid';
    badgeData.colorscheme = 'lightgrey';
    return true;
  } else {
    return false;
  }
};

//Usage:
checkErrorResponse(badgeData, err, res, {403: 'repo not found'}); // 403: repo not found
checkErrorResponse(badgeData, err, res, 'repo not found'); // 404: repo not found
checkErrorResponse(badgeData, err, res, {403: 'repo not found', 404: 'repo not found'}); // 403, 404: repo not found

That way if services return a strange error code, we can still set a relevant error message.

Copy link
Member

Choose a reason for hiding this comment

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

inaccessible would be a nice way to convey a permission problem. For our badges, thoguh, it usually means the service can't be reached.

Maybe not authorized?

Copy link
Member Author

@PyvesB PyvesB Aug 7, 2018

Choose a reason for hiding this comment

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

Yes, "not authorized" may be better suited for 403 response codes. "access denied" is a good alternative as well. I'm leaning towards a separate PR though, as this has not got much to do with "undefined" in GitHub badges.

@PyvesB PyvesB force-pushed the undefined_github branch from 210b715 to 4247937 Compare August 5, 2018 10:49
@PyvesB PyvesB changed the title [GitHub] Fix undefined badges - WIP [GitHub] Fix undefined badges Aug 5, 2018
@paulmelnikow
Copy link
Member

Is this waiting for another review? Let me know if you need me to take another look.

@PyvesB
Copy link
Member Author

PyvesB commented Aug 7, 2018

Yes, I wouldn't mind someone taking a look at the final result! 👍

@@ -10,10 +11,19 @@ function checkStateColor(s) {
return { pending: 'dbab09', success: '2cbe4e', failure: 'cb2431', error: 'cb2431' }[s];
}

function checkErrorResponse(badgeData, err, res, notFoundMessage = 'repo not found') {
const isError = standardCheckErrorResponse(badgeData, err, res, notFoundMessage);
if (res && res.statusCode === 422) {
Copy link
Member

Choose a reason for hiding this comment

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

It's not completely clear whether we need the side effect of standardCheckErrorResponse in this case. If we don't, would it be clearer to move this if to the top of the function, and then return standardCheckErrorResponse(...)?

'use strict';

const chai = require('chai');
const { assert } = chai;
Copy link
Member

Choose a reason for hiding this comment

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

Could you use the expect forms like most of our tests?

Copy link
Member Author

@PyvesB PyvesB Aug 8, 2018

Choose a reason for hiding this comment

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

Done, we should probably revisit error-helper.spec.js as well at some point, and possibly some other specs. 😉

const { assert } = chai;
const { checkErrorResponse } = require('./github-helpers');

chai.use(require('chai-as-promised'));
Copy link
Member

Choose a reason for hiding this comment

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

This could be removed.

@PyvesB PyvesB merged commit e1affea into badges:master Aug 8, 2018
@shields-deployment
Copy link

This pull request was merged to master branch. Now this change is waiting for deployment.
Deploys usually happen every few weeks. After deployment changes are copied to gh-pages branch.

This badge displays deployment status:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs in badges and the frontend service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants