|
| 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 { |
| 20 | + createPullRequest, |
| 21 | + CreatePullRequestResult, |
| 22 | + fetchWorkItemDetail, |
| 23 | + resolveGitHubToken, |
| 24 | + WorkItemDetail, |
| 25 | +} from '../../../utils/createPullRequest.js'; |
| 26 | + |
| 27 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 28 | +const messages = Messages.loadMessages('@salesforce/plugin-devops-center', 'devops.pull-request.create'); |
| 29 | +const commonErrorMessages = Messages.loadMessages('@salesforce/plugin-devops-center', 'commonErrors'); |
| 30 | + |
| 31 | +export default class DevopsPullRequestCreate extends SfCommand<CreatePullRequestResult> { |
| 32 | + public static readonly summary = messages.getMessage('summary'); |
| 33 | + public static readonly description = messages.getMessage('description'); |
| 34 | + public static readonly examples = messages.getMessages('examples'); |
| 35 | + |
| 36 | + public static readonly flags = { |
| 37 | + 'target-org': Flags.requiredOrg({ |
| 38 | + char: 'o', |
| 39 | + summary: messages.getMessage('flags.target-org.summary'), |
| 40 | + required: true, |
| 41 | + }), |
| 42 | + 'api-version': Flags.orgApiVersion(), |
| 43 | + 'work-item-name': Flags.string({ |
| 44 | + summary: messages.getMessage('flags.work-item-name.summary'), |
| 45 | + char: 'n', |
| 46 | + exactlyOne: ['work-item-name', 'work-item-id'], |
| 47 | + }), |
| 48 | + 'work-item-id': Flags.salesforceId({ |
| 49 | + summary: messages.getMessage('flags.work-item-id.summary'), |
| 50 | + char: 'w', |
| 51 | + exactlyOne: ['work-item-name', 'work-item-id'], |
| 52 | + }), |
| 53 | + title: Flags.string({ |
| 54 | + summary: messages.getMessage('flags.title.summary'), |
| 55 | + }), |
| 56 | + body: Flags.string({ |
| 57 | + summary: messages.getMessage('flags.body.summary'), |
| 58 | + }), |
| 59 | + }; |
| 60 | + |
| 61 | + public async run(): Promise<CreatePullRequestResult> { |
| 62 | + const { flags } = await this.parse(DevopsPullRequestCreate); |
| 63 | + const org: Org = flags['target-org']; |
| 64 | + const connection = org.getConnection(flags['api-version']); |
| 65 | + |
| 66 | + const filter = flags['work-item-name'] ? { name: flags['work-item-name'] } : { id: flags['work-item-id']! }; |
| 67 | + |
| 68 | + let detail: WorkItemDetail; |
| 69 | + try { |
| 70 | + detail = await fetchWorkItemDetail(connection, filter); |
| 71 | + } catch (error: unknown) { |
| 72 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 73 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 74 | + this.error(commonErrorMessages.getMessage('error.DevopsCenterNotEnabled')); |
| 75 | + } |
| 76 | + throw error; |
| 77 | + } |
| 78 | + |
| 79 | + if (!detail.branchName) { |
| 80 | + this.error(messages.getMessage('error.NoBranch', [detail.workItemName])); |
| 81 | + } |
| 82 | + if (!detail.repoOwner || !detail.repoName) { |
| 83 | + this.error(messages.getMessage('error.NoRepo', [detail.workItemName])); |
| 84 | + } |
| 85 | + if (!detail.targetBranch) { |
| 86 | + this.error(messages.getMessage('error.NoTargetBranch', [detail.workItemName])); |
| 87 | + } |
| 88 | + if (!detail.provider) { |
| 89 | + this.error(messages.getMessage('error.NoProvider', [detail.workItemName])); |
| 90 | + } |
| 91 | + |
| 92 | + let token: string | undefined; |
| 93 | + if (detail.provider === 'github') { |
| 94 | + token = await resolveGitHubToken(); |
| 95 | + } else { |
| 96 | + token = process.env.BITBUCKET_TOKEN; |
| 97 | + } |
| 98 | + if (!token) { |
| 99 | + this.error(messages.getMessage('error.NoToken', [detail.provider])); |
| 100 | + } |
| 101 | + |
| 102 | + const prTitle = flags['title'] ?? detail.subject; |
| 103 | + |
| 104 | + const result = await createPullRequest({ |
| 105 | + owner: detail.repoOwner, |
| 106 | + repo: detail.repoName, |
| 107 | + head: detail.branchName, |
| 108 | + base: detail.targetBranch, |
| 109 | + title: prTitle, |
| 110 | + body: flags['body'], |
| 111 | + provider: detail.provider, |
| 112 | + token, |
| 113 | + }); |
| 114 | + |
| 115 | + if (result.success) { |
| 116 | + this.log(`Successfully created pull request for ${detail.workItemName}.`); |
| 117 | + this.log(` Title: ${result.title ?? prTitle}`); |
| 118 | + this.log(` URL: ${result.url ?? ''}`); |
| 119 | + this.log(` Source: ${detail.workItemName} → ${result.targetBranch ?? detail.targetBranch}`); |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | +} |
0 commit comments