diff --git a/src/types/modules/act.d.ts b/src/types/modules/act.d.ts new file mode 100644 index 000000000000..5fe00ec479cf --- /dev/null +++ b/src/types/modules/act.d.ts @@ -0,0 +1,14 @@ +import type {StepIdentifier as ActStepIdentifier} from '@kie/act-js'; + +declare module '@kie/act-js' { + // eslint-disable-next-line rulesdir/no-inline-named-export + export declare type StepIdentifier = { + id?: string; + name: string; + run?: string; + mockWith?: string; + with?: string; + envs?: string[]; + inputs?: string[]; + } & Omit; +} diff --git a/workflow_tests/authorChecklist.test.js b/workflow_tests/authorChecklist.test.ts similarity index 87% rename from workflow_tests/authorChecklist.test.js rename to workflow_tests/authorChecklist.test.ts index 536ef929ca02..c0b1c7cf7533 100644 --- a/workflow_tests/authorChecklist.test.js +++ b/workflow_tests/authorChecklist.test.ts @@ -1,13 +1,17 @@ -const path = require('path'); -const kieMockGithub = require('@kie/mock-github'); -const utils = require('./utils/utils'); -const assertions = require('./assertions/authorChecklistAssertions'); -const mocks = require('./mocks/authorChecklistMocks'); -const ExtendedAct = require('./utils/ExtendedAct').default; +import type {MockStep} from '@kie/act-js/build/src/step-mocker/step-mocker.types'; +import * as kieMockGithub from '@kie/mock-github'; +import type {CreateRepositoryFile, MockGithub} from '@kie/mock-github'; +import path from 'path'; +import assertions from './assertions/authorChecklistAssertions'; +import mocks from './mocks/authorChecklistMocks'; +import ExtendedAct from './utils/ExtendedAct'; +import * as utils from './utils/utils'; jest.setTimeout(90 * 1000); -let mockGithub; -const FILES_TO_COPY_INTO_TEST_REPO = [ + +let mockGithub: MockGithub; + +const FILES_TO_COPY_INTO_TEST_REPO: CreateRepositoryFile[] = [ ...utils.deepCopy(utils.FILES_TO_COPY_INTO_TEST_REPO), { src: path.resolve(__dirname, '..', '.github', 'workflows', 'authorChecklist.yml'), @@ -18,7 +22,7 @@ const FILES_TO_COPY_INTO_TEST_REPO = [ describe('test workflow authorChecklist', () => { const githubToken = 'dummy_github_token'; - beforeAll(async () => { + beforeAll(() => { // in case of the tests being interrupted without cleanup the mock repo directory may be left behind // which breaks the next test run, this removes any possible leftovers utils.removeMockRepoDir(); @@ -47,11 +51,11 @@ describe('test workflow authorChecklist', () => { }; describe('actor is not OSBotify', () => { it('executes workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { @@ -66,11 +70,11 @@ describe('test workflow authorChecklist', () => { }); describe('actor is OSBotify', () => { it('does not execute workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { @@ -91,11 +95,11 @@ describe('test workflow authorChecklist', () => { }; describe('actor is not OSBotify', () => { it('executes workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { @@ -110,11 +114,11 @@ describe('test workflow authorChecklist', () => { }); describe('actor is OSBotify', () => { it('does not execute workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { @@ -135,11 +139,11 @@ describe('test workflow authorChecklist', () => { }; describe('actor is not OSBotify', () => { it('executes workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { @@ -154,11 +158,11 @@ describe('test workflow authorChecklist', () => { }); describe('actor is OSBotify', () => { it('does not execute workflow', async () => { - const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testAuthorChecklistWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'authorChecklist.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams(act, event, eventOptions, {}, githubToken); - const testMockSteps = { + const testMockSteps: MockStep = { checklist: mocks.AUTHORCHECKLIST__CHECKLIST__STEP_MOCKS, }; const result = await act.runEvent(event, { diff --git a/workflow_tests/cherryPick.test.js b/workflow_tests/cherryPick.test.ts similarity index 92% rename from workflow_tests/cherryPick.test.js rename to workflow_tests/cherryPick.test.ts index beae7f03b3ef..47a1c489df70 100644 --- a/workflow_tests/cherryPick.test.js +++ b/workflow_tests/cherryPick.test.ts @@ -1,13 +1,18 @@ -const path = require('path'); -const kieMockGithub = require('@kie/mock-github'); -const utils = require('./utils/utils'); -const assertions = require('./assertions/cherryPickAssertions'); -const mocks = require('./mocks/cherryPickMocks'); -const ExtendedAct = require('./utils/ExtendedAct').default; +import type {MockStep} from '@kie/act-js/build/src/step-mocker/step-mocker.types'; +import * as kieMockGithub from '@kie/mock-github'; +import type {CreateRepositoryFile, MockGithub} from '@kie/mock-github'; +import path from 'path'; +import assertions from './assertions/cherryPickAssertions'; +import mocks from './mocks/cherryPickMocks'; +import ExtendedAct from './utils/ExtendedAct'; +import type {MockJobs} from './utils/JobMocker'; +import * as utils from './utils/utils'; jest.setTimeout(90 * 1000); -let mockGithub; -const FILES_TO_COPY_INTO_TEST_REPO = [ + +let mockGithub: MockGithub; + +const FILES_TO_COPY_INTO_TEST_REPO: CreateRepositoryFile[] = [ ...utils.deepCopy(utils.FILES_TO_COPY_INTO_TEST_REPO), { src: path.resolve(__dirname, '..', '.github', 'workflows', 'cherryPick.yml'), @@ -16,7 +21,7 @@ const FILES_TO_COPY_INTO_TEST_REPO = [ ]; describe('test workflow cherryPick', () => { - beforeAll(async () => { + beforeAll(() => { // in case of the tests being interrupted without cleanup the mock repo directory may be left behind // which breaks the next test run, this removes any possible leftovers utils.removeMockRepoDir(); @@ -43,7 +48,7 @@ describe('test workflow cherryPick', () => { describe('actor is not deployer', () => { const actor = 'Dummy Author'; it('workflow ends after validate job', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams( @@ -61,11 +66,11 @@ describe('test workflow cherryPick', () => { PULL_REQUEST_NUMBER: '1234', }, ); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__FALSE__STEP_MOCKS, cherryPick: mocks.getCherryPickMockSteps(true, false), }; - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -93,14 +98,14 @@ describe('test workflow cherryPick', () => { const mergeConflicts = false; const versionsMatch = true; it('behaviour is the same as with actor being the deployer', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__FALSE__STEP_MOCKS, cherryPick: mocks.getCherryPickMockSteps(versionsMatch, mergeConflicts), }; - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -145,14 +150,14 @@ describe('test workflow cherryPick', () => { describe('version match', () => { const versionsMatch = true; it('workflow executes, PR approved and merged automatically', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, }; testMockSteps.cherryPick = mocks.getCherryPickMockSteps(versionsMatch, mergeConflicts); - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -193,14 +198,14 @@ describe('test workflow cherryPick', () => { describe('version does not match', () => { const versionsMatch = false; it('workflow executes, PR auto-assigned and commented, approved and merged automatically', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, }; testMockSteps.cherryPick = mocks.getCherryPickMockSteps(versionsMatch, mergeConflicts); - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -244,14 +249,14 @@ describe('test workflow cherryPick', () => { describe('version match', () => { const versionsMatch = true; it('workflow executes, PR auto-assigned and commented, not merged automatically', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, }; testMockSteps.cherryPick = mocks.getCherryPickMockSteps(versionsMatch, mergeConflicts); - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -292,14 +297,14 @@ describe('test workflow cherryPick', () => { describe('version does not match', () => { const versionsMatch = false; it('workflow executes, PR auto-assigned and commented, not merged automatically', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, }; testMockSteps.cherryPick = mocks.getCherryPickMockSteps(versionsMatch, mergeConflicts); - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { @@ -343,7 +348,7 @@ describe('test workflow cherryPick', () => { describe('automatic trigger', () => { const event = 'pull_request'; it('workflow does not execute', async () => { - const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') || ''; + const repoPath = mockGithub.repo.getPath('testCherryPickWorkflowRepo') ?? ''; const workflowPath = path.join(repoPath, '.github', 'workflows', 'cherryPick.yml'); let act = new ExtendedAct(repoPath, workflowPath); act = utils.setUpActParams( @@ -361,11 +366,11 @@ describe('test workflow cherryPick', () => { PULL_REQUEST_NUMBER: '1234', }, ); - const testMockSteps = { + const testMockSteps: MockStep = { validateActor: mocks.CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, cherryPick: mocks.getCherryPickMockSteps(true, false), }; - const testMockJobs = { + const testMockJobs: MockJobs = { createNewVersion: { steps: mocks.CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, outputs: { diff --git a/workflow_tests/mocks/cherryPickMocks.ts b/workflow_tests/mocks/cherryPickMocks.ts index e7762731c066..c1a33be868d6 100644 --- a/workflow_tests/mocks/cherryPickMocks.ts +++ b/workflow_tests/mocks/cherryPickMocks.ts @@ -110,4 +110,4 @@ const getCherryPickMockSteps = (upToDate: boolean, hasConflicts: boolean): StepI CHERRYPICK__CHERRYPICK__ANNOUNCES_A_CP_FAILURE_IN_THE_ANNOUNCE_SLACK_ROOM__STEP_MOCK, ]; -export {CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, CHERRYPICK__VALIDATEACTOR__FALSE__STEP_MOCKS, CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, getCherryPickMockSteps}; +export default {CHERRYPICK__CREATENEWVERSION__STEP_MOCKS, CHERRYPICK__VALIDATEACTOR__FALSE__STEP_MOCKS, CHERRYPICK__VALIDATEACTOR__TRUE__STEP_MOCKS, getCherryPickMockSteps}; diff --git a/workflow_tests/utils/JobMocker.ts b/workflow_tests/utils/JobMocker.ts index b6dc99771dd2..dadb85014d01 100644 --- a/workflow_tests/utils/JobMocker.ts +++ b/workflow_tests/utils/JobMocker.ts @@ -1,3 +1,4 @@ +import type {StepIdentifier} from '@kie/act-js'; import type {PathOrFileDescriptor} from 'fs'; import fs from 'fs'; import path from 'path'; @@ -11,26 +12,16 @@ type YamlWorkflow = { }; type MockJob = { - steps: MockJobStep[]; + steps: StepIdentifier[]; uses?: string; secrets?: string[]; with?: string; - outputs?: string[]; - runsOn?: string; + outputs?: Record; + runsOn: string; }; type MockJobs = Record; -type MockJobStep = { - id?: string; - name: string; - run?: string; - mockWith?: string; - with?: string; - envs?: string[]; - inputs?: string[]; -}; - class JobMocker { workflowFile: string; @@ -59,8 +50,8 @@ class JobMocker { jobWith = job.with; delete job.with; } - job.steps = mockJob.steps.map((step) => { - const mockStep: MockJobStep = { + job.steps = mockJob.steps.map((step): StepIdentifier => { + const mockStep: StepIdentifier = { name: step.name, run: step.mockWith, }; @@ -114,4 +105,4 @@ class JobMocker { } export default JobMocker; -export type {MockJob, MockJobs, YamlWorkflow, YamlMockJob, MockJobStep}; +export type {MockJob, MockJobs, YamlWorkflow, YamlMockJob}; diff --git a/workflow_tests/utils/preGenerateTest.ts b/workflow_tests/utils/preGenerateTest.ts index 7698e618432d..c5e54d9a11c0 100644 --- a/workflow_tests/utils/preGenerateTest.ts +++ b/workflow_tests/utils/preGenerateTest.ts @@ -1,10 +1,11 @@ /* eslint no-console: ["error", { allow: ["warn", "log"] }] */ +import type {StepIdentifier} from '@kie/act-js'; import type {PathLike} from 'fs'; import fs from 'fs'; import path from 'path'; import {exit} from 'process'; import yaml from 'yaml'; -import type {MockJobStep, YamlMockJob, YamlWorkflow} from './JobMocker'; +import type {YamlMockJob, YamlWorkflow} from './JobMocker'; const workflowsDirectory = path.resolve(__dirname, '..', '..', '.github', 'workflows'); const workflowTestsDirectory = path.resolve(__dirname, '..'); @@ -97,7 +98,7 @@ describe('test workflow ${workflowName}', () => { }); `; -const mockStepTemplate = (stepMockName: string, step: MockJobStep, jobId: string | undefined) => ` +const mockStepTemplate = (stepMockName: string, step: StepIdentifier, jobId: string | undefined) => ` const ${stepMockName} = utils.createMockStep( '${step.name ?? ''}', '${step.name ?? ''}',