1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00

feat: Trello board JSON import (#352)

Closes #27, closes #105
This commit is contained in:
Christoph Enne 2022-12-16 23:48:06 +01:00 committed by GitHub
parent c880a72f02
commit 948485c861
20 changed files with 537 additions and 89 deletions

View file

@ -1,7 +1,16 @@
const util = require('util');
const { v4: uuid } = require('uuid');
const Errors = {
PROJECT_NOT_FOUND: {
projectNotFound: 'Project not found',
},
NO_IMPORT_FILE_WAS_UPLOADED: {
noImportFileWasUploaded: 'No import file was uploaded',
},
INVALID_IMPORT_FILE: {
invalidImportFile: 'Invalid import file',
},
};
module.exports = {
@ -24,12 +33,26 @@ module.exports = {
type: 'string',
required: true,
},
importType: {
type: 'string',
isIn: Object.values(Board.ImportTypes),
},
requestId: {
type: 'string',
isNotEmptyString: true,
},
},
exits: {
projectNotFound: {
responseType: 'notFound',
},
noImportFileWasUploaded: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs) {
@ -49,10 +72,42 @@ module.exports = {
const values = _.pick(inputs, ['type', 'position', 'name']);
let boardImport;
if (inputs.importType && Object.values(Board.ImportTypes).includes(inputs.importType)) {
const upload = util.promisify((options, callback) =>
this.req.file('importFile').upload(options, (error, files) => callback(error, files)),
);
let files;
try {
files = await upload({
saveAs: uuid(),
maxBytes: null,
});
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
}
if (files.length === 0) {
throw Errors.NO_IMPORT_FILE_WAS_UPLOADED;
}
const file = _.last(files);
if (inputs.importType === Board.ImportTypes.TRELLO) {
boardImport = {
type: inputs.importType,
board: await sails.helpers.boards.processUploadedTrelloImportFile(file),
};
}
}
const { board, boardMembership } = await sails.helpers.boards.createOne(
values,
boardImport,
currentUser,
project,
inputs.requestId,
this.req,
);

View file

@ -5,6 +5,13 @@ module.exports = {
custom: (value) => _.isPlainObject(value) && _.isFinite(value.position),
required: true,
},
import: {
type: 'json',
custom: (value) =>
value.type &&
Object.values(Board.ImportTypes).includes(value.type) &&
_.isPlainObject(value.board),
},
user: {
type: 'ref',
required: true,
@ -13,6 +20,10 @@ module.exports = {
type: 'ref',
required: true,
},
requestId: {
type: 'string',
isNotEmptyString: true,
},
request: {
type: 'ref',
},
@ -54,6 +65,10 @@ module.exports = {
projectId: inputs.project.id,
}).fetch();
if (inputs.import && inputs.import.type === Board.ImportTypes.TRELLO) {
await sails.helpers.boards.importFromTrello(inputs.user, board, inputs.import.board);
}
const boardMembership = await BoardMembership.create({
boardId: board.id,
userId: inputs.user.id,
@ -66,6 +81,7 @@ module.exports = {
'boardCreate',
{
item: board,
requestId: inputs.requestId,
},
inputs.request,
);

View file

@ -0,0 +1,152 @@
module.exports = {
inputs: {
user: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
trelloBoard: {
type: 'json',
required: true,
},
},
async fn(inputs) {
const trelloToPlankaLabels = {};
const getTrelloLists = () => inputs.trelloBoard.lists.filter((list) => !list.closed);
const getUsedTrelloLabels = () => {
const result = {};
inputs.trelloBoard.cards
.map((card) => card.labels)
.flat()
.forEach((label) => {
result[label.id] = label;
});
return Object.values(result);
};
const getTrelloCardsOfList = (listId) =>
inputs.trelloBoard.cards.filter((card) => card.idList === listId && !card.closed);
const getAllTrelloCheckItemsOfCard = (cardId) =>
inputs.trelloBoard.checklists
.filter((checklist) => checklist.idCard === cardId)
.map((checklist) => checklist.checkItems)
.flat();
const getTrelloCommentsOfCard = (cardId) =>
inputs.trelloBoard.actions.filter(
(action) =>
action.type === 'commentCard' &&
action.data &&
action.data.card &&
action.data.card.id === cardId,
);
const getPlankaLabelColor = (trelloLabelColor) =>
Label.COLORS.find((color) => color.indexOf(trelloLabelColor) !== -1) || 'desert-sand';
const importCardLabels = async (plankaCard, trelloCard) => {
return Promise.all(
trelloCard.labels.map(async (trelloLabel) => {
return CardLabel.create({
cardId: plankaCard.id,
labelId: trelloToPlankaLabels[trelloLabel.id].id,
});
}),
);
};
const importTasks = async (plankaCard, trelloCard) => {
// TODO find workaround for tasks/checklist mismapping, see issue trello2planka#5
return Promise.all(
getAllTrelloCheckItemsOfCard(trelloCard.id).map(async (trelloCheckItem) => {
return Task.create({
cardId: plankaCard.id,
position: trelloCheckItem.pos,
name: trelloCheckItem.name,
isCompleted: trelloCheckItem.state === 'complete',
}).fetch();
}),
);
};
const importComments = async (plankaCard, trelloCard) => {
const trelloComments = getTrelloCommentsOfCard(trelloCard.id);
trelloComments.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
return Promise.all(
trelloComments.map(async (trelloComment) => {
return Action.create({
cardId: plankaCard.id,
userId: inputs.user.id,
type: 'commentCard',
data: {
text:
`${trelloComment.data.text}\n\n---\n*Note: imported comment, originally posted by ` +
`\n${trelloComment.memberCreator.fullName} (${trelloComment.memberCreator.username}) on ${trelloComment.date}*`,
},
}).fetch();
}),
);
};
const importCards = async (plankaList, trelloList) => {
return Promise.all(
getTrelloCardsOfList(trelloList.id).map(async (trelloCard) => {
const plankaCard = await Card.create({
boardId: inputs.board.id,
listId: plankaList.id,
creatorUserId: inputs.user.id,
position: trelloCard.pos,
name: trelloCard.name,
description: trelloCard.desc || null,
}).fetch();
await importCardLabels(plankaCard, trelloCard);
await importTasks(plankaCard, trelloCard);
await importComments(plankaCard, trelloCard);
return plankaCard;
}),
);
};
const importLabels = async () => {
return Promise.all(
getUsedTrelloLabels().map(async (trelloLabel) => {
const plankaLabel = await Label.create({
boardId: inputs.board.id,
name: trelloLabel.name || null,
color: getPlankaLabelColor(trelloLabel.color),
}).fetch();
trelloToPlankaLabels[trelloLabel.id] = plankaLabel;
}),
);
};
const importLists = async () => {
return Promise.all(
getTrelloLists().map(async (trelloList) => {
const plankaList = await List.create({
boardId: inputs.board.id,
name: trelloList.name,
position: trelloList.pos,
}).fetch();
return importCards(plankaList, trelloList);
}),
);
};
await importLabels();
await importLists();
},
};

View file

@ -0,0 +1,38 @@
const fs = require('fs').promises;
const rimraf = require('rimraf');
module.exports = {
inputs: {
file: {
type: 'json',
required: true,
},
},
exits: {
invalidFile: {},
},
async fn(inputs) {
const content = await fs.readFile(inputs.file.fd);
const trelloBoard = JSON.parse(content);
if (
!trelloBoard ||
!_.isArray(trelloBoard.lists) ||
!_.isArray(trelloBoard.cards) ||
!_.isArray(trelloBoard.checklists) ||
!_.isArray(trelloBoard.actions)
) {
throw 'invalidFile';
}
try {
rimraf.sync(inputs.file.fd);
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
return trelloBoard;
},
};

View file

@ -10,8 +10,13 @@ const Types = {
COLLECTION: 'collection',
};
const ImportTypes = {
TRELLO: 'trello',
};
module.exports = {
Types,
ImportTypes,
attributes: {
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗