forked from jdlrobson/gerrit-be-nice-to-me
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
171 lines (148 loc) · 6.09 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
( function( $ ) {
var xGerritAuth;
var isNewScreen;
var isv29OrGreater;
var gerrit_rpc_base = "/gerrit_ui/rpc/";
var current_change_id;
var gerrit_request_id = 1;
var commentPanelClass;
var commentAuthorClass;
var commentSummaryClass;
var commentPanelHeader;
var commentPanelMessage;
function colorComment( $commentPanel ) {
var author = $commentPanel.find( commentPanelAuthorClass ).text();
var username = $('.menuBarUserName').text()
var heading = $commentPanel.find( commentPanelMessage ).eq( 0 ).text(),
color = '#aaa';
if (author === username) {
color = 'blue';
} else if ( heading.match( /Code\-Review\-2/ ) || heading.match( /Verified\-2/ ) ) {
color = '#C90505';
} else if ( heading.match( /Code\-Review\-1/ ) || heading.match( /Verified\-1/ ) ) {
color = 'red';
} else if ( heading.match( /Code\-Review\+1/ ) ) {
color = 'yellow';
} else if ( heading.match( /Code\-Review\+2/ ) ) {
color = 'green';
}
$commentPanel.css( {
'border-left': 'solid 10px ' + color,
'border-top-left-radius': 0,
'border-bottom-left-radius': 0
} );
}
function postJSON(_url, data, func) {
$.ajax({type: "POST",
url: _url,
dataType: "json",
data: data,
headers: {"Content-Type":"application/json; charset=UTF-8",
"Accept":"application/json; application/jsonrequest; text/html",
"X-Gerrit-Auth": xGerritAuth
}
})
.done(func)
.fail(function(a){func("badvalue")});
}
function gerritJsonRPC(service, func, params, cb) {
gerrit_request_id +=1;
var url = document.location.origin+gerrit_rpc_base+service;
var jsonData = '{"jsonrpc":"2.0","method":"'+func+'","params":'+JSON.stringify(params)+',"id":'+gerrit_request_id+',"xsrfKey":"'+ xGerritAuth+'"}';
postJSON(url, jsonData, cb);
}
function getDetail(id, psid, cb) {
gerritJsonRPC("ChangeDetailService", "patchSetDetail2",[null,{"changeId":{"id":id},"patchSetId":psid},null], function(r){
cb(r.result);
});
}
function parseChangeId(href) {
var orig = href.substring(8)
orig = orig.substring(orig.indexOf('/'))
if (orig.indexOf('/c/') != -1)
orig = orig.substring(orig.indexOf('/c/')+3)
else if (orig.indexOf('/changes/') != -1)
orig = orig.substring(orig.indexOf('/changes/')+9)
else
orig = orig.substring(1)
if (orig.indexOf('/') != -1)
orig = orig.substring(0, orig.indexOf('/'))
return orig;
}
function listener( ev ) {
var $t = $( ev.target ), author, action;
if ( $t.hasClass( commentPanelClass ) ) { // force open comment panel
var $authorNode = $t.find( commentPanelAuthorClass );
author = $authorNode.text();
action = $t.find( commentPanelSummaryClass ).text();
if ( author === 'builder builder' || author === 'Review Bot' ||
action.indexOf( 'Uploaded patch set' ) === 0 ||
action.match( /was rebased$/ ) ) {
// make jenkins comments less prominent
$t.find( commentPanelHeader ).css( 'opacity', 0.6 );
}
else if ( !isNewScreen && action.match( /…$/) ) {
// expand comment
$t.find( commentPanelSummaryClass ).hide();
$t.find( '.commentPanelContent' ).show();
}
else if ( isNewScreen && action !== '' ) {
// expand comment
setTimeout(function() { // yield
$authorNode.trigger( "click" );
}, 0)
}
colorComment( $t );
} else if ( $t.hasClass( 'gwt-InlineHyperlink' ) && $t.text() === 'Permalink') {
current_change_id = parseChangeId($t.attr('href'));
} else if ( $t.hasClass( 'gwt-DisclosurePanel' ) ) {
var psText = $t.find( 'tr' ).eq( 0 ).find( 'td' ).eq( 2 ).text().replace('Patch Set ','')
getDetail(current_change_id, psText, function(r){
var parent = r.info.parents[0].id.id;
var comments = 0;
r.patches.forEach(function(a) {
comments += a.nbrComments;
});
if (comments > 0)
$( '<span/>' ).text( ' (' + comments + ')').appendTo( $t.find( 'tr' ).eq( 0 ).find( 'td' ).eq( 2 ) );
$( '<span/>' ).text( ' / ' + parent ).appendTo( $t.find( '.patchSetRevision' ).eq( 0 ) );
})
}
}
function setupClassNames( ) {
commentPanelClass = isNewScreen ? (isv29OrGreater ? 'com-google-gerrit-client-change-Message_BinderImpl_GenCss_style-closed' : 'GKSE20JDH4') : 'commentPanel';
commentPanelAuthorClass = isNewScreen ? (isv29OrGreater ? '.com-google-gerrit-client-change-Message_BinderImpl_GenCss_style-name' : '.GKSE20JDI4') : '.commentPanelAuthorCell';
commentPanelSummaryClass = isNewScreen ? (isv29OrGreater ? '.com-google-gerrit-client-change-Message_BinderImpl_GenCss_style-summary' : '.GKSE20JDJ4') : '.commentPanelSummary';
commentPanelHeader = isNewScreen ? (isv29OrGreater ? '.com-google-gerrit-client-change-Message_BinderImpl_GenCss_style-contents' : '.GKSE20JDF4') : '.commentPanelHeader';
commentPanelMessage = isNewScreen ? (isv29OrGreater ? '.com-google-gerrit-client-change-Message_BinderImpl_GenCss_style-summary' : '.GKSE20JDJ4') : '.commentPanelMessage p';
}
function isVersion29rGreater(version) {
var extra = version.indexOf("-");
if (extra != -1) version = version.substring(0, extra);
var versionParts = version.split('.');
if (versionParts.length < 2) {
if (versionParts.length == 1) {
return versionParts[0] >= 3;
}
return false;
}
var minorVersion = versionParts[1].split('-')[0];
return versionParts[0] >= 2 && minorVersion >= 9 ||
versionParts[0] >= 3;
}
function initialise(pageData) {
xGerritAuth = pageData.xGerritAuth;
// new screen can be triggered manually via url (/c2/), which doesn't reflect in json data
var hasNonGlobalV2Screen = document.location.href.indexOf('/#/c2/') != -1;
var hasV2ScreenInPreferences = pageData.account.generalPreferences.changeScreen ? pageData.account.generalPreferences.changeScreen.indexOf('CHANGE_SCREEN2') != -1 : true;
isNewScreen = hasNonGlobalV2Screen || hasV2ScreenInPreferences;
isv29OrGreater = isVersion29rGreater(pageData.version);
setupClassNames();
console.log("Loaded Gerrit-Be-Nice-To-Me @ Ullink. Using auth token: " + xGerritAuth + " on Gerrit v" + pageData.version);
}
document.addEventListener( 'DOMNodeInserted', listener, false );
$.get(document.location.origin.toString()).done(function(res) {
$.globalEval($(res).filter("#gerrit_hostpagedata").html());
initialise(gerrit_hostpagedata);
});
} )( jQuery );