// version 0.2
// 2008-10-06
// Copyright (c) 2007, Matěj Cepl
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
// Additions by Brennan Ashton
//
// ==UserScript==
// @name          Bugzilla buttons for Fedora bug triagers
// @namespace     http://www.ceplovi.cz/matej/progs/scripts
// @description   add buttons to ease pain of managing bugs in the RH Bugzilla
// @include https://bugzilla.redhat.com/show_bug.cgi*
// ==/UserScript==
var getLogsString = "Thanks for the bug report.  We have reviewed the information you "+
"have provided above, and there is some additional information we "+
"require that will be helpful in our diagnosis of this issue.\n\n "+
"Please attach your X server config file (/etc/X11/xorg.conf) and "+
"X server log file (/var/log/Xorg.*.log) to the bug report as "+
"individual uncompressed file attachments using the bugzilla file "+
"attachment link below.\n\n"+
"Could you please also try to run without any /etc/X11/xorg.conf "+
"whatsoever and let X11 autodetect your display and video card? "+
"Attach to this bug /var/log/Xorg.0.log from this attempt as well, "+
"please.\n\n"+
"We will review this issue again once you've had a chance to "+
"attach this information.\n\n"+
"Thanks in advance.";
var getLogsLiveCDString = "Thanks for the bug report.  We have reviewed the information you "+
"have provided above, and there is some additional information we "+
"require that will be helpful in our diagnosis of this issue.\n\n"+
"When the anaconda crashes, please, switch to the console (Ctrl+Alt+F2) "+
"and cp /tmp/X* to some other place -- USB stick, some other computer "+
"via network, some on the Internet, and please attach it to the bug report as "+
"individual uncompressed file attachments using the bugzilla file "+
"attachment link below.\n\n"+
"We will review this issue again once you've had a chance to "+
"attach this information.\n\n"+
"Thanks in advance.";
var dieNvidiaDieDieDie = 
"Thank you for the report. Fedora does not provide or support "+
"proprietary drivers. For users who are experiencing problems "+
"installing, configuring, or using the unsupported 3rd party "+
"proprietary \"nvidia\" video driver, Nvidia provides indirect "+
"customer support via an online web based support forum. Nvidia "+
"monitors these web forums for commonly reported problems and "+
"passes them on to Nvidia engineers for investigation.  Once "+
"they've isolated a particular problem, it is often fixed in "+
"a future video driver update.\n"+
"The NVNews Nvidia Linux driver forum is located at:\n\n"+
"http://www.nvnews.net/vbulletin/forumdisplay.php?s=&forumid=14\n\n"+
"Once you have reported this issue in the Nvidia web forums, "+
"others who may have experienced the particular problem may "+
"be able to assist.  If there is a real bug occurring, Nvidia "+
"will be able to determine this, and will likely resolve the "+
"issue in a future driver update for the operating system "+
"releases that they officially support.\n\n "+
"While Fedora does not support the proprietary Nvidia driver, "+
"users requiring technical help may also find the various "+
"X.Org and Fedora mailing lists helpful in finding "+
"assistance:\n\n"+
"X.Org mailing lists:\n"+
"http://www.freedesktop.org/XOrg/XorgMailingLists\n\n"+
"Fedora mailing lists:\n"+
"http://fedoraproject.org/wiki/Communicate#ML\n\n"+
"Setting status to \"CANTFIX\" (unsupported). ";
var getFirefoxBacktraceString = "Thanks for the bug report.  We have "+
"reviewed the information you have provided above, and there is "+
"some additional information we require that will be helpful in "+
"our diagnosis of this issue.\n\n"+
"Please install firefox-debuginfo; in order to do this you have to "+
"enable -debuginfo repository.\n\n"+
"\tyum install --enablerepo=\\*debuginfo firefox-debuginfo\n\n"+
"(if you use x86_64 firefox, install firefox-debuginfo.x86_64 "+
" package).\n\n"+
"Then run firefox with a parameter -g. That will start firefox "+
"running inside of gdb debugger. Then use command run and do "+
"whatever you did to make firefox crash. When it happens, you "+
"should go back to the gdb and run\n\n"+
"\t(gdb) thread apply all backtrace\n\n"+
"This produces usually many screens of the text. Copy all of them "+
"into a text editor and attach the file to the bug as an "+
"uncompressed attachment.\n\n"+
"We will review this issue again once you've had a chance to "+
"attach this information.\n\n"+
"Thanks in advance.";

var noResponseString = 
"The information we've requested above is required in order "+
"to review this problem report further and diagnose or fix the "+
"issue if it is still present.  Since it has been thirty days or "+
"more since we first requested additional information, we're assuming "+
"the problem is either no longer present in the current Fedora release, or "+
"that there is no longer any interest in tracking the problem.\n\n"+
"Setting status to \"CLOSED: INSUFFICIENT_DATA\".  If you still "+
"experience this problem after updating to our latest Fedora "+
"release and can provide the information previously requested, "+
"please feel free to reopen the bug report.\n\n"+
"Thank you in advance.";

var sentUpstreamString = "Thank you for the bug report.  At the moment, "+
" the Fedora developers are busy fixing other issues and do not have "+
"time to work on this one.  The best way to make sure your problem will "+
"get looked on is to report it to the authors of the program. "+
"Mostupstream authors use a bug tracking system like bugzilla, and more "+
"people who know the code will be looking at the bug report there.\n\n"+
"The upstream bug tracking system to use is: \n LINK HERE \n\n"+
"You are requested to add the bugzilla link here for tracking purposes. "+
"Please make sure the bug isn't already in the upstream bug tracker "+
"before filing it.";
var moveYourButString = "Reporter, could you please reply to the "+
"previous question? If you won't reply in one month, I will have to "+
"close this bug as INSUFFICIENT_DATA. Thank you. ";
var getEOL ="Thank you for your bug report.\n"+
"We are sorry, but the Fedora Project no longer maintains this version "+
"of Fedora. Please upgrade to the latest version and reopen this bug "+
"against that version if this bug exists there.\n"+
"As a result we are setting this bug to CLOSED:WONTFIX ";
var triagedString = "This bug has been triaged";

/* Bugzilla functions. */

/** Generic function to add new button to the page.
 * Actually copies new button from the old one (in order to have the same
 * look-and-feel, etc.
 * @param originalLocation object with the button to be copied from
 * @param newId string with the id of the new button; has to be unique in
                whole page
 * @param newLabel string with the label which will be shown to user
 * @param handlerFunc function used as handler when the button is clicked on
 * @param doSubmit bool optional whether the button should submit whole page
 *              (default true)
 *
 * @return none
 */
function addNewButton(originalLocation,newId,newLabel,handlerFunc,doSubmit) {
	if (doSubmit === null) { // missing optional argument
		doSubmit = true;
	}
    var newButton = originalButton.cloneNode(true);
    if (!doSubmit) {
    	newButton.setAttribute("type","button");
    }
    newButton.id=newId;
    newButton.setAttribute("value",newLabel);
    newButton.addEventListener('click',handlerFunc,true);
    var textNode = document.createTextNode("\u00A0");
    originalLocation.parentNode.insertBefore(textNode,originalLocation);
    originalLocation.parentNode.insertBefore(newButton,textNode);
}

/** Add text to the comment.
 * @param string2BAdded string to be added to the comment box
 *
 * @return none
 */
function addTextToComment(string2BAdded) {
	var commentTextarea = document.getElementById("comment");
	
	// don't remove the current content of the comment box,
	// just behave accordingly
	if (commentTextarea.value.length > 0) {
		commentTextarea.value += "\n\n";
	}
	commentTextarea.value += string2BAdded;
}

/** Add reminder to NEEDINFO threatening INSUFFICIENT_DATA if
 *  NEEDINFOee won't reply in a month.
 * @param evt event which called this handler
 *
 * @return none
 */
function addMoveYourButtHandler(evt) {
	addTextToComment(moveYourButString);
}
/** Add request for info by 'target'
 *  
 * 
 *
 * @return none
 */
function reqInfo(target) {
	var role = document.getElementById('needinfo_role');
	for(var i=0;i<role.length;i++){
		if (role[i].value==target){
			role[i].selected=true;
		}
	}
	if(document.getElementById('needinfo').checked==false){
		document.getElementById('needinfo').click();
	}
}

/** Add request for filing the bug upstream, so that I could close it.
 * @param evt event which called this handler
 *
 * @return none
 */
function addClosingUpstream(evt) {
	addTextToComment(sentUpstreamString);
	var status = document.getElementById('bug_status');
	for(var i=0;i<status.length;i++){
		if (status[i].value=='CLOSED'){
			status[i].selected=true;
		}
	}
	var res_set = document.getElementById('resolution_settings');
	res_set.style.display = 'inline';
	var res = document.getElementById('resolution');
	for(var i=0;i<res.length;i++){
		if (res[i].value=='UPSTREAM'){
			res[i].selected=true;
		}
	}
}

/** Add request for filing the bug upstream, so that I could close it.
 * @param evt event which called this handler
 *
 * @return none
 */
function addClosingEOL(evt) {
	addTextToComment(getEOL);
	var status = document.getElementById('bug_status');
	for(var i=0;i<status.length;i++){
		if (status[i].value=='CLOSED'){
			status[i].selected=true;
		}
	}
	var res_set = document.getElementById('resolution_settings');
	res_set.style.display = 'inline';
	var res = document.getElementById('resolution');
	for(var i=0;i<res.length;i++){
		if (res[i].value=='WONTFIX'){
			res[i].selected=true;
		}
	}
}
/** No response on NEEDINFO
 * @param evt event which called this handler
 *
 * @return none
 */
function addnoResponseNEEDINFO(evt) {
	addTextToComment(noResponseString);
	var status = document.getElementById('bug_status');
	for(var i=0;i<status.length;i++){
		if (status[i].value=='CLOSED'){
			status[i].selected=true;
		}
	}
	var res_set = document.getElementById('resolution_settings');
	res_set.style.display = 'inline';
	var res = document.getElementById('resolution');
	for(var i=0;i<res.length;i++){
		if (res[i].value=='INSUFFICIENT_DATA'){
			res[i].selected=true;
		}
	}
}

/** Add request for debugging information for Firefox
 * @param evt event which called this handler
 *
 * @return none
 */
function addFirefoxDebugInfo(evt) {
	addTextToComment(getFirefoxBacktraceString);
}
/** Add comment that the bug has been triaged and change status to 
 *  'ASSIGNED'
 * @param evt event which called this handler
 *
 * @return none
 */
function addTriaged(evt) {
	addTextToComment(triagedString);
	var status = document.getElementById('bug_status');
	for(var i=0;i<status.length;i++){
		if (status[i].value=='ASSIGNED'){
			status[i].selected=true;
		}
	}
}
/** Add keyword FutureFeature 
 *  
 * @param evt event which called this handler
 *
 * @return none
 */
function addFutureFeatureKW(evt) {
	//addTextToComment(futureFeatureString);
	var keyword = document.getElementById('keywords');
	if (keyword.value == '') {
		keyword.value = "FutureFeature";
	}else{
		keyword.value = keyword.value + ", FutureFeature";
	} 
}
/** Add keyword FutureFeature 
 *  
 * @param evt event which called this handler
 *
 * @return none
 */
function addPatchKW(evt) {
	//addTextToComment(patchString);
	var keyword = document.getElementById('keywords');
	if (keyword.value == '') {
		keyword.value = "Patch";
	}else{
		keyword.value = keyword.value + ", Patch";
	} 
}
/** Add request for /etc/X11/xorg.conf and /var/log/Xorg.0.log to the comment
 * @param evt event which called this handler
 *
 * @return none
 */
function addGetXLogsBlahHandler(evt) {
	addTextToComment(getLogsString);
	reqInfo('reporter');
}
/** We don't like binary only nvidia drivers
 * 
 * Enter to the comment box information for the reporter to make them unhappy
 * set bug to CLOSED/CANTFIX (but don't submit -- just to be sure)
 * 
 * @param evt event which called this handler
 *
 * @return none
 */
function addCloseNvidiaDriver(evt) {
	addTextToComment(dieNvidiaDieDieDie);
	var status = document.getElementById('bug_status');
	for(var i=0;i<status.length;i++){
		if (status[i].value=='CLOSED'){
			status[i].selected=true;
		}
	}
	var res_set = document.getElementById('resolution_settings');
	res_set.style.display = 'inline';
	var res = document.getElementById('resolution');
	for(var i=0;i<res.length;i++){
		if (res[i].value=='CANTFIX'){
			res[i].selected=true;
		}
	}
}
/** Add request for /tmp/X* from the Live CD to the comment
 * @param evt event which called this handler
 *
 * @return none
 */
function addGetXLogsOnLiveCDHandler(evt) {
	addTextToComment(getLogsLiveCDString);
	reqInfo('reporter');
}
/** Set NEEDINFO to reporter
 * 
 *
 * @return none
 */
function addNeedInfoReporter(evt) {
	reqInfo('reporter');
}
/** Set NEEDINFO to assignee
 * 
 *
 * @return none
 */
function addNeedInfoAssignee(evt) {
	reqInfo('assigned_to');
}

// Vygeneruj seznam <input> elementů ve stránce
var IBList = [];
var IBRawList = document.getElementsByTagName("input");
for (var i=0;i<IBRawList.length;i++) {
    if ((IBRawList[i].hasAttribute("type")) &&
    	(IBRawList[i].getAttribute("type")=="submit")) {
		IBList = IBList.concat(IBRawList[i]);
    }
}

var originalButton = IBList[1]; // source button to be copied from

var newBRElement = document.createElement("BR");
var textCommentBox = document.getElementById("comment");
var parElement = textCommentBox.parentNode;
parElement.insertBefore(newBRElement,textCommentBox);

// Add request for firefox debugging infor to the textform
//addNewButton(newBRElement,"addgetffbtrace","FF btrace",
//	addFirefoxDebugInfo,false);
// Add a no response closing string
addNewButton(newBRElement,"addnoresponse","No Response",
	     addnoResponseNEEDINFO,false);
// Add a "close upstream" CLOSED UPSTREAM
addNewButton(newBRElement,"addcloseupstream","Close UPSTREAM",
	     addClosingUpstream,false);
// Add a "don't laugh at NEEDINFO" reminder
addNewButton(newBRElement,"moveyourbutt","NEEDINFO Reminder",
	     addMoveYourButtHandler,false);
// Add a "don't laugh at EOL" reminder
addNewButton(newBRElement,"addcloseeol","Close EOL",
	     addClosingEOL,false);
// Add a "don't laugh at EOL" reminder
addNewButton(newBRElement,"triaged","Triaged",
	     addTriaged,false);
// Add FutureFeature keyword
addNewButton(newBRElement,"futurefeaturekw","FutureFeature KW",
	     addFutureFeatureKW,false);
// Add Patch keyword
addNewButton(newBRElement,"patchkw","Patch KW",
	     addPatchKW,false);
	     
newBRElement.style.fontSize = "200%";
newBRElement.style.clear = "all";
newBRElement.style.marginBottom = "0.25ex";


newBRElementBottom = document.createElement("BR");
if (textCommentBox.parentNode.lastChild == textCommentBox) {
	parElement.insertBefore(newBRElementBottom,textCommentBox);
}else{
	parElement.insertBefore(newBRElementBottom,textCommentBox.nextSibling);
}

newBRElementMiddle = document.createElement("BR");
parElement.insertBefore(newBRElementMiddle,newBRElementBottom);

newBRElementMiddle.style.fontSize = "200%";
newBRElementMiddle.style.clear = "all";
newBRElementMiddle.style.marginBottom = "0.25ex";

// Add "Please add /etc/X11/... " to the textform
//addNewButton(newBRElementBottom,"addgetlogstext","X logs",
//	     addGetXLogsBlahHandler,false);
// Close nvidia binary only bug
addNewButton(newBRElementBottom,"closenvidia","Close nVidia",
	     addCloseNvidiaDriver,false);

// Live CD logs
addNewButton(newBRElementBottom,"xloglivecdbtn","Live CD X logs",
	     addGetXLogsOnLiveCDHandler,false);

// NEEDINFO Reporter
addNewButton(newBRElementBottom,"needinforeporter","NEEDINFO Reporter",
	     addNeedInfoReporter,false);    

// NEEDINFO Assignee
addNewButton(newBRElementBottom,"needinfoassignee","NEEDINFO Assignee",
	     addNeedInfoAssignee,false);
	     	     	     
newBRElementBottom.style.fontSize = "200%";
newBRElementBottom.style.clear = "all";
newBRElementBottom.style.marginBottom = "0.25ex";

//move comment box and buttons above comments
var comments = document.getElementById("comments");
comments.parentNode.appendChild(comments);


