<extension>
  <title>Bugzilla</title>
  <description>Embeds a link to a bug and optionally reflects state</description>
  <uri.help>http://whacked.net</uri.help>

  <function>
    <name>link</name>
	<description>Embed a link to a bug and optionally reflect its state</description>
	<param name="id" type="num">bug id</param>
	<param name="state" type="bool" optional="true">reflect state</param>
	<param name="prefix" type="str" optional="true">string prefix</param>
    <return>
		<html xmlns:eval="http://mindtouch.com/2007/dekiscript">
			<head><style type="text/css">
.bugClosed { text-decoration: line-through; }
.bugzilla-xml { display: none; }
			</style></head>
			<body>
				<span class="bugzilla-xml"><pre eval:id="'bug-xml-' .. args.id"><eval:expr>web.text(config["bugzilla-uri"] .. "/show_bug.cgi?ctype=xml&amp;excludefield=attachmentdata&amp;id=" .. args.id .. "&amp;foobazo=" .. Date.Seconds(Date.Now))</eval:expr></pre></span>
				<span class="bugzilla-bug-link"><a eval:state="args.state ?? true" class="bugzilla-bug" eval:bugid="args.id" eval:href="config['bugzilla-uri'] .. '/show_bug.cgi?id=' .. args.id"><eval:if test="args.prefix != nil"><eval:expr>args.prefix</eval:expr></eval:if><eval:else>bug</eval:else><eval:expr>" " .. args.id</eval:expr></a></span>
			</body>
			<tail>
				<script type="text/javascript">
function html_entity_decode(str) {
	var ta = document.createElement("textarea");
	ta.innerHTML=str.replace(/&lt;/g,"&amp;lt;").replace(/>/g,"&amp;gt;");
	toReturn = ta.value;
	ta = null;
	return toReturn
}

function getText(bug, tag) {
	return bug.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

var a = DekiWiki.$("span.bugzilla-bug-link a")
	.each(function(i)
{
	this.id = this.id + "-" + i;
	var reflectState = this.getAttribute("state") == "true";
	var bugid = this.getAttribute("bugid");
	var xmldoc = html_entity_decode(document.getElementById("bug-xml-"+bugid).innerHTML);
	var bugxml = (new DOMParser()).parseFromString(xmldoc, "text/xml")
			.getElementsByTagName("bugzilla")[0].getElementsByTagName("bug")[0];
	var title = getText(bugxml, "short_desc");
	var status = getText(bugxml, "bug_status");
	var comments = bugxml.getElementsByTagName("long_desc");
	var lastComment = getText(comments[comments.length-1], "thetext");
	this.setAttribute("title", title);
	if (reflectState &amp;&amp; (status == "CLOSED" || status == "VERIFIED"
			|| status == "RESOLVED"))
		this.className = this.className + " bugClosed";
});
				</script>
			</tail>
		</html>
	</return>
  </function>


<function>
	<name>priority_bugs</name>
	<description>All open bugs of the given priority assigned to the specified person</description>
	<param name="assigned_to" type="str">Who the bug is assigned to</param>
	<param name="priority" type="str">Priority</param>
	<return>
		<html xmlns:eval="http://mindtouch.com/2007/dekiscript">
			<head><style type="text/css">
.bugzilla-csv { display: none; }
			</style></head>
			<body>
				<span class="bugzilla-priority-list">
					<pre class="bugzilla-csv"><eval:expr>web.text(config["bugzilla-uri"] .. "/buglist.cgi?query_format=advanced&amp;short_desc_type=allwordssubstr&amp;short_desc=&amp;long_desc_type=allwordssubstr&amp;long_desc=&amp;bug_file_loc_type=allwordssubstr&amp;bug_file_loc=&amp;status_whiteboard_type=allwordssubstr&amp;status_whiteboard=&amp;keywords_type=allwords&amp;keywords=&amp;deadlinefrom=&amp;deadlineto=&amp;bug_status=UNCONFIRMED&amp;bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;priority=" .. args.priority .. "&amp;emailassigned_to1=1&amp;emailtype1=exact&amp;email1=" .. args.assigned_to .. "&amp;emailtype2=substring&amp;email2=&amp;bugidtype=include&amp;bug_id=&amp;votes=&amp;chfieldfrom=&amp;chfieldto=Now&amp;chfieldvalue=&amp;cmdtype=doit&amp;order=Reuse+same+sort+as+last+time&amp;ctype=csv")</eval:expr></pre>
					<ul>
					</ul>
				</span>
			</body>
			<tail>
				<script type="text/javascript">
var a = DekiWiki.$("span.bugzilla-priority-list").each(function(i)
{
	var list = DekiWiki.$(this).find('ul');
	var buglist = DekiWiki.$(this).find('pre').html();
	buglist = buglist.split("\n");

	// shift off the column header
	buglist.shift();

	for (entry=0; entry &lt; buglist.length; entry++) {
		var cols = buglist[entry].split(",");
		var bugid = cols[0];
		var desc = cols[7].replace(/^"/,"").replace(/"$/,"");
		var str = "&lt;li&gt;Bug " + bugid + " - " + desc + "&lt;/li&gt;";
		DekiWiki.$(list).append(str);
	}
});
				</script>
			</tail>
		</html>
	</return>
</function>

<function>
	<name>fixed_bugs</name>
	<description>All bugs fixed by the specified person within a given date range</description>
	<param name="assigned_to" type="str">Who the bug was assigned to</param>
	<param name="start_date" type="str">Start date</param>
	<param name="end_date" type="str">End date</param>
	<return>
		<html xmlns:eval="http://mindtouch.com/2007/dekiscript">
			<head><style type="text/css">
.bugzilla-fixed-csv { display: none; }
			</style></head>
			<body>
				<span class="bugzilla-fixed-list">
					<pre class="bugzilla-fixed-csv"><eval:expr>web.text(config["bugzilla-uri"] .. "/buglist.cgi?query_format=advanced&amp;short_desc_type=allwordssubstr&amp;short_desc=&amp;long_desc_type=allwordssubstr&amp;long_desc=&amp;bug_file_loc_type=allwordssubstr&amp;bug_file_loc=&amp;status_whiteboard_type=allwordssubstr&amp;status_whiteboard=&amp;keywords_type=allwords&amp;keywords=&amp;deadlinefrom=&amp;deadlineto=&amp;bug_status=RESOLVED&amp;bug_status=VERIFIED&amp;emailassigned_to1=1&amp;emailtype1=exact&amp;email1=" .. args.assigned_to .. "&amp;emailtype2=substring&amp;email2=&amp;bugidtype=include&amp;bug_id=&amp;votes=&amp;chfieldfrom=" .. args.start_date .. "&amp;chfieldto=" .. args.end_date .. "&amp;chfield=resolution&amp;chfieldvalue=FIXED&amp;cmdtype=doit&amp;order=Reuse+same+sort+as+last+time&amp;field0-0-0=assigned_to&amp;type0-0-0=equals&amp;value0-0-0=&amp;field0-0-1=assigned_to&amp;type0-0-1=equals&amp;value0-0-1=&amp;ctype=csv&amp;columnlist=bug_id,keywords,short_desc");</eval:expr></pre>
					<ul>
					</ul>
				</span>
			</body>
			<tail>
				<script type="text/javascript">
var a = DekiWiki.$("span.bugzilla-fixed-list").each(function(i)
{
	var list = DekiWiki.$(this).find('ul');
	var buglist = DekiWiki.$(this).find('pre').html();
	buglist = buglist.split("\n");

	// shift off the column header
	buglist.shift();

	for (entry=0; entry &lt; buglist.length; entry++) {
		var cols = buglist[entry].split(",");
		var bugid = cols[0];
		cols.shift();
		var keywords = "";
		do {
			var thiscol = cols[0];
			cols.shift();
			if (thiscol == "")
				break;
			keywords += thiscol;
		} while (keywords[keywords.length-1] != '"');

		/*
		if (keywords != "") {
			keywords = keywords.replace(/^"/,"").replace(/"$/,"");
			keywords = " (" + keywords + ")";
		}
		*/
		if (keywords.indexOf("deployed-to-qa") >= 0) {
			keywords = " (deployed-to-qa)";
		} else if (keywords.indexOf("deployed-to-production") >= 0) {
			keywords = " (deployed-to-production)";
		} else
			keywords = "";

			
		var desc = cols.join(" ").replace(/^"/,"").replace(/"$/,"");
		var str = "&lt;li&gt;&lt;strike&gt;Bug " + bugid + "&lt;/strike&gt;" +
				keywords + " - " + desc + "&lt;/li&gt;";
		DekiWiki.$(list).append(str);
	}
});
				</script>
			</tail>
		</html>
	</return>
</function>



</extension>
