feat: migrate to ESM and update dependencies (#949)

* feat: migrate to ESM and update dependencies

- Migrated the project to ESM to support the latest `@actions/*` package versions.
- Updated dependencies to their latest versions, including `@actions/core`, `@actions/github`, and others.
- Replaced `.prettierrc.js` with `.prettierrc.json` for configuration.
- Updated ESLint configuration to use ESM and added new rules.
- Refactored test files to use `jest` with ESM imports.
- Removed deprecated files and updated import paths to use `.js` extensions.
- Added new files for improved configuration and removed unnecessary mocks.
- Updated README to reflect breaking changes in version 7.

* docs: update README to clarify ESM migration details and adjust TypeScript configuration
This commit is contained in:
Chiranjib Swain
2026-07-16 11:04:03 -05:00
committed by GitHub
parent b8dd2d9be0
commit bf12e9b00b
60 changed files with 15081 additions and 16281 deletions
+36 -19
View File
@@ -1,14 +1,31 @@
import {
getBranchName,
checkAnyBranch,
checkAllBranch,
toBranchMatchConfig,
BranchMatchConfig
} from '../src/branch';
import * as github from '@actions/github';
import {jest, describe, beforeEach, it, expect} from '@jest/globals';
import type {BranchMatchConfig} from '../src/branch.js';
jest.mock('@actions/core');
jest.mock('@actions/github');
jest.unstable_mockModule('@actions/core', () => ({
debug: jest.fn(),
info: jest.fn(),
warning: jest.fn(),
error: jest.fn()
}));
const mockGithubContext = {
payload: {
pull_request: {
number: 123,
head: {ref: 'head-branch-name'},
base: {ref: 'base-branch-name'}
}
},
repo: {owner: 'monalisa', repo: 'helloworld'}
} as any;
jest.unstable_mockModule('@actions/github', () => ({
context: mockGithubContext,
getOctokit: jest.fn()
}));
const {getBranchName, checkAnyBranch, checkAllBranch, toBranchMatchConfig} =
await import('../src/branch.js');
describe('getBranchName', () => {
describe('when the pull requests base branch is requested', () => {
@@ -28,10 +45,10 @@ describe('getBranchName', () => {
describe('checkAllBranch', () => {
beforeEach(() => {
github.context.payload.pull_request!.head = {
mockGithubContext.payload.pull_request.head = {
ref: 'test/feature/123'
};
github.context.payload.pull_request!.base = {
mockGithubContext.payload.pull_request.base = {
ref: 'main'
};
});
@@ -87,10 +104,10 @@ describe('checkAllBranch', () => {
describe('checkAnyBranch', () => {
beforeEach(() => {
github.context.payload.pull_request!.head = {
mockGithubContext.payload.pull_request.head = {
ref: 'test/feature/123'
};
github.context.payload.pull_request!.base = {
mockGithubContext.payload.pull_request.base = {
ref: 'main'
};
});
@@ -159,7 +176,7 @@ describe('toBranchMatchConfig', () => {
it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toEqual<BranchMatchConfig>({
expect(result).toEqual({
headBranch: ['testing']
});
});
@@ -169,7 +186,7 @@ describe('toBranchMatchConfig', () => {
it('sets headBranch in the matchConfig', () => {
const result = toBranchMatchConfig(stringConfig);
expect(result).toEqual<BranchMatchConfig>({
expect(result).toEqual({
headBranch: ['testing']
});
});
@@ -180,7 +197,7 @@ describe('toBranchMatchConfig', () => {
const config = {'base-branch': ['testing']};
it('sets baseBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toEqual<BranchMatchConfig>({
expect(result).toEqual({
baseBranch: ['testing']
});
});
@@ -190,7 +207,7 @@ describe('toBranchMatchConfig', () => {
it('sets baseBranch in the matchConfig', () => {
const result = toBranchMatchConfig(stringConfig);
expect(result).toEqual<BranchMatchConfig>({
expect(result).toEqual({
baseBranch: ['testing']
});
});
@@ -201,7 +218,7 @@ describe('toBranchMatchConfig', () => {
const config = {'base-branch': ['testing'], 'head-branch': ['testing']};
it('sets headBranch and baseBranch in the matchConfig', () => {
const result = toBranchMatchConfig(config);
expect(result).toEqual<BranchMatchConfig>({
expect(result).toEqual({
baseBranch: ['testing'],
headBranch: ['testing']
});
+25 -9
View File
@@ -1,5 +1,24 @@
import {
ChangedFilesMatchConfig,
import {jest, describe, it, expect} from '@jest/globals';
import type {ChangedFilesMatchConfig} from '../src/changedFiles.js';
jest.unstable_mockModule('@actions/core', () => ({
debug: jest.fn(),
info: jest.fn(),
warning: jest.fn(),
error: jest.fn()
}));
jest.unstable_mockModule('@actions/github', () => ({
context: {
payload: {
pull_request: {number: 123, head: {ref: 'head'}, base: {ref: 'base'}}
},
repo: {owner: 'monalisa', repo: 'helloworld'}
},
getOctokit: jest.fn()
}));
const {
checkAllChangedFiles,
checkAnyChangedFiles,
toChangedFilesMatchConfig,
@@ -7,10 +26,7 @@ import {
checkIfAllGlobsMatchAnyFile,
checkIfAnyGlobMatchesAllFiles,
checkIfAllGlobsMatchAllFiles
} from '../src/changedFiles';
jest.mock('@actions/core');
jest.mock('@actions/github');
} = await import('../src/changedFiles.js');
describe('checkAllChangedFiles', () => {
const changedFiles = ['foo.txt', 'bar.txt'];
@@ -92,7 +108,7 @@ describe('toChangedFilesMatchConfig', () => {
it('returns an empty object', () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({});
expect(result).toEqual({});
});
});
@@ -129,7 +145,7 @@ describe('toChangedFilesMatchConfig', () => {
it('sets the value in the config object', () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
expect(result).toEqual({
changedFiles: [{anyGlobToAnyFile: ['testing']}]
});
});
@@ -140,7 +156,7 @@ describe('toChangedFilesMatchConfig', () => {
it(`sets the string as an array in the config object`, () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
expect(result).toEqual({
changedFiles: [{anyGlobToAnyFile: ['testing']}]
});
});
+58 -19
View File
@@ -1,26 +1,65 @@
import {jest, describe, it, expect, beforeEach, beforeAll} from '@jest/globals';
import * as yaml from 'js-yaml';
import * as core from '@actions/core';
import * as api from '../src/api';
import {labeler} from '../src/labeler';
import * as github from '@actions/github';
import * as fs from 'fs';
import {checkMatchConfigs} from '../src/labeler';
import {
import type {
MatchConfig,
BaseMatchConfig
} from '../src/api/get-label-configs.js';
// Define API mock functions at module level
const getPullRequestsMock = jest.fn<any>();
const getLabelConfigsMock = jest.fn<any>();
const setLabelsMock = jest.fn<any>();
const getChangedFilesMock = jest.fn<any>();
const getContentMock = jest.fn<any>();
jest.unstable_mockModule('@actions/core', () => ({
getInput: jest.fn(),
getMultilineInput: jest.fn(),
getBooleanInput: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
error: jest.fn(),
warning: jest.fn(),
info: jest.fn(),
debug: jest.fn()
}));
jest.unstable_mockModule('@actions/github', () => ({
context: {
payload: {
pull_request: {
number: 123,
head: {ref: 'head-branch'},
base: {ref: 'base-branch'}
}
},
repo: {owner: 'monalisa', repo: 'helloworld'}
},
getOctokit: jest.fn()
}));
jest.unstable_mockModule('../src/api/index.js', () => ({
getPullRequests: getPullRequestsMock,
getLabelConfigs: getLabelConfigsMock,
setLabels: setLabelsMock,
getChangedFiles: getChangedFilesMock,
getContent: getContentMock
}));
const core = await import('@actions/core');
const github = await import('@actions/github');
const api = await import('../src/api/index.js');
const {labeler, checkMatchConfigs} = await import('../src/labeler.js');
const {
toMatchConfig,
getLabelConfigMapFromObject,
getLabelConfigResultFromObject,
BaseMatchConfig,
configUsesChangedFiles
} from '../src/api/get-label-configs';
jest.mock('@actions/core');
jest.mock('../src/api');
} = await import('../src/api/get-label-configs.js');
beforeAll(() => {
jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
return jest.requireActual('@actions/core').getInput(name, options);
});
(core.getInput as jest.Mock).mockImplementation(() => undefined);
});
const loadYaml = (filepath: string) => {
@@ -422,14 +461,14 @@ describe('labeler error handling', () => {
jest.resetAllMocks();
(github.getOctokit as jest.Mock).mockReturnValue(mockClient);
(api.getPullRequests as jest.Mock).mockReturnValue([
getPullRequestsMock.mockReturnValue([
{
...mockPullRequest,
data: {labels: [{name: 'old-label'}]}
}
]);
(api.getLabelConfigs as jest.Mock).mockResolvedValue({
getLabelConfigsMock.mockResolvedValue({
labelConfigs: new Map([['new-label', ['dummy-config']]]),
changedFilesLimit: undefined
});
@@ -439,7 +478,7 @@ describe('labeler error handling', () => {
});
it('throws a custom error for HttpError 403 with "unauthorized" message', async () => {
(api.setLabels as jest.Mock).mockRejectedValue({
setLabelsMock.mockRejectedValue({
name: 'HttpError',
status: 403,
message: 'Request failed with status code 403: Unauthorized'
@@ -456,7 +495,7 @@ describe('labeler error handling', () => {
status: 404,
message: 'Not Found'
};
(api.setLabels as jest.Mock).mockRejectedValue(unexpectedError);
setLabelsMock.mockRejectedValue(unexpectedError);
// NOTE: In the current implementation, labeler rethrows the raw error object (not an Error instance).
// `rejects.toThrow` only works with real Error objects, so here we must use `rejects.toEqual`.
@@ -469,7 +508,7 @@ describe('labeler error handling', () => {
name: 'HttpError',
message: 'Resource not accessible by integration'
};
(api.setLabels as jest.Mock).mockRejectedValue(error);
setLabelsMock.mockRejectedValue(error);
await labeler();
+101 -42
View File
@@ -1,23 +1,75 @@
import {run} from '../src/labeler';
import * as github from '@actions/github';
import * as core from '@actions/core';
import {
jest,
describe,
it,
test,
expect,
afterAll,
beforeEach
} from '@jest/globals';
import path from 'path';
import {fileURLToPath} from 'url';
import fs from 'fs';
jest.mock('@actions/core');
jest.mock('@actions/github');
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Define mock functions before mocking modules
const setLabelsMock = jest.fn<any>();
const reposMock = jest.fn<any>();
const paginateMock = jest.fn<any>();
const getPullMock = jest.fn<any>().mockResolvedValue({data: {labels: []}});
const listFilesMergeMock = jest.fn<any>().mockReturnValue({});
const coreGetInputMock = jest.fn<any>();
const coreGetMultilineInputMock = jest.fn<any>();
const coreGetBooleanInputMock = jest.fn<any>();
const coreErrorMock = jest.fn<any>();
const coreWarningMock = jest.fn<any>();
const coreSetFailedMock = jest.fn<any>();
const setOutputSpy = jest.fn<any>();
const mockGithubContext: any = {
payload: {
pull_request: {
number: 123,
head: {ref: 'head-branch-name'},
base: {ref: 'base-branch-name'}
}
},
repo: {owner: 'monalisa', repo: 'helloworld'}
};
jest.unstable_mockModule('@actions/core', () => ({
getInput: coreGetInputMock,
getMultilineInput: coreGetMultilineInputMock,
getBooleanInput: coreGetBooleanInputMock,
setOutput: setOutputSpy,
setFailed: coreSetFailedMock,
error: coreErrorMock,
warning: coreWarningMock,
info: jest.fn(),
debug: jest.fn()
}));
jest.unstable_mockModule('@actions/github', () => ({
context: mockGithubContext,
getOctokit: jest.fn(() => ({
rest: {
issues: {setLabels: setLabelsMock},
repos: {getContent: reposMock},
pulls: {
get: getPullMock,
listFiles: {endpoint: {merge: listFilesMergeMock}}
}
},
paginate: paginateMock
}))
}));
const {run} = await import('../src/labeler.js');
const gh = github.getOctokit('_');
const setLabelsMock = jest.spyOn(gh.rest.issues, 'setLabels');
const reposMock = jest.spyOn(gh.rest.repos, 'getContent');
const paginateMock = jest.spyOn(gh, 'paginate');
const getPullMock = jest.spyOn(gh.rest.pulls, 'get');
const readFileSyncMock = jest.spyOn(fs, 'readFileSync');
const existsSyncMock = jest.spyOn(fs, 'existsSync');
const coreErrorMock = jest.spyOn(core, 'error');
const coreWarningMock = jest.spyOn(core, 'warning');
const coreSetFailedMock = jest.spyOn(core, 'setFailed');
const setOutputSpy = jest.spyOn(core, 'setOutput');
class HttpError extends Error {
constructor(message: string) {
@@ -59,20 +111,27 @@ const configureInput = (
'pr-number': string[];
}>
) => {
jest
.spyOn(core, 'getInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);
jest
.spyOn(core, 'getMultilineInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);
jest
.spyOn(core, 'getBooleanInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);
coreGetInputMock.mockImplementation(
(name: unknown) => mockInput[name as keyof typeof mockInput]
);
coreGetMultilineInputMock.mockImplementation(
(name: unknown) => mockInput[name as keyof typeof mockInput]
);
coreGetBooleanInputMock.mockImplementation(
(name: unknown) => mockInput[name as keyof typeof mockInput]
);
};
afterAll(() => jest.restoreAllMocks());
afterAll(async () => {
jest.restoreAllMocks();
});
describe('run', () => {
beforeEach(() => {
getPullMock.mockResolvedValue({data: {labels: []}} as any);
mockGithubContext.payload.pull_request.head = {ref: 'head-branch-name'};
mockGithubContext.payload.pull_request.base = {ref: 'base-branch-name'};
});
it('(with dot: false) adds labels to PRs that match our glob patterns', async () => {
configureInput({});
usingLabelerConfigYaml('only_pdfs.yml');
@@ -169,7 +228,7 @@ describe('run', () => {
it('adds labels based on the branch names that match the regexp pattern', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'test/testing-time'};
mockGithubContext.payload.pull_request.head = {ref: 'test/testing-time'};
usingLabelerConfigYaml('branches.yml');
await run();
@@ -187,7 +246,7 @@ describe('run', () => {
it('adds multiple labels based on branch names that match different regexp patterns', async () => {
configureInput({});
github.context.payload.pull_request!.head = {
mockGithubContext.payload.pull_request.head = {
ref: 'test/feature/123'
};
usingLabelerConfigYaml('branches.yml');
@@ -213,7 +272,7 @@ describe('run', () => {
it('can support multiple branches by batching', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'fix/123'};
mockGithubContext.payload.pull_request.head = {ref: 'fix/123'};
usingLabelerConfigYaml('branches.yml');
await run();
@@ -231,7 +290,7 @@ describe('run', () => {
it('can support multiple branches by providing an array', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'array/123'};
mockGithubContext.payload.pull_request.head = {ref: 'array/123'};
usingLabelerConfigYaml('branches.yml');
await run();
@@ -644,7 +703,7 @@ describe('run', () => {
describe('changed-files-labels-limit', () => {
it('applies all labels when count is within limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('limit_3.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -667,7 +726,7 @@ describe('run', () => {
it('skips changed-files labels when count exceeds limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('limit_2.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -686,7 +745,7 @@ describe('run', () => {
it('still applies branch-based labels when changed-files limit is exceeded', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'test/some-feature'};
mockGithubContext.payload.pull_request.head = {ref: 'test/some-feature'};
usingLabelerConfigYaml('limit_1.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -711,7 +770,7 @@ describe('run', () => {
it('applies all labels when no limit is set', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('mixed_labels.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -736,7 +795,7 @@ describe('run', () => {
it('does not count preexisting labels toward the limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('limit_2.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -763,7 +822,7 @@ describe('run', () => {
it('skips new labels when new count exceeds limit even with preexisting', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('limit_2.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -784,7 +843,7 @@ describe('run', () => {
it('applies labels when new count equals the limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('limit_2.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -807,7 +866,7 @@ describe('run', () => {
it('skips all changed-files labels when limit is 0', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'test/some-feature'};
mockGithubContext.payload.pull_request.head = {ref: 'test/some-feature'};
usingLabelerConfigYaml('limit_0.yml');
mockGitHubResponseChangedFiles('components/a/file.ts');
getPullMock.mockResolvedValue(<any>{
@@ -831,7 +890,7 @@ describe('run', () => {
// a "changed-files label" and subject to the limit, even if it matches
// via the branch rule
configureInput({});
github.context.payload.pull_request!.head = {ref: 'test/some-feature'};
mockGithubContext.payload.pull_request.head = {ref: 'test/some-feature'};
usingLabelerConfigYaml('mixed_rules.yml');
mockGitHubResponseChangedFiles('unrelated/file.ts');
getPullMock.mockResolvedValue(<any>{
@@ -856,7 +915,7 @@ describe('run', () => {
describe('max-files-changed', () => {
it('applies labels when changed files count is within limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('max_files_5.yml');
mockGitHubResponseChangedFiles(
'components/a/file.ts',
@@ -880,7 +939,7 @@ describe('run', () => {
it('skips file-based labels when changed files exceed limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('max_files_5.yml');
mockGitHubResponseChangedFiles(
'components/a/file1.ts',
@@ -902,7 +961,7 @@ describe('run', () => {
it('applies labels when changed files count equals limit', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('max_files_5.yml');
mockGitHubResponseChangedFiles(
'components/a/file1.ts',
@@ -928,7 +987,7 @@ describe('run', () => {
it('still applies branch-based labels when max-files-changed is exceeded', async () => {
configureInput({});
github.context.payload.pull_request!.head = {ref: 'test/some-feature'};
mockGithubContext.payload.pull_request.head = {ref: 'test/some-feature'};
usingLabelerConfigYaml('max_files_with_branch.yml');
mockGitHubResponseChangedFiles(
'components/a/file1.ts',
@@ -954,7 +1013,7 @@ describe('run', () => {
it('preserves preexisting changed-files labels with sync-labels when max-files-changed is exceeded', async () => {
configureInput({'sync-labels': true});
github.context.payload.pull_request!.head = {ref: 'main'};
mockGithubContext.payload.pull_request.head = {ref: 'main'};
usingLabelerConfigYaml('max_files_5.yml');
mockGitHubResponseChangedFiles(
'unrelated/file1.ts',