Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide bot comments in oVirt Gerrit
// ==UserScript==
// @name Toggle CI comments in oVirt patches
// @include http://gerrit.ovirt.org/#/c/*
// @author Mike Kolesnik <mkolesni@redhat.com>
// @grant none
// @require http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==
// Set these values if necessary.
// Should comments be automatically hidden when the page opens.
var AUTO_HIDE = true;
// List of automatic accounts to hide (either email or name will work).
var CI_NAMES = ["oVirt Jenkins CI Server",
"automation@ovirt.org",
"gerrit-hooks"
];
// Don't touch the rest of the code!
var TOGGLE_BUTTON_ID = "toggleBotComments";
var TOGGLE_BUTTON_SELECTOR = "#" + TOGGLE_BUTTON_ID;
var SHOW_COMMENTS_LABEL = "Show automatic comments";
var HIDE_COMMENTS_LABEL = "Hide automatic comments";
var OLD_MENU_SELECTOR = ".linkMenuBar.commentPanelMenuBar";
var NEW_MENU_SELECTOR = "button div:contains('Expand All')";
var newScreen = true;
function oldGerritScreen() {
return $(OLD_MENU_SELECTOR).length > 0;
}
function botComments() {
if (oldGerritScreen()) {
return $("div").filter(function() {
return $.inArray(this.getAttribute('name'), CI_NAMES) >= 0
|| $.inArray(this.getAttribute('email'), CI_NAMES) >= 0;
});
} else {
return $("div").filter(function() {
return $.inArray(this.innerHTML, CI_NAMES) >= 0;
}).parent().parent().parent();
}
}
function botCommentsVisible() {
return botComments().is( ":visible" );
}
function addOldButton() {
var input = document.createElement("a");
input.href = "javascript:;";
input.className = "menuItem";
input.id = TOGGLE_BUTTON_ID;
input.innerHTML = botCommentsVisible()
? HIDE_COMMENTS_LABEL : SHOW_COMMENTS_LABEL;
input.onclick = function() {
botComments().toggle();
input.innerHTML = botCommentsVisible()
? HIDE_COMMENTS_LABEL : SHOW_COMMENTS_LABEL;
}
$(OLD_MENU_SELECTOR).append(input);
$(OLD_MENU_SELECTOR).children()
.not(TOGGLE_BUTTON_SELECTOR).addClass("linkMenuItemNotLast");
}
function addNewButton() {
var input = document.createElement("button");
input.id = TOGGLE_BUTTON_ID;
var label = document.createElement("div");
input.appendChild(label);
label.innerHTML = "<div>" + botCommentsVisible()
? HIDE_COMMENTS_LABEL : SHOW_COMMENTS_LABEL + "</div>";
input.onclick = function() {
botComments().toggle();
label.innerHTML = botCommentsVisible()
? HIDE_COMMENTS_LABEL : SHOW_COMMENTS_LABEL;
}
$(NEW_MENU_SELECTOR).parent().parent().append(input);
}
// Run in loop because gerrit loads the content dynamically, causing:
// 1. The menu bar is only accessible after a while
// 2. When you navigate in gerrit, greasemonkey scripts don't get re-triggered
window.setInterval(function() {
if ($(TOGGLE_BUTTON_SELECTOR).length == 0) {
newScreen = true;
if (oldGerritScreen()) {
addOldButton();
} else {
addNewButton();
}
}
if (AUTO_HIDE
&& newScreen
&& $(TOGGLE_BUTTON_SELECTOR).length != 0
&& botCommentsVisible()) {
newScreen = false;
$(TOGGLE_BUTTON_SELECTOR).click();
}
}, 1000);
@vandonr

This comment has been minimized.

Copy link

vandonr commented Sep 8, 2014

Hey, I borrowed your idea of running the script in a loop for gerrit, but the script is run twice in parallel : it's launched once when the page shows "loading gerrit code review", then the page is reloaded asynchronously to show the list of changes, and it triggers a second execution of the script without stopping the first one.

It's not really problematic, but it's less clean. Just wanted to let you know, if you find a solution I'm interested !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.