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. 11
      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 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 = [];

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 { 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.');
}

2
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) => {

11
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;
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-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",

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 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}`);
}

Loading…
Cancel
Save