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"
url="[[_computeCommitInfoPath(_changeNum, _patchNum)]]"
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" hidden$="{{_loading}}">
<div class="headerContainer">
@ -176,7 +181,9 @@ limitations under the License.
</table>
</section>
<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>
<gr-file-list id="fileList"
change-num="[[_changeNum]]"
@ -233,6 +240,7 @@ limitations under the License.
_loading: Boolean,
_headerContainerEl: Object,
_headerEl: Object,
_projectConfig: Object,
_scrollHandler: Function,
},
@ -320,6 +328,10 @@ limitations under the License.
return '/changes/' + changeNum + '/comments';
},
_computeProjectConfigPath: function(project) {
return '/projects/' + project + '/config';
},
_computeDetailQueryParams: function() {
var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.ALL_REVISIONS,

View File

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

View File

@ -14,27 +14,13 @@
'use strict';
function GrLinkTextParser(callback) {
function GrLinkTextParser(linkConfig, callback) {
this.linkConfig = linkConfig;
this.callback = callback;
Object.preventExtensions(this);
}
// TODO(mmccoy): Move these patterns to Gerrit project config
// (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.SUB_REGEX = /\$(\d+)/;
GrLinkTextParser.prototype.addText = function(text, href) {
if (!text) {
@ -43,15 +29,6 @@ GrLinkTextParser.prototype.addText = function(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) {
linkify(text, {
callback: this.parseChunk.bind(this)
@ -62,30 +39,28 @@ GrLinkTextParser.prototype.parseChunk = function(text, href) {
if (href) {
this.addText(text, href);
} else {
this.parseLinks(text, GrLinkTextParser.CUSTOM_LINKS);
this.parseLinks(text, this.linkConfig);
}
};
GrLinkTextParser.prototype.parseLinks = function(text, patterns) {
for (var i = patterns.length - 1; i >= 0; i--) {
var PATTERN = patterns[i].pattern;
var URL = patterns[i].url;
for (var p in patterns) {
var pattern = new RegExp(patterns[p].match);
var match = text.match(pattern);
if (!match) { continue; }
var match = text.match(PATTERN);
if (!match){
continue;
var link = patterns[p].link;
var subMatch = link.match(GrLinkTextParser.SUB_REGEX);
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);
this.addText(before);
this.addText(match[1]);
text = text.substr(match.index + match[0].length);
if (match[1] !== 'Change-Id: ') {
this.addBugText('Issue ' + match[2], patterns[i], match[2]);
} else {
this.addBugText(match[2], patterns[i], match[2]);
};
};
this.addText(match[0], link);
}
this.addText(text);
};

View File

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

View File

@ -39,47 +39,54 @@ limitations under the License.
setup(function() {
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 = [
{
'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',
'linkedText': 'Bug: <a href=\"https://code.google.com/p/gerrit/issues/detail?id=3650\" target=\"_blank\">Issue 3650</a>'
'text': 'Issue 3650',
'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">Issue 3650</a>'
},
{
'text': 'Feature: 3650',
'linkedText': 'Feature: <a href=\"https://code.google.com/p/gerrit/issues/detail?id=3650\" target=\"_blank\">Issue 3650</a>'
'text': 'bug 3650',
'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">bug 3650</a>'
},
{
'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() {
// Reguar inline link.
element._contentChanged(testStrings[0].text)
element._contentChanged(testStrings[0].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[0].linkedText);
});
test('Bug pattern was parsed and linked', function() {
// "Bug:" pattern.
element._contentChanged(testStrings[1].text)
// "Issue/Bug" pattern.
element._contentChanged(testStrings[1].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[1].linkedText);
});
test('Feature pattern was parsed and linked', function() {
// "Feature:" pattern.
element._contentChanged(testStrings[2].text)
element._contentChanged(testStrings[2].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[2].linkedText);
});
test('Change-Id pattern was parsed and linked', function() {
// "Change-Id:" pattern.
element._contentChanged(testStrings[3].text)
element._contentChanged(testStrings[3].text, element.config);
assert.equal(element.$.output.innerHTML, testStrings[3].linkedText);
});
});

View File

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