Skip to content

Commit

Permalink
fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
gagoar committed Aug 24, 2020
1 parent de263fd commit b74191a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
5 changes: 5 additions & 0 deletions __mocks__/package5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "otherPackage",
"contributors": [{ "name": "Other Friend", "email": "[email protected]" }],
"author": { "name": "George the Builder", "email": "[email protected]" }
}
63 changes: 55 additions & 8 deletions __tests__/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Generate', () => {
`);
});

it('should generate a CODEOWNERS FILE with package.maintainers field using cosmiconfig', async () => {
it('should generate a CODEOWNERS FILE with package.contributors and package.author field using cosmiconfig', async () => {
search.mockImplementationOnce(() =>
Promise.resolve({
isEmpty: false,
Expand All @@ -137,15 +137,10 @@ describe('Generate', () => {
'dir2/dir1/package.json': '../__mocks__/package2.json',
'dir6/package.json': '../__mocks__/package3.json',
'dir7/package.json': '../__mocks__/package4.json',
'dir8/package.json': '../__mocks__/package5.json',
};

sync.mockReturnValueOnce([
...Object.keys(packageFiles),
'dir5/package.json',
'dir2/dir1/package.json',
'dir6/package.json',
'dir7/package.json',
]);
sync.mockReturnValueOnce(Object.keys(packageFiles));

sync.mockReturnValueOnce(['.gitignore']);
const withAddedPackageFiles = { ...packageFiles, ...withGitIgnore };
Expand All @@ -165,6 +160,58 @@ describe('Generate', () => {
dir5/ [email protected] [email protected]
# Rule extracted from dir2/dir1/package.json
dir2/dir1/ [email protected] [email protected]
# Rule extracted from dir8/package.json
dir8/ [email protected] [email protected]
# Rule extracted from dir1/CODEOWNERS
dir1/*.ts @eeny @meeny
# Rule extracted from dir1/CODEOWNERS
dir1/README.md @miny
# Rule extracted from dir2/CODEOWNERS
dir2/*.ts @moe
# Rule extracted from dir2/CODEOWNERS
dir2/dir3/*.ts @miny
# Rule extracted from dir2/dir3/CODEOWNERS
dir2/dir3/*.ts @miny
#################################### Generated content - do not edit! ####################################"
`);
});
it('should generate a CODEOWNERS FILE with package.maintainers field using cosmiconfig', async () => {
search.mockImplementationOnce(() =>
Promise.resolve({
isEmpty: false,
filepath: '/some/package.json',
config: {
output: 'CODEOWNERS',
useMaintainers: true,
},
})
);

const packageFiles = {
...files,
'dir5/package.json': '../__mocks__/package1.json',
'dir2/dir1/package.json': '../__mocks__/package2.json',
'dir6/package.json': '../__mocks__/package3.json',
'dir7/package.json': '../__mocks__/package4.json',
};

sync.mockReturnValueOnce(Object.keys(packageFiles));

sync.mockReturnValueOnce(['.gitignore']);
const withAddedPackageFiles = { ...packageFiles, ...withGitIgnore };
readFile.mockImplementation((file: keyof typeof withAddedPackageFiles, callback: Callback) => {
const content = readFileSync(path.join(__dirname, withAddedPackageFiles[file]));
callback(null, content);
});

await generateCommand({ parent: {} });
expect(search).toHaveBeenCalled();
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
"#################################### Generated content - do not edit! ####################################
# This block has been generated with codeowners-generator (for more information https://github.com/gagoar/codeowners-generator/README.md)
# To re-generate, run \`npm run codeowners-generator generate\`. Don't worry, the content outside this block will be kept.
# Rule extracted from dir5/package.json
dir5/ [email protected] [email protected]
# Rule extracted from dir2/dir1/package.json
Expand Down
Binary file added images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions src/utils/codeowners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ export const loadCodeOwnerFiles = async (dirname: string, files: string[]): Prom
interface PACKAGE {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
maintainers: any[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
contributors: any[];
author?: Record<string, string>;
}

const getOwnersFromMaintainerField = (filePath: string, content: string): ownerRule => {
try {
const { maintainers = [] } = JSON.parse(content) as PACKAGE;
const { maintainers = [], contributors = [], author } = JSON.parse(content) as PACKAGE;

const packageOwners = [...maintainers, ...contributors];

if (author) {
packageOwners.unshift(author);
}

let owners = [] as string[];
if (maintainers.length) {
owners = maintainers.reduce((memo, maintainer) => {
if (packageOwners.length) {
owners = packageOwners.reduce((memo, maintainer) => {
if (isString(maintainer)) {
const matches = maintainer.match(MAINTAINERS_EMAIL_PATTERN);
if (matches?.length) return [...memo, matches[1]];
Expand Down

0 comments on commit b74191a

Please sign in to comment.