It is possible using the details from Salesforce Metadata Api/ Tooling Api to build changesets to retrieve and modify the contents of a change set via the Metadata API.
However, the whole process depends on knowing the “Change Set Name” that can then be used as the Package Name against the Metadata API.
Is there a programmatic way to discover the package names of the changesets that exist in an org?
Ideally this would be an official API.
See also, Salesforce Idea: Expose the native change sets via an API
To get a list of outbound change sets using the Salesforce CLI some set up would be required. A file that contains the following:
PageReference pr=new PageReference('/changemgmt/listOutboundChangeSet.apexp'); Blob output=pr.getContent(); System.debug('### Content = \n' + output.toString());
Then the command
sfdx force:apex:execute -f changeset.apex --json -u <username>
This would return the change set page as part of a json response.
It is possible to parse the HTML from the response, I’ve used cheerio for this, and then loop through the rows of the change set table searching for the ids.
A full working example as a self contained plugin for SFDX is available at https://github.com/BrettMN/sfdx-wipd-plugin
With the plugin it is possible to get the json output with providing only a username but it does not currently return the id of the change set
To see where the response is handle Line 44 of
commands/changeset_list.js is where it begins.