-
Notifications
You must be signed in to change notification settings - Fork 13
/
package1Version.ts
174 lines (165 loc) · 6.91 KB
/
package1Version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import os from 'node:os';
import type { Schema } from '@jsforce/jsforce-node';
import { Connection, Lifecycle, Messages, PollingClient, StatusResult } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import {
IPackageVersion1GP,
Package1VersionCreateRequest,
Package1VersionEvents,
PackagingSObjects,
} from '../interfaces';
import MetadataPackageVersion = PackagingSObjects.MetadataPackageVersion;
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/packaging', 'package1Version');
/**
* Provides the ability to get, list, and create 1st generation package versions.
*
* **Examples**
*
* List all 1GP package versions in the org:
*
* `const pkgList = await Package1Version.list(connection);`
*
* Create a new 1GP package vesion in the org:
*
* `const myPkg = await Package1Version.create(connection, options, pollingOptions);`
*
* More implementation examples are in the plugin here: https://github.com/salesforcecli/plugin-packaging/tree/main/src/commands/force/package1/
*/
export class Package1Version implements IPackageVersion1GP {
/**
* Package1Version Constructor - Class to be used with 1st generation package versions
*
* @param connection: Connection to the org
* @param id: 04t ID of the package version
*/
public constructor(private connection: Connection, private id: string) {
if (!id.startsWith('04t')) {
throw messages.createError('invalid04tId', [id]);
}
}
/**
* Will create a PackageUploadRequest object based on the options provided, will poll for completion if pollingOptions are provided
*
* @param connection: Connection to the org
* @param options: Package1VersionCreateRequest options for the new PackageUploadRequest to be created with
* @param pollingOptions: options to set frequency, and duration of polling. Default to not poll
*/
public static async create(
connection: Connection,
options: Package1VersionCreateRequest,
pollingOptions = { frequency: Duration.seconds(5), timeout: Duration.seconds(0) }
): Promise<PackagingSObjects.PackageUploadRequest> {
if (!options.MetadataPackageId?.startsWith('033')) {
throw messages.createError('missingMetadataPackageId');
}
if (!options.VersionName) {
throw messages.createError('missingVersionName');
}
const createRequest = await connection.tooling.sobject('PackageUploadRequest').create(options);
if (createRequest.success) {
if (pollingOptions.timeout.seconds) {
const timeout = pollingOptions.timeout.seconds;
const pollingClient = await PollingClient.create({
poll: () =>
Package1Version.packageUploadPolling(
connection,
createRequest.id,
timeout,
pollingOptions.frequency.seconds
),
...pollingOptions,
});
return pollingClient.subscribe<PackagingSObjects.PackageUploadRequest>();
} else {
// jsforce templates weren't working when setting the type to PackageUploadRequest, so we have to cast `as unknown as PackagingSObjects.PackageUploadRequest`
return (await connection.tooling
.sobject('PackageUploadRequest')
.retrieve(createRequest.id)) as unknown as PackagingSObjects.PackageUploadRequest;
}
} else {
throw messages.createError('createFailed', [JSON.stringify(createRequest)]);
}
}
/**
* Returns the status of a PackageUploadRequest
*
* @param connection Connection to the target org
* @param id 0HD Id of the PackageUploadRequest
*/
public static async getCreateStatus(
connection: Connection,
id: string
): Promise<PackagingSObjects.PackageUploadRequest> {
if (!id.startsWith('0HD')) {
throw messages.createError('invalid0HDId', [id]);
}
return (await connection.tooling
.sobject('PackageUploadRequest')
.retrieve(id)) as unknown as PackagingSObjects.PackageUploadRequest;
}
/**
* Lists package versions available in the org. If package ID is supplied, only list versions of that package,
* otherwise, list all package versions, up to 10,000. If more records are needed use the `SF_ORG_MAX_QUERY_LIMIT` env var.
*
* @param connection Connection to the org
* @param id: optional, if present, ID of package to list versions for (starts with 033)
* @returns Array of package version results
*/
public static async list(connection: Connection, id?: string): Promise<MetadataPackageVersion[]> {
if (id && !id?.startsWith('033')) {
// we have to check that it is present, and starts with 033
// otherwise, undefined doesn't start with 033 and will trigger this error, when it shouldn't
throw messages.createError('invalid033Id', [id]);
}
const query = `SELECT Id,MetadataPackageId,Name,ReleaseState,MajorVersion,MinorVersion,PatchVersion,BuildNumber FROM MetadataPackageVersion ${
id ? `WHERE MetadataPackageId = '${id}'` : ''
} ORDER BY MetadataPackageId, MajorVersion, MinorVersion, PatchVersion, BuildNumber`;
return (
await connection.autoFetchQuery<PackagingSObjects.MetadataPackageVersion & Schema>(query, {
tooling: true,
})
)?.records;
}
private static async packageUploadPolling(
connection: Connection,
id: string,
timeout: number,
frequency: number
): Promise<StatusResult> {
const pollingResult = await connection.tooling.sobject('PackageUploadRequest').retrieve(id);
switch (pollingResult.Status) {
case 'SUCCESS':
return { completed: true, payload: pollingResult };
case 'IN_PROGRESS':
case 'QUEUED':
timeout -= frequency;
await Lifecycle.getInstance().emit(Package1VersionEvents.create.progress, { timeout, pollingResult });
return { completed: false, payload: pollingResult };
default: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const errors = pollingResult?.Errors?.errors as Error[];
if (errors?.length > 0) {
throw messages.createError('package1VersionCreateCommandUploadFailure', [
errors.map((e: Error) => e.message).join(os.EOL),
]);
} else {
throw messages.createError('package1VersionCreateCommandUploadFailureDefault');
}
}
}
}
/**
* Queries the org for the package version with the given ID
*/
public async getPackageVersion(): Promise<MetadataPackageVersion[]> {
const query = `SELECT Id, MetadataPackageId, Name, ReleaseState, MajorVersion, MinorVersion, PatchVersion, BuildNumber FROM MetadataPackageVersion WHERE id = '${this.id}'`;
return (await this.connection.tooling.query<PackagingSObjects.MetadataPackageVersion>(query)).records;
}
}