index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const ghGot = require('gh-got');
  3. function searchCommits(email, token) {
  4. return ghGot('search/commits', {
  5. token,
  6. query: {
  7. q: `author-email:${email}`,
  8. sort: 'author-date',
  9. // eslint-disable-next-line camelcase
  10. per_page: 1
  11. },
  12. headers: {
  13. accept: 'application/vnd.github.cloak-preview',
  14. 'user-agent': 'https://github.com/sindresorhus/github-username'
  15. }
  16. }).then(result => {
  17. const data = result.body;
  18. if (data.total_count === 0) {
  19. throw new Error(`Couldn't find username for \`${email}\``);
  20. }
  21. return data.items[0].author.login;
  22. });
  23. }
  24. module.exports = (email, token) => {
  25. if (!(typeof email === 'string' && email.includes('@'))) {
  26. return Promise.reject(new Error('Email required'));
  27. }
  28. return ghGot('search/users', {
  29. token,
  30. query: {
  31. q: `${email} in:email`
  32. },
  33. headers: {
  34. 'user-agent': 'https://github.com/sindresorhus/github-username'
  35. }
  36. }).then(result => {
  37. const data = result.body;
  38. if (data.total_count === 0) {
  39. return searchCommits(email, token);
  40. }
  41. return data.items[0].login;
  42. });
  43. };