Skip to content

Fix issues around remote repository for sending Build status reports #6017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
from readthedocs.doc_builder.loader import get_builder_class
from readthedocs.doc_builder.python_environments import Conda, Virtualenv
from readthedocs.oauth.models import RemoteRepository
from readthedocs.oauth.services import registry
from readthedocs.oauth.services.github import GitHubService
from readthedocs.projects.models import APIProject, Feature
from readthedocs.search.utils import index_new_files, remove_indexed_files
Expand Down Expand Up @@ -1867,6 +1868,7 @@ def send_build_status(build_pk, status):
:param status: build status failed, pending, or success to be sent.
"""
build = Build.objects.get(pk=build_pk)

try:
if build.project.remote_repository.account.provider == 'github':
service = GitHubService(
Expand All @@ -1878,10 +1880,35 @@ def send_build_status(build_pk, status):
service.send_build_status(build, status)

except RemoteRepository.DoesNotExist:
log.info('Remote repository does not exist for %s', build.project)
# Get the service provider for the project
for service_cls in registry:
if service_cls.is_project_service(build.project):
service = service_cls
break
else:
log.warning('There are no registered services in the application.')
return False

# Try to loop through all project users to get their social accounts
for user in build.project.users.all():
user_accounts = service.for_user(user)
# Try to loop through users all social accounts to send a successful request
for account in user_accounts:
# Currently we only support GitHub Status API
if account.provider_name == 'GitHub':
success = account.send_build_status(build, status)
if success:
return True

log.info(
'No social account or repository permission available for %s',
build.project
)
return False

except Exception:
log.exception('Send build status task failed for %s', build.project)
return False

# TODO: Send build status for other providers.

Expand Down
19 changes: 17 additions & 2 deletions readthedocs/rtd_tests/tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def test_fileify_logging_when_wrong_version_pk(self, mock_logger):
mock_logger.warning.assert_called_with("Version not found for given kwargs. {'pk': 345343}")

@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
def test_send_build_status_task(self, send_build_status):
def test_send_build_status_task_with_remote_repo(self, send_build_status):
social_account = get(SocialAccount, provider='github')
remote_repo = get(RemoteRepository, account=social_account, project=self.project)
remote_repo.users.add(self.eric)
Expand All @@ -348,7 +348,22 @@ def test_send_build_status_task(self, send_build_status):
send_build_status.assert_called_once_with(external_build, BUILD_STATUS_SUCCESS)

@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
def test_send_build_status_task_without_remote_repo(self, send_build_status):
def test_send_build_status_task_with_social_account(self, send_build_status):
social_account = get(SocialAccount, user=self.eric, provider='github')

self.project.repo = 'https://github.com/test/test/'
self.project.save()

external_version = get(Version, project=self.project, type=EXTERNAL)
external_build = get(
Build, project=self.project, version=external_version
)
tasks.send_build_status(external_build.id, BUILD_STATUS_SUCCESS)

send_build_status.assert_called_once_with(external_build, BUILD_STATUS_SUCCESS)

@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
def test_send_build_status_task_without_remote_repo_or_social_account(self, send_build_status):
external_version = get(Version, project=self.project, type=EXTERNAL)
external_build = get(
Build, project=self.project, version=external_version
Expand Down