Skip to content

Commit

Permalink
feat: add node api for AllContributors GitHub bot (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam authored Jan 9, 2019
1 parent 0f1bc8f commit 8aa15c9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"bin": {
"all-contributors": "dist/cli.js"
},
"main": "dist/api.js",
"files": [
"dist"
],
Expand Down
22 changes: 22 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -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,
}
29 changes: 1 addition & 28 deletions src/contributors/__tests__/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import addContributor from '../add'
import fixtures from './fixtures'

function mockInfoFetcher(username) {
return Promise.resolve({
Expand All @@ -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: [
Expand Down
25 changes: 25 additions & 0 deletions src/contributors/__tests__/addWithDetails.js
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions src/contributors/__tests__/fixtures/contributors.json
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions src/contributors/__tests__/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import contributors from './contributors.json'

export default function fixtures() {
const options = {
contributors,
}
return {options}
}
21 changes: 21 additions & 0 deletions src/contributors/addWithDetails.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8aa15c9

Please sign in to comment.