2021-10-20 18:13:53 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import {exit} from 'process'
|
2022-02-14 17:38:45 +02:00
|
|
|
import {ArchiveUtils} from '../util/archive'
|
2021-10-20 18:13:53 +02:00
|
|
|
import {Block} from '../../webapp/src/blocks/block'
|
2022-04-01 04:19:04 +02:00
|
|
|
import {Board} from '../../webapp/src/blocks/board'
|
2021-10-20 18:13:53 +02:00
|
|
|
import {IPropertyOption, IPropertyTemplate, createBoard} from '../../webapp/src/blocks/board'
|
|
|
|
import {createBoardView} from '../../webapp/src/blocks/boardView'
|
|
|
|
import {Card, createCard} from '../../webapp/src/blocks/card'
|
|
|
|
import {createTextBlock} from '../../webapp/src/blocks/textBlock'
|
|
|
|
import {Utils} from './utils'
|
|
|
|
import xml2js, {ParserOptions} from 'xml2js'
|
|
|
|
import TurndownService from 'turndown'
|
|
|
|
|
|
|
|
// HACKHACK: To allow Utils.CreateGuid to work
|
|
|
|
(global.window as any) = {}
|
|
|
|
|
|
|
|
const optionColors = [
|
|
|
|
'propColorGray',
|
|
|
|
'propColorBrown',
|
|
|
|
'propColorOrange',
|
|
|
|
'propColorYellow',
|
|
|
|
'propColorGreen',
|
|
|
|
'propColorBlue',
|
|
|
|
'propColorPurple',
|
|
|
|
'propColorPink',
|
|
|
|
'propColorRed',
|
|
|
|
]
|
|
|
|
let optionColorIndex = 0
|
|
|
|
|
|
|
|
var turndownService = new TurndownService()
|
|
|
|
|
|
|
|
async function run(inputFile: string, outputFile: string): Promise<number> {
|
|
|
|
console.log(`input: ${inputFile}`)
|
|
|
|
console.log(`output: ${outputFile}`)
|
|
|
|
|
|
|
|
if (!inputFile) {
|
|
|
|
showHelp()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(inputFile)) {
|
|
|
|
console.error(`File not found: ${inputFile}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read input
|
|
|
|
console.log(`Reading ${inputFile}`)
|
|
|
|
const inputData = fs.readFileSync(inputFile, 'utf-8')
|
|
|
|
|
|
|
|
if (!inputData) {
|
|
|
|
console.error(`Unable to read data from file: ${inputFile}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Read ${Math.round(inputData.length / 1024)} KB`)
|
|
|
|
|
|
|
|
const parserOptions: ParserOptions = {
|
|
|
|
explicitArray: false
|
|
|
|
}
|
|
|
|
const parser = new xml2js.Parser(parserOptions);
|
|
|
|
const input = await parser.parseStringPromise(inputData)
|
|
|
|
|
|
|
|
if (!input?.rss?.channel) {
|
|
|
|
console.error(`No channels in xml: ${inputFile}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
const channel = input.rss.channel
|
|
|
|
const items = channel.item
|
|
|
|
|
|
|
|
// console.dir(items);
|
|
|
|
|
|
|
|
// Convert
|
2022-04-01 04:19:04 +02:00
|
|
|
const [boards, blocks] = convert(items)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
// Save output
|
|
|
|
// TODO: Stream output
|
2022-04-01 04:19:04 +02:00
|
|
|
const outputData = ArchiveUtils.buildBlockArchive(boards, blocks)
|
2021-10-20 18:13:53 +02:00
|
|
|
fs.writeFileSync(outputFile, outputData)
|
|
|
|
console.log(`Exported ${blocks.length} block(s) to ${outputFile}`)
|
|
|
|
|
|
|
|
return blocks.length
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:19:04 +02:00
|
|
|
function convert(items: any[]): [Board[], Block[]] {
|
|
|
|
const boards: Board[] = []
|
2021-10-20 18:13:53 +02:00
|
|
|
const blocks: Block[] = []
|
|
|
|
|
|
|
|
// Board
|
|
|
|
const board = createBoard()
|
|
|
|
board.title = 'Jira import'
|
|
|
|
|
|
|
|
// Compile standard properties
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties = []
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const priorityProperty = buildCardPropertyFromValues('Priority', items.map(o => o.priority?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(priorityProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const statusProperty = buildCardPropertyFromValues('Status', items.map(o => o.status?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(statusProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const resolutionProperty = buildCardPropertyFromValues('Resolution', items.map(o => o.resolution?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(resolutionProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const typeProperty = buildCardPropertyFromValues('Type', items.map(o => o.type?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(typeProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const assigneeProperty = buildCardPropertyFromValues('Assignee', items.map(o => o.assignee?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(assigneeProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const reporterProperty = buildCardPropertyFromValues('Reporter', items.map(o => o.reporter?._))
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(reporterProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const originalUrlProperty: IPropertyTemplate = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
name: 'Original URL',
|
|
|
|
type: 'url',
|
|
|
|
options: []
|
|
|
|
}
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(originalUrlProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
const createdDateProperty: IPropertyTemplate = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
name: 'Created Date',
|
|
|
|
type: 'date',
|
|
|
|
options: []
|
|
|
|
}
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(createdDateProperty)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
2022-04-01 04:19:04 +02:00
|
|
|
boards.push(board)
|
2021-10-20 18:13:53 +02:00
|
|
|
|
|
|
|
// Board view
|
|
|
|
const view = createBoardView()
|
|
|
|
view.title = 'Board View'
|
|
|
|
view.fields.viewType = 'board'
|
2022-04-01 04:19:04 +02:00
|
|
|
view.boardId = board.id
|
2021-10-20 18:13:53 +02:00
|
|
|
view.parentId = board.id
|
|
|
|
blocks.push(view)
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
console.log(
|
|
|
|
`Item: ${item.summary}, ` +
|
|
|
|
`priority: ${item.priority?._}, ` +
|
|
|
|
`status: ${item.status?._}, ` +
|
|
|
|
`type: ${item.type?._}`)
|
|
|
|
|
|
|
|
const card = createCard()
|
|
|
|
card.title = item.summary
|
2022-04-01 04:19:04 +02:00
|
|
|
card.boardId = board.id
|
2021-10-20 18:13:53 +02:00
|
|
|
card.parentId = board.id
|
|
|
|
|
|
|
|
// Map standard properties
|
|
|
|
if (item.priority?._) { setSelectProperty(card, priorityProperty, item.priority._) }
|
|
|
|
if (item.status?._) { setSelectProperty(card, statusProperty, item.status._) }
|
|
|
|
if (item.resolution?._) { setSelectProperty(card, resolutionProperty, item.resolution._) }
|
|
|
|
if (item.type?._) { setSelectProperty(card, typeProperty, item.type._) }
|
|
|
|
if (item.assignee?._) { setSelectProperty(card, assigneeProperty, item.assignee._) }
|
|
|
|
if (item.reporter?._) { setSelectProperty(card, reporterProperty, item.reporter._) }
|
|
|
|
|
|
|
|
if (item.link) { setProperty(card, originalUrlProperty.id, item.link)}
|
|
|
|
if (item.created) {
|
|
|
|
const dateInMs = Date.parse(item.created)
|
|
|
|
setProperty(card, createdDateProperty.id, dateInMs.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Map custom properties
|
|
|
|
|
|
|
|
if (item.description) {
|
|
|
|
const description = turndownService.turndown(item.description)
|
|
|
|
console.log(`\t${description}`)
|
|
|
|
const text = createTextBlock()
|
|
|
|
text.title = description
|
2022-04-01 04:19:04 +02:00
|
|
|
text.boardId = board.id
|
2021-10-20 18:13:53 +02:00
|
|
|
text.parentId = card.id
|
|
|
|
blocks.push(text)
|
|
|
|
|
|
|
|
card.fields.contentOrder = [text.id]
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks.push(card)
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:19:04 +02:00
|
|
|
return [boards, blocks]
|
2021-10-20 18:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildCardPropertyFromValues(propertyName: string, allValues: string[]) {
|
|
|
|
const options: IPropertyOption[] = []
|
|
|
|
|
|
|
|
// Remove empty and duplicate values
|
|
|
|
const values = allValues.
|
|
|
|
filter(o => !!o).
|
|
|
|
filter((x, y) => allValues.indexOf(x) == y);
|
|
|
|
|
|
|
|
for (const value of values) {
|
|
|
|
const optionId = Utils.createGuid()
|
|
|
|
const color = optionColors[optionColorIndex % optionColors.length]
|
|
|
|
optionColorIndex += 1
|
|
|
|
const option: IPropertyOption = {
|
|
|
|
id: optionId,
|
|
|
|
value,
|
|
|
|
color,
|
|
|
|
}
|
|
|
|
options.push(option)
|
|
|
|
}
|
|
|
|
|
|
|
|
const cardProperty: IPropertyTemplate = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
name: propertyName,
|
|
|
|
type: 'select',
|
|
|
|
options
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Property: ${propertyName}, values: ${values}`)
|
|
|
|
|
|
|
|
return cardProperty
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSelectProperty(card: Card, cardProperty: IPropertyTemplate, propertyValue: string) {
|
|
|
|
const option = optionForPropertyValue(cardProperty, propertyValue)
|
|
|
|
if (option) {
|
|
|
|
card.fields.properties[cardProperty.id] = option.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setProperty(card: Card, cardPropertyId: string, propertyValue: string) {
|
|
|
|
card.fields.properties[cardPropertyId] = propertyValue
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionForPropertyValue(cardProperty: IPropertyTemplate, propertyValue: string): IPropertyOption | null {
|
|
|
|
const option = cardProperty.options.find(o => o.value === propertyValue)
|
|
|
|
if (!option) {
|
|
|
|
console.error(`Property value not found: ${propertyValue}`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return option
|
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp() {
|
2022-10-19 21:33:06 +02:00
|
|
|
console.log('import -i <input.xml> -o [output.boardarchive]')
|
2021-10-20 18:13:53 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
export { run }
|