2022-05-27 06:23:52 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2022-06-01 05:57:32 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-05-21 13:11:32 +00:00
|
|
|
const isProduction = require('vn-loopback/server/boot/isProduction');
|
2022-05-27 06:23:52 +00:00
|
|
|
|
2022-05-25 12:59:28 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('upload', {
|
2022-05-27 11:39:33 +00:00
|
|
|
description: 'Upload and attach a access file',
|
2022-05-25 12:59:28 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'appName',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The app name'
|
2022-12-19 06:26:07 +00:00
|
|
|
}, {
|
2022-12-22 06:12:09 +00:00
|
|
|
arg: 'toVersion',
|
2022-05-25 12:59:28 +00:00
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: `The new version number`
|
2022-12-19 06:26:07 +00:00
|
|
|
}, {
|
2022-05-25 12:59:28 +00:00
|
|
|
arg: 'branch',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: `The branch name`
|
2022-12-22 06:12:09 +00:00
|
|
|
}, {
|
|
|
|
arg: 'fromVersion',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: `The old version number`
|
2023-01-31 12:46:42 +00:00
|
|
|
}, {
|
|
|
|
arg: 'description',
|
|
|
|
type: 'string',
|
|
|
|
required: false,
|
|
|
|
description: `The description of changes`
|
2023-01-12 14:25:51 +00:00
|
|
|
}, {
|
2022-11-22 13:29:42 +00:00
|
|
|
arg: 'unlock',
|
|
|
|
type: 'boolean',
|
|
|
|
required: false,
|
|
|
|
description: `It allows unlock the app`
|
2022-05-25 12:59:28 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/upload`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
2023-02-28 12:45:36 +00:00
|
|
|
Self.upload = async(
|
|
|
|
ctx,
|
|
|
|
appName,
|
|
|
|
toVersion,
|
|
|
|
branch,
|
|
|
|
fromVersion,
|
|
|
|
description,
|
2023-02-28 13:35:34 +00:00
|
|
|
unlock,
|
|
|
|
options
|
2023-02-28 12:45:36 +00:00
|
|
|
) => {
|
2022-05-25 12:59:28 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
2022-11-22 13:29:42 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2022-05-27 06:23:52 +00:00
|
|
|
const TempContainer = models.TempContainer;
|
|
|
|
const AccessContainer = models.AccessContainer;
|
|
|
|
const fileOptions = {};
|
|
|
|
let tx;
|
2022-05-25 12:59:28 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
let srcFile;
|
2022-05-27 06:23:52 +00:00
|
|
|
try {
|
2023-01-12 14:25:51 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2022-11-22 13:29:42 +00:00
|
|
|
const mdbApp = await models.MdbApp.findById(appName, null, myOptions);
|
2022-12-01 06:22:13 +00:00
|
|
|
|
2023-01-12 10:52:27 +00:00
|
|
|
if (mdbApp && mdbApp.locked && mdbApp.userFk != userId) {
|
2022-12-01 06:22:13 +00:00
|
|
|
throw new UserError($t('App locked', {
|
|
|
|
userId: mdbApp.userFk
|
|
|
|
}));
|
|
|
|
}
|
2022-11-22 13:29:42 +00:00
|
|
|
|
2022-12-22 06:12:09 +00:00
|
|
|
const existBranch = await models.MdbBranch.findOne({
|
|
|
|
where: {name: branch}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
if (!existBranch)
|
|
|
|
throw new UserError('Not exist this branch');
|
|
|
|
|
2023-01-09 17:28:09 +00:00
|
|
|
let lastMethod = await Self.last(ctx, appName, myOptions);
|
|
|
|
lastMethod.version++;
|
2022-12-22 06:12:09 +00:00
|
|
|
|
2023-01-09 17:28:09 +00:00
|
|
|
if (lastMethod.version != toVersion)
|
2022-12-22 06:12:09 +00:00
|
|
|
throw new UserError('Try again');
|
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
const tempContainer = await TempContainer.container('access');
|
|
|
|
const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
|
|
|
|
const files = Object.values(uploaded.files).map(file => {
|
|
|
|
return file[0];
|
|
|
|
});
|
|
|
|
const uploadedFile = files[0];
|
2022-05-27 11:27:05 +00:00
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
2022-05-31 07:06:15 +00:00
|
|
|
srcFile = path.join(file.client.root, file.container, file.name);
|
2022-05-27 06:23:52 +00:00
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
const accessContainer = await AccessContainer.container('.archive');
|
|
|
|
const destinationFile = path.join(
|
2022-12-22 06:12:09 +00:00
|
|
|
accessContainer.client.root, accessContainer.name, appName, `${toVersion}.7z`);
|
2022-05-31 07:06:15 +00:00
|
|
|
|
2024-05-21 13:11:32 +00:00
|
|
|
if (!isProduction())
|
2022-06-01 05:57:32 +00:00
|
|
|
await fs.unlink(srcFile);
|
|
|
|
else {
|
2022-05-31 07:06:15 +00:00
|
|
|
await fs.move(srcFile, destinationFile, {
|
|
|
|
overwrite: true
|
|
|
|
});
|
|
|
|
await fs.chmod(destinationFile, 0o644);
|
|
|
|
|
2022-06-01 05:57:32 +00:00
|
|
|
const existBranch = await models.MdbBranch.findOne({
|
|
|
|
where: {name: branch}
|
2022-11-22 13:29:42 +00:00
|
|
|
}, myOptions);
|
2022-06-01 05:57:32 +00:00
|
|
|
|
|
|
|
if (!existBranch)
|
|
|
|
throw new UserError('Not exist this branch');
|
|
|
|
|
2022-05-31 07:06:15 +00:00
|
|
|
const branchPath = path.join(accessContainer.client.root, 'branches', branch);
|
|
|
|
await fs.mkdir(branchPath, {recursive: true});
|
|
|
|
|
|
|
|
const destinationBranch = path.join(branchPath, `${appName}.7z`);
|
2022-12-22 06:12:09 +00:00
|
|
|
const destinationRelative = `../../.archive/${appName}/${toVersion}.7z`;
|
2022-05-31 07:06:15 +00:00
|
|
|
try {
|
|
|
|
await fs.unlink(destinationBranch);
|
|
|
|
} catch (e) {}
|
2022-08-05 11:24:48 +00:00
|
|
|
await fs.symlink(destinationRelative, destinationBranch);
|
2022-05-31 07:06:15 +00:00
|
|
|
|
|
|
|
if (branch == 'master') {
|
2022-08-09 09:49:28 +00:00
|
|
|
const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`);
|
2022-12-22 06:12:09 +00:00
|
|
|
const rootRelative = `./.archive/${appName}/${toVersion}.7z`;
|
2022-05-31 07:06:15 +00:00
|
|
|
try {
|
|
|
|
await fs.unlink(destinationRoot);
|
|
|
|
} catch (e) {}
|
2022-08-09 09:49:28 +00:00
|
|
|
await fs.symlink(rootRelative, destinationRoot);
|
2022-05-31 07:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 12:46:42 +00:00
|
|
|
if (description) {
|
|
|
|
let formatDesc;
|
|
|
|
const mainBranches = new Set(['master', 'test', 'dev']);
|
|
|
|
if (mainBranches.has(branch))
|
|
|
|
formatDesc = `> :branch_${branch}: `;
|
|
|
|
else
|
|
|
|
formatDesc = `> :branch: `;
|
|
|
|
|
|
|
|
formatDesc += `*${appName.toUpperCase()}* v.${toVersion} `;
|
|
|
|
|
2023-01-31 13:47:24 +00:00
|
|
|
const oldVersion = await models.MdbVersionTree.findOne({
|
2023-02-28 12:45:36 +00:00
|
|
|
where: {
|
|
|
|
version: fromVersion,
|
|
|
|
app: appName
|
|
|
|
},
|
2023-01-31 12:46:42 +00:00
|
|
|
fields: ['branchFk']
|
2023-01-31 13:47:24 +00:00
|
|
|
}, myOptions);
|
2023-01-31 12:46:42 +00:00
|
|
|
|
2023-02-28 12:45:36 +00:00
|
|
|
if (!oldVersion || branch == oldVersion.branchFk)
|
2023-01-31 12:46:42 +00:00
|
|
|
formatDesc += `[*${branch}*]: `;
|
|
|
|
else
|
2023-01-31 13:47:24 +00:00
|
|
|
formatDesc += `[*${oldVersion.branchFk}* » *${branch}*]: `;
|
2023-01-31 12:46:42 +00:00
|
|
|
|
2023-01-31 13:47:24 +00:00
|
|
|
const params = await models.MdbConfig.findOne(myOptions);
|
|
|
|
const issueTrackerUrl = params.issueTrackerUrl;
|
|
|
|
const issueNumberRegex = params.issueNumberRegex;
|
|
|
|
const chatDestination = params.chatDestination;
|
2023-01-31 12:46:42 +00:00
|
|
|
|
|
|
|
const regex = new RegExp(issueNumberRegex, 'g');
|
|
|
|
formatDesc += description.replace(regex, (match, issueId) => {
|
|
|
|
const newUrl = issueTrackerUrl.replace('{index}', issueId);
|
|
|
|
return `[#${issueId}](${newUrl})`;
|
|
|
|
});
|
2022-05-27 06:23:52 +00:00
|
|
|
|
2023-01-31 12:46:42 +00:00
|
|
|
await models.Chat.send(ctx, chatDestination, formatDesc, myOptions);
|
|
|
|
}
|
2023-01-09 17:28:09 +00:00
|
|
|
await models.MdbVersionTree.create({
|
|
|
|
app: appName,
|
|
|
|
version: toVersion,
|
|
|
|
branchFk: branch,
|
|
|
|
fromVersion,
|
2023-01-31 12:46:42 +00:00
|
|
|
userFk: userId,
|
|
|
|
description,
|
2023-01-09 17:28:09 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
2022-06-01 05:57:32 +00:00
|
|
|
await models.MdbVersion.upsert({
|
|
|
|
app: appName,
|
|
|
|
branchFk: branch,
|
2022-12-22 06:12:09 +00:00
|
|
|
version: toVersion
|
|
|
|
}, myOptions);
|
2022-05-31 07:06:15 +00:00
|
|
|
|
2022-11-22 13:43:15 +00:00
|
|
|
if (unlock) await models.MdbApp.unlock(ctx, appName, myOptions);
|
2022-05-31 07:06:15 +00:00
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
2022-05-31 07:06:15 +00:00
|
|
|
|
2022-06-01 05:57:32 +00:00
|
|
|
if (fs.existsSync(srcFile))
|
2022-12-22 06:12:09 +00:00
|
|
|
fs.unlink(srcFile);
|
2022-05-31 07:06:15 +00:00
|
|
|
|
2022-05-27 06:23:52 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2022-05-25 12:59:28 +00:00
|
|
|
};
|
|
|
|
};
|