Skip to content

Commit

Permalink
Merge branch 'sg-uberinternal'
Browse files Browse the repository at this point in the history
* sg-uberinternal:
  Add tests for browsing github repositories on sourcegraph
  Test on namespaced repositories
  Namespace all sg.uberinternal.com tests under uber
  Fix tests
  Switch uber sourcegraph to sg.uberinternal.com
  • Loading branch information
albertyw committed Jan 28, 2024
2 parents 063f2a5 + 7c17a24 commit 8a7ae1f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 25 deletions.
20 changes: 12 additions & 8 deletions git_browse/sourcegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


PUBLIC_SOURCEGRAPH_URL = "https://sourcegraph.com/"
UBER_SOURCEGRAPH_URL = "https://sourcegraph.uberinternal.com/"
UBER_SOURCEGRAPH_URL = "https://sg.uberinternal.com/"


class SourcegraphHost(typedefs.Host):
Expand Down Expand Up @@ -40,14 +40,18 @@ def set_host_class(self, host_class: type[typedefs.Host]) -> None:
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():
Expand Down
92 changes: 75 additions & 17 deletions git_browse/tests/test_sourcegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,40 @@
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]


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 = "[email protected]:a/b"
repo = "[email protected]: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 = "[email protected]:a/b.git"
repo = "[email protected]: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")

Expand All @@ -39,31 +44,84 @@ def test_get_url_commit(self) -> None:
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 = "[email protected]: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 = "[email protected]: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",
)

0 comments on commit 8a7ae1f

Please sign in to comment.