Skip to content
Snippets Groups Projects
Commit 538522d0 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor: codeOwners use readLocalFile

The current branch should be drived off the targetBranch anyway
parent 74c5a162
No related branches found
No related tags found
No related merge requests found
import { mock } from 'jest-mock-extended'; import { mock } from 'jest-mock-extended';
import { git, platform } from '../../../test/util'; import { fs, platform } from '../../../test/util';
import { Pr } from '../../platform'; import { Pr } from '../../platform';
import { codeOwnersForPr } from './code-owners'; import { codeOwnersForPr } from './code-owners';
jest.mock('../../util/git'); jest.mock('../../util/fs');
describe('workers/pr/code-owners', () => { describe('workers/pr/code-owners', () => {
describe('codeOwnersForPr', () => { describe('codeOwnersForPr', () => {
...@@ -13,13 +13,13 @@ describe('workers/pr/code-owners', () => { ...@@ -13,13 +13,13 @@ describe('workers/pr/code-owners', () => {
pr = mock<Pr>(); pr = mock<Pr>();
}); });
it('returns global code owner', async () => { it('returns global code owner', async () => {
git.getFile.mockResolvedValueOnce(['* @jimmy'].join('\n')); fs.readLocalFile.mockResolvedValueOnce(['* @jimmy'].join('\n'));
platform.getPrFiles.mockResolvedValueOnce(['README.md']); platform.getPrFiles.mockResolvedValueOnce(['README.md']);
const codeOwners = await codeOwnersForPr(pr); const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual(['@jimmy']); expect(codeOwners).toEqual(['@jimmy']);
}); });
it('returns more specific code owners', async () => { it('returns more specific code owners', async () => {
git.getFile.mockResolvedValueOnce( fs.readLocalFile.mockResolvedValueOnce(
['* @jimmy', 'package.json @john @maria'].join('\n') ['* @jimmy', 'package.json @john @maria'].join('\n')
); );
platform.getPrFiles.mockResolvedValueOnce(['package.json']); platform.getPrFiles.mockResolvedValueOnce(['package.json']);
...@@ -27,7 +27,7 @@ describe('workers/pr/code-owners', () => { ...@@ -27,7 +27,7 @@ describe('workers/pr/code-owners', () => {
expect(codeOwners).toEqual(['@john', '@maria']); expect(codeOwners).toEqual(['@john', '@maria']);
}); });
it('ignores comments and leading/trailing whitespace', async () => { it('ignores comments and leading/trailing whitespace', async () => {
git.getFile.mockResolvedValueOnce( fs.readLocalFile.mockResolvedValueOnce(
[ [
'# comment line', '# comment line',
' \t ', ' \t ',
...@@ -41,19 +41,21 @@ describe('workers/pr/code-owners', () => { ...@@ -41,19 +41,21 @@ describe('workers/pr/code-owners', () => {
expect(codeOwners).toEqual(['@john', '@maria']); expect(codeOwners).toEqual(['@john', '@maria']);
}); });
it('returns empty array when no code owners set', async () => { it('returns empty array when no code owners set', async () => {
git.getFile.mockResolvedValueOnce(null); fs.readLocalFile.mockResolvedValueOnce(null);
platform.getPrFiles.mockResolvedValueOnce(['package.json']); platform.getPrFiles.mockResolvedValueOnce(['package.json']);
const codeOwners = await codeOwnersForPr(pr); const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]); expect(codeOwners).toEqual([]);
}); });
it('returns empty array when no code owners match', async () => { 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']); platform.getPrFiles.mockResolvedValueOnce(['yarn.lock']);
const codeOwners = await codeOwnersForPr(pr); const codeOwners = await codeOwnersForPr(pr);
expect(codeOwners).toEqual([]); expect(codeOwners).toEqual([]);
}); });
it('returns empty array when error occurs', async () => { it('returns empty array when error occurs', async () => {
git.getFile.mockImplementationOnce((_, __) => { fs.readLocalFile.mockImplementationOnce((_, __) => {
throw new Error(); throw new Error();
}); });
const codeOwners = await codeOwnersForPr(pr); const codeOwners = await codeOwnersForPr(pr);
...@@ -67,7 +69,7 @@ describe('workers/pr/code-owners', () => { ...@@ -67,7 +69,7 @@ describe('workers/pr/code-owners', () => {
]; ];
codeOwnerFilePaths.forEach((codeOwnerFilePath) => { codeOwnerFilePaths.forEach((codeOwnerFilePath) => {
it(`detects code owner file at '${codeOwnerFilePath}'`, async () => { it(`detects code owner file at '${codeOwnerFilePath}'`, async () => {
git.getFile.mockImplementation((path, _) => { fs.readLocalFile.mockImplementation((path, _) => {
if (path === codeOwnerFilePath) { if (path === codeOwnerFilePath) {
return Promise.resolve(['* @mike'].join('\n')); return Promise.resolve(['* @mike'].join('\n'));
} }
......
import ignore from 'ignore'; import ignore from 'ignore';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { Pr, platform } from '../../platform'; import { Pr, platform } from '../../platform';
import { getFile } from '../../util/git'; import { readLocalFile } from '../../util/fs';
export async function codeOwnersForPr(pr: Pr): Promise<string[]> { export async function codeOwnersForPr(pr: Pr): Promise<string[]> {
try { try {
const codeOwnersFile = const codeOwnersFile =
(await getFile('CODEOWNERS', pr.targetBranch)) || (await readLocalFile('CODEOWNERS', 'utf8')) ||
(await getFile('.github/CODEOWNERS', pr.targetBranch)) || (await readLocalFile('.github/CODEOWNERS', 'utf8')) ||
(await getFile('.gitlab/CODEOWNERS', pr.targetBranch)) || (await readLocalFile('.gitlab/CODEOWNERS', 'utf8')) ||
(await getFile('docs/CODEOWNERS', pr.targetBranch)); (await readLocalFile('docs/CODEOWNERS', 'utf8'));
if (!codeOwnersFile) { if (!codeOwnersFile) {
return []; return [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment