Skip to content

Commit

Permalink
fix: add PackageVersion.listCreated, NUT
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Aug 30, 2022
1 parent c5114d0 commit 31280aa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/packagingInterfacesAndType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export type PackageVersionCreateOptions = Partial<
export type PackageVersionCreateRequestQueryOptions = {
createdlastdays?: number;
connection?: Connection;
status?: string;
status?: 'Queued' | 'InProgress' | 'Success' | 'Error';
};

export type ProfileApiOptions = {
Expand Down
8 changes: 8 additions & 0 deletions src/package/packageVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Duration } from '@salesforce/kit';
import {
PackageSaveResult,
PackageVersionCreateOptions,
PackageVersionCreateRequestQueryOptions,
PackageVersionCreateRequestResult,
PackageVersionListOptions,
PackageVersionListResult,
Expand All @@ -31,6 +32,7 @@ import { PackageVersionCreate } from './packageVersionCreate';
import { getPackageVersionReport } from './packageVersionReport';
import { getCreatePackageVersionCreateRequestReport } from './packageVersionCreateRequestReport';
import { listPackageVersions } from './packageVersionList';
import { list } from './packageVersionCreateRequest';

Messages.importMessagesDirectory(__dirname);

Expand Down Expand Up @@ -120,6 +122,12 @@ export class PackageVersion {
});
}

public async createdList(
options?: Omit<PackageVersionCreateRequestQueryOptions, 'connection'>
): Promise<PackageVersionCreateRequestResult[]> {
return await list({ ...options, connection: this.connection });
}

/**
* Convenience function that will wait for a package version to be created.
*
Expand Down
22 changes: 6 additions & 16 deletions src/package/packageVersionCreateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ const QUERY =
'%s' + // WHERE, if applicable
'ORDER BY CreatedDate';
const ERROR_QUERY = "SELECT Message FROM Package2VersionCreateRequestError WHERE ParentRequest.Id = '%s'";
const STATUSES = ['Queued', 'InProgress', 'Success', 'Error'];

export async function list(
options: PackageVersionCreateRequestQueryOptions = {}
options?: PackageVersionCreateRequestQueryOptions
): Promise<PackageVersionCreateRequestResult[]> {
const whereClause = _constructWhere();
const whereClause = _constructWhere(options);
return _query(util.format(QUERY, whereClause), options.connection);
}

Expand Down Expand Up @@ -84,28 +83,19 @@ async function _queryErrors(
}

function _constructWhere(options?: PackageVersionCreateRequestQueryOptions): string {
const where = [];
const where: string[] = [];

// filter on created date, days ago: 0 for today, etc
if (!util.isNullOrUndefined(this.options.createdlastdays)) {
if (options?.createdlastdays) {
if (options.createdlastdays < 0) {
throw messages.createError('invalidDaysNumber', ['createdlastdays', options.createdlastdays]);
}
where.push(`CreatedDate = LAST_N_DAYS:${this.options.createdlastdays as string}`);
where.push(`CreatedDate = LAST_N_DAYS:${options.createdlastdays}`);
}

// filter on errors
if (options.status) {
const foundStatus = STATUSES.find((status) => status.toLowerCase() === this.options.status.toLowerCase());
if (!foundStatus) {
const args = [options.status];
STATUSES.forEach((status) => {
args.push(status);
});
throw messages.createError('invalidStatus', args);
}

where.push(`Status = '${foundStatus}'`);
where.push(`Status = '${options.status.toLowerCase()}'`);
}

return where.length > 0 ? `WHERE ${where.join(' AND ')}` : '';
Expand Down
58 changes: 58 additions & 0 deletions test/package/packageTest.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,64 @@ describe('Integration tests for @salesforce/packaging library', function () {
expect(result.success).to.be.true;
expect(result.errors).to.deep.equal([]);
});

it('will list all of the created package versions', async () => {
const pkg = new PackageVersion({ connection: devHubOrg.getConnection(), project });
const result = await pkg.createdList();
expect(result).to.have.length.at.least(1);
expect(result[0]).to.have.all.keys(
'Id',
'Status',
'Package2Id',
'Package2VersionId',
'SubscriberPackageVersionId',
'Tag',
'Branch',
'Error',
'CreatedDate',
'HasMetadataRemoved',
'CreatedBy'
);
expect(result[0].Id.startsWith('08c')).to.be.true;
expect(result[0].Package2Id.startsWith('0Ho')).to.be.true;
expect(result[0].Package2VersionId.startsWith('05i')).to.be.true;
expect(result[0].SubscriberPackageVersionId.startsWith('04t')).to.be.true;
expect(result[0].Id.startsWith('08c')).to.be.true;
});

it('will list all of the created package versions (status = error)', async () => {
const pkg = new PackageVersion({ connection: devHubOrg.getConnection(), project });
const result = await pkg.createdList({ status: 'Success' });
result.map((res) => {
// we should've filtered to only successful package versions1
expect(res.Status).to.equal('Success');
});
});

it('will list all of the created package versions (createdLastDays = 3)', async () => {
const pkg = new PackageVersion({ connection: devHubOrg.getConnection(), project });
const result = await pkg.createdList({ createdlastdays: 3 });
expect(result).to.have.length.at.least(1);
expect(result[0]).to.have.all.keys(
'Id',
'Status',
'Package2Id',
'Package2VersionId',
'SubscriberPackageVersionId',
'Tag',
'Branch',
'Error',
'CreatedDate',
'HasMetadataRemoved',
'CreatedBy'
);
const createdDate = new Date(result[0].CreatedDate);
const currentDate = new Date();
expect(currentDate > createdDate).to.be.true;
// this package should've been made within the last 3 days
expect(currentDate.getDate() - 3 > createdDate.getDate()).to.be.false;
expect(currentDate.getDate() - 2 > createdDate.getDate()).to.be.true;
});
});

describe('install the package in scratch org', () => {
Expand Down

0 comments on commit 31280aa

Please sign in to comment.