From 338e8e22bbe41f40f7fb81ce1a273d9d8a93a9c9 Mon Sep 17 00:00:00 2001 From: Richard Kotze Date: Tue, 7 Feb 2023 00:30:28 +0000 Subject: [PATCH] Require config to fetch from github This needs to be set to true to find missing initials from GitHub. Config = git-mob-config.github-fetch true #97 --- .../src/git-authors/compose-authors.spec.ts | 22 +++++++++++++++++-- .../src/git-authors/compose-authors.ts | 5 +++-- packages/git-mob/src/git-mob-commands.js | 8 ++++++- packages/git-mob/test-helpers/.git-coauthors | 20 ----------------- 4 files changed, 30 insertions(+), 25 deletions(-) delete mode 100644 packages/git-mob/test-helpers/.git-coauthors diff --git a/packages/git-mob/src/git-authors/compose-authors.spec.ts b/packages/git-mob/src/git-authors/compose-authors.spec.ts index ed1c3c7..9c53a4d 100644 --- a/packages/git-mob/src/git-authors/compose-authors.spec.ts +++ b/packages/git-mob/src/git-authors/compose-authors.spec.ts @@ -2,6 +2,7 @@ import test from 'ava'; import type { SinonSandbox, SinonStub } from 'sinon'; import { createSandbox, assert } from 'sinon'; import { composeAuthors, findMissingAuthors } from './compose-authors'; +import { mobConfig } from '../git-mob-commands'; const authorsJson = { coauthors: { @@ -56,6 +57,7 @@ test('No missing author initials', t => { test('Search GitHub for missing co-authors', async t => { const fetchAuthorsStub = sandbox.stub().resolves(gitHubAuthors); + sandbox.stub(mobConfig, 'fetchFromGitHub').returns(true); await composeAuthors( ['rkotze', 'dideler', 'jd'], @@ -68,8 +70,9 @@ test('Search GitHub for missing co-authors', async t => { }, 'Not called with ["rkotze", "dideler"]'); }); -test('Create author list only from co-author file', async t => { +test('Create author list from GitHub and co-author file', async t => { const fetchAuthorsStub = sandbox.stub().resolves(gitHubAuthors); + sandbox.stub(mobConfig, 'fetchFromGitHub').returns(true); const authorList = await composeAuthors( ['rkotze', 'dideler', 'jd'], @@ -96,6 +99,7 @@ test('Save missing co-author', async t => { }, ]; const fetchAuthorsStub = sandbox.stub().resolves(rkotzeAuthor); + sandbox.stub(mobConfig, 'fetchFromGitHub').returns(true); await composeAuthors( ['rkotze', 'jd'], @@ -121,7 +125,7 @@ test('Save missing co-author', async t => { }, 'Not called with GitMobCoauthors type'); }); -test('Create author list from GitHub and co-author file', async t => { +test('Create author list from co-author file only', async t => { const fetchAuthorsStub = sandbox.stub().resolves([]); const authorList = await composeAuthors( @@ -147,3 +151,17 @@ test('Throw error if author not found', async t => { ) ); }); + +test('Throw error if author not found because fetch from GitHub is false', async t => { + const fetchAuthorsStub = sandbox.stub().resolves(gitHubAuthors); + + const error = await t.throwsAsync(async () => + composeAuthors( + ['rkotze', 'dideler', 'jd'], + authorsJson.coauthors, + fetchAuthorsStub + ) + ); + + t.regex(error ? error.message : '', /author with initials "rkotze" not found!/i); +}); diff --git a/packages/git-mob/src/git-authors/compose-authors.ts b/packages/git-mob/src/git-authors/compose-authors.ts index 3c4117b..55e01be 100644 --- a/packages/git-mob/src/git-authors/compose-authors.ts +++ b/packages/git-mob/src/git-authors/compose-authors.ts @@ -1,6 +1,7 @@ import type { Author } from 'git-mob-core'; import { fetchGitHubAuthors } from 'git-mob-core'; import { saveAuthorList } from '../manage-authors/add-coauthor'; +import { mobConfig } from '../git-mob-commands'; import { authorBaseFormat } from './author-base-format'; async function composeAuthors( @@ -10,7 +11,7 @@ async function composeAuthors( saveAuthors = saveAuthorList ): Promise { const missing = findMissingAuthors(initials, coAuthorList); - if (missing.length > 0) { + if (missing.length > 0 && mobConfig.fetchFromGitHub()) { const fetchedAuthors = await getAuthors(missing, 'git-mob-cli'); const gitMobList = { coauthors: { ...coAuthorList, ...transformToAuthorList(fetchedAuthors) }, @@ -60,7 +61,7 @@ function buildFormatAuthorList( } function noAuthorFoundError(initials: string): Error { - return new Error(`Author with initials "${initials}" not found!`); + throw new Error(`Author with initials "${initials}" not found!`); } export { findMissingAuthors, composeAuthors }; diff --git a/packages/git-mob/src/git-mob-commands.js b/packages/git-mob/src/git-mob-commands.js index 78fd937..60e3db6 100644 --- a/packages/git-mob/src/git-mob-commands.js +++ b/packages/git-mob/src/git-mob-commands.js @@ -18,7 +18,12 @@ function resetMob() { function useLocalTemplate() { const localTemplate = config.get('--local git-mob-config.use-local-template'); - return localTemplate && localTemplate === 'true'; + return localTemplate === 'true'; +} + +function fetchFromGitHub() { + const githubFetch = config.get('--global git-mob-config.github-fetch'); + return githubFetch === 'true'; } function setGitAuthor(name, email) { @@ -28,6 +33,7 @@ function setGitAuthor(name, email) { const mobConfig = { useLocalTemplate, + fetchFromGitHub, }; export { diff --git a/packages/git-mob/test-helpers/.git-coauthors b/packages/git-mob/test-helpers/.git-coauthors deleted file mode 100644 index 0d2095f..0000000 --- a/packages/git-mob/test-helpers/.git-coauthors +++ /dev/null @@ -1,20 +0,0 @@ -{ - "coauthors": { - "jd": { - "name": "Jane Doe", - "email": "jane@findmypast.com" - }, - "fb": { - "name": "Frances Bar", - "email": "frances-bar@findmypast.com" - }, - "rkotze": { - "name": "Richard", - "email": "rich@gitmob.com" - }, - "dideler": { - "name": "Denis", - "email": "denis@gitmob.com" - } - } -} \ No newline at end of file