mirror of
https://github.com/plankanban/planka.git
synced 2025-08-04 21:15:25 +02:00
Add an import feature for import Trello board as JSON to Planka
This commit is contained in:
parent
cf21bef9ee
commit
a128874e95
22 changed files with 752 additions and 34620 deletions
157
server/api/controllers/projects/import.js
Normal file
157
server/api/controllers/projects/import.js
Normal file
|
@ -0,0 +1,157 @@
|
|||
const got = require('got');
|
||||
|
||||
const Errors = {
|
||||
INVALID_IMPORT_FILE: {
|
||||
invalidImport: 'Invalid Import',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
exits: {
|
||||
cardNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
uploadError: {
|
||||
responseType: 'unprocessableEntity',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
this.req.file('file').upload(sails.helpers.downloadImportData(), async (error, files) => {
|
||||
try {
|
||||
const data = JSON.parse(files[0].extra);
|
||||
const { project, projectMembership } = await sails.helpers.createProject(
|
||||
currentUser,
|
||||
{ name: data.name },
|
||||
this.req,
|
||||
true,
|
||||
false,
|
||||
);
|
||||
|
||||
// Création du tableau
|
||||
const board = await sails.helpers.createBoard(
|
||||
project,
|
||||
{ name: 'Premier tableau', position: 0, type: 'kanban' },
|
||||
this.req,
|
||||
);
|
||||
|
||||
// Création des labels
|
||||
const labels = new Map();
|
||||
/* eslint-disable no-await-in-loop */
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const l of data.labels) {
|
||||
labels.set(
|
||||
l.id,
|
||||
await sails.helpers.createLabel(board, {
|
||||
name: l.name || '?',
|
||||
color: await sails.helpers.getColorFromTrello(l.color),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Création des listes
|
||||
const lists = new Map();
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const l of data.lists) {
|
||||
lists.set(
|
||||
l.id,
|
||||
await sails.helpers.createList(board, {
|
||||
name: l.name || '?',
|
||||
position: l.pos,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Création des cartes
|
||||
const cards = new Map();
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const c of data.cards) {
|
||||
const card = await sails.helpers.createCard(
|
||||
board,
|
||||
lists.get(c.idList),
|
||||
{
|
||||
position: Math.round(c.pos),
|
||||
name: c.name,
|
||||
description: c.desc || null,
|
||||
},
|
||||
currentUser,
|
||||
this.req,
|
||||
);
|
||||
cards.set(c.id, card);
|
||||
c.idLabels.forEach(async (l) => {
|
||||
await sails.helpers.createCardLabel(card, labels.get(l), this.req);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const a of c.attachments) {
|
||||
if (a.url.startsWith('https://trello-attachments.s3.amazonaws.com/'))
|
||||
got
|
||||
.stream(a.url)
|
||||
.pipe(sails.helpers.importAttachmentReceiver(a, card, currentUser, {}, this.req));
|
||||
}
|
||||
}
|
||||
|
||||
// Création des commentaires
|
||||
const listOfComments = data.actions.filter((a) => a.type === 'commentCard');
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const comment of listOfComments) {
|
||||
const userName =
|
||||
data.members.find((u) => u.id === comment.idMemberCreator).fullName || 'Inconnu';
|
||||
await sails.helpers.createAction(
|
||||
cards.get(comment.data.card.id),
|
||||
currentUser,
|
||||
{
|
||||
type: 'commentCard',
|
||||
data: { text: `${userName} - ${comment.data.text}` },
|
||||
},
|
||||
this.req,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const listOfTask of data.checklists) {
|
||||
const card = cards.get(listOfTask.idCard);
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const task of listOfTask.checkItems) {
|
||||
await sails.helpers.createTask(
|
||||
card,
|
||||
{
|
||||
name: task.name,
|
||||
isCompleted: task.state === 'complete',
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`user:${projectMembership.userId}`,
|
||||
'projectCreate',
|
||||
{
|
||||
item: project,
|
||||
included: {
|
||||
users: [currentUser],
|
||||
projectMemberships: [projectMembership],
|
||||
boards: await sails.helpers.getBoardsForProject(project.id),
|
||||
},
|
||||
},
|
||||
this.req,
|
||||
);
|
||||
|
||||
return exits.success({
|
||||
item: project,
|
||||
included: {
|
||||
users: [currentUser],
|
||||
projectMemberships: [projectMembership],
|
||||
boards: await sails.helpers.getBoardsForProject(project.id),
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
throw Errors.INVALID_IMPORT_FILE;
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
|
@ -15,6 +15,10 @@ module.exports = {
|
|||
type: 'boolean',
|
||||
defaultsTo: false,
|
||||
},
|
||||
withBroadcast: {
|
||||
type: 'boolean',
|
||||
defaultsTo: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
|
@ -25,19 +29,20 @@ module.exports = {
|
|||
userId: inputs.user.id,
|
||||
}).fetch();
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`user:${projectMembership.userId}`,
|
||||
'projectCreate',
|
||||
{
|
||||
item: project,
|
||||
included: {
|
||||
users: [inputs.user],
|
||||
projectMemberships: [projectMembership],
|
||||
boards: [],
|
||||
if (inputs.withBroadcast)
|
||||
sails.sockets.broadcast(
|
||||
`user:${projectMembership.userId}`,
|
||||
'projectCreate',
|
||||
{
|
||||
item: project,
|
||||
included: {
|
||||
users: [inputs.user],
|
||||
projectMemberships: [projectMembership],
|
||||
boards: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
return exits.success(
|
||||
inputs.withProjectMembership
|
||||
|
|
33
server/api/helpers/download-import-data.js
Normal file
33
server/api/helpers/download-import-data.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const util = require('util');
|
||||
const stream = require('stream');
|
||||
const streamToArray = require('stream-to-array');
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
|
||||
fn(inputs, exits) {
|
||||
const receiver = stream.Writable({
|
||||
objectMode: true,
|
||||
});
|
||||
|
||||
let firstFileHandled = false;
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
receiver._write = async (file, receiverEncoding, done) => {
|
||||
if (firstFileHandled) {
|
||||
file.pipe(new stream.Writable());
|
||||
|
||||
return done();
|
||||
}
|
||||
firstFileHandled = true;
|
||||
|
||||
const buffer = await streamToArray(file).then((parts) =>
|
||||
Buffer.concat(parts.map((part) => (util.isBuffer(part) ? part : Buffer.from(part)))),
|
||||
);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
file.extra = buffer.toString('UTF-8');
|
||||
return done();
|
||||
};
|
||||
|
||||
return exits.success(receiver);
|
||||
},
|
||||
};
|
35
server/api/helpers/get-color-from-trello.js
Normal file
35
server/api/helpers/get-color-from-trello.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
module.exports = {
|
||||
inputs: {
|
||||
color: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs, exits) {
|
||||
switch (inputs.color) {
|
||||
case 'green':
|
||||
return exits.success('bright-moss');
|
||||
case 'yellow':
|
||||
return exits.success('egg-yellow');
|
||||
case 'orange':
|
||||
return exits.success('pumpkin-orange');
|
||||
case 'red':
|
||||
return exits.success('berry-red');
|
||||
case 'purple':
|
||||
return exits.success('red-burgundy');
|
||||
case 'blue':
|
||||
return exits.success('lagoon-blue');
|
||||
case 'sky':
|
||||
return exits.success('morning-sky');
|
||||
case 'lime':
|
||||
return exits.success('sunny-grass');
|
||||
case 'pink':
|
||||
return exits.success('pink-tulip');
|
||||
case 'black':
|
||||
return exits.success('dark-granite');
|
||||
default:
|
||||
return exits.success('berry-red');
|
||||
}
|
||||
},
|
||||
};
|
81
server/api/helpers/import-attachment-receiver.js
Normal file
81
server/api/helpers/import-attachment-receiver.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const writeFile = util.promisify(fs.writeFile);
|
||||
|
||||
module.exports = {
|
||||
sync: true,
|
||||
inputs: {
|
||||
attachment: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
card: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
user: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
values: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
fn(inputs, exits) {
|
||||
const dirname = uuid();
|
||||
const filename = inputs.attachment.fileName;
|
||||
const rootPath = path.join(sails.config.custom.attachmentsPath, dirname);
|
||||
fs.mkdirSync(rootPath);
|
||||
const writeStream = fs.createWriteStream(path.join(rootPath, filename));
|
||||
writeStream.on('finish', async () => {
|
||||
const image = sharp(fs.readFileSync(path.join(rootPath, filename)));
|
||||
let imageMetadata;
|
||||
|
||||
try {
|
||||
imageMetadata = await image.metadata();
|
||||
} catch (error) {} // eslint-disable-line no-empty
|
||||
|
||||
if (imageMetadata) {
|
||||
let cover256Buffer;
|
||||
if (imageMetadata.height > imageMetadata.width) {
|
||||
cover256Buffer = await image.resize(256, 320).jpeg().toBuffer();
|
||||
} else {
|
||||
cover256Buffer = await image
|
||||
.resize({
|
||||
width: 256,
|
||||
})
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
const thumbnailsPath = path.join(rootPath, 'thumbnails');
|
||||
fs.mkdirSync(thumbnailsPath);
|
||||
|
||||
await writeFile(path.join(thumbnailsPath, 'cover-256.jpg'), cover256Buffer);
|
||||
|
||||
await sails.helpers.createAttachment(
|
||||
inputs.card,
|
||||
inputs.user,
|
||||
{
|
||||
dirname,
|
||||
filename: inputs.attachment.name,
|
||||
isImage: !!imageMetadata,
|
||||
name: inputs.attachment.name,
|
||||
},
|
||||
null,
|
||||
inputs.request,
|
||||
);
|
||||
}
|
||||
});
|
||||
return exits.success(writeStream);
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue