Skip to content

Commit

Permalink
feat: support gh refresh tokens
Browse files Browse the repository at this point in the history
Depends on codecov/shared#27
TODO: after that is merged update the sha reference in requirements.in

Adds support for github app refresh tokens

codecov/engineering-team#162
  • Loading branch information
giovanni-guidini committed Aug 30, 2023
1 parent 3afda2c commit 38f7e78
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ factory-boy
fakeredis
freezegun
git+ssh://[email protected]/codecov/[email protected]#egg=codecovopentelem
git+ssh://[email protected]/codecov/shared.git@5fc16f44995155104f617fe2fcda4cc426954cd2#egg=shared
git+ssh://[email protected]/codecov/shared.git@680951c4849074db131353b251581ee03029a72a#egg=shared
gunicorn
https://github.com/photocrowd/django-cursor-pagination/archive/f560902696b0c8509e4d95c10ba0d62700181d84.tar.gz
minio
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile requirements.in
Expand Down Expand Up @@ -325,7 +325,7 @@ sentry-sdk==1.19.1
# via -r requirements.in
setproctitle==1.1.10
# via -r requirements.in
shared @ git+ssh://[email protected]/codecov/shared.git@5fc16f44995155104f617fe2fcda4cc426954cd2
shared @ git+ssh://[email protected]/codecov/shared.git@680951c4849074db131353b251581ee03029a72a
# via -r requirements.in
simplejson==3.17.2
# via -r requirements.in
Expand Down Expand Up @@ -374,6 +374,7 @@ typing-extensions==4.6.2
# via
# ariadne
# shared
# starlette
uritemplate==4.1.1
# via drf-spectacular
urllib3==1.26.13
Expand Down
2 changes: 1 addition & 1 deletion services/repo_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_token_refresh_callback(
Produces a callback function that will encode and update the oauth token of an owner.
This callback is passed to the TorngitAdapter for the service.
"""
if service != Service.GITLAB and service != Service.GITLAB_ENTERPRISE:
if service == Service.BITBUCKET or service == Service.BITBUCKET_SERVER:
return None

@sync_to_async
Expand Down
28 changes: 28 additions & 0 deletions services/tests/test_repo_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def setUp(self):
def get_owner_gl(self):
return Owner.objects.filter(ownerid=self.repo_gl.author.ownerid).first()

@sync_to_async
def get_owner_gh(self):
return Owner.objects.filter(ownerid=self.repo_gh.author.ownerid).first()

def test_get_torngit_with_names(self):
provider = RepoProviderService().get_by_name(
self.repo_gh.author,
Expand Down Expand Up @@ -93,6 +97,30 @@ async def test_refresh_callback(self):
assert saved_token["key"] == "00001023102301"
assert saved_token["refresh_token"] == "20349230952"

@pytest.mark.asyncio
async def test_refresh_callback_github(self):
provider = RepoProviderService().get_by_name(
self.repo_gh.author,
self.repo_gh.name,
self.repo_gh.author,
self.repo_gh.author.service,
)
assert isinstance(Github(), type(provider))
assert provider._on_token_refresh is not None
assert inspect.isawaitable(provider._on_token_refresh())
owner = await self.get_owner_gh()
saved_token = encryptor.decrypt_token(owner.oauth_token)
assert saved_token["key"] == "testaaft3ituvli790m1yajovjv5eg0r4j0264iw"
assert "refresh_token" not in saved_token

new_token = {"key": "00001023102301", "refresh_token": "20349230952"}
await provider._on_token_refresh(new_token)
owner = await self.get_owner_gh()
assert owner.username == "ThiagoCodecov"
saved_token = encryptor.decrypt_token(owner.oauth_token)
assert saved_token["key"] == "00001023102301"
assert saved_token["refresh_token"] == "20349230952"

def test_get_adapter_returns_adapter_for_repo_authors_service(self):
some_other_user = OwnerFactory(service="github")
repo = RepositoryFactory.create(
Expand Down

0 comments on commit 38f7e78

Please sign in to comment.