From e48f0054883bcef98796607bc2fc4648a427a89c Mon Sep 17 00:00:00 2001 From: apostolof Date: Thu, 4 Feb 2021 00:07:15 +0200 Subject: [PATCH] feat: add support for version tagging --- .../src/controllers/download.js | 17 ++++++++++++++--- .../src/controllers/upload.js | 18 ++++++++++++++++++ .../src/middleware/upload.js | 2 +- .../src/utils/storageUtils.js | 13 ++++++++++--- packages/concordia-contracts/package.json | 2 +- .../utils/contractsProviderUtils.js | 7 ++++--- 6 files changed, 48 insertions(+), 11 deletions(-) diff --git a/packages/concordia-contracts-provider/src/controllers/download.js b/packages/concordia-contracts-provider/src/controllers/download.js index 10241c2..fa77b78 100755 --- a/packages/concordia-contracts-provider/src/controllers/download.js +++ b/packages/concordia-contracts-provider/src/controllers/download.js @@ -1,10 +1,21 @@ import * as fs from 'fs'; import path from 'path'; -import getStorageLocation from '../utils/storageUtils'; +import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; const downloadContracts = async (req, res) => { - const { params: { hash } } = req; - const directoryPath = getStorageLocation(hash); + const { params: { hash: hashOrTag } } = req; + let directoryPath = getStorageLocation(hashOrTag); + + if (!fs.existsSync(directoryPath)) { + const tagsDirectory = getTagsDirectory(); + + if (fs.existsSync(tagsDirectory)) { + const tagFilePath = path.join(tagsDirectory, hashOrTag); + const tagReference = fs.readFileSync(tagFilePath, 'utf-8'); + + directoryPath = getStorageLocation(tagReference); + } + } const contracts = []; diff --git a/packages/concordia-contracts-provider/src/controllers/upload.js b/packages/concordia-contracts-provider/src/controllers/upload.js index 018a047..346c567 100755 --- a/packages/concordia-contracts-provider/src/controllers/upload.js +++ b/packages/concordia-contracts-provider/src/controllers/upload.js @@ -1,9 +1,27 @@ +import path from 'path'; +import fs from 'fs'; import upload from '../middleware/upload'; +import { getTagsDirectory } from '../utils/storageUtils'; + +const addOrTransferTag = (tag, hash) => { + const tagsDirectory = getTagsDirectory(); + const tagFilePath = path.join(tagsDirectory, tag); + + fs.mkdirSync(tagsDirectory, { recursive: true }); + fs.writeFileSync(tagFilePath, hash); +}; const uploadContracts = async (req, res) => { try { await upload(req, res); + const { body: { tag } } = req; + const { params: { hash } } = req; + + if (tag) { + addOrTransferTag(tag, hash); + } + if (req.files.length <= 0) { return res.send('You must select at least 1 file.'); } diff --git a/packages/concordia-contracts-provider/src/middleware/upload.js b/packages/concordia-contracts-provider/src/middleware/upload.js index 39a0ec0..c55af11 100755 --- a/packages/concordia-contracts-provider/src/middleware/upload.js +++ b/packages/concordia-contracts-provider/src/middleware/upload.js @@ -1,7 +1,7 @@ import * as util from 'util'; import * as fs from 'fs'; import multer from 'multer'; -import getStorageLocation from '../utils/storageUtils'; +import { getStorageLocation } from '../utils/storageUtils'; const storage = multer.diskStorage({ destination: (req, file, callback) => { diff --git a/packages/concordia-contracts-provider/src/utils/storageUtils.js b/packages/concordia-contracts-provider/src/utils/storageUtils.js index 6471d88..be851ea 100644 --- a/packages/concordia-contracts-provider/src/utils/storageUtils.js +++ b/packages/concordia-contracts-provider/src/utils/storageUtils.js @@ -1,10 +1,17 @@ import path from 'path'; import constants from '../constants'; -const getStorageLocation = (hash) => { +export const getStorageLocation = (hash) => { const UPLOADS_DIRECTORY = process.env.UPLOAD_CONTRACTS_DIRECTORY || constants.uploadsDirectory; - return path.join(UPLOADS_DIRECTORY, hash); + if (hash) { + return path.join(UPLOADS_DIRECTORY, hash); + } + + return UPLOADS_DIRECTORY; }; -export default getStorageLocation; +export const getTagsDirectory = () => { + const uploadsPath = getStorageLocation(); + return path.join(uploadsPath, '/tags'); +}; diff --git a/packages/concordia-contracts/package.json b/packages/concordia-contracts/package.json index 367ccbf..c1cfd6d 100644 --- a/packages/concordia-contracts/package.json +++ b/packages/concordia-contracts/package.json @@ -13,7 +13,7 @@ "migrate": "yarn _migrate --network develop && yarn upload", "migrate-reset": "yarn _migrate --network develop --reset && yarn upload", "_migrate": "yarn truffle migrate", - "upload": "node ./utils/contractsProviderUtils.js $npm_package_version" + "upload": "node ./utils/contractsProviderUtils.js ${npm_package_version}-dev latest" }, "dependencies": { "@openzeppelin/contracts": "~3.2.0", diff --git a/packages/concordia-contracts/utils/contractsProviderUtils.js b/packages/concordia-contracts/utils/contractsProviderUtils.js index 3d79162..2bd52a3 100644 --- a/packages/concordia-contracts/utils/contractsProviderUtils.js +++ b/packages/concordia-contracts/utils/contractsProviderUtils.js @@ -8,14 +8,15 @@ const uploadContractsToProviderUnirest = (versionHash, tag) => { const CONTRACTS_PROVIDER_PORT = process.env.CONTRACTS_PROVIDER_PORT || defaults.contractsProviderPort; const uploadPath = `http://${CONTRACTS_PROVIDER_HOST}:${CONTRACTS_PROVIDER_PORT}/contracts/${versionHash}`; - const req = unirest('POST', uploadPath); + const request = unirest('POST', uploadPath) + .field('tag', tag); contracts - .forEach((contract) => req + .forEach((contract) => request .attach('contracts', path.join(__dirname, '../', 'build/', `${contract.contractName}.json`))); console.log(`Uploading to ${uploadPath}`); - req.end((res) => { + request.end((res) => { if (res.error) { throw new Error(`Failed to upload contracts to provider: ${res.error}`); }