feat: add node api for AllContributors GitHub bot (#135)

This commit is contained in:
Jake Bolam 2019-01-09 22:37:49 +08:00 committed by GitHub
parent 0f1bc8fa75
commit 8aa15c9ed8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 28 deletions

View file

@ -5,6 +5,7 @@
"bin": {
"all-contributors": "dist/cli.js"
},
"main": "dist/api.js",
"files": [
"dist"
],

22
src/api.js Normal file
View 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,
}

View file

@ -1,4 +1,5 @@
import addContributor from '../add'
import fixtures from './fixtures'
function mockInfoFetcher(username) {
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() {
const options = {
contributors: [

View 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)
})

View 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"]
}
]

View file

@ -0,0 +1,8 @@
import contributors from './contributors.json'
export default function fixtures() {
const options = {
contributors,
}
return {options}
}

View 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)
}