|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { Messages, Org } from '@salesforce/core'; |
| 9 | +import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
| 10 | +import { ux } from '@oclif/core'; |
| 11 | +import { fetchWorkItems } from '../../../utils/workItems'; |
| 12 | +import { WorkItem } from '../../../utils/types'; |
| 13 | + |
| 14 | +Messages.importMessagesDirectory(__dirname); |
| 15 | +const messages = Messages.loadMessages('@salesforce/plugin-devops-center', 'devops.work-item.list'); |
| 16 | + |
| 17 | +export type DevopsWorkItemListResult = { |
| 18 | + workItems: WorkItem[]; |
| 19 | +}; |
| 20 | + |
| 21 | +export default class DevopsWorkItemList extends SfCommand<DevopsWorkItemListResult> { |
| 22 | + public static readonly summary = messages.getMessage('summary'); |
| 23 | + public static readonly description = messages.getMessage('description'); |
| 24 | + public static readonly examples = messages.getMessages('examples'); |
| 25 | + |
| 26 | + public static readonly flags = { |
| 27 | + 'target-org': Flags.requiredOrg({ |
| 28 | + summary: messages.getMessage('flags.target-org.summary'), |
| 29 | + char: 'o', |
| 30 | + required: true, |
| 31 | + }), |
| 32 | + 'project-id': Flags.string({ |
| 33 | + summary: messages.getMessage('flags.project-id.summary'), |
| 34 | + char: 'p', |
| 35 | + required: true, |
| 36 | + }), |
| 37 | + }; |
| 38 | + |
| 39 | + public async run(): Promise<DevopsWorkItemListResult> { |
| 40 | + const { flags } = await this.parse(DevopsWorkItemList); |
| 41 | + const org: Org = flags['target-org']; |
| 42 | + const projectId: string = flags['project-id']; |
| 43 | + const connection = org.getConnection('65.0'); |
| 44 | + |
| 45 | + let workItems: WorkItem[]; |
| 46 | + try { |
| 47 | + workItems = await fetchWorkItems(connection, projectId); |
| 48 | + } catch (error: unknown) { |
| 49 | + const errMsg = error instanceof Error ? error.message : String(error); |
| 50 | + if (errMsg.includes('sObject type') && errMsg.includes('is not supported')) { |
| 51 | + this.error( |
| 52 | + 'DevOps Center is not enabled in this org. Enable DevOps Center in Setup before using this command.' |
| 53 | + ); |
| 54 | + } |
| 55 | + throw error; |
| 56 | + } |
| 57 | + |
| 58 | + if (workItems.length === 0) { |
| 59 | + this.log('No work items found for this project.'); |
| 60 | + } else { |
| 61 | + const tableData = workItems.map((wi) => ({ |
| 62 | + Name: wi.name, |
| 63 | + Subject: wi.subject ?? '', |
| 64 | + Status: wi.status, |
| 65 | + Branch: wi.WorkItemBranch ?? '', |
| 66 | + 'Target Branch': wi.TargetBranch ?? '', |
| 67 | + })); |
| 68 | + |
| 69 | + ux.styledHeader('DevOps Center Work Items'); |
| 70 | + ux.table(tableData, { |
| 71 | + Name: { header: 'Name' }, |
| 72 | + Subject: { header: 'Subject' }, |
| 73 | + Status: { header: 'Status' }, |
| 74 | + Branch: { header: 'Branch' }, |
| 75 | + 'Target Branch': { header: 'Target Branch' }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return { workItems }; |
| 80 | + } |
| 81 | +} |
0 commit comments