Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit e6941c1

Browse files
authored
feat: stage jobs observe and update their source (#252)
1 parent 9ee765f commit e6941c1

1 file changed

Lines changed: 100 additions & 2 deletions

File tree

src/state/Store.tsx

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
Config,
3+
Job,
34
parameters,
45
workflow,
5-
Workflow,
66
} from '@circleci/circleci-config-sdk';
77
import { PipelineParameterLiteral } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Parameters/types/CustomParameterLiterals.types';
88
import { WorkflowJobAbstract } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Workflow';
@@ -21,7 +21,6 @@ import { store } from '../App';
2121
import { ConfirmationModalModel } from '../components/containers/ConfirmationModal';
2222
import DefinitionsMenu from '../components/menus/definitions/DefinitionsMenu';
2323
import { OrbImportWithMeta } from '../components/menus/definitions/OrbDefinitionsMenu';
24-
import { JobMapping } from '../mappings/components/JobMapping';
2524
import {
2625
setWorkflowDefinition,
2726
WorkflowStage,
@@ -187,6 +186,7 @@ export type StoreActions = AllDefinitionActions & {
187186
removeWorkflowElement: Action<StoreModel, string>;
188187
updateWorkflowElement: Action<StoreModel, { id: string; data: any }>;
189188
setWorkflowElements: Action<StoreModel, Elements<any>>;
189+
observeWorkflowSources: ThunkOn<StoreActions, UpdateType<Job>>;
190190

191191
importOrb: Action<StoreModel, OrbImportWithMeta>;
192192
unimportOrb: Action<StoreModel, OrbImportWithMeta>;
@@ -514,7 +514,105 @@ const Actions: StoreActions = {
514514
value: new WorkflowStage(wf.name, wf.id, jobs, wf.when, elements),
515515
});
516516
}),
517+
observeWorkflowSources: thunkOn(
518+
(actions) => actions.update_jobs,
519+
(actions, thunk) => {
520+
const state = store.getState();
521+
const payload = thunk.payload;
522+
523+
Object.values(state.definitions.workflows).forEach((workflowDef) => {
524+
const wf = workflowDef.value;
525+
const change = payload as unknown as UpdateType<Job>;
526+
const oldName = change.old.name;
527+
const newName = change.new.name;
528+
const changedName = oldName !== newName;
529+
530+
const elements = wf.elements.map((element) => {
531+
if (element.type === 'jobs' && element.data.job.name === oldName) {
532+
const wfJob = element.data as workflow.WorkflowJob;
533+
534+
return {
535+
...element,
536+
data: new workflow.WorkflowJob(
537+
change.new,
538+
wfJob.parameters,
539+
wfJob.pre_steps,
540+
wfJob.post_steps,
541+
),
542+
id: newName,
543+
};
544+
} else if (element.type === 'requires' && changedName) {
545+
const connection = element as Connection;
546+
547+
if (connection.source === oldName) {
548+
return {
549+
...element,
550+
source: newName,
551+
sourceHandle: `${newName}_source`,
552+
};
553+
} else if (connection.target === oldName) {
554+
return {
555+
...element,
556+
target: newName,
557+
targetHandle: `${newName}_target`,
558+
};
559+
}
560+
}
561+
562+
return element;
563+
});
564+
565+
// TODO: optimize this
566+
const jobs = wf.jobs.map((staged) => {
567+
if (
568+
staged.name === oldName &&
569+
staged instanceof workflow.WorkflowJob
570+
) {
571+
return new workflow.WorkflowJob(
572+
change.new,
573+
staged.parameters,
574+
staged.pre_steps,
575+
staged.post_steps,
576+
);
577+
}
578+
579+
if (staged.parameters?.requires && changedName) {
580+
const requires = staged.parameters.requires.map((req) => {
581+
if (req === oldName) {
582+
return newName;
583+
} else {
584+
return req;
585+
}
586+
});
517587

588+
if (staged instanceof workflow.WorkflowJob) {
589+
return new workflow.WorkflowJob(
590+
staged.job,
591+
{
592+
...staged.parameters,
593+
requires,
594+
},
595+
staged.pre_steps,
596+
staged.post_steps,
597+
);
598+
}
599+
600+
return new workflow.WorkflowJobApproval(staged.name, {
601+
...staged.parameters,
602+
requires,
603+
});
604+
}
605+
606+
return staged;
607+
});
608+
609+
actions.update_workflows({
610+
old: wf,
611+
new: new WorkflowStage(wf.name, wf.id, jobs, wf.when, elements),
612+
});
613+
});
614+
},
615+
),
518616
importOrb: action((state, payload) => {
519617
const orb = state.definitions.orbs[payload.name];
520618
if (!orb) {

0 commit comments

Comments
 (0)