From 8aa15c9ed8e9e0f0200144097d0c502a7a27029e Mon Sep 17 00:00:00 2001 From: Jake Bolam Date: Wed, 9 Jan 2019 22:37:49 +0800 Subject: [PATCH] feat: add node api for AllContributors GitHub bot (#135) --- package.json | 1 + src/api.js | 22 ++++++++++++++ src/contributors/__tests__/add.js | 29 +------------------ src/contributors/__tests__/addWithDetails.js | 25 ++++++++++++++++ .../__tests__/fixtures/contributors.json | 28 ++++++++++++++++++ src/contributors/__tests__/fixtures/index.js | 8 +++++ src/contributors/addWithDetails.js | 21 ++++++++++++++ 7 files changed, 106 insertions(+), 28 deletions(-) create mode 100644 src/api.js create mode 100644 src/contributors/__tests__/addWithDetails.js create mode 100644 src/contributors/__tests__/fixtures/contributors.json create mode 100644 src/contributors/__tests__/fixtures/index.js create mode 100644 src/contributors/addWithDetails.js diff --git a/package.json b/package.json index df8065d..7464012 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "bin": { "all-contributors": "dist/cli.js" }, + "main": "dist/api.js", "files": [ "dist" ], diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..21231bb --- /dev/null +++ b/src/api.js @@ -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, +} diff --git a/src/contributors/__tests__/add.js b/src/contributors/__tests__/add.js index 2e8f71c..4879c39 100644 --- a/src/contributors/__tests__/add.js +++ b/src/contributors/__tests__/add.js @@ -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: [ diff --git a/src/contributors/__tests__/addWithDetails.js b/src/contributors/__tests__/addWithDetails.js new file mode 100644 index 0000000..f2e7ebe --- /dev/null +++ b/src/contributors/__tests__/addWithDetails.js @@ -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) +}) diff --git a/src/contributors/__tests__/fixtures/contributors.json b/src/contributors/__tests__/fixtures/contributors.json new file mode 100644 index 0000000..d38dc8b --- /dev/null +++ b/src/contributors/__tests__/fixtures/contributors.json @@ -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"] + } +] diff --git a/src/contributors/__tests__/fixtures/index.js b/src/contributors/__tests__/fixtures/index.js new file mode 100644 index 0000000..6940fcc --- /dev/null +++ b/src/contributors/__tests__/fixtures/index.js @@ -0,0 +1,8 @@ +import contributors from './contributors.json' + +export default function fixtures() { + const options = { + contributors, + } + return {options} +} diff --git a/src/contributors/addWithDetails.js b/src/contributors/addWithDetails.js new file mode 100644 index 0000000..5f85c0f --- /dev/null +++ b/src/contributors/addWithDetails.js @@ -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) +}