Skip to content

Commit 7507add

Browse files
committed
add option to inject codeowners
1 parent 4c44917 commit 7507add

File tree

4 files changed

+25
-52
lines changed

4 files changed

+25
-52
lines changed

__fixtures__/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { jest } from '@jest/globals';
44
export const debug = jest.fn<typeof core.debug>();
55
export const error = jest.fn<typeof core.error>();
66
export const info = jest.fn<typeof core.info>();
7+
export const notice = jest.fn<typeof core.notice>();
78
export const getInput = jest.fn<typeof core.getInput>();
89
export const setOutput = jest.fn<typeof core.setOutput>();
910
export const setFailed = jest.fn<typeof core.setFailed>();
1011
export const warning = jest.fn<typeof core.warning>();
12+
export const startGroup = jest.fn<typeof core.startGroup>();
13+
export const endGroup = jest.fn<typeof core.endGroup>();
14+
export const isDebug = jest.fn<typeof core.isDebug>();

__tests__/main.test.ts

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,22 @@
55
* functions and objects. For example, the core module is mocked in this test,
66
* so that the actual '@actions/core' module is not imported.
77
*/
8-
import { jest } from '@jest/globals'
9-
import * as core from '../__fixtures__/core.js'
10-
import { wait } from '../__fixtures__/wait.js'
8+
import { jest } from '@jest/globals';
9+
import * as core from '../__fixtures__/core.js';
1110

1211
// Mocks should be declared before the module being tested is imported.
13-
jest.unstable_mockModule('@actions/core', () => core)
14-
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))
12+
jest.unstable_mockModule('@actions/core', () => core);
1513

1614
// The module being tested should be imported dynamically. This ensures that the
1715
// mocks are used in place of any actual dependencies.
18-
const { run } = await import('../src/main.js')
19-
20-
describe('main.ts', () => {
21-
beforeEach(() => {
22-
// Set the action's inputs as return values from core.getInput().
23-
core.getInput.mockImplementation(() => '500')
24-
25-
// Mock the wait function so that it does not actually wait.
26-
wait.mockImplementation(() => Promise.resolve('done!'))
27-
})
28-
29-
afterEach(() => {
30-
jest.resetAllMocks()
31-
})
32-
33-
it('Sets the time output', async () => {
34-
await run()
35-
36-
// Verify the time output was set.
37-
expect(core.setOutput).toHaveBeenNthCalledWith(
38-
1,
39-
'time',
40-
// Simple regex to match a time string in the format HH:MM:SS.
41-
expect.stringMatching(/^\d{2}:\d{2}:\d{2}/)
42-
)
43-
})
44-
45-
it('Sets a failed status', async () => {
46-
// Clear the getInput mock and return an invalid value.
47-
core.getInput.mockClear().mockReturnValueOnce('this is not a number')
48-
49-
// Clear the wait mock and return a rejected promise.
50-
wait
51-
.mockClear()
52-
.mockRejectedValueOnce(new Error('milliseconds is not a number'))
53-
54-
await run()
55-
56-
// Verify that the action was marked as failed.
57-
expect(core.setFailed).toHaveBeenNthCalledWith(
58-
1,
59-
'milliseconds is not a number'
60-
)
61-
})
62-
})
16+
const { runAction } = await import('../src/main.js');
17+
18+
test('run action', async () => {
19+
core.isDebug.mockReturnValue(false);
20+
return runAction({
21+
'include-gitignore': false,
22+
'ignore-default': false,
23+
files: '',
24+
allRulesMustHit: false,
25+
});
26+
});

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ inputs:
2525
description: Wether all rules must hit for the action to pass. Do not use when using files input to check only modified files.
2626
required: false
2727
default: 'false'
28+
codeownersContent:
29+
description: The content of the CODEOWNERS file. If not provided, the action will look for a CODEOWNERS file in the repository.
30+
required: false
2831

2932
# Define your outputs here.
3033
outputs:

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Input {
88
'ignore-default': boolean;
99
files: string;
1010
allRulesMustHit: boolean;
11+
codeownersContent?: string;
1112
}
1213

1314
function getInputs(): Input {
@@ -16,6 +17,7 @@ function getInputs(): Input {
1617
result['ignore-default'] = getBoolInput('ignore-default');
1718
result.allRulesMustHit = getBoolInput('allRulesMustHit');
1819
result.files = core.getInput('files');
20+
result.codeownersContent = core.getInput('codeownersContent');
1921
return result;
2022
}
2123

@@ -27,7 +29,7 @@ export const runAction = async (input: Input): Promise<void> => {
2729
let filesToCheck: string[] = [];
2830
core.startGroup(`Loading files to check.`);
2931
if (input.files) {
30-
filesToCheck = input.files.split(' ');
32+
filesToCheck = input.files.split(' ').map((file) => file.startsWith('/') ? file : `/${file}`);
3133
} else {
3234
filesToCheck = await (await glob.create('*')).glob();
3335
if (input['include-gitignore'] === true) {
@@ -56,7 +58,7 @@ export const runAction = async (input: Input): Promise<void> => {
5658
core.endGroup();
5759

5860
core.startGroup('Parsing CODEOWNERS File');
59-
const codeownerContent = getCodeownerContent();
61+
const codeownerContent = input.codeownersContent || getCodeownerContent();
6062
let parsedCodeowners = parseCodeowners(codeownerContent);
6163
if (input['ignore-default'] === true) {
6264
parsedCodeowners = parsedCodeowners.filter((rule) => rule.pattern !== '*');

0 commit comments

Comments
 (0)