Skip to content

Commit

Permalink
feat: support Gitlab self hosted instance(all-contributors#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchaoying committed Jul 31, 2018
1 parent d2ce3be commit 971107a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
13 changes: 12 additions & 1 deletion .all-contributorsrc
Expand Up @@ -224,6 +224,17 @@
"contributions": [
"doc"
]
},
{
"login": "xuchaoying",
"name": "C.Y.Xu",
"avatar_url": "https://avatars2.githubusercontent.com/u/8073251?v=4",
"profile": "https://github.com/xuchaoying",
"contributions": [
"code",
"doc"
]
}
]
],
"repoType": "github"
}
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -10,7 +10,7 @@
[![version][version-badge]][package] [![downloads][downloads-badge]][downloads]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-23-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-24-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Watch on GitHub][github-watch-badge]][github-watch]
[![Star on GitHub][github-star-badge]][github-star]
Expand Down Expand Up @@ -137,6 +137,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 add
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_privete_token
# set private token on windows
set PRIVATE_TOKEN=your_privete_token
```

### Check for missing contributors

Use `check` to compare contributors from GitHub with the ones credited in your
Expand Down Expand Up @@ -185,7 +197,7 @@ Thanks goes to these wonderful people
| :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars3.githubusercontent.com/u/8212?v=3" width="100px;"/><br /><sub><b>Jerod Santo</b></sub>](https://jerodsanto.net)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=jerodsanto "Code") | [<img src="https://avatars1.githubusercontent.com/u/574871?v=3" width="100px;"/><br /><sub><b>Kevin Jalbert</b></sub>](https://github.com/kevinjalbert)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=kevinjalbert "Code") | [<img src="https://avatars3.githubusercontent.com/u/5038030?v=4" width="100px;"/><br /><sub><b>tunnckoCore</b></sub>](https://i.am.charlike.online)<br />[🔧](#tool-charlike "Tools") | [<img src="https://avatars2.githubusercontent.com/u/304450?v=4" width="100px;"/><br /><sub><b>Mehdi Achour</b></sub>](https://machour.idk.tn/)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=machour "Code") | [<img src="https://avatars1.githubusercontent.com/u/8344688?v=4" width="100px;"/><br /><sub><b>Roy Revelt</b></sub>](https://codsen.com)<br />[🐛](https://github.com/jfmengels/all-contributors-cli/issues?q=author%3Arevelt "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/422331?v=4" width="100px;"/><br /><sub><b>Chris Vickery</b></sub>](https://github.com/chrisinajar)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=chrisinajar "Code") |
| [<img src="https://avatars2.githubusercontent.com/u/1026002?v=4" width="100px;"/><br /><sub><b>Bryce Reynolds</b></sub>](https://github.com/brycereynolds)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=brycereynolds "Code") | [<img src="https://avatars3.githubusercontent.com/u/2322305?v=4" width="100px;"/><br /><sub><b>James, please</b></sub>](http://www.jmeas.com)<br />[🤔](#ideas-jmeas "Ideas, Planning, & Feedback") [💻](https://github.com/jfmengels/all-contributors-cli/commits?author=jmeas "Code") | [<img src="https://avatars3.githubusercontent.com/u/1057324?v=4" width="100px;"/><br /><sub><b>Spyros Ioakeimidis</b></sub>](http://www.spyros.io)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=spirosikmd "Code") | [<img src="https://avatars3.githubusercontent.com/u/12335761?v=4" width="100px;"/><br /><sub><b>Fernando Costa</b></sub>](https://github.com/fadc80)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=fadc80 "Code") | [<img src="https://avatars0.githubusercontent.com/u/197404?v=4" width="100px;"/><br /><sub><b>snipe</b></sub>](https://snipe.net)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=snipe "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/997157?v=4" width="100px;"/><br /><sub><b>Gant Laborde</b></sub>](http://gantlaborde.com/)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=GantMan "Code") |
| [<img src="https://avatars2.githubusercontent.com/u/17708702?v=4" width="100px;"/><br /><sub><b>Md Zubair Ahmed</b></sub>](https://in.linkedin.com/in/mzubairahmed)<br />[📖](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") | [<img src="https://avatars3.githubusercontent.com/u/6177621?v=4" width="100px;"/><br /><sub><b>Divjot Singh</b></sub>](http://bogas04.github.io)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=bogas04 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/15315098?v=4" width="100px;"/><br /><sub><b>João Marques</b></sub>](https://github.com/tigermarques)<br />[💻](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") | [<img src="https://avatars3.githubusercontent.com/u/1192452?v=4" width="100px;"/><br /><sub><b>Andrew Lisowski</b></sub>](http://hipstersmoothie.com)<br />[💻](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") | [<img src="https://avatars3.githubusercontent.com/u/1736154?v=4" width="100px;"/><br /><sub><b>Xianming Zhong</b></sub>](https://github.com/chinesedfan)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chinesedfan "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/17708702?v=4" width="100px;"/><br /><sub><b>Md Zubair Ahmed</b></sub>](https://in.linkedin.com/in/mzubairahmed)<br />[📖](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") | [<img src="https://avatars3.githubusercontent.com/u/6177621?v=4" width="100px;"/><br /><sub><b>Divjot Singh</b></sub>](http://bogas04.github.io)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=bogas04 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/15315098?v=4" width="100px;"/><br /><sub><b>João Marques</b></sub>](https://github.com/tigermarques)<br />[💻](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") | [<img src="https://avatars3.githubusercontent.com/u/1192452?v=4" width="100px;"/><br /><sub><b>Andrew Lisowski</b></sub>](http://hipstersmoothie.com)<br />[💻](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") | [<img src="https://avatars3.githubusercontent.com/u/1736154?v=4" width="100px;"/><br /><sub><b>Xianming Zhong</b></sub>](https://github.com/chinesedfan)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=chinesedfan "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/8073251?v=4" width="100px;"/><br /><sub><b>C.Y.Xu</b></sub>](https://github.com/xuchaoying)<br />[💻](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Code") [📖](https://github.com/jfmengels/all-contributors-cli/commits?author=xuchaoying "Documentation") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the
Expand Down
17 changes: 13 additions & 4 deletions package.json
Expand Up @@ -5,7 +5,9 @@
"bin": {
"all-contributors": "dist/cli.js"
},
"files": ["dist"],
"files": [
"dist"
],
"engines": {
"node": ">=4"
},
Expand All @@ -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 <jfm.engels@gmail.com>",
"license": "MIT",
"bugs": {
Expand All @@ -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": {
Expand Down
31 changes: 31 additions & 0 deletions src/repo/__tests__/gitlab.js
Expand Up @@ -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', ''),
)
})
34 changes: 27 additions & 7 deletions src/repo/gitlab.js
@@ -1,15 +1,26 @@
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'
}

return request
.get({
url: `${hostname}/api/v4/users?username=${username}`,
url: addPrivateToken(
`${hostname}/api/v4/users?username=${username}`,
privateToken,
),
headers: {
'User-Agent': 'request',
},
Expand All @@ -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 {
Expand All @@ -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',
},
Expand All @@ -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',
},
Expand Down
3 changes: 3 additions & 0 deletions 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',
Expand Down Expand Up @@ -81,6 +82,7 @@ const getUserInfo = function(username, repoType, repoHost) {
return SUPPORTED_REPO_TYPES[repoType].getUserInfo(
username,
getHostname(repoType, repoHost),
privateToken,
)
}
return null
Expand All @@ -92,6 +94,7 @@ const getContributors = function(owner, name, repoType, repoHost) {
owner,
name,
getHostname(repoType, repoHost),
privateToken,
)
}
return null
Expand Down

0 comments on commit 971107a

Please sign in to comment.