1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-24 13:43:12 +02:00
focalboard/import/trello/importTrello.ts

152 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-02-18 01:55:59 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2021-02-16 21:34:58 +02:00
import * as fs from 'fs'
import minimist from 'minimist'
import {exit} from 'process'
2021-03-02 23:21:55 +02:00
import {ArchiveUtils} from '../../webapp/src/blocks/archive'
import {Block} from '../../webapp/src/blocks/block'
import {IPropertyOption, IPropertyTemplate, createBoard} from '../../webapp/src/blocks/board'
import {createBoardView} from '../../webapp/src/blocks/boardView'
import {createCard} from '../../webapp/src/blocks/card'
import {createTextBlock} from '../../webapp/src/blocks/textBlock'
2021-02-16 21:34:58 +02:00
import {Trello} from './trello'
import {Utils} from './utils'
// HACKHACK: To allow Utils.CreateGuid to work
(global.window as any) = {}
2021-02-22 20:54:41 +02:00
const optionColors = [
// 'propColorDefault',
'propColorGray',
'propColorBrown',
'propColorOrange',
'propColorYellow',
'propColorGreen',
'propColorBlue',
'propColorPurple',
'propColorPink',
'propColorRed',
]
let optionColorIndex = 0
2021-02-16 21:34:58 +02:00
function main() {
const args: minimist.ParsedArgs = minimist(process.argv.slice(2))
const inputFile = args['i']
const outputFile = args['o'] || 'archive.focalboard'
if (!inputFile) {
showHelp()
}
2021-02-18 01:55:59 +02:00
if (!fs.existsSync(inputFile)) {
console.error(`File not found: ${inputFile}`)
exit(2)
}
2021-02-16 21:34:58 +02:00
// Read input
const inputData = fs.readFileSync(inputFile, 'utf-8')
const input = JSON.parse(inputData) as Trello
// Convert
2021-03-02 23:21:55 +02:00
const blocks = convert(input)
2021-02-16 21:34:58 +02:00
// Save output
2021-03-02 23:21:55 +02:00
// TODO: Stream output
const outputData = ArchiveUtils.buildBlockArchive(blocks)
2021-02-16 21:34:58 +02:00
fs.writeFileSync(outputFile, outputData)
console.log(`Exported to ${outputFile}`)
}
function convert(input: Trello): Block[] {
const blocks: Block[] = []
2021-02-16 21:34:58 +02:00
// Board
const board = createBoard()
2021-02-16 21:34:58 +02:00
console.log(`Board: ${input.name}`)
board.rootId = board.id
board.title = input.name
board.fields.description = input.desc
2021-02-16 21:34:58 +02:00
// Convert lists (columns) to a Select property
const optionIdMap = new Map<string, string>()
const options: IPropertyOption[] = []
input.lists.forEach(list => {
const optionId = Utils.createGuid()
optionIdMap.set(list.id, optionId)
2021-02-22 20:54:41 +02:00
const color = optionColors[optionColorIndex % optionColors.length]
optionColorIndex += 1
2021-02-16 21:34:58 +02:00
const option: IPropertyOption = {
id: optionId,
value: list.name,
2021-02-22 20:54:41 +02:00
color,
2021-02-16 21:34:58 +02:00
}
options.push(option)
})
const cardProperty: IPropertyTemplate = {
id: Utils.createGuid(),
name: 'List',
type: 'select',
options
}
board.fields.cardProperties = [cardProperty]
2021-02-16 21:34:58 +02:00
blocks.push(board)
// Board view
const view = createBoardView()
2021-02-16 21:34:58 +02:00
view.title = 'Board View'
view.fields.viewType = 'board'
2021-02-16 21:34:58 +02:00
view.rootId = board.id
view.parentId = board.id
blocks.push(view)
// Cards
input.cards.forEach(card => {
console.log(`Card: ${card.name}`)
const outCard = createCard()
2021-02-16 21:34:58 +02:00
outCard.title = card.name
outCard.rootId = board.id
outCard.parentId = board.id
// Map lists to Select property options
if (card.idList) {
const optionId = optionIdMap.get(card.idList)
if (optionId) {
outCard.fields.properties[cardProperty.id] = optionId
2021-02-16 21:34:58 +02:00
} else {
console.warn(`Invalid idList: ${card.idList} for card: ${card.name}`)
}
} else {
console.warn(`Missing idList for card: ${card.name}`)
}
blocks.push(outCard)
if (card.desc) {
// console.log(`\t${card.desc}`)
const text = createTextBlock()
2021-02-16 21:34:58 +02:00
text.title = card.desc
text.rootId = board.id
text.parentId = outCard.id
blocks.push(text)
outCard.fields.contentOrder = [text.id]
2021-02-16 21:34:58 +02:00
}
})
console.log('')
console.log(`Found ${input.cards.length} card(s).`)
2021-03-02 23:21:55 +02:00
return blocks
2021-02-16 21:34:58 +02:00
}
function showHelp() {
console.log('import -i <input.json> -o [output.focalboard]')
2021-02-18 01:55:59 +02:00
exit(1)
2021-02-16 21:34:58 +02:00
}
main()