mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 21:46:29 +00:00
feat: add node api for AllContributors GitHub bot (#135)
This commit is contained in:
parent
0f1bc8fa75
commit
8aa15c9ed8
7 changed files with 106 additions and 28 deletions
|
@ -5,6 +5,7 @@
|
||||||
"bin": {
|
"bin": {
|
||||||
"all-contributors": "dist/cli.js"
|
"all-contributors": "dist/cli.js"
|
||||||
},
|
},
|
||||||
|
"main": "dist/api.js",
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
|
|
22
src/api.js
Normal file
22
src/api.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// All Contributors Node JS API
|
||||||
|
// This is not yet ready for main-stream usage, API implementation may change at any time without warning
|
||||||
|
// This is to support adding contributors using the AllContributors GitHub Bot (see github.com/all-contributors/all-contributors-bot
|
||||||
|
// These Node API's are intended to be network and side effect free, everything should be in memory with no io to network/disk
|
||||||
|
|
||||||
|
const chalk = require('chalk')
|
||||||
|
|
||||||
|
const addContributorWithDetails = require('./contributors/addWithDetails')
|
||||||
|
const generate = require('./generate')
|
||||||
|
|
||||||
|
process.stdout.write(
|
||||||
|
chalk.yellow(
|
||||||
|
`${chalk.bold(
|
||||||
|
'WARNING',
|
||||||
|
)} :: Using the all-contributors node-api comes with zero guarantees of stability and may contain breaking changes without warning\n`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
addContributorWithDetails,
|
||||||
|
generate,
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import addContributor from '../add'
|
import addContributor from '../add'
|
||||||
|
import fixtures from './fixtures'
|
||||||
|
|
||||||
function mockInfoFetcher(username) {
|
function mockInfoFetcher(username) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
|
@ -9,34 +10,6 @@ function mockInfoFetcher(username) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function fixtures() {
|
|
||||||
const options = {
|
|
||||||
contributors: [
|
|
||||||
{
|
|
||||||
login: 'login1',
|
|
||||||
name: 'Some name',
|
|
||||||
avatar_url: 'www.avatar.url',
|
|
||||||
profile: 'www.profile.url',
|
|
||||||
contributions: ['code'],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
login: 'login2',
|
|
||||||
name: 'Some name',
|
|
||||||
avatar_url: 'www.avatar.url',
|
|
||||||
profile: 'www.profile.url',
|
|
||||||
contributions: [{type: 'blog', url: 'www.blog.url/path'}, 'code'],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Missing Login',
|
|
||||||
avatar_url: 'www.avatar.url',
|
|
||||||
profile: 'www.profile.url',
|
|
||||||
contributions: ['code'],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
return {options}
|
|
||||||
}
|
|
||||||
|
|
||||||
function caseFixtures() {
|
function caseFixtures() {
|
||||||
const options = {
|
const options = {
|
||||||
contributors: [
|
contributors: [
|
||||||
|
|
25
src/contributors/__tests__/addWithDetails.js
Normal file
25
src/contributors/__tests__/addWithDetails.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import addContributorWithDetails from '../addWithDetails'
|
||||||
|
import fixtures from './fixtures'
|
||||||
|
|
||||||
|
test('add new contributor without going to the network', async () => {
|
||||||
|
const {options} = fixtures()
|
||||||
|
const userDetails = {
|
||||||
|
login: 'jakebolam',
|
||||||
|
contributions: ['code', 'security'],
|
||||||
|
name: 'Jake Bolam',
|
||||||
|
avatar_url: 'my-avatar.example.com',
|
||||||
|
profile: 'jakebolam.com',
|
||||||
|
}
|
||||||
|
|
||||||
|
const contributors = await addContributorWithDetails({
|
||||||
|
options,
|
||||||
|
login: userDetails.login,
|
||||||
|
contributions: userDetails.contributions,
|
||||||
|
name: userDetails.name,
|
||||||
|
avatar_url: userDetails.avatar_url,
|
||||||
|
profile: userDetails.profile,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(contributors.length).toBe(options.contributors.length + 1)
|
||||||
|
expect(contributors[options.contributors.length]).toEqual(userDetails)
|
||||||
|
})
|
28
src/contributors/__tests__/fixtures/contributors.json
Normal file
28
src/contributors/__tests__/fixtures/contributors.json
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"login": "login1",
|
||||||
|
"name": "Some name",
|
||||||
|
"avatar_url": "www.avatar.url",
|
||||||
|
"profile": "www.profile.url",
|
||||||
|
"contributions": ["code"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"login": "login2",
|
||||||
|
"name": "Some name",
|
||||||
|
"avatar_url": "www.avatar.url",
|
||||||
|
"profile": "www.profile.url",
|
||||||
|
"contributions": [
|
||||||
|
{
|
||||||
|
"type": "blog",
|
||||||
|
"url": "www.blog.url/path"
|
||||||
|
},
|
||||||
|
"code"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Missing Login",
|
||||||
|
"avatar_url": "www.avatar.url",
|
||||||
|
"profile": "www.profile.url",
|
||||||
|
"contributions": ["code"]
|
||||||
|
}
|
||||||
|
]
|
8
src/contributors/__tests__/fixtures/index.js
Normal file
8
src/contributors/__tests__/fixtures/index.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import contributors from './contributors.json'
|
||||||
|
|
||||||
|
export default function fixtures() {
|
||||||
|
const options = {
|
||||||
|
contributors,
|
||||||
|
}
|
||||||
|
return {options}
|
||||||
|
}
|
21
src/contributors/addWithDetails.js
Normal file
21
src/contributors/addWithDetails.js
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
const addContributor = require('./add')
|
||||||
|
|
||||||
|
// Adds a contributor without going to the network (you supply the additional fields)
|
||||||
|
module.exports = function addContributorWithDetails({
|
||||||
|
options,
|
||||||
|
login,
|
||||||
|
contributions,
|
||||||
|
name,
|
||||||
|
avatar_url,
|
||||||
|
profile,
|
||||||
|
}) {
|
||||||
|
const infoFetcherNoNetwork = function() {
|
||||||
|
return Promise.resolve({
|
||||||
|
login,
|
||||||
|
name,
|
||||||
|
avatar_url,
|
||||||
|
profile,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return addContributor(options, login, contributions, infoFetcherNoNetwork)
|
||||||
|
}
|
Loading…
Reference in a new issue