All files / kanbn/src/controller history.js

99.04% Statements 104/105
88.09% Branches 37/42
100% Functions 3/3
99.04% Lines 104/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 1061x 1x 1x 1x 1x 3x 3x 3x 1x 1x 3x 1x 1x 3x 1x 1x 3x 1x 1x 3x 3x 1x 2x 2x 1x 1x 1x 2x 1x 1x 9x 9x 9x 1x 1x 1x 8x 8x 8x 8x 9x 1x 1x 1x 1x 1x 8x 8x 8x 9x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 7x 7x 7x 9x 1x 1x 7x 7x 7x 9x 1x 1x 7x 7x 7x 7x 7x 2x 2x 2x 5x 7x 1x 1x 1x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 7x 7x   7x 1x  
const kanbn = require('../main');
const utility = require('../utility');
const chrono = require('chrono-node');
const formatDate = require('dateformat');
 
function formatHistoryEvent(event) {
	const details = [];
	if ('column' in event.event) {
		details.push(`column: ${event.event.column}`);
	}
	if ('fromColumn' in event.event || 'toColumn' in event.event) {
		details.push(`column: ${event.event.fromColumn || '?'} -> ${event.event.toColumn || '?'}`);
	}
	if ('fromProgress' in event.event || 'toProgress' in event.event) {
		details.push(`progress: ${event.event.fromProgress || 0} -> ${event.event.toProgress || 0}`);
	}
	if ('author' in event.event && event.event.author) {
		details.push(`author: ${event.event.author}`);
	}
	return details.join(', ');
}
 
function formatMetadataEvent(event, dateFormat) {
	if (!('field' in event.event) || !('value' in event.event) || !(event.event.value instanceof Date)) {
		return '';
	}
	return `${event.event.field}: ${formatDate(event.event.value, dateFormat)}`;
}
 
module.exports = async args => {
 
	// Make sure kanbn has been initialised
	if (!await kanbn.initialised()) {
		utility.error('Kanbn has not been initialised in this folder\nTry running: {b}kanbn init{b}');
		return;
	}
	const index = await kanbn.getIndex();
 
	// Get sprint numbers or names
	let sprints = null;
	if (args.sprint) {
		sprints = utility.arrayArg(args.sprint).map(s => {
			const sprintNumber = parseInt(s);
			return isNaN(sprintNumber) ? s : sprintNumber;
		});
	}
 
	// Get dates
	let dates = null;
	if (args.date) {
		dates = utility.arrayArg(args.date);
		if (dates.length) {
			for (let i = 0; i < dates.length; i++) {
				const dateValue = chrono.parseDate(dates[i]);
				if (dateValue === null) {
					utility.error('Unable to parse date');
					return;
				}
				dates[i] = dateValue;
			}
		}
	}
 
	// Get assigned
	let assigned = null;
	if (args.assigned) {
		assigned = utility.strArg(args.assigned);
	}
 
	// Get task ids
	let taskIds = null;
	if (args.task) {
		taskIds = utility.arrayArg(args.task).map((taskId) => utility.trimLeftEscapeCharacters(taskId));
	}
 
	kanbn
	.history(sprints, dates, assigned, taskIds)
	.then(data => {
		if (args.json) {
			console.log(JSON.stringify(data, null, 2));
			return;
		}
 
		if (data.events.length === 0) {
			console.log('No history events found');
			return;
		}
 
		const dateFormat = kanbn.getDateFormat(index);
		const lines = data.events.map((event) => {
			const eventDetails = event.source === 'history'
				? formatHistoryEvent(event)
				: formatMetadataEvent(event, dateFormat);
			const source = event.source === 'history' ? event.eventType : `metadata.${event.eventType}`;
			const line = `${formatDate(event.date, dateFormat)} | ${event.task.id} | ${source}`;
			return eventDetails
				? `${line} | ${eventDetails}`
				: line;
		});
		console.log(lines.join('\n'));
	})
	.catch(error => {
		utility.error(error);
	});
};