From 0d1346d04e48704ab9ae9b0e54ade495b6e9aecf Mon Sep 17 00:00:00 2001 From: "C.Y.Xu" Date: Fri, 3 Aug 2018 05:51:04 +0800 Subject: [PATCH] feat: support Gitlab self hosted instance (#95) * feat: support Gitlab self hosted instance (#89) * docs: fix some doc typo fixes: #89 --- .all-contributorsrc | 13 +++++++++++-- README.md | 15 ++++++++++++++- package.json | 17 +++++++++++++---- src/repo/__tests__/gitlab.js | 31 +++++++++++++++++++++++++++++++ src/repo/gitlab.js | 34 +++++++++++++++++++++++++++------- src/repo/index.js | 3 +++ 6 files changed, 99 insertions(+), 14 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 499d6aa4..1719b5f9 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -2,6 +2,7 @@ "projectOwner": "jfmengels", "projectName": "all-contributors-cli", "imageSize": 100, + "repoType": "github", "commit": false, "contributorsPerLine": 6, "contributors": [ @@ -225,6 +226,15 @@ "doc" ] }, + { + "login": "xuchaoying", + "name": "C.Y.Xu", + "avatar_url": "https://avatars2.githubusercontent.com/u/8073251?v=4", + "profile": "https://github.com/xuchaoying", + "contributions": [ + "code", + ] + }, { "login": "chris-dura", "name": "Dura", @@ -234,6 +244,5 @@ "doc" ] } - ], - "repoType": "github" + ] } diff --git a/README.md b/README.md index 4c98121a..58ecdadc 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,18 @@ Where `username` is the user's GitHub or Gitlab username, and `contribution` is - tutorial: [✅](# 'Tutorials') - video: [📹](# 'Videos') +Please note that if you are using a self-hosted gitlab instance, before adding +contributor, you need to set an environment variable named `PRIVATE_TOKEN` first. + +> Private token is the personal access token to authenticate with the GitLab API. + +```console +# set private token on linux +export PRIVATE_TOKEN=your_private_token +# set private token on windows +set PRIVATE_TOKEN=your_private_token +``` + ### Check for missing contributors Use `check` to compare contributors from GitHub with the ones credited in your @@ -205,7 +217,8 @@ Thanks goes to these wonderful people | :---: | :---: | :---: | :---: | :---: | :---: | | [
Jerod Santo](https://jerodsanto.net)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=jerodsanto "Code") | [
Kevin Jalbert](https://github.com/kevinjalbert)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=kevinjalbert "Code") | [
tunnckoCore](https://i.am.charlike.online)
[🔧](#tool-charlike "Tools") | [
Mehdi Achour](https://machour.idk.tn/)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=machour "Code") | [
Roy Revelt](https://codsen.com)
[🐛](https://github.com/jfmengels/all-contributors-cli/issues?q=author%3Arevelt "Bug reports") | [
Chris Vickery](https://github.com/chrisinajar)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=chrisinajar "Code") | | [
Bryce Reynolds](https://github.com/brycereynolds)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=brycereynolds "Code") | [
James, please](http://www.jmeas.com)
[🤔](#ideas-jmeas "Ideas, Planning, & Feedback") [💻](https://github.com/jfmengels/all-contributors-cli/commits?author=jmeas "Code") | [
Spyros Ioakeimidis](http://www.spyros.io)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=spirosikmd "Code") | [
Fernando Costa](https://github.com/fadc80)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=fadc80 "Code") | [
snipe](https://snipe.net)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=snipe "Documentation") | [
Gant Laborde](http://gantlaborde.com/)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=GantMan "Code") | -| [
Md Zubair Ahmed](https://in.linkedin.com/in/mzubairahmed)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Documentation") [🐛](https://github.com/jfmengels/all-contributors-cli/issues?q=author%3AM-ZubairAhmed "Bug reports") [💻](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Code") [⚠️](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Tests") | [
Divjot Singh](http://bogas04.github.io)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=bogas04 "Documentation") | [
João Marques](https://github.com/tigermarques)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=tigermarques "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=tigermarques "Documentation") [🤔](#ideas-tigermarques "Ideas, Planning, & Feedback") | [
Andrew Lisowski](http://hipstersmoothie.com)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Documentation") [⚠️](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Tests") | [
Xianming Zhong](https://github.com/chinesedfan)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chinesedfan "Documentation") | [
Dura](https://github.com/chris-dura)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chris-dura "Documentation") | +| [
Md Zubair Ahmed](https://in.linkedin.com/in/mzubairahmed)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Documentation") [🐛](https://github.com/jfmengels/all-contributors-cli/issues?q=author%3AM-ZubairAhmed "Bug reports") [💻](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Code") [⚠️](https://github.com/jfmengels/all-contributors-cli/commits?author=M-ZubairAhmed "Tests") | [
Divjot Singh](http://bogas04.github.io)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=bogas04 "Documentation") | [
João Marques](https://github.com/tigermarques)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=tigermarques "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=tigermarques "Documentation") [🤔](#ideas-tigermarques "Ideas, Planning, & Feedback") | [
Andrew Lisowski](http://hipstersmoothie.com)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Documentation") [⚠️](https://github.com/jfmengels/all-contributors-cli/commits?author=hipstersmoothie "Tests") | [
Xianming Zhong](https://github.com/chinesedfan)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chinesedfan "Documentation") | [
Dura](https://github.com/chris-dura)
[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chris-dura "Documentation") | | [
C.Y.Xu](https://github.com/xuchaoying)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Documentation") | +| [
C.Y.Xu](https://github.com/xuchaoying)
[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Documentation") | diff --git a/package.json b/package.json index 310ce11e..290ac66f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "bin": { "all-contributors": "dist/cli.js" }, - "files": ["dist"], + "files": [ + "dist" + ], "engines": { "node": ">=4" }, @@ -21,7 +23,10 @@ "type": "git", "url": "https://github.com/jfmengels/all-contributors-cli.git" }, - "keywords": ["all-contributors", "contributors"], + "keywords": [ + "all-contributors", + "contributors" + ], "author": "Jeroen Engels ", "license": "MIT", "bugs": { @@ -38,10 +43,14 @@ "yargs": "^10.0.3" }, "devDependencies": { - "kcd-scripts": "^0.29.0", + "kcd-scripts": "^0.30.0", "nock": "^9.1.0" }, - "eslintIgnore": ["node_modules", "coverage", "dist"], + "eslintIgnore": [ + "node_modules", + "coverage", + "dist" + ], "eslintConfig": { "extends": "./node_modules/kcd-scripts/eslint.js", "rules": { diff --git a/src/repo/__tests__/gitlab.js b/src/repo/__tests__/gitlab.js index fc69b5bc..c00f708e 100644 --- a/src/repo/__tests__/gitlab.js +++ b/src/repo/__tests__/gitlab.js @@ -103,3 +103,34 @@ test('retrieve user from a different gitlab registry', async () => { ) expect(info.name).toBe('No Display Name') }) + +test('retrieve user from a gitlab registry that needs a token', async () => { + nock('http://gitlab.needtoken.com:3000') + .get('/api/v4/users?username=nodisplayname&private_token=faketoken') + .reply(200, [ + { + username: 'nodisplayname', + name: 'No Display Name', + avatar_url: + 'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon', + web_url: 'https://gitlab.com/nodisplayname', + }, + ]) + + const info = await getUserInfo( + 'nodisplayname', + 'http://gitlab.needtoken.com:3000', + 'faketoken', + ) + expect(info.name).toBe('No Display Name') +}) + +test('handle error when no token is offered', async () => { + nock('http://gitlab.needtoken.com:3000') + .get('/api/v4/users?username=nodisplayname') + .reply(200, []) + + await rejects( + getUserInfo('nodisplayname', 'http://gitlab.needtoken.com:3000', ''), + ) +}) diff --git a/src/repo/gitlab.js b/src/repo/gitlab.js index 4c82f87e..2115f105 100644 --- a/src/repo/gitlab.js +++ b/src/repo/gitlab.js @@ -1,7 +1,15 @@ const pify = require('pify') const request = pify(require('request')) -const getUserInfo = function(username, hostname) { +const addPrivateToken = (url, privateToken = '') => { + if (privateToken === '') return url + + return `${url}&private_token=${privateToken}` + .replace(/\?/g, '&') + .replace('&', '?') +} + +const getUserInfo = function(username, hostname, privateToken) { /* eslint-disable complexity */ if (!hostname) { hostname = 'https://gitlab.com' @@ -9,7 +17,10 @@ const getUserInfo = function(username, hostname) { return request .get({ - url: `${hostname}/api/v4/users?username=${username}`, + url: addPrivateToken( + `${hostname}/api/v4/users?username=${username}`, + privateToken, + ), headers: { 'User-Agent': 'request', }, @@ -22,6 +33,11 @@ const getUserInfo = function(username, hostname) { throw new Error(`User ${username} not found`) } + // no private token present + if (body.message) { + throw new Error(body.message) + } + const user = body[0] return { @@ -35,14 +51,17 @@ const getUserInfo = function(username, hostname) { }) } -const getContributors = function(owner, name, hostname) { +const getContributors = function(owner, name, hostname, privateToken) { if (!hostname) { hostname = 'https://gitlab.com' } return request .get({ - url: `${hostname}/api/v4/projects?search=${name}`, + url: addPrivateToken( + `${hostname}/api/v4/projects?search=${name}`, + privateToken, + ), headers: { 'User-Agent': 'request', }, @@ -69,9 +88,10 @@ const getContributors = function(owner, name, hostname) { return request .get({ - url: `${hostname}/api/v4/projects/${ - project.id - }/repository/contributors`, + url: addPrivateToken( + `${hostname}/api/v4/projects/${project.id}/repository/contributors`, + privateToken, + ), headers: { 'User-Agent': 'request', }, diff --git a/src/repo/index.js b/src/repo/index.js index db698253..ac3196c8 100644 --- a/src/repo/index.js +++ b/src/repo/index.js @@ -1,6 +1,7 @@ const githubAPI = require('./github') const gitlabAPI = require('./gitlab') +const privateToken = (process.env && process.env.PRIVATE_TOKEN) || '' const SUPPORTED_REPO_TYPES = { github: { value: 'github', @@ -81,6 +82,7 @@ const getUserInfo = function(username, repoType, repoHost) { return SUPPORTED_REPO_TYPES[repoType].getUserInfo( username, getHostname(repoType, repoHost), + privateToken, ) } return null @@ -92,6 +94,7 @@ const getContributors = function(owner, name, repoType, repoHost) { owner, name, getHostname(repoType, repoHost), + privateToken, ) } return null