Skip to content

Commit 2b33b0f

Browse files
Fix status reporting on PRs with the magic exit code (#9807)
* Fix status reporting on PRs with the magic exit code This was because we were using this logic to return a relative URL, and then sending it to GitHub: * https://github.com/readthedocs/readthedocs.org/blob/f86b5376ffc70242bdd677cb02f016429ff6bd4b/readthedocs/oauth/services/github.py#L459-L460 * https://github.com/readthedocs/readthedocs.org/blob/f86b5376ffc70242bdd677cb02f016429ff6bd4b/readthedocs/builds/models.py#L343-L349 Fixes #9791 * Fix link_to_build defaults * Fix tests * Comment on usage * Fix test * Use same logic as absolute_url * VCS commit status: send absolute URL to docs when built successfully - remove `link_to_build` since it's not required - base the decition on `Build.state` and `Build.success` --------- Co-authored-by: Manuel Kaufmann <[email protected]>
1 parent b3c26c8 commit 2b33b0f

File tree

8 files changed

+39
-35
lines changed

8 files changed

+39
-35
lines changed

readthedocs/builds/tasks.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ def delete_closed_external_versions(limit=200, days=30 * 3):
261261
build_pk=last_build.pk,
262262
commit=last_build.commit,
263263
status=status,
264-
link_to_build=True,
265264
)
266265
except Exception:
267266
log.exception(
@@ -377,7 +376,7 @@ def sync_versions_task(project_pk, tags_data, branches_data, **kwargs):
377376
default_retry_delay=60,
378377
queue='web'
379378
)
380-
def send_build_status(build_pk, commit, status, link_to_build=False):
379+
def send_build_status(build_pk, commit, status):
381380
"""
382381
Send Build Status to Git Status API for project external versions.
383382
@@ -432,7 +431,6 @@ def send_build_status(build_pk, commit, status, link_to_build=False):
432431
build=build,
433432
commit=commit,
434433
state=status,
435-
link_to_build=link_to_build,
436434
)
437435

438436
if success:
@@ -449,7 +447,11 @@ def send_build_status(build_pk, commit, status, link_to_build=False):
449447
# Try to loop through services for users all social accounts
450448
# to send successful build status
451449
for service in services:
452-
success = service.send_build_status(build, commit, status)
450+
success = service.send_build_status(
451+
build,
452+
commit,
453+
status,
454+
)
453455
if success:
454456
log.debug(
455457
'Build status report sent correctly using an user account.',

readthedocs/core/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def prepare_build(
106106
version_type=version.type,
107107
build_pk=build.id,
108108
commit=commit,
109-
status=BUILD_STATUS_PENDING
109+
status=BUILD_STATUS_PENDING,
110110
)
111111

112112
if version.type != EXTERNAL:

readthedocs/oauth/services/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def update_webhook(self, project, integration):
293293
"""
294294
raise NotImplementedError
295295

296-
def send_build_status(self, build, commit, state, link_to_build=False):
296+
def send_build_status(self, build, commit, state):
297297
"""
298298
Create commit status for project.
299299
@@ -303,7 +303,6 @@ def send_build_status(self, build, commit, state, link_to_build=False):
303303
:type commit: str
304304
:param state: build state failure, pending, or success.
305305
:type state: str
306-
:param link_to_build: If true, link to the build page regardless the state.
307306
:returns: boolean based on commit status creation was successful or not.
308307
:rtype: Bool
309308
"""

readthedocs/oauth/services/github.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from readthedocs.api.v2.client import api
1414
from readthedocs.builds import utils as build_utils
15-
from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, SELECT_BUILD_STATUS
15+
from readthedocs.builds.constants import BUILD_FINAL_STATES, SELECT_BUILD_STATUS
1616
from readthedocs.core.permissions import AdminPermission
1717
from readthedocs.integrations.models import Integration
1818

@@ -431,7 +431,7 @@ def update_webhook(self, project, integration):
431431
integration.remove_secret()
432432
return (False, resp)
433433

434-
def send_build_status(self, build, commit, state, link_to_build=False):
434+
def send_build_status(self, build, commit, state):
435435
"""
436436
Create GitHub commit status for project.
437437
@@ -441,7 +441,6 @@ def send_build_status(self, build, commit, state, link_to_build=False):
441441
:type state: str
442442
:param commit: commit sha of the pull request
443443
:type commit: str
444-
:param link_to_build: If true, link to the build page regardless the state.
445444
:returns: boolean based on commit status creation was successful or not.
446445
:rtype: Bool
447446
"""
@@ -452,12 +451,14 @@ def send_build_status(self, build, commit, state, link_to_build=False):
452451
# select the correct state and description.
453452
github_build_state = SELECT_BUILD_STATUS[state]['github']
454453
description = SELECT_BUILD_STATUS[state]['description']
455-
456-
target_url = build.get_full_url()
457454
statuses_url = f'https://api.github.com/repos/{owner}/{repo}/statuses/{commit}'
458455

459-
if not link_to_build and state == BUILD_STATUS_SUCCESS:
456+
if build.state in BUILD_FINAL_STATES and build.success:
457+
# Link to the documentation for this version
460458
target_url = build.version.get_absolute_url()
459+
else:
460+
# Link to the build detail's page
461+
target_url = build.get_full_url()
461462

462463
context = f'{settings.RTD_BUILD_STATUS_API_NAME}:{project.slug}'
463464

readthedocs/oauth/services/gitlab.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from requests.exceptions import RequestException
1212

1313
from readthedocs.builds import utils as build_utils
14-
from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, SELECT_BUILD_STATUS
14+
from readthedocs.builds.constants import BUILD_FINAL_STATES, SELECT_BUILD_STATUS
1515
from readthedocs.integrations.models import Integration
1616

1717
from ..constants import GITLAB
@@ -518,7 +518,7 @@ def update_webhook(self, project, integration):
518518
integration.remove_secret()
519519
return (False, resp)
520520

521-
def send_build_status(self, build, commit, state, link_to_build=False):
521+
def send_build_status(self, build, commit, state):
522522
"""
523523
Create GitLab commit status for project.
524524
@@ -528,7 +528,6 @@ def send_build_status(self, build, commit, state, link_to_build=False):
528528
:type state: str
529529
:param commit: commit sha of the pull request
530530
:type commit: str
531-
:param link_to_build: If true, link to the build page regardless the state.
532531
:returns: boolean based on commit status creation was successful or not.
533532
:rtype: Bool
534533
"""
@@ -545,10 +544,12 @@ def send_build_status(self, build, commit, state, link_to_build=False):
545544
gitlab_build_state = SELECT_BUILD_STATUS[state]['gitlab']
546545
description = SELECT_BUILD_STATUS[state]['description']
547546

548-
target_url = build.get_full_url()
549-
550-
if not link_to_build and state == BUILD_STATUS_SUCCESS:
547+
if build.state in BUILD_FINAL_STATES and build.success:
548+
# Link to the documentation for this version
551549
target_url = build.version.get_absolute_url()
550+
else:
551+
# Link to the build detail's page
552+
target_url = build.get_full_url()
552553

553554
context = f'{settings.RTD_BUILD_STATUS_API_NAME}:{project.slug}'
554555

readthedocs/projects/tasks/builds.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
483483
)
484484

485485
# NOTE: why we wouldn't have `self.data.build_commit` here?
486-
# This attribute is set when we get it after clonning the repository
486+
# This attribute is set when we get it after cloning the repository
487487
#
488488
# Oh, I think this is to differentiate a task triggered with
489489
# `Build.commit` than a one triggered just with the `Version` to build

readthedocs/rtd_tests/tests/test_celery.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
from messages_extends.models import Message
99

1010
from readthedocs.builds import tasks as build_tasks
11-
from readthedocs.builds.constants import (
12-
BUILD_STATUS_SUCCESS,
13-
EXTERNAL,
14-
LATEST,
15-
)
11+
from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, EXTERNAL, LATEST
1612
from readthedocs.builds.models import Build, Version
1713
from readthedocs.oauth.models import RemoteRepository, RemoteRepositoryRelation
1814
from readthedocs.projects.models import Project
@@ -105,7 +101,6 @@ def test_send_build_status_with_remote_repo_github(self, send_build_status):
105101
build=external_build,
106102
commit=external_build.commit,
107103
state=BUILD_STATUS_SUCCESS,
108-
link_to_build=False,
109104
)
110105
self.assertEqual(Message.objects.filter(user=self.eric).count(), 0)
111106

@@ -125,7 +120,9 @@ def test_send_build_status_with_social_account_github(self, send_build_status):
125120
)
126121

127122
send_build_status.assert_called_once_with(
128-
external_build, external_build.commit, BUILD_STATUS_SUCCESS
123+
external_build,
124+
external_build.commit,
125+
BUILD_STATUS_SUCCESS,
129126
)
130127
self.assertEqual(Message.objects.filter(user=self.eric).count(), 0)
131128

@@ -171,7 +168,6 @@ def test_send_build_status_with_remote_repo_gitlab(self, send_build_status):
171168
build=external_build,
172169
commit=external_build.commit,
173170
state=BUILD_STATUS_SUCCESS,
174-
link_to_build=False,
175171
)
176172
self.assertEqual(Message.objects.filter(user=self.eric).count(), 0)
177173

@@ -191,7 +187,9 @@ def test_send_build_status_with_social_account_gitlab(self, send_build_status):
191187
)
192188

193189
send_build_status.assert_called_once_with(
194-
external_build, external_build.commit, BUILD_STATUS_SUCCESS
190+
external_build,
191+
external_build.commit,
192+
BUILD_STATUS_SUCCESS,
195193
)
196194
self.assertEqual(Message.objects.filter(user=self.eric).count(), 0)
197195

@@ -209,4 +207,3 @@ def test_send_build_status_no_remote_repo_or_social_account_gitlab(self, send_bu
209207

210208
send_build_status.assert_not_called()
211209
self.assertEqual(Message.objects.filter(user=self.eric).count(), 1)
212-

readthedocs/rtd_tests/tests/test_projects_tasks.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ def setUp(self):
2121
@patch('readthedocs.projects.tasks.utils.send_build_status')
2222
def test_send_external_build_status_with_external_version(self, send_build_status):
2323
send_external_build_status(
24-
self.external_version.type, self.external_build.id,
25-
self.external_build.commit, BUILD_STATUS_SUCCESS
24+
self.external_version.type,
25+
self.external_build.id,
26+
self.external_build.commit,
27+
BUILD_STATUS_SUCCESS,
2628
)
2729

2830
send_build_status.delay.assert_called_once_with(
2931
self.external_build.id,
3032
self.external_build.commit,
31-
BUILD_STATUS_SUCCESS
33+
BUILD_STATUS_SUCCESS,
3234
)
3335

3436
@patch('readthedocs.projects.tasks.utils.send_build_status')
3537
def test_send_external_build_status_with_internal_version(self, send_build_status):
3638
send_external_build_status(
37-
self.internal_version.type, self.internal_build.id,
38-
self.external_build.commit, BUILD_STATUS_SUCCESS
39+
self.internal_version.type,
40+
self.internal_build.id,
41+
self.external_build.commit,
42+
BUILD_STATUS_SUCCESS,
3943
)
4044

4145
send_build_status.delay.assert_not_called()

0 commit comments

Comments
 (0)