This site is no longer actively maintained. It exists for historical purposes as an example of Phabricator integration and Lua scripting.

Module:Workboard

From cpt
Revision as of 10:13, 1 August 2019 by Ccicalese (talk | contribs)

Documentation for this module may be created at Module:Workboard/doc

local p = {}

function p.priorityToNumber(priority)
	local priority_map = {};
	priority_map['pink'] = 1
	priority_map['red'] = 2
	priority_map['orange'] = 3
	priority_map['yellow'] = 4
	priority_map['sky'] = 5
	priority_map['violet'] = 6
	if priority_map[priority] then
		return priority_map[priority]
	else
		return '7'
	end
end		

function p.priorityToColor(priority)	
	local priority_map = {};
	priority_map[1] = '#da49be'
	priority_map[2] = '#c0392b'
	priority_map[3] = '#e67e22'
	priority_map[4] = '#f1c40f'
	priority_map[5] = '#3498db'
	priority_map[6] = '#8e44ad'
	priority_map[7] = '#000'
	if priority_map[priority] then
		return priority_map[priority]
	else
		return '#000'
	end
end
	
function p.showWorkboard(frame)
	local s = ''
	local initiative = frame.args[1]
	local result = mw.smw.getQueryResult(
		'[[Has subobject::<q>[[Project::CPT Initiatives]][[Column::' ..
		initiative .. ']]</q>]][[Status::open||stalled]]' ..
		'|?CPT Column|?Priority|link=none|limit=500' )
	if result ~= nil then
		local tasks = {}
		local columns = {}
		local priorities = {}
		local empty = true
		local missing = false
		for k1,v1 in pairs( result.results ) do
			local column = v1.printouts['CPT Column'][1]
			empty = false
			if column ~= nil then
				tasks[v1.fulltext] = column
				columns[column] = true
			else
				tasks[v1.fulltext] = false
				missing = true
			end
			priorities[v1.fulltext] = p.priorityToNumber( v1.printouts['Priority'][1] )
		end
		if ( empty ) then
			s = 'No results'
		else
			local sorted_columns = {}
			for k1,v1 in pairs ( columns ) do
				table.insert( sorted_columns, k1 )
			end
			table.sort( sorted_columns )
			s = s .. '{| style="border-spacing:10px;border-collapse:separate;"\n'
			for k1,v1 in pairs( sorted_columns ) do
				s = s .. '! style="border:1px solid #000;padding:5px;background-color:#bbb;text-align:center;" |' .. v1 .. '\n'
			end
			if ( missing ) then
				s = s .. '! style="border:1px solid #000;padding:5px;background-color:#bbb;text-align:center;" |BACKLOG\n'
			end
			s = s .. '|-\n'
			for k1,v1 in pairs( sorted_columns ) do
				s = s .. '| style="vertical-align:top;min-width:200px;" |\n'
				for priority = 1,7 do
					for k2,v2 in pairs ( tasks ) do
						if priorities[k2] == priority and v1 == v2 then
							s = s .. '{| width=100% style="margin-bottom:10px;"\n| ' ..
								'style="border:1px solid #000;border-left:4px solid ' ..
								p.priorityToColor( priorities[k2] ) ..
								';padding:5px;text-align:center;vertical-align:middle;" |' ..
								'[[File:Phab.png|20px|link=https://phabricator.wikimedia.org/' ..
								string.gsub( k2, 'Phab:', '' ) .. ']] [[' .. k2 .. ']]\n|}\n'
						end
					end
				end
			end
			if ( missing ) then
				s = s .. '| style="vertical-align:top;min-width:200px;" |\n'
				for priority = 1,7 do
					for k1,v1 in pairs ( tasks ) do
						if priorities[k1] == priority and v1 == false then
							s = s .. '{| width=100% style="margin-bottom:10px;"\n| ' ..
								'style="border:1px solid #000;border-left:4px solid ' ..
								p.priorityToColor( priorities[k1] ) ..
								';padding:5px;text-align:center;vertical-align:middle;" |' ..
								'[[File:Phab.png|20px|link=https://phabricator.wikimedia.org/' ..
								string.gsub( k1, 'Phab:', '' ) .. ']] [[' .. k1 .. ']]\n|}\n'
						end
					end
				end
			end
			s = s .. '|}'
		end
	end
	return s
end

return p