Auto-link change descriptions based on server config

Also has nice side-effect of fixing the current linkify tests.

Change-Id: Ief7341a755d5430257fc3768d74909b9a59cdd59
This commit is contained in:
Andrew Bonventre 2015-12-28 15:22:50 -05:00
parent 1009c6bfb1
commit 5caa69d965
6 changed files with 64 additions and 64 deletions

View File

@ -107,6 +107,11 @@ limitations under the License.
<gr-ajax id="commitInfoXHR" <gr-ajax id="commitInfoXHR"
url="[[_computeCommitInfoPath(_changeNum, _patchNum)]]" url="[[_computeCommitInfoPath(_changeNum, _patchNum)]]"
last-response="{{_commitInfo}}"></gr-ajax> last-response="{{_commitInfo}}"></gr-ajax>
<!-- TODO(andybons): Cache the project config. -->
<gr-ajax id="configXHR"
auto
url="[[_computeProjectConfigPath(_change.project)]]"
last-response="{{_projectConfig}}"></gr-ajax>
<div class="container loading" hidden$="{{!_loading}}">Loading...</div> <div class="container loading" hidden$="{{!_loading}}">Loading...</div>
<div class="container" hidden$="{{_loading}}"> <div class="container" hidden$="{{_loading}}">
<div class="headerContainer"> <div class="headerContainer">
@ -176,7 +181,9 @@ limitations under the License.
</table> </table>
</section> </section>
<section class="summary"> <section class="summary">
<gr-linked-text pre content="[[_commitInfo.message]]"></gr-linked-text> <gr-linked-text pre
content="[[_commitInfo.message]]"
config="[[_projectConfig.commentlinks]]"></gr-linked-text>
</section> </section>
<gr-file-list id="fileList" <gr-file-list id="fileList"
change-num="[[_changeNum]]" change-num="[[_changeNum]]"
@ -233,6 +240,7 @@ limitations under the License.
_loading: Boolean, _loading: Boolean,
_headerContainerEl: Object, _headerContainerEl: Object,
_headerEl: Object, _headerEl: Object,
_projectConfig: Object,
_scrollHandler: Function, _scrollHandler: Function,
}, },
@ -320,6 +328,10 @@ limitations under the License.
return '/changes/' + changeNum + '/comments'; return '/changes/' + changeNum + '/comments';
}, },
_computeProjectConfigPath: function(project) {
return '/projects/' + project + '/config';
},
_computeDetailQueryParams: function() { _computeDetailQueryParams: function() {
var options = Changes.listChangesOptionsToHex( var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.ALL_REVISIONS, Changes.ListChangesOption.ALL_REVISIONS,

View File

@ -34,15 +34,18 @@ limitations under the License.
is: 'gr-linked-text', is: 'gr-linked-text',
properties: { properties: {
content: { content: String,
type: String, config: Object,
observer: '_contentChanged'
}
}, },
_contentChanged: function(newValue) { observers: [
'_contentChanged(content, config)',
],
_contentChanged: function(content, config) {
var output = this.$.output; var output = this.$.output;
var parser = new GrLinkTextParser(function(text, href) { output.textContent = '';
var parser = new GrLinkTextParser(config, function(text, href) {
if (href) { if (href) {
var a = document.createElement('a'); var a = document.createElement('a');
a.href = href; a.href = href;
@ -53,7 +56,7 @@ limitations under the License.
output.appendChild(document.createTextNode(text)); output.appendChild(document.createTextNode(text));
} }
}); });
parser.parse(newValue); parser.parse(content);
} }
}); });
</script> </script>

View File

@ -14,27 +14,13 @@
'use strict'; 'use strict';
function GrLinkTextParser(callback) { function GrLinkTextParser(linkConfig, callback) {
this.linkConfig = linkConfig;
this.callback = callback; this.callback = callback;
Object.preventExtensions(this); Object.preventExtensions(this);
} }
// TODO(mmccoy): Move these patterns to Gerrit project config GrLinkTextParser.SUB_REGEX = /\$(\d+)/;
// (https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-config)
GrLinkTextParser.CUSTOM_LINKS = [
{
'pattern': /^(Change-Id: )(.+)$/mi,
'url': 'https://gerrit-review.googlesource.com/r/'
},
{
'pattern': /^(Feature: )Issue ?(.+)$/mi,
'url': 'https://code.google.com/p/gerrit/issues/detail?id='
},
{
'pattern': /^(Bug: )Issue ?(.+)$/mi,
'url': 'https://code.google.com/p/gerrit/issues/detail?id='
}
];
GrLinkTextParser.prototype.addText = function(text, href) { GrLinkTextParser.prototype.addText = function(text, href) {
if (!text) { if (!text) {
@ -43,15 +29,6 @@ GrLinkTextParser.prototype.addText = function(text, href) {
this.callback(text, href); this.callback(text, href);
}; };
GrLinkTextParser.prototype.addBugText = function(text, tracker, bugID) {
if (tracker) {
var href = tracker.url + encodeURIComponent(bugID);
this.addText(text, href);
return;
}
this.addText(text);
};
GrLinkTextParser.prototype.parse = function(text) { GrLinkTextParser.prototype.parse = function(text) {
linkify(text, { linkify(text, {
callback: this.parseChunk.bind(this) callback: this.parseChunk.bind(this)
@ -62,30 +39,28 @@ GrLinkTextParser.prototype.parseChunk = function(text, href) {
if (href) { if (href) {
this.addText(text, href); this.addText(text, href);
} else { } else {
this.parseLinks(text, GrLinkTextParser.CUSTOM_LINKS); this.parseLinks(text, this.linkConfig);
} }
}; };
GrLinkTextParser.prototype.parseLinks = function(text, patterns) { GrLinkTextParser.prototype.parseLinks = function(text, patterns) {
for (var i = patterns.length - 1; i >= 0; i--) { for (var p in patterns) {
var PATTERN = patterns[i].pattern; var pattern = new RegExp(patterns[p].match);
var URL = patterns[i].url; var match = text.match(pattern);
if (!match) { continue; }
var match = text.match(PATTERN); var link = patterns[p].link;
if (!match){ var subMatch = link.match(GrLinkTextParser.SUB_REGEX);
continue; link = link.replace(GrLinkTextParser.SUB_REGEX, match[subMatch[1]]);
// PolyGerrit doesn't use hash-based navigation like GWT. Account for this.
if (link[0] == '#') {
link = link.substr(1);
} }
var before = text.substr(0, match.index); var before = text.substr(0, match.index);
this.addText(before); this.addText(before);
this.addText(match[1]);
text = text.substr(match.index + match[0].length); text = text.substr(match.index + match[0].length);
if (match[1] !== 'Change-Id: ') { this.addText(match[0], link);
this.addBugText('Issue ' + match[2], patterns[i], match[2]); }
} else {
this.addBugText(match[2], patterns[i], match[2]);
};
};
this.addText(text); this.addText(text);
}; };

View File

@ -41,6 +41,8 @@ limitations under the License.
setup(function() { setup(function() {
element = fixture('basic'); element = fixture('basic');
element.$.configXHR.auto = false;
server = sinon.fakeServer.create(); server = sinon.fakeServer.create();
// Eat any requests made by elements in this suite. // Eat any requests made by elements in this suite.
server.respondWith( server.respondWith(

View File

@ -39,47 +39,54 @@ limitations under the License.
setup(function() { setup(function() {
element = fixture('basic'); element = fixture('basic');
element.config = {
ph: {
match: '([Bb]ug|[Ii]ssue)\\s*#?(\\d+)',
link: 'https://code.google.com/p/gerrit/issues/detail?id=$2'
},
changeid: {
'match': '(I[0-9a-f]{8,40})',
'link': '#/q/$1'
}
};
testStrings = [ testStrings = [
{ {
'text': 'https://code.google.com/p/gerrit/issues/detail?id=3650', 'text': 'https://code.google.com/p/gerrit/issues/detail?id=3650',
'linkedText': '<a href=\"https://code.google.com/p/gerrit/issues/detail?id=3650\" target=\"_blank\">https://code.google.com/p/gerrit/issues/detail?id=3650</a>' 'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">https://code.google.com/p/gerrit/issues/detail?id=3650</a>'
}, },
{ {
'text': 'Bug: 3650', 'text': 'Issue 3650',
'linkedText': 'Bug: <a href=\"https://code.google.com/p/gerrit/issues/detail?id=3650\" target=\"_blank\">Issue 3650</a>' 'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">Issue 3650</a>'
}, },
{ {
'text': 'Feature: 3650', 'text': 'bug 3650',
'linkedText': 'Feature: <a href=\"https://code.google.com/p/gerrit/issues/detail?id=3650\" target=\"_blank\">Issue 3650</a>' 'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">bug 3650</a>'
}, },
{ {
'text': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e', 'text': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e',
'linkedText': 'Change-Id: <a href=\"https://gerrit.googlesource.com/gerrit/+/I11d6a37f5e9b5df0486f6c922d8836dfa780e03e\" target=\"_blank\">I11d6a37f5e9b5df0486f6c922d8836dfa780e03e</a>' 'linkedText': 'Change-Id: <a href="/q/I11d6a37f5e9b5df0486f6c922d8836dfa780e03e" target="_blank">I11d6a37f5e9b5df0486f6c922d8836dfa780e03e</a>'
} }
]; ];
}); });
test('URL pattern was parsed and linked.', function() { test('URL pattern was parsed and linked.', function() {
// Reguar inline link. // Reguar inline link.
element._contentChanged(testStrings[0].text) element._contentChanged(testStrings[0].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[0].linkedText); assert.equal(element.$.output.innerHTML, testStrings[0].linkedText);
}); });
test('Bug pattern was parsed and linked', function() { test('Bug pattern was parsed and linked', function() {
// "Bug:" pattern. // "Issue/Bug" pattern.
element._contentChanged(testStrings[1].text) element._contentChanged(testStrings[1].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[1].linkedText); assert.equal(element.$.output.innerHTML, testStrings[1].linkedText);
});
test('Feature pattern was parsed and linked', function() { element._contentChanged(testStrings[2].text, element.config);
// "Feature:" pattern.
element._contentChanged(testStrings[2].text)
assert.equal(element.$.output.innerHTML, testStrings[2].linkedText); assert.equal(element.$.output.innerHTML, testStrings[2].linkedText);
}); });
test('Change-Id pattern was parsed and linked', function() { test('Change-Id pattern was parsed and linked', function() {
// "Change-Id:" pattern. // "Change-Id:" pattern.
element._contentChanged(testStrings[3].text) element._contentChanged(testStrings[3].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[3].linkedText); assert.equal(element.$.output.innerHTML, testStrings[3].linkedText);
}); });
}); });

View File

@ -50,6 +50,7 @@ func main() {
http.HandleFunc("/changes/", handleRESTProxy) http.HandleFunc("/changes/", handleRESTProxy)
http.HandleFunc("/accounts/", handleRESTProxy) http.HandleFunc("/accounts/", handleRESTProxy)
http.HandleFunc("/config/", handleRESTProxy) http.HandleFunc("/config/", handleRESTProxy)
http.HandleFunc("/projects/", handleRESTProxy)
http.HandleFunc("/accounts/self/detail", handleAccountDetail) http.HandleFunc("/accounts/self/detail", handleAccountDetail)
log.Println("Serving on port", *port) log.Println("Serving on port", *port)
log.Fatal(http.ListenAndServe(*port, &server{})) log.Fatal(http.ListenAndServe(*port, &server{}))