|
| 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 { detectConflicts, fetchBranches, DetectConflictsResult } from '../../../utils/detectConflicts.js'; |
| 20 | +import { fetchWorkItemDetail } from '../../../utils/createPullRequest.js'; |
| 21 | + |
| 22 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 23 | +const messages = Messages.loadMessages('@salesforce/plugin-devops-center', 'devops.conflict.detect'); |
| 24 | +const commonErrorMessages = Messages.loadMessages('@salesforce/plugin-devops-center', 'commonErrors'); |
| 25 | + |
| 26 | +export default class DevopsConflictDetect extends SfCommand<DetectConflictsResult> { |
| 27 | + public static readonly summary = messages.getMessage('summary'); |
| 28 | + public static readonly description = messages.getMessage('description'); |
| 29 | + public static readonly examples = messages.getMessages('examples'); |
| 30 | + |
| 31 | + public static readonly flags = { |
| 32 | + 'target-org': Flags.requiredOrg(), |
| 33 | + 'api-version': Flags.orgApiVersion(), |
| 34 | + 'work-item-id': Flags.salesforceId({ |
| 35 | + summary: messages.getMessage('flags.work-item-id.summary'), |
| 36 | + required: true, |
| 37 | + char: 'w', |
| 38 | + }), |
| 39 | + 'repo-path': Flags.directory({ |
| 40 | + summary: messages.getMessage('flags.repo-path.summary'), |
| 41 | + char: 'p', |
| 42 | + required: true, |
| 43 | + exists: true, |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + public async run(): Promise<DetectConflictsResult> { |
| 48 | + const { flags } = await this.parse(DevopsConflictDetect); |
| 49 | + const org: Org = flags['target-org']; |
| 50 | + const connection = org.getConnection(flags['api-version']); |
| 51 | + const workItemId = flags['work-item-id']; |
| 52 | + const repoPath = flags['repo-path']; |
| 53 | + |
| 54 | + const detail = await this.fetchWorkItem(connection, workItemId); |
| 55 | + |
| 56 | + if (!detail.branchName) { |
| 57 | + this.error(messages.getMessage('error.NoBranch', [detail.workItemName])); |
| 58 | + } |
| 59 | + if (!detail.targetBranch) { |
| 60 | + this.error(messages.getMessage('error.NoTargetBranch', [detail.workItemName])); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + fetchBranches(repoPath, detail.branchName, detail.targetBranch); |
| 65 | + } catch (error: unknown) { |
| 66 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 67 | + this.error(messages.getMessage('error.GitFetchFailed', [errMsg])); |
| 68 | + } |
| 69 | + |
| 70 | + const conflicts = detectConflicts({ |
| 71 | + repoPath, |
| 72 | + sourceBranch: detail.branchName, |
| 73 | + targetBranch: detail.targetBranch, |
| 74 | + }); |
| 75 | + |
| 76 | + const result: DetectConflictsResult = { |
| 77 | + workItemId, |
| 78 | + workItemName: detail.workItemName, |
| 79 | + sourceBranch: detail.branchName, |
| 80 | + targetBranch: detail.targetBranch, |
| 81 | + hasConflicts: conflicts.length > 0, |
| 82 | + conflicts, |
| 83 | + }; |
| 84 | + |
| 85 | + this.printOutput(result); |
| 86 | + |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + private async fetchWorkItem( |
| 91 | + connection: Parameters<typeof fetchWorkItemDetail>[0], |
| 92 | + workItemId: string |
| 93 | + ): ReturnType<typeof fetchWorkItemDetail> { |
| 94 | + try { |
| 95 | + return await fetchWorkItemDetail(connection, { id: workItemId }); |
| 96 | + } catch (error: unknown) { |
| 97 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 98 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 99 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 100 | + } |
| 101 | + if (errMsg.includes('not found')) { |
| 102 | + this.error(messages.getMessage('error.WorkItemNotFound', [workItemId])); |
| 103 | + } |
| 104 | + throw error; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private printOutput(result: DetectConflictsResult): void { |
| 109 | + if (result.hasConflicts) { |
| 110 | + this.styledHeader(`Conflicts Detected for ${result.workItemName}`); |
| 111 | + this.log(`Source Branch: ${result.sourceBranch}`); |
| 112 | + this.log(`Target Branch: ${result.targetBranch}`); |
| 113 | + this.table({ |
| 114 | + data: result.conflicts.map((c) => ({ |
| 115 | + 'File Path': c.filePath, |
| 116 | + 'Conflict Type': c.conflictType, |
| 117 | + })), |
| 118 | + columns: ['File Path', 'Conflict Type'], |
| 119 | + }); |
| 120 | + this.log(messages.getMessage('info.ConflictsFound', [result.conflicts.length])); |
| 121 | + } else { |
| 122 | + this.log(messages.getMessage('info.NoConflicts', [result.workItemName])); |
| 123 | + this.log(` Source Branch: ${result.sourceBranch}`); |
| 124 | + this.log(` Target Branch: ${result.targetBranch}`); |
| 125 | + this.log(' Ready to promote.'); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments