Skip to content

Commit 7d67816

Browse files
committed
feat: Github repositories and extension names are locked.
1 parent 7ce80b1 commit 7d67816

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

src/api/publishGithubRelease.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import {downloader} from "../utils/downloader.js";
66
import {ZipUtils} from "../utils/zipUtils.js";
77
import {valid, lte, clean} from "semver";
88
import {
9-
FIELD_RELEASE_ID, RELEASE_DETAILS_TABLE, EXTENSION_SIZE_LIMIT_MB, BASE_URL,
10-
EXTENSION_DOWNLOAD_DIR, PROCESSING_TIMEOUT_MS, EXTENSIONS_DETAILS_TABLE, FIELD_EXTENSION_ID, EXTENSIONS_BUCKET
9+
FIELD_RELEASE_ID,
10+
RELEASE_DETAILS_TABLE,
11+
EXTENSION_SIZE_LIMIT_MB,
12+
BASE_URL,
13+
EXTENSION_DOWNLOAD_DIR,
14+
PROCESSING_TIMEOUT_MS,
15+
EXTENSIONS_DETAILS_TABLE,
16+
FIELD_EXTENSION_ID,
17+
EXTENSIONS_BUCKET,
18+
REGISTRY_FILE
1119
} from "../constants.js";
1220
import fs from "fs";
1321
import {S3} from "../s3.js";
@@ -99,11 +107,12 @@ async function _validateAlreadyReleased(release) {
99107
error: `Release ${releaseRef} already published!`};
100108
}
101109
if(existingRelease?.status === RELEASE_STATUS_PROCESSING){
102-
if((Date.now() - existingRelease.lastUpdatedDateUTC) > PROCESSING_TIMEOUT_MS){
110+
let timeDiff = Date.now() - existingRelease.lastUpdatedDateUTC;
111+
if(timeDiff > PROCESSING_TIMEOUT_MS){
103112
console.log(`Retrying release ${releaseRef} as process timeout exceeded.`);
104113
} else {
105114
throw {status: HTTP_STATUS_CODES.BAD_REQUEST,
106-
error: `Release ${releaseRef} is already being processed. Please wait or retry after ${PROCESSING_TIMEOUT_MS/1000} Seconds`};
115+
error: `Release ${releaseRef} is already being processed. Please wait or retry after ${(PROCESSING_TIMEOUT_MS-timeDiff)/1000} Seconds`};
107116
}
108117
}
109118
return {
@@ -366,8 +375,43 @@ async function _UpdateReleaseSuccess(release, existingReleaseInfo, registryPKGJS
366375
}
367376
}
368377

378+
/**
379+
* A github repository that has already published an extension with name "x" cannot start publishing an extension with
380+
* another name "y". Github repositories and extension names are locked.
381+
* @param newExtensionName
382+
* @param ownerRepo
383+
* @param issueMessages
384+
* @return {Promise<void>}
385+
* @private
386+
*/
387+
async function _validateExtensionNameForRepo(newExtensionName, ownerRepo, issueMessages) {
388+
ownerRepo = ownerRepo.endsWith('/') ? ownerRepo.slice(0, -1) : ownerRepo; // remove trailing slash
389+
let registry = JSON.parse(await S3.getObject(EXTENSIONS_BUCKET, REGISTRY_FILE));
390+
let extensionIDs = Object.keys(registry);
391+
for(let extensionID of extensionIDs) {
392+
let extension = registry[extensionID];
393+
if(!extension.ownerRepo) {
394+
continue;
395+
}
396+
let existingOwner = extension.ownerRepo;
397+
existingOwner = existingOwner.endsWith('/') ?
398+
existingOwner.slice(0, -1) : existingOwner; // remove trailing slash
399+
if(ownerRepo === existingOwner && extension.metadata.name !== newExtensionName) {
400+
let message = `Release failed: Github Repository ${ownerRepo} can only publish extension with name <b>${extension.metadata.name}</b>, `+
401+
`but the package.json in zip file had name <b>${newExtensionName}</b>. If you wish to change the name of your extension, `+
402+
`please raise an <a href="https://github.com/phcode-dev/phoenix/issues">issue here.</a>`;
403+
console.error(message);
404+
issueMessages.push(message);
405+
throw {status: HTTP_STATUS_CODES.CONFLICT,
406+
updatePublishErrors: true,
407+
error: message};
408+
}
409+
}
410+
}
411+
369412
async function _updateRegistryJSONinDB(existingRegistryPKGVersion, existingRegistryDocumentId, registryPKGJSON,
370413
issueMessages) {
414+
await _validateExtensionNameForRepo(registryPKGJSON.metadata.name, registryPKGJSON.ownerRepo, issueMessages);
371415
let status;
372416
registryPKGJSON.syncPending = 'Y';// coco db doesnt support boolean queries yet
373417
registryPKGJSON.EXTENSION_ID = registryPKGJSON.metadata.name;
@@ -388,6 +432,7 @@ async function _updateRegistryJSONinDB(existingRegistryPKGVersion, existingRegis
388432
` while this release was being published?\n If so you may have to update your version number and make a new release with higher version number.`;
389433
issueMessages.push(message);
390434
throw {status: HTTP_STATUS_CODES.CONFLICT,
435+
updatePublishErrors: true,
391436
error: message};
392437
}
393438
}
@@ -408,6 +453,7 @@ export async function publishGithubRelease(request, reply) {
408453
if(newGithubReleaseDetails.draft || newGithubReleaseDetails.prerelease){
409454
issueMessages.push(`Draft or PreRelease builds cannot be published.`);
410455
throw {status: HTTP_STATUS_CODES.BAD_REQUEST,
456+
updatePublishErrors: true,
411457
error: `Draft or PreRelease builds cannot be published.`};
412458
}
413459
const extensionZipAsset = _validateGitHubReleaseAssets(newGithubReleaseDetails, issueMessages);

0 commit comments

Comments
 (0)