Skip to content

Commit

Permalink
refactor: codeOwners use readLocalFile
Browse files Browse the repository at this point in the history
The current branch should be drived off the targetBranch anyway
  • Loading branch information
rarkins committed Jul 4, 2020
1 parent 74c5a16 commit 538522d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 11 additions & 9 deletions lib/workers/pr/code-owners.spec.ts
@@ -1,9 +1,9 @@
import { mock } from 'jest-mock-extended';
import { git, platform } from '../../../test/util';
import { fs, platform } from '../../../test/util';
import { Pr } from '../../platform';
import { codeOwnersForPr } from './code-owners';

jest.mock('../../util/git');
jest.mock('../../util/fs');

describe('workers/pr/code-owners', () => {
describe('codeOwnersForPr', () => {
Expand All @@ -13,21 +13,21 @@ describe('workers/pr/code-owners', () => {
pr = mock<Pr>();
});
it('returns global code owner', async () => {
git.getFile.mockResolvedValueOnce(['* @jimmy'].join('\n'));
fs.readLocalFile.mockResolvedValueOnce(['* @jimmy'].join('\n'));
platform.getPrFiles.mockResolvedValueOnce(['README.md']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@jimmy']);
});
it('returns more specific code owners', async () => {
git.getFile.mockResolvedValueOnce(
fs.readLocalFile.mockResolvedValueOnce(
['* @jimmy', 'package.json @john @maria'].join('\n')
);
platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@john', '@maria']);
});
it('ignores comments and leading/trailing whitespace', async () => {
git.getFile.mockResolvedValueOnce(
fs.readLocalFile.mockResolvedValueOnce(
[
'# comment line',
' \t ',
Expand All @@ -41,19 +41,21 @@ describe('workers/pr/code-owners', () => {
expect(codeOwners).toEqual(['@john', '@maria']);
});
it('returns empty array when no code owners set', async () => {
git.getFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce(null);
platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]);
});
it('returns empty array when no code owners match', async () => {
git.getFile.mockResolvedValueOnce(['package-lock.json @mike'].join('\n'));
fs.readLocalFile.mockResolvedValueOnce(
['package-lock.json @mike'].join('\n')
);
platform.getPrFiles.mockResolvedValueOnce(['yarn.lock']);
const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]);
});
it('returns empty array when error occurs', async () => {
git.getFile.mockImplementationOnce((_, __) => {
fs.readLocalFile.mockImplementationOnce((_, __) => {
throw new Error();
});
const codeOwners = await codeOwnersForPr(pr);
Expand All @@ -67,7 +69,7 @@ describe('workers/pr/code-owners', () => {
];
codeOwnerFilePaths.forEach((codeOwnerFilePath) => {
it(`detects code owner file at '${codeOwnerFilePath}'`, async () => {
git.getFile.mockImplementation((path, _) => {
fs.readLocalFile.mockImplementation((path, _) => {
if (path === codeOwnerFilePath) {
return Promise.resolve(['* @mike'].join('\n'));
}
Expand Down
10 changes: 5 additions & 5 deletions lib/workers/pr/code-owners.ts
@@ -1,15 +1,15 @@
import ignore from 'ignore';
import { logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { getFile } from '../../util/git';
import { readLocalFile } from '../../util/fs';

export async function codeOwnersForPr(pr: Pr): Promise<string[]> {
try {
const codeOwnersFile =
(await getFile('CODEOWNERS', pr.targetBranch)) ||
(await getFile('.github/CODEOWNERS', pr.targetBranch)) ||
(await getFile('.gitlab/CODEOWNERS', pr.targetBranch)) ||
(await getFile('docs/CODEOWNERS', pr.targetBranch));
(await readLocalFile('CODEOWNERS', 'utf8')) ||
(await readLocalFile('.github/CODEOWNERS', 'utf8')) ||
(await readLocalFile('.gitlab/CODEOWNERS', 'utf8')) ||
(await readLocalFile('docs/CODEOWNERS', 'utf8'));

if (!codeOwnersFile) {
return [];
Expand Down

0 comments on commit 538522d

Please sign in to comment.