Skip to content

Commit

Permalink
Migrate to @octokit/rest (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Dec 23, 2020
1 parent 2640b0d commit 9e95e07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
42 changes: 15 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
'use strict';
const ghGot = require('gh-got');

async function searchCommits(email, token) {
const result = await ghGot('search/commits', {
token,
query: {
q: `author-email:${email}`,
sort: 'author-date',
// eslint-disable-next-line camelcase
per_page: 1
},
headers: {
accept: 'application/vnd.github.cloak-preview',
'user-agent': 'https://github.com/sindresorhus/github-username'
}
const {Octokit} = require('@octokit/rest');

async function searchCommits(octokit, email) {
const {data} = await octokit.search.commits({
q: `author-email:${email}`,
sort: 'author-date',
// eslint-disable-next-line camelcase
per_page: 1
});

const {body: data} = result;

if (data.total_count === 0) {
throw new Error(`Couldn't find username for \`${email}\``);
}
Expand All @@ -30,20 +21,17 @@ module.exports = async (email, token) => {
throw new Error('Email required');
}

const result = await ghGot('search/users', {
token,
query: {
q: `${email} in:email`
},
headers: {
'user-agent': 'https://github.com/sindresorhus/github-username'
}
const octokit = new Octokit({
auth: token,
userAgent: 'https://github.com/sindresorhus/github-username'
});

const {body: data} = result;
const {data} = await octokit.search.users({
q: `${email} in:email`
});

if (data.total_count === 0) {
return searchCommits(email, token);
return searchCommits(octokit, email);
}

return data.items[0].login;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"git"
],
"dependencies": {
"gh-got": "^8.1.0"
"@octokit/rest": "^18.0.6"
},
"devDependencies": {
"ava": "^1.4.1",
Expand Down

0 comments on commit 9e95e07

Please sign in to comment.