Repository: albertyw/git-browse

Issue:
Support sg.uberinternal.com
Instead of `https://sourcegraph.uberinternal.com/code.uber.internal/<repo_namespace>/<repo_name>`
Use `https://sg.uberinternal.com/code.uber.internal/uber-code/<repo_namespace>-<repo_name>`

Comments:

Patch:
diff --git a/git_browse/sourcegraph.py b/git_browse/sourcegraph.py
index 0b6bb06..404364a 100644
--- a/git_browse/sourcegraph.py
+++ b/git_browse/sourcegraph.py
@@ -4,7 +4,7 @@ from git_browse import phabricator, typedefs
 
 
 PUBLIC_SOURCEGRAPH_URL = "https://sourcegraph.com/"
-UBER_SOURCEGRAPH_URL = "https://sourcegraph.uberinternal.com/"
+UBER_SOURCEGRAPH_URL = "https://sg.uberinternal.com/"
 
 
 class SourcegraphHost(typedefs.Host):
@@ -40,14 +40,18 @@ class SourcegraphHost(typedefs.Host):
         self.host_class = host_class
 
     def get_url(self, git_object: typedefs.GitObject) -> str:
-        sourcegraph_url = PUBLIC_SOURCEGRAPH_URL
         if self.host_class == phabricator.PhabricatorHost:
-            sourcegraph_url = UBER_SOURCEGRAPH_URL
-        repository_url = "%s%s/%s" % (
-            sourcegraph_url,
-            self.host,
-            self.repository,
-        )
+            repository_url = "%s%s/uber-code/%s" % (
+                UBER_SOURCEGRAPH_URL,
+                self.host,
+                self.repository.replace('/', '-'),
+            )
+        else:
+            repository_url = "%s%s/%s" % (
+                PUBLIC_SOURCEGRAPH_URL,
+                self.host,
+                self.repository,
+            )
         if git_object.is_commit_hash():
             return self.commit_hash_url(repository_url, git_object)
         if git_object.is_root():


Test patch:
diff --git a/git_browse/tests/test_sourcegraph.py b/git_browse/tests/test_sourcegraph.py
index 418513e..bfc999e 100644
--- a/git_browse/tests/test_sourcegraph.py
+++ b/git_browse/tests/test_sourcegraph.py
@@ -2,7 +2,7 @@ import os
 import pathlib
 import unittest
 
-from git_browse import sourcegraph, phabricator, typedefs
+from git_browse import github, sourcegraph, phabricator, typedefs
 
 BASE_DIRECTORY = pathlib.Path(__file__).parents[2]
 
@@ -10,27 +10,32 @@ BASE_DIRECTORY = pathlib.Path(__file__).parents[2]
 class SourcegraphHost(unittest.TestCase):
     def setUp(self) -> None:
         self.obj = sourcegraph.SourcegraphHost(
+            typedefs.GitConfig("", "master"),
+            "github.com",
+            "albertyw/git-browse",
+        )
+        self.uber_obj = sourcegraph.SourcegraphHost(
             typedefs.GitConfig("", "master"),
             "code.uber.internal",
-            "asdf",
+            "asdf/qwer",
         )
-        self.obj.host_class = phabricator.PhabricatorHost
+        self.uber_obj.host_class = phabricator.PhabricatorHost
 
     def test_init(self) -> None:
-        self.assertEqual(self.obj.host, "code.uber.internal")
-        self.assertEqual(self.obj.repository, "asdf")
+        self.assertEqual(self.obj.host, "github.com")
+        self.assertEqual(self.obj.repository, "albertyw/git-browse")
 
     def test_create(self) -> None:
-        repo = "gitolite@code.uber.internal:a/b"
+        repo = "git@github.com:a/b"
         git_config = typedefs.GitConfig(repo, "master")
-        git_config.try_url_match(phabricator.UBER_SSH_GITOLITE_URL)
+        git_config.try_url_match(github.GITHUB_SSH_URL)
         obj = sourcegraph.SourcegraphHost.create(git_config)
         self.assertEqual(obj.repository, "a/b")
 
     def test_create_dot_git(self) -> None:
-        repo = "gitolite@code.uber.internal:a/b.git"
+        repo = "git@github.com:a/b.git"
         git_config = typedefs.GitConfig(repo, "master")
-        git_config.try_url_match(phabricator.UBER_SSH_GITOLITE_URL)
+        git_config.try_url_match(github.GITHUB_SSH_URL)
         obj = sourcegraph.SourcegraphHost.create(git_config)
         self.assertEqual(obj.repository, "a/b")
 
@@ -39,31 +44,84 @@ class SourcegraphHost(unittest.TestCase):
         url = self.obj.get_url(git_object)
         self.assertEqual(
             url,
-            sourcegraph.UBER_SOURCEGRAPH_URL
-            + "code.uber.internal/asdf/-/commit/abcd",
+            sourcegraph.PUBLIC_SOURCEGRAPH_URL
+            + "github.com/albertyw/git-browse/-/commit/abcd",
         )
 
     def test_get_url_root(self) -> None:
         git_object = typedefs.FocusObject(os.sep)
         url = self.obj.get_url(git_object)
         self.assertEqual(
-            url, sourcegraph.UBER_SOURCEGRAPH_URL + "code.uber.internal/asdf"
+            url, sourcegraph.PUBLIC_SOURCEGRAPH_URL + "github.com/albertyw/git-browse"
         )
 
     def test_get_url_directory(self) -> None:
-        git_object = typedefs.FocusObject("zxcv" + os.sep)
+        git_object = typedefs.FocusObject("git_browse" + os.sep)
         url = self.obj.get_url(git_object)
         self.assertEqual(
             url,
-            sourcegraph.UBER_SOURCEGRAPH_URL
-            + "code.uber.internal/asdf/-/tree/zxcv/",
+            sourcegraph.PUBLIC_SOURCEGRAPH_URL
+            + "github.com/albertyw/git-browse/-/tree/git_browse/",
         )
 
     def test_get_url_file(self) -> None:
-        git_object = typedefs.FocusObject("zxcv")
+        git_object = typedefs.FocusObject("README.md")
         url = self.obj.get_url(git_object)
+        self.assertEqual(
+            url,
+            sourcegraph.PUBLIC_SOURCEGRAPH_URL
+            + "github.com/albertyw/git-browse/-/blob/README.md",
+        )
+
+    def test_uber_init(self) -> None:
+        self.assertEqual(self.uber_obj.host, "code.uber.internal")
+        self.assertEqual(self.uber_obj.repository, "asdf/qwer")
+
+    def test_uber_create(self) -> None:
+        repo = "gitolite@code.uber.internal:a/b"
+        git_config = typedefs.GitConfig(repo, "master")
+        git_config.try_url_match(phabricator.UBER_SSH_GITOLITE_URL)
+        obj = sourcegraph.SourcegraphHost.create(git_config)
+        self.assertEqual(obj.repository, "a/b")
+
+    def test_uber_create_dot_git(self) -> None:
+        repo = "gitolite@code.uber.internal:a/b.git"
+        git_config = typedefs.GitConfig(repo, "master")
+        git_config.try_url_match(phabricator.UBER_SSH_GITOLITE_URL)
+        obj = sourcegraph.SourcegraphHost.create(git_config)
+        self.assertEqual(obj.repository, "a/b")
+
+    def test_uber_get_url_commit(self) -> None:
+        git_object = typedefs.FocusHash("abcd")
+        url = self.uber_obj.get_url(git_object)
+        self.assertEqual(
+            url,
+            sourcegraph.UBER_SOURCEGRAPH_URL
+            + "code.uber.internal/uber-code/asdf-qwer/-/commit/abcd",
+        )
+
+    def test_uber_get_url_root(self) -> None:
+        git_object = typedefs.FocusObject(os.sep)
+        url = self.uber_obj.get_url(git_object)
+        self.assertEqual(
+            url, sourcegraph.UBER_SOURCEGRAPH_URL
+            + "code.uber.internal/uber-code/asdf-qwer"
+        )
+
+    def test_uber_get_url_directory(self) -> None:
+        git_object = typedefs.FocusObject("zxcv" + os.sep)
+        url = self.uber_obj.get_url(git_object)
+        self.assertEqual(
+            url,
+            sourcegraph.UBER_SOURCEGRAPH_URL
+            + "code.uber.internal/uber-code/asdf-qwer/-/tree/zxcv/",
+        )
+
+    def test_uber_get_url_file(self) -> None:
+        git_object = typedefs.FocusObject("zxcv")
+        url = self.uber_obj.get_url(git_object)
         self.assertEqual(
             url,
             sourcegraph.UBER_SOURCEGRAPH_URL
-            + "code.uber.internal/asdf/-/blob/zxcv",
+            + "code.uber.internal/uber-code/asdf-qwer/-/blob/zxcv",
         )