Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change mdcoverage url for getCurrentApiVersion #1191

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions messages/sdr.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ A StaticResource must have an associated .resource file, missing %s.resource-met

The %s operation is missing a job ID. Initialize an operation with an ID, or start a new job.

# missingApiVersion

Could not determine an API version to use for the set of metadata components.

# missingApiVersion.actions

Set a `sourceApiVersion` or `apiVersion` on the ComponentSet.
Set an API version to use with the environment variable, `SF_ORG_API_VERSION`.

# invalid_xml_parsing

error parsing %s due to:\n message: %s\n line: %s\n code: %s
Expand Down
12 changes: 11 additions & 1 deletion src/collections/componentSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,17 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
* @returns Object representation of a package manifest
*/
public async getObject(destructiveType?: DestructiveChangesType): Promise<PackageManifestObject> {
const version = this.sourceApiVersion ?? this.apiVersion ?? `${await getCurrentApiVersion()}.0`;
let version: string;
try {
version =
this.sourceApiVersion ??
this.apiVersion ??
process.env.SF_ORG_API_VERSION ??
shetzel marked this conversation as resolved.
Show resolved Hide resolved
`${await getCurrentApiVersion()}.0`;
} catch (err: unknown) {
const cause = err instanceof Error ? err : isString(err) ? Error(err) : undefined;
throw messages.createError('missingApiVersion', undefined, undefined, 1, cause);
}

// If this ComponentSet has components marked for delete, we need to
// only include those components in a destructiveChanges.xml and
Expand Down
2 changes: 1 addition & 1 deletion src/registry/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getProxiedOptions = (url: string): OptionsOfTextResponseBody => ({

export const getCurrentApiVersion = async (): Promise<number> =>
(
await got(getProxiedOptions('https://mdcoverage.secure.force.com/services/apexrest/report')).json<{
await got(getProxiedOptions('https://dx-extended-coverage.my.salesforce-sites.com/services/apexrest/report')).json<{
peternhale marked this conversation as resolved.
Show resolved Hide resolved
versions: { selected: number };
}>()
).versions.selected;
Expand Down
62 changes: 60 additions & 2 deletions test/collections/componentSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { join } from 'node:path';
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
import { assert, expect } from 'chai';
import { SinonStub } from 'sinon';
import { AuthInfo, ConfigAggregator, Connection, Lifecycle, Messages } from '@salesforce/core';
import { AuthInfo, ConfigAggregator, Connection, Lifecycle, Messages, SfError } from '@salesforce/core';
import {
ComponentSet,
ComponentSetBuilder,
Expand Down Expand Up @@ -676,8 +676,18 @@ describe('ComponentSet', () => {
});

describe('getObject', () => {
const sfOrgApiVersion = process.env.SF_ORG_API_VERSION;
let getCurrentApiVersionStub: SinonStub;

beforeEach(() => {
$$.SANDBOX.stub(coverage, 'getCurrentApiVersion').resolves(testApiVersion);
getCurrentApiVersionStub = $$.SANDBOX.stub(coverage, 'getCurrentApiVersion').resolves(testApiVersion);
});

afterEach(() => {
process.env.SF_ORG_API_VERSION = sfOrgApiVersion;
if (!sfOrgApiVersion) {
delete process.env.SF_ORG_API_VERSION;
}
});

it('should return an object representing the package manifest', async () => {
Expand All @@ -701,6 +711,7 @@ describe('ComponentSet', () => {
version: testApiVersionAsString,
},
});
expect(getCurrentApiVersionStub.calledOnce).to.be.true;
});

it('should allow the componentSet to set the apiVersion', async () => {
Expand All @@ -725,6 +736,52 @@ describe('ComponentSet', () => {
version: testApiVersionAsString,
},
});
expect(getCurrentApiVersionStub.called).to.be.false;
});

it('should get an API version from env var', async () => {
const set = ComponentSet.fromSource({
fsPaths: ['.'],
registry: registryAccess,
tree: manifestFiles.TREE,
});
process.env.SF_ORG_API_VERSION = testApiVersionAsString;
expect(await set.getObject()).to.deep.equal({
Package: {
types: [
{
name: registry.types.customobjecttranslation.name,
members: ['a'],
},
{
name: registry.types.staticresource.name,
members: ['b', 'c'],
},
],
version: testApiVersionAsString,
},
});
expect(getCurrentApiVersionStub.called).to.be.false;
});

it('should throw an error when API version is missing', async () => {
getCurrentApiVersionStub.reset();
const causeErr = new Error('HTTP 404 - mdcoverage url is down');
getCurrentApiVersionStub.throws(causeErr);
const set = ComponentSet.fromSource({
fsPaths: ['.'],
registry: registryAccess,
tree: manifestFiles.TREE,
});
try {
await set.getObject();
expect(true, 'Expected an error to be thrown from getObject()').to.be.false;
} catch (err: unknown) {
expect(err).to.be.instanceOf(SfError);
const error = err as SfError;
expect(error.name).to.equal('MissingApiVersionError');
expect(error.cause).to.equal(causeErr);
}
});

it('should return an object representing destructive changes manifest', async () => {
Expand Down Expand Up @@ -798,6 +855,7 @@ describe('ComponentSet', () => {
version: set.sourceApiVersion,
},
});
expect(getCurrentApiVersionStub.called).to.be.false;
});

it('should interpret folder components as members of the type they are a container for', async () => {
Expand Down