Skip to content

Commit 55820c7

Browse files
authored
Merge pull request #6017 from saadmk11/fix-social-account-issue
Fix issues around remote repository for sending Build status reports
2 parents eaae969 + 85ba158 commit 55820c7

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

readthedocs/projects/tasks.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from readthedocs.doc_builder.loader import get_builder_class
6464
from readthedocs.doc_builder.python_environments import Conda, Virtualenv
6565
from readthedocs.oauth.models import RemoteRepository
66+
from readthedocs.oauth.services import registry
6667
from readthedocs.oauth.services.github import GitHubService
6768
from readthedocs.projects.models import APIProject, Feature
6869
from readthedocs.search.utils import index_new_files, remove_indexed_files
@@ -1888,6 +1889,7 @@ def send_build_status(build_pk, commit, status):
18881889
:param status: build status failed, pending, or success to be sent.
18891890
"""
18901891
build = Build.objects.get(pk=build_pk)
1892+
18911893
try:
18921894
if build.project.remote_repository.account.provider == 'github':
18931895
service = GitHubService(
@@ -1899,10 +1901,35 @@ def send_build_status(build_pk, commit, status):
18991901
service.send_build_status(build, commit, status)
19001902

19011903
except RemoteRepository.DoesNotExist:
1902-
log.info('Remote repository does not exist for %s', build.project)
1904+
# Get the service provider for the project
1905+
for service_cls in registry:
1906+
if service_cls.is_project_service(build.project):
1907+
service = service_cls
1908+
break
1909+
else:
1910+
log.warning('There are no registered services in the application.')
1911+
return False
1912+
1913+
# Try to loop through all project users to get their social accounts
1914+
for user in build.project.users.all():
1915+
user_accounts = service.for_user(user)
1916+
# Try to loop through users all social accounts to send a successful request
1917+
for account in user_accounts:
1918+
# Currently we only support GitHub Status API
1919+
if account.provider_name == 'GitHub':
1920+
success = account.send_build_status(build, status)
1921+
if success:
1922+
return True
1923+
1924+
log.info(
1925+
'No social account or repository permission available for %s',
1926+
build.project
1927+
)
1928+
return False
19031929

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

19071934
# TODO: Send build status for other providers.
19081935

readthedocs/rtd_tests/tests/test_celery.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def test_fileify_logging_when_wrong_version_pk(self, mock_logger):
334334
mock_logger.warning.assert_called_with("Version not found for given kwargs. {'pk': 345343}")
335335

336336
@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
337-
def test_send_build_status_task(self, send_build_status):
337+
def test_send_build_status_task_with_remote_repo(self, send_build_status):
338338
social_account = get(SocialAccount, provider='github')
339339
remote_repo = get(RemoteRepository, account=social_account, project=self.project)
340340
remote_repo.users.add(self.eric)
@@ -352,7 +352,22 @@ def test_send_build_status_task(self, send_build_status):
352352
)
353353

354354
@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
355-
def test_send_build_status_task_without_remote_repo(self, send_build_status):
355+
def test_send_build_status_task_with_social_account(self, send_build_status):
356+
social_account = get(SocialAccount, user=self.eric, provider='github')
357+
358+
self.project.repo = 'https://github.com/test/test/'
359+
self.project.save()
360+
361+
external_version = get(Version, project=self.project, type=EXTERNAL)
362+
external_build = get(
363+
Build, project=self.project, version=external_version
364+
)
365+
tasks.send_build_status(external_build.id, BUILD_STATUS_SUCCESS)
366+
367+
send_build_status.assert_called_once_with(external_build, BUILD_STATUS_SUCCESS)
368+
369+
@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
370+
def test_send_build_status_task_without_remote_repo_or_social_account(self, send_build_status):
356371
external_version = get(Version, project=self.project, type=EXTERNAL)
357372
external_build = get(
358373
Build, project=self.project, version=external_version

0 commit comments

Comments
 (0)