From 74f3b710a6ff9777c09c5e67921f212244336b5c Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Fri, 14 Aug 2020 11:21:34 +0200 Subject: [PATCH] refactor: move from "request" to "node fetch" (#277) * feat: migrate from request to node-fetch * docs(readme): add favna for all contributors * fix: forgot to refactor this one * style(gitlab): fixed formatting issues Co-authored-by: Berkmann18 --- .all-contributorsrc | 9 ++++ README.md | 100 +++++++++++++++++++++++--------------------- package.json | 2 +- src/repo/github.js | 59 ++++++++++++-------------- src/repo/gitlab.js | 81 ++++++++++++++++------------------- 5 files changed, 124 insertions(+), 127 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 55828fd2..9d65158e 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -474,6 +474,15 @@ "contributions": [ "code" ] + }, + { + "login": "Favna", + "name": "Jeroen Claassens", + "avatar_url": "https://avatars3.githubusercontent.com/u/4019718?v=4", + "profile": "https://favware.tech/", + "contributions": [ + "code" + ] } ], "skipCi": true diff --git a/README.md b/README.md index 2c45b3e6..2dd08f56 100644 --- a/README.md +++ b/README.md @@ -80,73 +80,77 @@ Thanks goes to these wonderful people - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + + + +

Jeroen Engels

πŸ’» πŸ“– ⚠️

Kent C. Dodds

πŸ“– πŸ’»

JoΓ£o GuimarΓ£es

πŸ’»

Ben Briggs

πŸ’»

Itai Steinherz

πŸ“– πŸ’»

Alex Jover

πŸ’» πŸ“–

Jeroen Engels

πŸ’» πŸ“– ⚠️

Kent C. Dodds

πŸ“– πŸ’»

JoΓ£o GuimarΓ£es

πŸ’»

Ben Briggs

πŸ’»

Itai Steinherz

πŸ“– πŸ’»

Alex Jover

πŸ’» πŸ“–

Jerod Santo

πŸ’»

Kevin Jalbert

πŸ’»

tunnckoCore

πŸ”§

Mehdi Achour

πŸ’»

Roy Revelt

πŸ›

Chris Vickery

πŸ’»

Jerod Santo

πŸ’»

Kevin Jalbert

πŸ’»

tunnckoCore

πŸ”§

Mehdi Achour

πŸ’»

Roy Revelt

πŸ›

Chris Vickery

πŸ’»

Bryce Reynolds

πŸ’»

James, please

πŸ€” πŸ’»

Spyros Ioakeimidis

πŸ’»

Fernando Costa

πŸ’»

snipe

πŸ“–

Gant Laborde

πŸ’»

Bryce Reynolds

πŸ’»

James, please

πŸ€” πŸ’»

Spyros Ioakeimidis

πŸ’»

Fernando Costa

πŸ’»

snipe

πŸ“–

Gant Laborde

πŸ’»

Md Zubair Ahmed

πŸ“– πŸ› πŸ’» ⚠️

Divjot Singh

πŸ“–

JoΓ£o Marques

πŸ’» πŸ“– πŸ€”

Andrew Lisowski

πŸ’» πŸ“– ⚠️

Xianming Zhong

πŸ“–

C.Y.Xu

πŸ’»

Md Zubair Ahmed

πŸ“– πŸ› πŸ’» ⚠️

Divjot Singh

πŸ“–

JoΓ£o Marques

πŸ’» πŸ“– πŸ€”

Andrew Lisowski

πŸ’» πŸ“– ⚠️

Xianming Zhong

πŸ“–

C.Y.Xu

πŸ’»

Dura

πŸ“–

Jake Bolam

πŸš‡ πŸ’» πŸ“– ⚠️ πŸ‘€ πŸ’¬

Maximilian Berkmann

πŸ’» ⚠️ πŸ“– πŸ”§ 🚧 πŸ‘€ πŸ’¬

tbenning

🎨

ehmicky

πŸ’»

James George

πŸ’»

Dura

πŸ“–

Jake Bolam

πŸš‡ πŸ’» πŸ“– ⚠️ πŸ‘€ πŸ’¬

Maximilian Berkmann

πŸ’» ⚠️ πŸ“– πŸ”§ 🚧 πŸ‘€ πŸ’¬

tbenning

🎨

ehmicky

πŸ’»

James George

πŸ’»

Nick Schonning

πŸ’»

Cezar Augusto

πŸ“–

Jeppe Reinhold

πŸ’»

Rachel M. Carmena

πŸ’»

simon3000

⚠️

SnOβ‚‚WMaN

πŸ’»

Nick Schonning

πŸ’»

Cezar Augusto

πŸ“–

Jeppe Reinhold

πŸ’»

Rachel M. Carmena

πŸ’»

simon3000

⚠️

SnOβ‚‚WMaN

πŸ’»

Fabrizio

πŸ› πŸ’»

kharaone

πŸ’»

Marcelo Alves

πŸ’» ⚠️

Anand Chowdhary

⚠️ πŸ› πŸ’»

Nicolas Goutay

πŸ’»

Tyler Krupicka

πŸ’» ⚠️

Fabrizio

πŸ› πŸ’»

kharaone

πŸ’»

Marcelo Alves

πŸ’» ⚠️

Anand Chowdhary

⚠️ πŸ› πŸ’»

Nicolas Goutay

πŸ’»

Tyler Krupicka

πŸ’» ⚠️

Stefano Moia

πŸ’»

IlaΓ― Deutel

πŸ“¦

Piotr StΔ™pniewski

πŸ› πŸ’» ⚠️

Gregor Martynus

πŸ‘€ πŸ’¬

Jeff Wen

πŸ‘€

PaweΕ‚ Kowalski

πŸ’»

Stefano Moia

πŸ’»

IlaΓ― Deutel

πŸ“¦

Piotr StΔ™pniewski

πŸ› πŸ’» ⚠️

Gregor Martynus

πŸ‘€ πŸ’¬

Jeff Wen

πŸ‘€

PaweΕ‚ Kowalski

πŸ’»

Jeroen Claassens

πŸ’»
+ This project follows the diff --git a/package.json b/package.json index 9b570640..1f2364dd 100644 --- a/package.json +++ b/package.json @@ -49,8 +49,8 @@ "inquirer": "^7.0.4", "json-fixer": "^1.5.1", "lodash": "^4.11.2", + "node-fetch": "^2.6.0", "pify": "^5.0.0", - "request": "^2.72.0", "yargs": "^15.0.1" }, "devDependencies": { diff --git a/src/repo/github.js b/src/repo/github.js index c1d297be..990f7baf 100644 --- a/src/repo/github.js +++ b/src/repo/github.js @@ -1,7 +1,6 @@ const url = require('url') -const pify = require('pify') -const request = pify(require('request')) -const { parseHttpUrl, isValidHttpUrl } = require('../util/url') +const fetch = require('node-fetch') +const {parseHttpUrl, isValidHttpUrl} = require('../util/url') /** * Get the host based on public or enterprise GitHub. @@ -23,16 +22,16 @@ function getApiHost(hostname) { return hostname.replace(/:\/\//, '://api.') } -function getRequestHeaders(optionalPrivateToken = '') { - const requestHeaders = { - 'User-Agent': 'request', +function getFetchHeaders(optionalPrivateToken = '') { + const fetchHeaders = { + 'User-Agent': 'node-fetch', } if (optionalPrivateToken && optionalPrivateToken.length > 0) { - requestHeaders.Authorization = `token ${optionalPrivateToken}` + fetchHeaders.Authorization = `token ${optionalPrivateToken}` } - return requestHeaders + return fetchHeaders } function getNextLink(link) { @@ -50,22 +49,20 @@ function getNextLink(link) { } function getContributorsPage(githubUrl, optionalPrivateToken) { - return request - .get({ - url: githubUrl, - headers: getRequestHeaders(optionalPrivateToken), - }) - .then(res => { - const body = JSON.parse(res.body) - if (res.statusCode >= 400) { - if (res.statusCode === 404) { - throw new Error('No contributors found on the GitHub repository') - } + return fetch(githubUrl, { + headers: getFetchHeaders(optionalPrivateToken), + }).then(res => { + if (res.status === 404 || res.status >= 500) { + throw new Error('No contributors found on the GitLab repository') + } + + return res.json().then(body => { + if (res.status >= 400 || !res.ok) { throw new Error(body.message) } const contributorsIds = body.map(contributor => contributor.login) - const nextLink = getNextLink(res.headers.link) + const nextLink = getNextLink(res.headers.get('link')) if (nextLink) { return getContributorsPage(nextLink, optionalPrivateToken).then( nextContributors => { @@ -76,9 +73,10 @@ function getContributorsPage(githubUrl, optionalPrivateToken) { return contributorsIds }) + }) } -const getUserInfo = function(username, hostname, optionalPrivateToken) { +const getUserInfo = function (username, hostname, optionalPrivateToken) { if (!username) { throw new Error( `No login when adding a contributor. Please specify a username.`, @@ -86,20 +84,16 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) { } const root = getApiHost(hostname) - return request - .get({ - url: `${root}/users/${username}`, - headers: getRequestHeaders(optionalPrivateToken), - }) - .then(res => { - const body = JSON.parse(res.body) - + return fetch(`${root}/users/${username}`, { + headers: getFetchHeaders(optionalPrivateToken), + }).then(res => + res.json().then(body => { let profile = isValidHttpUrl(body.blog) ? body.blog : body.html_url // Check for authentication required if ( (!profile && body.message.includes('Must authenticate')) || - res.statusCode === 401 + res.status === 401 ) { throw new Error( `Missing authentication for GitHub API. Did you set PRIVATE_TOKEN?`, @@ -121,10 +115,11 @@ const getUserInfo = function(username, hostname, optionalPrivateToken) { avatar_url: body.avatar_url, profile, } - }) + }), + ) } -const getContributors = function(owner, name, hostname, optionalPrivateToken) { +const getContributors = function (owner, name, hostname, optionalPrivateToken) { const root = getApiHost(hostname) const contributorsUrl = `${root}/repos/${owner}/${name}/contributors?per_page=100` return getContributorsPage(contributorsUrl, optionalPrivateToken) diff --git a/src/repo/gitlab.js b/src/repo/gitlab.js index 2115f105..7269e479 100644 --- a/src/repo/gitlab.js +++ b/src/repo/gitlab.js @@ -1,5 +1,4 @@ -const pify = require('pify') -const request = pify(require('request')) +const fetch = require('node-fetch') const addPrivateToken = (url, privateToken = '') => { if (privateToken === '') return url @@ -9,25 +8,24 @@ const addPrivateToken = (url, privateToken = '') => { .replace('&', '?') } -const getUserInfo = function(username, hostname, privateToken) { +const getUserInfo = function (username, hostname, privateToken) { /* eslint-disable complexity */ if (!hostname) { hostname = 'https://gitlab.com' } - return request - .get({ - url: addPrivateToken( - `${hostname}/api/v4/users?username=${username}`, - privateToken, - ), + return fetch( + addPrivateToken( + `${hostname}/api/v4/users?username=${username}`, + privateToken, + ), + { headers: { - 'User-Agent': 'request', + 'User-Agent': 'node-fetch', }, - }) - .then(res => { - const body = JSON.parse(res.body) - + }, + ).then(res => + res.json().then(body => { // Gitlab returns an array of users. If it is empty, it means the username provided does not exist if (!body || body.length === 0) { throw new Error(`User ${username} not found`) @@ -48,27 +46,20 @@ const getUserInfo = function(username, hostname, privateToken) { ? user.web_url : `http://${user.web_url}`, } - }) + }), + ) } -const getContributors = function(owner, name, hostname, privateToken) { +const getContributors = function (owner, name, hostname, privateToken) { if (!hostname) { hostname = 'https://gitlab.com' } - return request - .get({ - url: addPrivateToken( - `${hostname}/api/v4/projects?search=${name}`, - privateToken, - ), - headers: { - 'User-Agent': 'request', - }, - }) - .then(res => { - const projects = JSON.parse(res.body) - + return fetch( + addPrivateToken(`${hostname}/api/v4/projects?search=${name}`, privateToken), + {headers: {'User-Agent': 'node-fetch'}}, + ).then(res => + res.json().then(projects => { // Gitlab returns an array of users. If it is empty, it means the username provided does not exist if (!projects || projects.length === 0) { throw new Error(`Project ${owner}/${name} not found`) @@ -86,27 +77,25 @@ const getContributors = function(owner, name, hostname, privateToken) { throw new Error(`Project ${owner}/${name} not found`) } - return request - .get({ - url: addPrivateToken( - `${hostname}/api/v4/projects/${project.id}/repository/contributors`, - privateToken, - ), - headers: { - 'User-Agent': 'request', - }, - }) - .then(newRes => { - const contributors = JSON.parse(newRes.body) - if (newRes.statusCode >= 400) { - if (newRes.statusCode === 404) { - throw new Error('No contributors found on the GitLab repository') - } + return fetch( + addPrivateToken( + `${hostname}/api/v4/projects/${project.id}/repository/contributors`, + privateToken, + ), + {headers: {'User-Agent': 'node-fetch'}}, + ).then(newRes => { + if (newRes.status === 404 || newRes.status >= 500) { + throw new Error('No contributors found on the GitLab repository') + } + return newRes.json().then(contributors => { + if (newRes.status >= 400 || !newRes.ok) { throw new Error(contributors.message) } return contributors.map(item => item.name) }) - }) + }) + }), + ) } module.exports = {