diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts index 444d6efe9b..d97bf10543 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts @@ -60,7 +60,7 @@ import { scaffolderActionRules } from '../../service/rules'; import { createCounterMetric, createHistogramMetric } from '../../util/metrics'; import { BackstageLoggerTransport, WinstonLogger } from './logger'; import { convertFiltersToRecord } from '../../util/templating'; -import { CheckpointState } from './StorageTaskBroker'; +import { CheckpointState } from '@backstage/plugin-scaffolder-node'; type NunjucksWorkflowRunnerOptions = { workingDirectory: string; diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/StorageTaskBroker.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/StorageTaskBroker.ts index a92d4134f5..9a872e7b3f 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/StorageTaskBroker.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/StorageTaskBroker.ts @@ -33,36 +33,17 @@ import { TaskStatus, } from '@backstage/plugin-scaffolder-node'; import { WorkspaceProvider } from '@backstage/plugin-scaffolder-node/alpha'; -import { - JsonObject, - JsonValue, - Observable, - createDeferred, -} from '@backstage/types'; +import { JsonObject, Observable, createDeferred } from '@backstage/types'; import ObservableImpl from 'zen-observable'; import { DefaultWorkspaceService, WorkspaceService } from './WorkspaceService'; import { readDuration } from './helper'; import { InternalTaskSecrets, TaskStore } from './types'; - -type CheckpointStatus = 'failed' | 'success'; - -export type CheckpointSuccessState = { - status: Extract; - value: JsonValue; -}; - -export type CheckpointFailedState = { - status: Extract; - reason: string; -}; - -export type CheckpointState = { - [key: string]: CheckpointSuccessState | CheckpointFailedState; -}; - -export type UpdateCheckpointOptions = { - key: string; -} & CheckpointState[keyof CheckpointState]; +import { + CheckpointState, + CheckpointSuccessState, + CheckpointFailedState, + UpdateCheckpointOptions, +} from '@backstage/plugin-scaffolder-node'; type TaskState = { checkpoints: CheckpointState; diff --git a/plugins/scaffolder-node/src/actions/types.ts b/plugins/scaffolder-node/src/actions/types.ts index f13d256332..e7e678450d 100644 --- a/plugins/scaffolder-node/src/actions/types.ts +++ b/plugins/scaffolder-node/src/actions/types.ts @@ -23,6 +23,8 @@ import { BackstageCredentials, LoggerService, } from '@backstage/backend-plugin-api'; +import { CheckpointOptions } from '../checkpoints'; + /** * ActionContext is passed into scaffolder actions. * @public @@ -36,10 +38,9 @@ export type ActionContext< secrets?: TaskSecrets; workspacePath: string; input: TActionInput; - checkpoint(opts: { - key: string; - fn: () => Promise | T; - }): Promise; + checkpoint( + opts: CheckpointOptions, + ): Promise; output( name: keyof TActionOutput, value: TActionOutput[keyof TActionOutput], diff --git a/plugins/scaffolder-node/src/checkpoints/index.ts b/plugins/scaffolder-node/src/checkpoints/index.ts new file mode 100644 index 0000000000..5d542de408 --- /dev/null +++ b/plugins/scaffolder-node/src/checkpoints/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './types'; diff --git a/plugins/scaffolder-node/src/checkpoints/types.ts b/plugins/scaffolder-node/src/checkpoints/types.ts new file mode 100644 index 0000000000..eac3213075 --- /dev/null +++ b/plugins/scaffolder-node/src/checkpoints/types.ts @@ -0,0 +1,78 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { JsonValue } from '@backstage/types'; + +/** + * The status of a checkpoint, indicating whether it succeeded or failed. + * + * @public + */ +export type CheckpointStatus = 'failed' | 'success'; + +/** + * Represents a successful checkpoint state with a value. + * + * @public + */ +export type CheckpointSuccessState = { + status: Extract; + value: JsonValue; +}; + +/** + * Represents a failed checkpoint state with a reason for failure. + * + * @public + */ +export type CheckpointFailedState = { + status: Extract; + reason: string; +}; + +/** + * A map of checkpoint keys to their states. + * + * @public + */ +export type CheckpointState = { + [key: string]: CheckpointSuccessState | CheckpointFailedState; +}; + +/** + * Options for updating a checkpoint in a task. + * + * @public + */ +export type UpdateCheckpointOptions = { + key: string; +} & CheckpointState[keyof CheckpointState]; + +/** + * Options for checkpoint function invocation. + * + * @public + */ +export type CheckpointOptions = { + /** + * Unique key for the checkpoint + */ + key: string; + /** + * Function to execute for the checkpoint + */ + fn: () => Promise | T; +}; diff --git a/plugins/scaffolder-node/src/index.ts b/plugins/scaffolder-node/src/index.ts index 5d49ee874c..d44f0e9c50 100644 --- a/plugins/scaffolder-node/src/index.ts +++ b/plugins/scaffolder-node/src/index.ts @@ -24,3 +24,4 @@ export * from './actions'; export * from './tasks'; export * from './files'; export * from './types'; +export * from './checkpoints'; diff --git a/plugins/scaffolder-node/src/tasks/types.ts b/plugins/scaffolder-node/src/tasks/types.ts index d8cfd24530..cc9023db8b 100644 --- a/plugins/scaffolder-node/src/tasks/types.ts +++ b/plugins/scaffolder-node/src/tasks/types.ts @@ -16,7 +16,8 @@ import { BackstageCredentials } from '@backstage/backend-plugin-api'; import { TaskSpec } from '@backstage/plugin-scaffolder-common'; -import { JsonObject, JsonValue, Observable } from '@backstage/types'; +import { JsonObject, Observable } from '@backstage/types'; +import { UpdateCheckpointOptions } from '../checkpoints'; /** * TaskSecrets @@ -129,19 +130,7 @@ export interface TaskContext { | undefined >; - updateCheckpoint?( - options: - | { - key: string; - status: 'success'; - value: JsonValue; - } - | { - key: string; - status: 'failed'; - reason: string; - }, - ): Promise; + updateCheckpoint?(options: UpdateCheckpointOptions): Promise; serializeWorkspace?(options: { path: string }): Promise;