From 5caa69d965d005d7879b6916bf5db86a6ca87ce1 Mon Sep 17 00:00:00 2001 From: Andrew Bonventre Date: Mon, 28 Dec 2015 15:22:50 -0500 Subject: [PATCH] Auto-link change descriptions based on server config Also has nice side-effect of fixing the current linkify tests. Change-Id: Ief7341a755d5430257fc3768d74909b9a59cdd59 --- .../app/elements/gr-change-view.html | 14 ++++- .../app/elements/gr-linked-text.html | 17 +++--- polygerrit-ui/app/scripts/link-text-parser.js | 59 ++++++------------- .../app/test/gr-change-view-test.html | 2 + .../app/test/gr-linked-text-test.html | 35 ++++++----- polygerrit-ui/server.go | 1 + 6 files changed, 64 insertions(+), 64 deletions(-) diff --git a/polygerrit-ui/app/elements/gr-change-view.html b/polygerrit-ui/app/elements/gr-change-view.html index 38e3ea84b1..1d626e7c74 100644 --- a/polygerrit-ui/app/elements/gr-change-view.html +++ b/polygerrit-ui/app/elements/gr-change-view.html @@ -107,6 +107,11 @@ limitations under the License. + +
Loading...
@@ -176,7 +181,9 @@ limitations under the License.
- +
diff --git a/polygerrit-ui/app/scripts/link-text-parser.js b/polygerrit-ui/app/scripts/link-text-parser.js index d3435c3220..e80d19bbd0 100644 --- a/polygerrit-ui/app/scripts/link-text-parser.js +++ b/polygerrit-ui/app/scripts/link-text-parser.js @@ -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); }; diff --git a/polygerrit-ui/app/test/gr-change-view-test.html b/polygerrit-ui/app/test/gr-change-view-test.html index 867156084f..084d4a8d2a 100644 --- a/polygerrit-ui/app/test/gr-change-view-test.html +++ b/polygerrit-ui/app/test/gr-change-view-test.html @@ -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( diff --git a/polygerrit-ui/app/test/gr-linked-text-test.html b/polygerrit-ui/app/test/gr-linked-text-test.html index 45ea2fb971..727e018348 100644 --- a/polygerrit-ui/app/test/gr-linked-text-test.html +++ b/polygerrit-ui/app/test/gr-linked-text-test.html @@ -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': 'https://code.google.com/p/gerrit/issues/detail?id=3650' + 'linkedText': 'https://code.google.com/p/gerrit/issues/detail?id=3650' }, { - 'text': 'Bug: 3650', - 'linkedText': 'Bug: Issue 3650' + 'text': 'Issue 3650', + 'linkedText': 'Issue 3650' }, { - 'text': 'Feature: 3650', - 'linkedText': 'Feature: Issue 3650' + 'text': 'bug 3650', + 'linkedText': 'bug 3650' }, { 'text': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e', - 'linkedText': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e' + 'linkedText': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e' } ]; }); 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); }); }); diff --git a/polygerrit-ui/server.go b/polygerrit-ui/server.go index 5fe6e36ac4..59a09b088c 100644 --- a/polygerrit-ui/server.go +++ b/polygerrit-ui/server.go @@ -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{}))