Skip to content

Commit fc05fd7

Browse files
committed
Changes to download latest release version of the API
1 parent 35848b0 commit fc05fd7

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/download/exchangeDownloader.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,28 +237,28 @@ describe("exchangeDownloader", () => {
237237
describe("getVersionByDeployment", () => {
238238
const scope = nock("https://anypoint.mulesoft.com/exchange/api/v1/assets");
239239

240-
it("should return a version if no deployment is specified", async () => {
240+
it("should return the latest version if no deployment is specified", async () => {
241241
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
242242

243243
return expect(
244244
getVersionByDeployment("AUTH_TOKEN", REST_API)
245-
).to.eventually.equal("0.0.7");
245+
).to.eventually.equal("0.0.42");
246246
});
247247

248-
it("should return a version if a deployment exists", async () => {
248+
it("should return the latest version if a deployment exists", async () => {
249249
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
250250

251251
return expect(
252252
getVersionByDeployment("AUTH_TOKEN", REST_API, /production/i)
253-
).to.eventually.equal("0.0.7");
253+
).to.eventually.equal("0.0.42");
254254
});
255255

256-
it("should return the base version if the deployment does not exist", async () => {
256+
it("should return the latest version if the deployment does not exist", async () => {
257257
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
258258

259259
return expect(
260260
getVersionByDeployment("AUTH_TOKEN", REST_API, /NOT AVAILABLE/i)
261-
).to.eventually.equal(getAssetWithoutVersion.version);
261+
).to.eventually.equal("0.0.42");
262262
});
263263

264264
it("should return undefined if the asset does not exist", async () => {
@@ -343,7 +343,7 @@ describe("exchangeDownloader", () => {
343343
scope
344344
.get("/shop-products-categories-api-v1")
345345
.reply(200, getAssetWithVersion)
346-
.get("/shop-products-categories-api-v1/0.0.1")
346+
.get("/shop-products-categories-api-v1/0.0.42")
347347
.reply(200, getAssetWithVersion);
348348

349349
return expect(search("searchString")).to.eventually.deep.equal([
@@ -390,7 +390,7 @@ describe("exchangeDownloader", () => {
390390
asset.fatRaml = {
391391
classifier: "fat-raml",
392392
packaging: "zip",
393-
externalLink: "https://short.url/raml.zip",
393+
externalLink: "https://short.url/test",
394394
createdDate: "2020-02-05T21:26:01.199Z",
395395
md5: "87b3ad2b2aa17639b52f0cc83c5a8d40",
396396
sha1: "f2b9b2de50b7250616e2eea8843735b57235c22b",
@@ -400,7 +400,7 @@ describe("exchangeDownloader", () => {
400400
scope
401401
.get("/shop-products-categories-api-v1")
402402
.reply(200, getAssetWithoutVersion)
403-
.get("/shop-products-categories-api-v1/0.0.7")
403+
.get("/shop-products-categories-api-v1/0.0.42")
404404
.reply(200, getAssetWithoutVersion);
405405

406406
return expect(search("searchString")).to.eventually.deep.equal([asset]);

src/download/exchangeDownloader.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const ANYPOINT_API_URI_V1 = `${ANYPOINT_BASE_URI}/api/v1`;
2929
const ANYPOINT_API_URI_V2 = `${ANYPOINT_BASE_URI}/api/v2`;
3030
const DEPLOYMENT_DEPRECATION_WARNING =
3131
"The 'deployment' argument is deprecated. The latest RAML specification that is published to Anypoint Exchange will be downloaded always.";
32-
32+
// Only allows MAJOR.MINOR.PATCH (no suffixes). see https://semver.org/
33+
const releaseSemverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
3334
/**
3435
* Makes an HTTP call to the url with the options passed. If the calls due to
3536
* a 5xx, 408, 420 or 429, it retries the call with the retry options passed
@@ -266,8 +267,31 @@ export async function getVersionByDeployment(
266267
return;
267268
}
268269

269-
// return the most recent version of an asset from the rest API
270-
return asset.version;
270+
if (!asset.instances) {
271+
ramlToolLogger.error(
272+
`${logPrefix} The rest API ${restApi.assetId} is missing asset.instances`
273+
);
274+
return;
275+
}
276+
277+
const releaseAssetVersions = asset.instances.filter((instance) => {
278+
return instance.version && releaseSemverRegex.test(instance.version);
279+
});
280+
281+
if (releaseAssetVersions.length === 0) {
282+
// If there is no release version, just return the asset version
283+
// TBD: should we skip downloading the asset?
284+
return asset.version;
285+
}
286+
// Sort versions and get the latest
287+
return releaseAssetVersions.sort((instanceA, instanceB) => {
288+
const [aMajor, aMinor, aPatch] = instanceA.version.split(".").map(Number);
289+
const [bMajor, bMinor, bPatch] = instanceB.version.split(".").map(Number);
290+
291+
if (aMajor !== bMajor) return bMajor - aMajor;
292+
if (aMinor !== bMinor) return bMinor - aMinor;
293+
return bPatch - aPatch;
294+
})[0].version;
271295
}
272296

273297
/**

0 commit comments

Comments
 (0)