|
| 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 { attachProject, AttachProjectResult, findExistingAttachment } from '../../../utils/attachProject.js'; |
| 20 | + |
| 21 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 22 | +const messages = Messages.loadMessages('@salesforce/plugin-devops-center', 'devops.pipeline.attach-project'); |
| 23 | +const commonErrorMessages = Messages.loadMessages('@salesforce/plugin-devops-center', 'commonErrors'); |
| 24 | + |
| 25 | +export default class DevopsPipelineAttachProject extends SfCommand<AttachProjectResult> { |
| 26 | + public static readonly summary = messages.getMessage('summary'); |
| 27 | + public static readonly description = messages.getMessage('description'); |
| 28 | + public static readonly examples = messages.getMessages('examples'); |
| 29 | + |
| 30 | + public static readonly flags = { |
| 31 | + 'target-org': Flags.requiredOrg({ |
| 32 | + char: 'o', |
| 33 | + summary: messages.getMessage('flags.target-org.summary'), |
| 34 | + required: true, |
| 35 | + }), |
| 36 | + 'api-version': Flags.orgApiVersion(), |
| 37 | + 'pipeline-id': Flags.string({ |
| 38 | + summary: messages.getMessage('flags.pipeline-id.summary'), |
| 39 | + required: true, |
| 40 | + }), |
| 41 | + 'project-id': Flags.string({ |
| 42 | + summary: messages.getMessage('flags.project-id.summary'), |
| 43 | + required: true, |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + public async run(): Promise<AttachProjectResult> { |
| 48 | + const { flags } = await this.parse(DevopsPipelineAttachProject); |
| 49 | + const org: Org = flags['target-org']; |
| 50 | + const connection = org.getConnection(flags['api-version']); |
| 51 | + const projectId = flags['project-id']; |
| 52 | + const pipelineId = flags['pipeline-id']; |
| 53 | + |
| 54 | + let existingPipelineId: string | undefined; |
| 55 | + try { |
| 56 | + existingPipelineId = await findExistingAttachment(connection, projectId); |
| 57 | + } catch (error: unknown) { |
| 58 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 59 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 60 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 61 | + } |
| 62 | + throw error; |
| 63 | + } |
| 64 | + |
| 65 | + if (existingPipelineId) { |
| 66 | + this.error(messages.getMessage('error.AlreadyAttached', [projectId, existingPipelineId])); |
| 67 | + } |
| 68 | + |
| 69 | + let result: AttachProjectResult; |
| 70 | + try { |
| 71 | + result = await attachProject({ connection, projectId, pipelineId }); |
| 72 | + } catch (error: unknown) { |
| 73 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 74 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 75 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 76 | + } |
| 77 | + throw error; |
| 78 | + } |
| 79 | + |
| 80 | + if (result.success) { |
| 81 | + this.log('Successfully attached project to pipeline.'); |
| 82 | + this.log(` Project ID: ${projectId}`); |
| 83 | + this.log(` Pipeline ID: ${pipelineId}`); |
| 84 | + } else { |
| 85 | + this.error(`Failed to attach project to pipeline: ${result.error ?? ''}`); |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | + } |
| 90 | +} |
0 commit comments