Skip to content

Commit

Permalink
Require config to fetch from github
Browse files Browse the repository at this point in the history
This needs to be set to true to find missing initials from GitHub.
Config = git-mob-config.github-fetch true

#97
  • Loading branch information
rkotze committed Feb 7, 2023
1 parent d2b876b commit 338e8e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
22 changes: 20 additions & 2 deletions packages/git-mob/src/git-authors/compose-authors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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(
Expand All @@ -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);
});
5 changes: 3 additions & 2 deletions packages/git-mob/src/git-authors/compose-authors.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -10,7 +11,7 @@ async function composeAuthors(
saveAuthors = saveAuthorList
): Promise<string[]> {
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) },
Expand Down Expand Up @@ -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 };
8 changes: 7 additions & 1 deletion packages/git-mob/src/git-mob-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,6 +33,7 @@ function setGitAuthor(name, email) {

const mobConfig = {
useLocalTemplate,
fetchFromGitHub,
};

export {
Expand Down
20 changes: 0 additions & 20 deletions packages/git-mob/test-helpers/.git-coauthors

This file was deleted.

0 comments on commit 338e8e2

Please sign in to comment.