[#7381] adjust intra-project linking logic for GC hosted domains
diff --git a/ForgeImporters/forgeimporters/google/__init__.py b/ForgeImporters/forgeimporters/google/__init__.py
index 8c9081e..b848151 100644
--- a/ForgeImporters/forgeimporters/google/__init__.py
+++ b/ForgeImporters/forgeimporters/google/__init__.py
@@ -61,8 +61,12 @@
             qs = parse_qs(href.query)
             gc_link = not href.netloc or href.netloc == 'code.google.com'
             path_parts = href.path.split('/')
-            target_project = path_parts[
-                2] if gc_link and len(path_parts) >= 3 else ''
+            target_project = None
+            if gc_link:
+                if len(path_parts) >= 5 and path_parts[1] == 'a':
+                    target_project = '/'.join(path_parts[1:5])
+                elif len(path_parts) >= 3:
+                    target_project = path_parts[2]
             internal_link = target_project == project_name
             if gc_link and internal_link and 'id' in qs:
                 # rewrite issue 123 project-internal issue links
@@ -76,6 +80,8 @@
                 fragment = '[%s](%s)' % (
                     h.plain2markdown(
                         fragment.text, preserve_multiple_spaces=True, has_html_entities=True),
+                    # possibly need to adjust this URL for /a/ hosted domain URLs,
+                    # but it seems fragment['href'] always starts with / so it replaces the given path
                     urljoin('https://code.google.com/p/%s/issues/' %
                             project_name, fragment['href']),
                 )
diff --git a/ForgeImporters/forgeimporters/google/tests/test_init.py b/ForgeImporters/forgeimporters/google/tests/test_init.py
index 8acb0e3..2c536cd 100644
--- a/ForgeImporters/forgeimporters/google/tests/test_init.py
+++ b/ForgeImporters/forgeimporters/google/tests/test_init.py
@@ -18,9 +18,11 @@
 from nose.tools import assert_equal
 from mock import patch
 from formencode.validators import Invalid
+from BeautifulSoup import BeautifulSoup
 
 from allura.tests import decorators as td
 from forgeimporters.google import GoogleCodeProjectNameValidator, GoogleCodeProjectExtractor
+from forgeimporters.google import _as_markdown
 
 
 class TestGoogleCodeProjectNameValidator(object):
@@ -81,3 +83,37 @@
         )
         with td.raises(Invalid):
             GoogleCodeProjectNameValidator()._to_python('http://code.google.com/a/eclipselabs.org/bogus')
+
+
+class Test_as_markdown(object):
+
+    # this is covered by functional tests (useing test-issue.html)
+    # but adding some unit tests for easier verification of hosted domain link rewriting
+
+    def test_link_within_proj(self):
+        html = BeautifulSoup('''<pre>Foo: <a href="/p/myproj/issues/detail?id=1">issue 1</a></pre>''')
+        assert_equal(
+            _as_markdown(html.first(), 'myproj'),
+            'Foo: [issue 1](#1)'
+        )
+
+    def test_link_other_proj(self):
+        html = BeautifulSoup('''<pre>Foo: <a href="/p/other-project/issues/detail?id=1">issue other-project:1</a></pre>''')
+        assert_equal(
+            _as_markdown(html.first(), 'myproj'),
+            'Foo: [issue other-project:1](https://code.google.com/p/other-project/issues/detail?id=1)'
+        )
+
+    def test_link_hosted_domain_within_proj(self):
+        html = BeautifulSoup('''<pre>Foo: <a href="/a/eclipselabs.org/p/myproj/issues/detail?id=1">issue 1</a></pre>''')
+        assert_equal(
+            _as_markdown(html.first(), 'a/eclipselabs.org/p/myproj'),
+            'Foo: [issue 1](#1)'
+        )
+
+    def test_link_hosted_domain_other_proj(self):
+        html = BeautifulSoup('''<pre>Foo: <a href="/a/eclipselabs.org/p/other-proj/issues/detail?id=1">issue 1</a></pre>''')
+        assert_equal(
+            _as_markdown(html.first(), 'a/eclipselabs.org/p/myproj'),
+            'Foo: [issue 1](https://code.google.com/a/eclipselabs.org/p/other-proj/issues/detail?id=1)'
+        )