From e1d5c9a9bb75e2c4912b407b9fd717bc1fcca8d2 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 8 Jan 2023 23:27:53 +0800 Subject: [PATCH] refactor: use urllib instead of request BREAKING CHANGE: Drop Node.js < 14 support --- .github/workflows/ci.yml | 5 ++--- lib/git-contributor.js | 20 ++++++++++---------- package.json | 16 ++++++++-------- test/git-contributor.test.js | 20 ++++++++------------ 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a21806..29ca8fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - node-version: [ 16 ] + node-version: [ 14, 16, 18 ] steps: - name: Checkout Git Source uses: actions/checkout@v3 @@ -29,7 +29,6 @@ jobs: - name: Install dependencies run: | - npm i npm@6 -g npm i - name: Continuous integration @@ -38,4 +37,4 @@ jobs: - name: Code coverage uses: codecov/codecov-action@v3.0.0 with: - token: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/lib/git-contributor.js b/lib/git-contributor.js index 9503937..17e587d 100644 --- a/lib/git-contributor.js +++ b/lib/git-contributor.js @@ -8,8 +8,7 @@ const { parse } = require('url'); const path = require('path'); -const Promise = require('bluebird'); -const request = require('request-promise'); +const httpclient = require('urllib'); const pkg = require('../package'); @@ -26,17 +25,18 @@ const gitLog2MailList = () => { const getInfoFromGithub = maillist => { const api = 'https://api.github.com/search/users?q='; const tasks = maillist.map(email => { + const uri = `${api}${encodeURIComponent(email + ' in:email type:user')}`; const options = { - uri: `${api}${encodeURIComponent(email + ' in:email type:user')}`, - json: true, + dataType: 'json', headers: { 'user-agent': Date.now() - } + }, + timeout: 30000 }; if (process.env.OAUTH_TOKEN) { options.headers['Authorization'] = `token ${process.env.OAUTH_TOKEN}`; } - return request(options); + return httpclient.request(uri, options).then(res => res.data); }); return Promise.all(tasks).then(list => { @@ -52,16 +52,16 @@ const getInfoFromGithubNewAPI = info => { // https://docs.github.com/en/rest/reference/repos#list-repository-contributors get top 100 contributors const uri = `https://api.github.com/repos/${info.groupName}/${info.repoName}/contributors?per_page=100`; const options = { - uri, - json: true, + dataType: 'json', headers: { 'user-agent': Date.now() - } + }, + timeout: 30000 }; if (process.env.OAUTH_TOKEN) { options.headers['Authorization'] = `token ${process.env.OAUTH_TOKEN}`; } - return request(options); + return httpclient.request(uri, options).then(res => res.data); }; const format = list => { diff --git a/package.json b/package.json index eda772d..9b24ffe 100644 --- a/package.json +++ b/package.json @@ -20,29 +20,29 @@ }, "homepage": "https://github.com/xudafeng/git-contributor", "dependencies": { - "bluebird": "^3.5.1", "commander": "^2.15.1", - "request": "^2.85.0", - "request-promise": "^4.2.2", + "urllib": "^3.10.0", "xutil": "^1.0.11" }, "devDependencies": { "babel-eslint": "^8.2.2", - "co-mocha": "*", + "c8": "^7.12.0", "eslint": "*", "eslint-plugin-mocha": "^4.11.0", - "mocha": "*", - "nyc": "^11.6.0", + "mocha": "^10.2.0", "pre-commit": "*" }, "scripts": { "ci": "npm run lint && npm run test", - "test": "nyc --reporter=lcov --reporter=text mocha", + "test": "c8 --reporter=lcov --reporter=text mocha", "lint": "eslint . --fix", - "contributor": "./bin/git-contributor" + "contributor": "./bin/git-contributor.js" }, "pre-commit": [ "lint" ], + "engines": { + "node": ">= 14.17.0" + }, "license": "MIT" } diff --git a/test/git-contributor.test.js b/test/git-contributor.test.js index d5d542d..7d99664 100644 --- a/test/git-contributor.test.js +++ b/test/git-contributor.test.js @@ -5,22 +5,18 @@ const assert = require('assert'); const contributor = require('..'); describe('test', () => { - it('should be ok', (done) => { - contributor.getAuthor().then(list => { - const res = contributor.genMarkDown(list); - assert.equal(/auto updated/.test(res.content), true); - done(); - }); + it('should be ok', async () => { + const list = await contributor.getAuthor(); + const res = contributor.genMarkDown(list); + assert.equal(/auto updated/.test(res.content), true); }); - it('should handle more than 12 authors', (done) => { - contributor.getAuthor({ + it('should handle more than 12 authors', async () => { + const list = await contributor.getAuthor({ cwd: '/', url: 'git://github.com/macacajs/macacajs.github.io.git' - }).then(list => { - const res = contributor.genMarkDown(list); - assert.equal(res.content.split('|\n').length >= 4, true); - done(); }); + const res = contributor.genMarkDown(list); + assert.equal(res.content.split('|\n').length >= 4, true); }); });