view bug-list.xsl @ 4:f8f966388b68

xslt: bugzilla -> taskjuggler
author cin
date Mon, 24 Aug 2015 20:50:23 +0300
parents 36ae3ec94442
children d2efec56373f
line wrap: on
line source

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"
	extension-element-prefixes="exsl">
	<xsl:output method="text" indent="yes" />

	<xsl:variable name="resources"
		select="document('resources.xml')/resources/resource" />

	<xsl:variable name="bugs" select="/bugzilla/bug" />

	<xsl:key name="bugid" match="/bugzilla/bug" use="string(bug_id)" />

	<xsl:variable name="roots" select="$bugs[not(blocked[key('bugid',.)])]" />

	<!-- BUILD BUG TREE -->
	<xsl:variable name="tree">
		<xsl:apply-templates mode="tree" select="$roots" />
	</xsl:variable>

	<xsl:template match="bug" mode="tree">
		<xsl:element name="bug">
			<xsl:attribute name="id"><xsl:value-of select="bug_id" /></xsl:attribute>
			<xsl:if test="component = 'product' or not(number(estimated_time))">
				<xsl:attribute name="product"><xsl:value-of select="boolean(1)" /></xsl:attribute>
			</xsl:if>
			<xsl:for-each select="dependson">
				<xsl:apply-templates mode="tree" select="key('bugid', .)" />
			</xsl:for-each>
		</xsl:element>
	</xsl:template>

	<!-- CALCULATE RELATIONS -->
	<xsl:variable name="relations">
		<xsl:apply-templates select="$bugs" mode="relations" />
	</xsl:variable>

	<xsl:template match="bug" mode="relations">
		<xsl:variable name="bugid" select="string(bug_id)" />
		<bug id="{$bugid}">
			<xsl:apply-templates select="exsl:node-set($tree)//bug[@id = $bugid]"
				mode="traverse-relations">
			</xsl:apply-templates>
		</bug>
	</xsl:template>

	<xsl:template match="bug" mode="traverse-relations">
		<xsl:variable name="bugid" select="@id" />
		<xsl:variable name="path" select="ancestor::bug" />
		<xsl:variable name="level" select="count($path)" />
		<xsl:for-each select="$path[@product]">
			<xsl:variable name="pos" select="position()" />
			<xsl:variable name="rank" select="$level - $pos" />
			<rel container="{@id}" rank="{$rank}" level="{$level}">
				<xsl:for-each select="$path[position() >= $pos and @product]">
					<bug id="{@id}" />
				</xsl:for-each>
			</rel>
		</xsl:for-each>
	</xsl:template>

	<!-- CALCULATE STRUCTURE -->

	<xsl:variable name="parents">
		<xsl:apply-templates select="exsl:node-set($relations)"
			mode="parents" />
	</xsl:variable>

	<xsl:template match="bug" mode="parents">
		<xsl:variable name="parent"
			select="rel[ not(../rel/@rank &lt; @rank) ][1]" />
		<bug id="{@id}" parent="{$parent/@container}" rank="{$parent/@rank}"
			level="{$parent/@level}" />
	</xsl:template>

	<xsl:variable name="structure">
		<xsl:apply-templates select="$roots" mode="structure" />
	</xsl:variable>

	<xsl:template match="bug" mode="structure">
		<xsl:variable name="id" select="string(bug_id)" />
		<xsl:variable name="self" select="." />
		<bug id="{$id}">
			<xsl:for-each select="exsl:node-set($parents)/bug[@parent = $id]">
				<xsl:variable name="child" select="@id" />
				<xsl:apply-templates select="$bugs[bug_id = $child]"
					mode="structure" />
			</xsl:for-each>
			<xsl:variable name="dependencies"
				select="dependson[not(text() = exsl:node-set($parents)/bug[@parent = $id]/@id)]" />
			<xsl:if test="$dependencies">
				<depends>
					<xsl:for-each select="$dependencies">
						<bug id="{.}" />
					</xsl:for-each>
				</depends>
			</xsl:if>
		</bug>
	</xsl:template>

	<!-- output -->
	<xsl:template match="/">
		<xsl:apply-templates select="exsl:node-set($structure)/bug">
			<xsl:with-param name="indent" select="0" />
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="bug">
		<xsl:param name="indent" select="0" />
		<xsl:variable name="id" select="@id" />
		<xsl:variable name="details" select="$bugs[bug_id = $id]" />
		<xsl:call-template name="start-task">
			<xsl:with-param name="indent" select="$indent" />
			<xsl:with-param name="id" select="@id" />
			<xsl:with-param name="desc" select="$details/short_desc" />
		</xsl:call-template>

		<xsl:for-each select="$details/*">
			<xsl:variable name="directive">
				<xsl:apply-templates select="." mode="task-details" />
			</xsl:variable>
			<xsl:if test="string($directive)">
				<xsl:call-template name="begin-line">
					<xsl:with-param name="indent" select="$indent + 1" />
				</xsl:call-template>
				<xsl:value-of select="$directive" />
				<xsl:call-template name="end-line" />
			</xsl:if>
		</xsl:for-each>

		<xsl:apply-templates>
			<xsl:with-param name="indent" select="$indent + 1" />
		</xsl:apply-templates>

		<xsl:call-template name="end-task">
			<xsl:with-param name="indent" select="$indent" />
		</xsl:call-template>
	</xsl:template>

	<xsl:template match="estimated_time" mode="task-details">
		<xsl:if test="number(.)">
			<xsl:value-of select="concat('effort ', .,'h')" />
		</xsl:if>
	</xsl:template>

	<xsl:template match="assigned_to" mode="task-details">
		<xsl:variable name="email" select="string(.)" />
		<xsl:variable name="resource" select="$resources[@email = $email]/@id" />
		<xsl:if test="$resource">
			<xsl:value-of select="concat('allocate ', $resource)" />
		</xsl:if>
	</xsl:template>
	<xsl:template match="text()" mode="task-details">
	</xsl:template>

	<xsl:template match="depends">
		<xsl:param name="indent" select="0" />
		<xsl:call-template name="begin-line">
			<xsl:with-param name="indent" select="$indent" />
		</xsl:call-template>
		<xsl:text>depends </xsl:text>
		<xsl:apply-templates>
			<xsl:with-param name="referer" select=".." />
		</xsl:apply-templates>
		<xsl:call-template name="end-line" />
	</xsl:template>

	<xsl:template match="depends/bug">
		<xsl:param name="referer" />
		<xsl:call-template name="bug-reference">
			<xsl:with-param name="id" select="@id" />
			<xsl:with-param name="referer" select="$referer" />
		</xsl:call-template>
		<xsl:if test="position() != last()">
			<xsl:text>, </xsl:text>
		</xsl:if>
	</xsl:template>


	<xsl:template match="text()" />



	<xsl:template name="bug-local-name">
		<xsl:param name="id" />
		<xsl:value-of select="concat('bug',$id)" />
	</xsl:template>


	<xsl:template name="bug-reference">
		<xsl:param name="id" />
		<xsl:param name="referer" />

		<xsl:variable name="refererPathFragment">
			<bug id="#root" />
			<xsl:for-each select="$referer/ancestor-or-self::bug">
				<bug id="{@id}" />
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="targetPathFragment">
			<bug id="#root" />
			<xsl:for-each
				select="exsl:node-set($structure)//bug[local-name(..) != 'depends' and @id = $id]/ancestor-or-self::bug">
				<bug id="{@id}" />
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="path"
			select="exsl:node-set($refererPathFragment)/bug" />
		<xsl:variable name="targetPath"
			select="exsl:node-set($targetPathFragment)/bug" />

		<!-- find closest shared container id -->
		<xsl:variable name="join"
			select="($path[@id = $targetPath/@id])[position() = last() ]/@id" />

		<!-- how many levels we need to up -->
		<xsl:variable name="depth"
			select="count($path[@id = $join]/following-sibling::node())" />

		<!-- DEBUG -->
		<!-- <xsl:value-of select="concat('id=', $id, ',referer=', $referer/@id)" 
			/> <xsl:text>, path=</xsl:text> <xsl:for-each select="$targetPath"> <xsl:value-of 
			select="concat(name(),@id)" /> <xsl:if test="position() != last()"> <xsl:text>-</xsl:text> 
			</xsl:if> </xsl:for-each> -->
		<!-- DEBUG -->
		<!-- <xsl:value-of select="concat(', join=', $join,', depth=', $depth,' 
			')" /> -->

		<!-- PRINT REFERENCE -->
		<xsl:call-template name="repeat">
			<xsl:with-param name="value" select="'!'" />
			<xsl:with-param name="count" select="$depth" />
		</xsl:call-template>
		<xsl:for-each select="$targetPath[@id = $join]/following-sibling::node()">
			<xsl:call-template name="bug-local-name">
				<xsl:with-param name="id" select="@id" />
			</xsl:call-template>
			<xsl:if test="position() != last()">
				<xsl:text>.</xsl:text>
			</xsl:if>
		</xsl:for-each>

	</xsl:template>

	<xsl:template name="start-task">
		<xsl:param name="indent" />
		<xsl:param name="id" />
		<xsl:param name="desc" />
		<xsl:call-template name="begin-line">
			<xsl:with-param name="indent" select="$indent" />
		</xsl:call-template>
		<xsl:text>task </xsl:text>
		<xsl:call-template name="bug-local-name">
			<xsl:with-param name="id" select="$id" />
		</xsl:call-template>
		<xsl:value-of select="concat(' &quot;',$desc,'&quot; {')" />
		<xsl:call-template name="end-line" />
	</xsl:template>

	<xsl:template name="end-task">
		<xsl:param name="indent" />
		<xsl:call-template name="begin-line">
			<xsl:with-param name="indent" select="$indent" />
		</xsl:call-template>
		<xsl:text>}</xsl:text>
		<xsl:call-template name="end-line" />
	</xsl:template>

	<xsl:template name="begin-line">
		<xsl:param name="indent" />
		<xsl:if test="number($indent) > 0">
			<xsl:text>    </xsl:text>
			<xsl:call-template name="begin-line">
				<xsl:with-param name="indent" select="$indent - 1" />
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template name="end-line">
		<xsl:text>&#xa;</xsl:text>
	</xsl:template>

	<xsl:template name="repeat">
		<xsl:param name="value" />
		<xsl:param name="count" select="0" />

		<xsl:if test="number($count)">
			<xsl:copy-of select="$value" />
			<xsl:call-template name="repeat">
				<xsl:with-param name="value" select="$value" />
				<xsl:with-param name="count" select="$count - 1" />
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>