Skip to content

Commit

Permalink
fix(authors): Revert "feat(authors): scrape authors from github"
Browse files Browse the repository at this point in the history
This reverts commit e5c9ddf.
  • Loading branch information
moki committed Apr 12, 2023
1 parent 5ba12ea commit 7c807ce
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 122 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export const REGEXP_INCLUDE_FILE_PATH = /(?<=[(]).+(?=[)])/g;
// Include example: author: authorLogin
// Regexp result: authorLogin
export const REGEXP_AUTHOR = /(?<=author:\s).+(?=\r?\n)/g;
export const REGEXP_CONTRIBUTORS = /(?<=contributors:\s).+(?=\r?\n)*?/g;

export const MIN_CHUNK_SIZE = Number(process.env.MIN_CHUNK_SIZE) || 1000;
export const WORKERS_COUNT = Number(process.env.WORKERS_COUNT) || (os.cpus().length - 1);
Expand Down
2 changes: 0 additions & 2 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ export interface Contributor {
login: string;
name: string;
url: string;
date?: string;
hash?: string;
}

export interface Contributors {
Expand Down
52 changes: 7 additions & 45 deletions src/services/authors.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,24 @@
import os from 'os';

import {logger, replaceDoubleToSingleQuotes} from '../utils';
import {REGEXP_AUTHOR, REGEXP_CONTRIBUTORS} from '../constants';
import {replaceDoubleToSingleQuotes} from '../utils';
import {REGEXP_AUTHOR} from '../constants';
import {VCSConnector} from '../vcs-connector/connector-models';
import {isEarlier, parseDataFromJSONString} from './utils';
import {Contributor} from '../models';


export function getContributorArrayFromMetadata(metadata: string[], filePath = '') {
const contributors: Contributor[] = [];

for (const meta of metadata) {
const matchContributors = meta.match(REGEXP_CONTRIBUTORS);
try {
if (matchContributors) {
contributors.push(...parseDataFromJSONString(matchContributors[0]));
}
} catch (err) {
logger.warn(filePath, JSON.stringify(err));
}
}

return contributors;
}


async function updateAuthorMetadataString(
fileMetadata = '',
vcsConnector?: VCSConnector,
newMetadatas: string[] = [],
): Promise<string> {
async function updateAuthorMetadataString(defaultMetadata = '', vcsConnector?: VCSConnector): Promise<string> {
if (!vcsConnector) {
return fileMetadata;
return defaultMetadata;
}

const matchAuthor = fileMetadata.match(REGEXP_AUTHOR);
const matchAuthor = defaultMetadata.match(REGEXP_AUTHOR);

if (matchAuthor && matchAuthor?.length > 0) {
const authorLogin = matchAuthor[0];
const user = await getAuthorDetails(vcsConnector, authorLogin);

if (user) {
return fileMetadata.replace(authorLogin, user);
}
}

const contributors = getContributorArrayFromMetadata(newMetadatas);

if (contributors.length && !matchAuthor) {
contributors.sort((a, b) => isEarlier(a.date, b.date) ? -1 : 1);
const user = await getAuthorDetails(vcsConnector, contributors[0]);

if (user) {
return `${fileMetadata}${os.EOL}author: ${user}`;
return defaultMetadata.replace(authorLogin, user);
}
}

return fileMetadata;
return defaultMetadata;
}

async function getAuthorDetails(vcsConnector: VCSConnector, author: string | object): Promise<string | null> {
Expand Down
6 changes: 1 addition & 5 deletions src/services/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ async function getContentWithUpdatedDynamicMetadata(
const [, fileMetadata, , fileMainContent] = matches;
let updatedDefaultMetadata = '';

updatedDefaultMetadata = await updateAuthorMetadataString(
fileMetadata,
options.vcsConnector,
newMetadatas,
);
updatedDefaultMetadata = await updateAuthorMetadataString(fileMetadata, options.vcsConnector);

return `${getUpdatedMetadataString(newMetadatas, updatedDefaultMetadata)}${fileMainContent}`;
}
Expand Down
17 changes: 0 additions & 17 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import evalExp from '@doc-tools/transform/lib/liquid/evaluation';
import {Filter, TextItems} from '../models';
import liquid from '@doc-tools/transform/lib/liquid';
import {ArgvService} from './index';
import {replaceSingleToDoubleQuotes} from '../utils';

export interface FilterFilesOptions {
resolveConditions?: boolean;
Expand Down Expand Up @@ -150,19 +149,3 @@ export function liquidField(input: string, vars: Record<string, unknown>, path:
export function isObject(o: unknown): o is object {
return typeof o === 'object' && o !== null;
}

export function isEarlier(oldDate?: string, newDate?: string) {
if (!newDate) {
return false;
}

if (!oldDate) {
return true;
}

return new Date(oldDate) > new Date(newDate);
}

export function parseDataFromJSONString(dataString: string) {
return JSON.parse(replaceSingleToDoubleQuotes(dataString));
}
4 changes: 0 additions & 4 deletions src/utils/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,3 @@ export function joinSinglePageResults(singlePageResults: SinglePageResult[], roo
export function replaceDoubleToSingleQuotes(str: string): string {
return str.replace(/"/g, '\'');
}

export function replaceSingleToDoubleQuotes(str: string): string {
return str.replace(/'/g, '"');
}
1 change: 0 additions & 1 deletion src/vcs-connector/connector-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export interface GithubCommitDTO {
author: {
name: string;
email: string;
date: string;
};
};
author: {
Expand Down
30 changes: 5 additions & 25 deletions src/vcs-connector/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from '../constants';
import {addSlashPrefix, execAsync, logger} from '../utils';
import {validateConnectorFields} from './connector-validator';
import {isEarlier} from '../services/utils';

const contributorsByPath: Map<string, FileContributors> = new Map();
const contributorsData: Map<string, Contributor | null> = new Map();
Expand Down Expand Up @@ -147,7 +146,6 @@ async function getContributorDataByHashCommit(httpClientByToken: Octokit, hashCo
login,
name: commit.author.name,
url,
date: commit.author.date,
};
}

Expand Down Expand Up @@ -190,36 +188,18 @@ function addContributorForPath(paths: string[], newContributor: Contributors, ha
contributors: newContributor,
hasIncludes,
});

return;
}

const oldContributors = contributorsByPath.get(normalizePath);
const newContributorKey = Object.keys(newContributor)?.[0];

if (newContributorKey) {
const contributors = {
contributorsByPath.set(normalizePath, {
contributors: {
...oldContributors?.contributors,
...newContributor,
};

if (!oldContributors?.contributors?.[newContributorKey]) {
contributorsByPath.set(normalizePath, {
contributors,
hasIncludes,
});
} else if (
isEarlier(
newContributor[newContributorKey]?.date,
oldContributors?.contributors[newContributorKey]?.date,
)
) {
contributorsByPath.set(normalizePath, {
contributors,
hasIncludes,
});
}
}
},
hasIncludes,
});
});
}

Expand Down
14 changes: 4 additions & 10 deletions tests/integrations/services/metadataContributors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os from 'os';
import {readFileSync} from 'fs';
import {normalize} from 'path';
import {metadataBorder} from '../../../src/constants';
Expand Down Expand Up @@ -86,8 +85,6 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {
[contributorFirst.email]: contributorFirst,
[contributorSecond.email]: contributorSecond,
};
const expectedAuthorString: string = replaceDoubleToSingleQuotes(
JSON.stringify(contributorFirst));
const expectedContributorsArray: Contributor[] = Object.values(expectedContributors);
const expectedContributorsString: string =
replaceDoubleToSingleQuotes(JSON.stringify(expectedContributorsArray));
Expand All @@ -102,7 +99,7 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {

const splitedFiledContent = fileContent.split(metadataBorder);
splitedFiledContent[1] =
`${splitedFiledContent[1]}${os.EOL}author: ${expectedAuthorString}${os.EOL}contributors: ${expectedContributorsString}${сarriage}`;
`${splitedFiledContent[1]}contributors: ${expectedContributorsString}${сarriage}`;
const expectedFileContent = splitedFiledContent.join(metadataBorder);
expect(updatedFileContent).toEqual(expectedFileContent);
});
Expand All @@ -119,8 +116,6 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {
const expectedContributors: Contributors = {
[contributorFirst.email]: contributorFirst,
};
const expectedAuthorString: string = replaceDoubleToSingleQuotes(
JSON.stringify(contributorFirst));
const expectedContributorsArray: Contributor[] = Object.values(expectedContributors);
const expectedContributorsString: string =
replaceDoubleToSingleQuotes(JSON.stringify(expectedContributorsArray));
Expand All @@ -135,7 +130,7 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {

const splitedFiledContent = fileContent.split(metadataBorder);
splitedFiledContent[1] =
`${splitedFiledContent[1]}${os.EOL}author: ${expectedAuthorString}${os.EOL}contributors: ${expectedContributorsString}${сarriage}`;
`${splitedFiledContent[1]}contributors: ${expectedContributorsString}${сarriage}`;
const expectedFileContent = splitedFiledContent.join(metadataBorder);
expect(updatedFileContent).toEqual(expectedFileContent);
});
Expand Down Expand Up @@ -200,8 +195,7 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {
`and includes files and ${item.title}`, async () => {
const expectedContributorsString: string = replaceDoubleToSingleQuotes(
JSON.stringify(item.expectedContributorsArray));
const expectedAuthorString: string = replaceDoubleToSingleQuotes(
JSON.stringify(contributorFirst));

metaDataOptions.vcsConnector.getContributorsByPath = (path: string) => Promise.resolve({
contributors: getFileContributors(path),
hasIncludes: item.getHasIncludes(path),
Expand All @@ -213,7 +207,7 @@ describe('getContentWithUpdatedMetadata (Contributors)', () => {

const splitedFiledContent = fileContent.split(metadataBorder);
splitedFiledContent[1] =
`${splitedFiledContent[1]}${os.EOL}author: ${expectedAuthorString}${os.EOL}contributors: ${expectedContributorsString}${сarriage}`;
`${splitedFiledContent[1]}contributors: ${expectedContributorsString}${сarriage}`;
const expectedFileContent = splitedFiledContent.join(metadataBorder);
expect(updatedFileContent).toEqual(expectedFileContent);
});
Expand Down
15 changes: 3 additions & 12 deletions tests/units/services/authors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ describe('updateAuthorMetadataString', () => {
test('when "defaultMetadata" is empty', async () => {
const expectedMetadata = '';

const authorDetails = await updateAuthorMetadataString(
expectedMetadata,
defaultVCSConnector,
);
const authorDetails = await updateAuthorMetadataString(expectedMetadata, defaultVCSConnector);

expect(authorDetails).toEqual(expectedMetadata);
});
Expand All @@ -114,10 +111,7 @@ describe('updateAuthorMetadataString', () => {

defaultVCSConnector.getUserByLogin = () => Promise.resolve(null);

const updatedMetadata = await updateAuthorMetadataString(
expectedMetadata,
defaultVCSConnector,
);
const updatedMetadata = await updateAuthorMetadataString(expectedMetadata, defaultVCSConnector);

expect(updatedMetadata).toEqual(expectedMetadata);
});
Expand All @@ -131,10 +125,7 @@ describe('updateAuthorMetadataString', () => {
const authorDetails = units.replaceDoubleToSingleQuotes(JSON.stringify(author));
defaultVCSConnector.getUserByLogin = () => Promise.resolve(author);

const updatedMetadata = await updateAuthorMetadataString(
defaultMetadata,
defaultVCSConnector,
);
const updatedMetadata = await updateAuthorMetadataString(defaultMetadata, defaultVCSConnector);

const matchAuthor = defaultMetadata.match(REGEXP_AUTHOR);
const expectedMetadata = defaultMetadata.replace(matchAuthor[0], authorDetails);
Expand Down

0 comments on commit 7c807ce

Please sign in to comment.