Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'cherryPick.test.js' and 'authorChecklist.test.js' workflow tests to TypeScript #38467

Merged
merged 13 commits into from
Mar 26, 2024
Merged
14 changes: 14 additions & 0 deletions src/types/modules/act.d.ts
Original file line number Diff line number Diff line change
@@ -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<ActStepIdentifier, 'name' | 'id' | 'run' | 'mockWith'>;
Copy link
Contributor

@blazejkustra blazejkustra May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StepIdentifier becomes any because Omit<ActStepIdentifier, 'name' | 'id' | 'run' | 'mockWith'> is of type any too. Could you give some context why ActStepIdentifier was used here? @VickyStash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blazejkustra see the explanation there, I've reused the approach of that PR to keep it consistent at that moment

}
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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();
Expand Down Expand Up @@ -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, {
Expand All @@ -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, {
Expand All @@ -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, {
Expand All @@ -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, {
Expand All @@ -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, {
Expand All @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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();
Expand All @@ -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(
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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(
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion workflow_tests/mocks/cherryPickMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
23 changes: 7 additions & 16 deletions workflow_tests/utils/JobMocker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {StepIdentifier} from '@kie/act-js';
import type {PathOrFileDescriptor} from 'fs';
import fs from 'fs';
import path from 'path';
Expand All @@ -11,26 +12,16 @@ type YamlWorkflow = {
};

type MockJob = {
steps: MockJobStep[];
steps: StepIdentifier[];
uses?: string;
secrets?: string[];
with?: string;
outputs?: string[];
runsOn?: string;
outputs?: Record<string, string>;
runsOn: string;
};

type MockJobs = Record<string, MockJob>;

type MockJobStep = {
id?: string;
name: string;
run?: string;
mockWith?: string;
with?: string;
envs?: string[];
inputs?: string[];
};

class JobMocker {
workflowFile: string;

Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -114,4 +105,4 @@ class JobMocker {
}

export default JobMocker;
export type {MockJob, MockJobs, YamlWorkflow, YamlMockJob, MockJobStep};
export type {MockJob, MockJobs, YamlWorkflow, YamlMockJob};
Loading
Loading