Skip to content

Commit 6a1896f

Browse files
committed
chore: package.json validation in zip
1 parent 7063e2c commit 6a1896f

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

package-lock.json

Lines changed: 34 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"fastify": "4.12.0",
7272
"follow-redirects": "^1.15.2",
7373
"node-fetch": "^3.3.0",
74-
"node-stream-zip": "^1.15.0"
74+
"node-stream-zip": "^1.15.0",
75+
"semver": "^7.3.8"
7576
}
7677
}

src/api/publishGithubRelease.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {getRepoDetails, getReleaseDetails, createIssue} from "../github.js";
44
import db from "../db.js";
55
import {downloader} from "../utils/downloader.js";
66
import {ZipUtils} from "../utils/zipUtils.js";
7+
import {valid, lte} from "semver";
78
import {
89
FIELD_RELEASE_ID, RELEASE_DETAILS_TABLE, EXTENSION_SIZE_LIMIT_MB, BASE_URL,
910
EXTENSION_DOWNLOAD_DIR, PROCESSING_TIMEOUT_MS, EXTENSIONS_DETAILS_TABLE, FIELD_EXTENSION_ID
@@ -161,9 +162,36 @@ async function _validateExtensionPackageJson(githubReleaseTag, packageJSON, issu
161162
throw new Error("Error getting extensionPKG details from db: " + releaseRef);
162163
}
163164
registryPKG = registryPKG.documents.length === 1 ? registryPKG.documents[0] : null;
165+
let error = "";
164166
if(registryPKG && registryPKG.owner !== newOwner) {
165-
let error = `Extension of the same name "${packageJSON.name}" already exists (owned by https://github.com/${registryPKG.owner.split(":")[1]}).`;
166-
issueMessages.push(error);
167+
let errorMsg = `Extension of the same name "${packageJSON.name}" already exists (owned by https://github.com/${registryPKG.owner.split(":")[1]}). Please choose a different extension name.`;
168+
error = error + errorMsg;
169+
issueMessages.push(errorMsg);
170+
throw {status: HTTP_STATUS_CODES.BAD_REQUEST,
171+
updatePublishErrors: true,
172+
error};
173+
}
174+
if(!valid(packageJSON.version)) {
175+
let errorMsg = `Invalid package version "${packageJSON.version}" in zip.`;
176+
error = error + `\n${errorMsg}`;
177+
issueMessages.push(errorMsg);
178+
}
179+
if(registryPKG) {
180+
for(let versionInfo of registryPKG.versions){
181+
if(versionInfo.version === packageJSON.version){
182+
let errorMsg = `Package version "${packageJSON.version}" already published on ${versionInfo.published}. Please update version number to above ${registryPKG.metadata.version}.`;
183+
error = error + `\n${errorMsg}`;
184+
issueMessages.push(errorMsg);
185+
break;
186+
}
187+
}
188+
if(lte(packageJSON.version, registryPKG.metadata.version)){
189+
let errorMsg = `Package version should be greater than ${registryPKG.metadata.version}, but received "${packageJSON.version}".`;
190+
error = error + `\n${errorMsg}`;
191+
issueMessages.push(errorMsg);
192+
}
193+
}
194+
if(error){
167195
throw {status: HTTP_STATUS_CODES.BAD_REQUEST,
168196
updatePublishErrors: true,
169197
error};

test/unit/api/publishGithubRelease.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {ZipUtils} from "../../../src/utils/zipUtils.js";
77
import {publishGithubRelease, getPublishGithubReleaseSchema} from "../../../src/api/publishGithubRelease.js";
88
import {getSimpleGetReply, getSimpleGETRequest} from '../data/simple-request.js';
99
import {VALID_PACKAGE_JSON} from "../data/packagejson.js";
10+
import registryJSON from "../data/registry.js";
1011
import Ajv from "ajv";
1112
import {initGitHubClient} from "../../../src/github.js";
1213
import {EXTENSION_SIZE_LIMIT_MB} from "../../../src/constants.js";

0 commit comments

Comments
 (0)