|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Messages, Org } from '@salesforce/core'; |
| 18 | +import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
| 19 | +import { addPipelineStage, AddPipelineStageResult } from '../../../../utils/addPipelineStage.js'; |
| 20 | +import { fetchPipelineStages } from '../../../../utils/pipelineUtils.js'; |
| 21 | +import { PipelineStageRecord } from '../../../../utils/types.js'; |
| 22 | + |
| 23 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 24 | +const messages = Messages.loadMessages('@salesforce/plugin-devops-center', 'devops.pipeline.stage.add'); |
| 25 | +const commonErrorMessages = Messages.loadMessages('@salesforce/plugin-devops-center', 'commonErrors'); |
| 26 | + |
| 27 | +export default class DevopsPipelineStageAdd extends SfCommand<AddPipelineStageResult> { |
| 28 | + public static readonly summary = messages.getMessage('summary'); |
| 29 | + public static readonly description = messages.getMessage('description'); |
| 30 | + public static readonly examples = messages.getMessages('examples'); |
| 31 | + |
| 32 | + public static readonly flags = { |
| 33 | + 'target-org': Flags.requiredOrg({ |
| 34 | + char: 'o', |
| 35 | + summary: messages.getMessage('flags.target-org.summary'), |
| 36 | + required: true, |
| 37 | + }), |
| 38 | + 'api-version': Flags.orgApiVersion(), |
| 39 | + 'pipeline-id': Flags.salesforceId({ |
| 40 | + summary: messages.getMessage('flags.pipeline-id.summary'), |
| 41 | + required: true, |
| 42 | + char: undefined, |
| 43 | + }), |
| 44 | + name: Flags.string({ |
| 45 | + summary: messages.getMessage('flags.name.summary'), |
| 46 | + char: 'n', |
| 47 | + required: true, |
| 48 | + }), |
| 49 | + 'next-stage-id': Flags.salesforceId({ |
| 50 | + summary: messages.getMessage('flags.next-stage-id.summary'), |
| 51 | + required: true, |
| 52 | + char: undefined, |
| 53 | + }), |
| 54 | + }; |
| 55 | + |
| 56 | + public async run(): Promise<AddPipelineStageResult> { |
| 57 | + const { flags } = await this.parse(DevopsPipelineStageAdd); |
| 58 | + const org: Org = flags['target-org']; |
| 59 | + const connection = org.getConnection(flags['api-version']); |
| 60 | + const pipelineId = flags['pipeline-id']; |
| 61 | + const nextStageId = flags['next-stage-id']; |
| 62 | + |
| 63 | + let stages: PipelineStageRecord[]; |
| 64 | + try { |
| 65 | + stages = await fetchPipelineStages(connection, pipelineId); |
| 66 | + } catch (error: unknown) { |
| 67 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 68 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 69 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 70 | + } |
| 71 | + throw error; |
| 72 | + } |
| 73 | + |
| 74 | + if (!stages.some((s) => s.Id === nextStageId)) { |
| 75 | + this.error(messages.getMessage('error.StageNotFound', [nextStageId, pipelineId])); |
| 76 | + } |
| 77 | + |
| 78 | + let result: AddPipelineStageResult; |
| 79 | + try { |
| 80 | + result = await addPipelineStage({ |
| 81 | + connection, |
| 82 | + pipelineId, |
| 83 | + name: flags['name'], |
| 84 | + nextStageId, |
| 85 | + }); |
| 86 | + } catch (error: unknown) { |
| 87 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 88 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 89 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 90 | + } |
| 91 | + throw error; |
| 92 | + } |
| 93 | + |
| 94 | + if (result.success) { |
| 95 | + this.log(`Successfully added stage "${result.name ?? ''}" to the pipeline.`); |
| 96 | + this.log(` Stage ID: ${result.stageId ?? ''}`); |
| 97 | + this.log(` Position: before "${nextStageId}"`); |
| 98 | + this.log(` Pipeline ID: ${pipelineId}`); |
| 99 | + } else { |
| 100 | + this.error(`Failed to add stage: ${result.error ?? ''}`); |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | +} |
0 commit comments