Browse Source

feat: add support for version tagging

develop
Apostolos Fanakis 4 years ago
parent
commit
e48f005488
  1. 17
      packages/concordia-contracts-provider/src/controllers/download.js
  2. 18
      packages/concordia-contracts-provider/src/controllers/upload.js
  3. 2
      packages/concordia-contracts-provider/src/middleware/upload.js
  4. 13
      packages/concordia-contracts-provider/src/utils/storageUtils.js
  5. 2
      packages/concordia-contracts/package.json
  6. 7
      packages/concordia-contracts/utils/contractsProviderUtils.js

17
packages/concordia-contracts-provider/src/controllers/download.js

@ -1,10 +1,21 @@
import * as fs from 'fs'; import * as fs from 'fs';
import path from 'path'; import path from 'path';
import getStorageLocation from '../utils/storageUtils'; import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils';
const downloadContracts = async (req, res) => { const downloadContracts = async (req, res) => {
const { params: { hash } } = req; const { params: { hash: hashOrTag } } = req;
const directoryPath = getStorageLocation(hash); 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 = []; const contracts = [];

18
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 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) => { const uploadContracts = async (req, res) => {
try { try {
await upload(req, res); await upload(req, res);
const { body: { tag } } = req;
const { params: { hash } } = req;
if (tag) {
addOrTransferTag(tag, hash);
}
if (req.files.length <= 0) { if (req.files.length <= 0) {
return res.send('You must select at least 1 file.'); return res.send('You must select at least 1 file.');
} }

2
packages/concordia-contracts-provider/src/middleware/upload.js

@ -1,7 +1,7 @@
import * as util from 'util'; import * as util from 'util';
import * as fs from 'fs'; import * as fs from 'fs';
import multer from 'multer'; import multer from 'multer';
import getStorageLocation from '../utils/storageUtils'; import { getStorageLocation } from '../utils/storageUtils';
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: (req, file, callback) => { destination: (req, file, callback) => {

13
packages/concordia-contracts-provider/src/utils/storageUtils.js

@ -1,10 +1,17 @@
import path from 'path'; import path from 'path';
import constants from '../constants'; import constants from '../constants';
const getStorageLocation = (hash) => { export const getStorageLocation = (hash) => {
const UPLOADS_DIRECTORY = process.env.UPLOAD_CONTRACTS_DIRECTORY || constants.uploadsDirectory; 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');
};

2
packages/concordia-contracts/package.json

@ -13,7 +13,7 @@
"migrate": "yarn _migrate --network develop && yarn upload", "migrate": "yarn _migrate --network develop && yarn upload",
"migrate-reset": "yarn _migrate --network develop --reset && yarn upload", "migrate-reset": "yarn _migrate --network develop --reset && yarn upload",
"_migrate": "yarn truffle migrate", "_migrate": "yarn truffle migrate",
"upload": "node ./utils/contractsProviderUtils.js $npm_package_version" "upload": "node ./utils/contractsProviderUtils.js ${npm_package_version}-dev latest"
}, },
"dependencies": { "dependencies": {
"@openzeppelin/contracts": "~3.2.0", "@openzeppelin/contracts": "~3.2.0",

7
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 CONTRACTS_PROVIDER_PORT = process.env.CONTRACTS_PROVIDER_PORT || defaults.contractsProviderPort;
const uploadPath = `http://${CONTRACTS_PROVIDER_HOST}:${CONTRACTS_PROVIDER_PORT}/contracts/${versionHash}`; 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 contracts
.forEach((contract) => req .forEach((contract) => request
.attach('contracts', path.join(__dirname, '../', 'build/', `${contract.contractName}.json`))); .attach('contracts', path.join(__dirname, '../', 'build/', `${contract.contractName}.json`)));
console.log(`Uploading to ${uploadPath}`); console.log(`Uploading to ${uploadPath}`);
req.end((res) => { request.end((res) => {
if (res.error) { if (res.error) {
throw new Error(`Failed to upload contracts to provider: ${res.error}`); throw new Error(`Failed to upload contracts to provider: ${res.error}`);
} }

Loading…
Cancel
Save