Skip to content

Commit 49716a7

Browse files
committed
commit sha passed to build status reporting task
1 parent d028222 commit 49716a7

File tree

8 files changed

+61
-29
lines changed

8 files changed

+61
-29
lines changed

readthedocs/core/utils/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def prepare_build(
133133

134134
if build:
135135
# Send pending Build Status using Git Status API for External Builds.
136-
send_external_build_status(version=version, build_pk=build.id, status=BUILD_STATUS_PENDING)
136+
send_external_build_status(
137+
version_type=version.type, build_pk=build.id,
138+
commit=version.identifier, status=BUILD_STATUS_PENDING
139+
)
137140

138141
return (
139142
update_docs_task.signature(

readthedocs/oauth/services/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,14 @@ def update_webhook(self, project, integration):
250250
"""
251251
raise NotImplementedError
252252

253-
def send_build_status(self, build, state):
253+
def send_build_status(self, build, commit, state):
254254
"""
255255
Create commit status for project.
256256
257257
:param build: Build to set up commit status for
258258
:type build: Build
259+
:param commit: commit sha of the pull/merge request
260+
:type commit: str
259261
:param state: build state failure, pending, or success.
260262
:type state: str
261263
:returns: boolean based on commit status creation was successful or not.

readthedocs/oauth/services/github.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from readthedocs.api.v2.client import api
1414
from readthedocs.builds import utils as build_utils
1515
from readthedocs.builds.constants import (
16-
BUILD_STATUS_PENDING,
1716
SELECT_BUILD_STATUS,
1817
RTD_BUILD_STATUS_API_NAME
1918
)
@@ -316,26 +315,23 @@ def update_webhook(self, project, integration):
316315
)
317316
return (False, resp)
318317

319-
def send_build_status(self, build, state):
318+
def send_build_status(self, build, commit, state):
320319
"""
321320
Create GitHub commit status for project.
322321
323322
:param build: Build to set up commit status for
324323
:type build: Build
325324
:param state: build state failure, pending, or success.
326325
:type state: str
326+
:param commit: commit sha of the pull request
327+
:type commit: str
327328
:returns: boolean based on commit status creation was successful or not.
328329
:rtype: Bool
329330
"""
330331
session = self.get_session()
331332
project = build.project
332333
owner, repo = build_utils.get_github_username_repo(url=project.repo)
333334

334-
if state == BUILD_STATUS_PENDING:
335-
build_sha = build.version.identifier
336-
else:
337-
build_sha = build.commit
338-
339335
# select the correct state and description.
340336
github_build_state = SELECT_BUILD_STATUS[state]['github']
341337
description = SELECT_BUILD_STATUS[state]['description']
@@ -351,7 +347,7 @@ def send_build_status(self, build, state):
351347

352348
try:
353349
resp = session.post(
354-
f'https://api.github.com/repos/{owner}/{repo}/statuses/{build_sha}',
350+
f'https://api.github.com/repos/{owner}/{repo}/statuses/{commit}',
355351
data=json.dumps(data),
356352
headers={'content-type': 'application/json'},
357353
)

readthedocs/projects/tasks.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,25 @@ def run_build(self, docker, record):
579579
if self.build_env.failed:
580580
self.send_notifications(self.version.pk, self.build['id'])
581581
send_external_build_status(
582-
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_FAILURE
582+
version_type=self.version.type,
583+
build_pk=self.build['id'],
584+
commit=self.build['commit'],
585+
status=BUILD_STATUS_FAILURE
583586
)
584587
elif self.build_env.successful:
585588
send_external_build_status(
586-
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_SUCCESS
589+
version_type=self.version.type,
590+
build_pk=self.build['id'],
591+
commit=self.build['commit'],
592+
status=BUILD_STATUS_SUCCESS
587593
)
588594
else:
589595
msg = 'Unhandled Build Status'
590596
send_external_build_status(
591-
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_FAILURE
597+
version_type=self.version.type,
598+
build_pk=self.build['id'],
599+
commit=self.build['commit'],
600+
status=BUILD_STATUS_FAILURE
592601
)
593602
log.warning(
594603
LOG_TEMPLATE,
@@ -1822,11 +1831,12 @@ def retry_domain_verification(domain_pk):
18221831

18231832

18241833
@app.task(queue='web')
1825-
def send_build_status(build_pk, status):
1834+
def send_build_status(build_pk, commit, status):
18261835
"""
18271836
Send Build Status to Git Status API for project external versions.
18281837
18291838
:param build_pk: Build primary key
1839+
:param commit: commit sha of the pull/merge request
18301840
:param status: build status failed, pending, or success to be sent.
18311841
"""
18321842
build = Build.objects.get(pk=build_pk)
@@ -1838,7 +1848,7 @@ def send_build_status(build_pk, status):
18381848
)
18391849

18401850
# send Status report using the API.
1841-
service.send_build_status(build, status)
1851+
service.send_build_status(build, commit, status)
18421852

18431853
except RemoteRepository.DoesNotExist:
18441854
log.info('Remote repository does not exist for %s', build.project)
@@ -1849,16 +1859,17 @@ def send_build_status(build_pk, status):
18491859
# TODO: Send build status for other providers.
18501860

18511861

1852-
def send_external_build_status(version, build_pk, status):
1862+
def send_external_build_status(version_type, build_pk, commit, status):
18531863
"""
18541864
Check if build is external and Send Build Status for project external versions.
18551865
1856-
:param version: Version instance
1866+
:param version_type: Version type e.g EXTERNAL, BRANCH, TAG
18571867
:param build_pk: Build pk
1868+
:param commit: commit sha of the pull/merge request
18581869
:param status: build status failed, pending, or success to be sent.
18591870
"""
18601871

18611872
# Send status reports for only External (pull/merge request) Versions.
1862-
if version.type == EXTERNAL:
1873+
if version_type == EXTERNAL:
18631874
# call the task that actually send the build status.
1864-
send_build_status.delay(build_pk, status)
1875+
send_build_status.delay(build_pk, commit, status)

readthedocs/rtd_tests/tests/test_celery.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,22 @@ def test_send_build_status_task(self, send_build_status):
343343
external_build = get(
344344
Build, project=self.project, version=external_version
345345
)
346-
tasks.send_build_status(external_build.id, BUILD_STATUS_SUCCESS)
346+
tasks.send_build_status(
347+
external_build.id, external_build.commit, BUILD_STATUS_SUCCESS
348+
)
347349

348-
send_build_status.assert_called_once_with(external_build, BUILD_STATUS_SUCCESS)
350+
send_build_status.assert_called_once_with(
351+
external_build, external_build.commit, BUILD_STATUS_SUCCESS
352+
)
349353

350354
@patch('readthedocs.projects.tasks.GitHubService.send_build_status')
351355
def test_send_build_status_task_without_remote_repo(self, send_build_status):
352356
external_version = get(Version, project=self.project, type=EXTERNAL)
353357
external_build = get(
354358
Build, project=self.project, version=external_version
355359
)
356-
tasks.send_build_status(external_build.id, BUILD_STATUS_SUCCESS)
360+
tasks.send_build_status(
361+
external_build.id, external_build.commit, BUILD_STATUS_SUCCESS
362+
)
357363

358364
send_build_status.assert_not_called()

readthedocs/rtd_tests/tests/test_config_integration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,11 @@ def get_update_docs_task(self):
368368
config=load_yaml_config(self.version),
369369
project=self.project,
370370
version=self.version,
371-
build={'id': 99, 'state': BUILD_STATE_TRIGGERED}
371+
build={
372+
'id': 99,
373+
'state': BUILD_STATE_TRIGGERED,
374+
'commit': 'abc859dada4faf'
375+
}
372376
)
373377
return update_docs
374378

readthedocs/rtd_tests/tests/test_oauth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def test_send_build_status_successful(self, session, mock_logger):
156156
session().post.return_value.status_code = 201
157157
success = self.service.send_build_status(
158158
self.external_build,
159+
self.external_build.commit,
159160
BUILD_STATUS_SUCCESS
160161
)
161162

@@ -172,6 +173,7 @@ def test_send_build_status_404_error(self, session, mock_logger):
172173
session().post.return_value.status_code = 404
173174
success = self.service.send_build_status(
174175
self.external_build,
176+
self.external_build.commit,
175177
BUILD_STATUS_SUCCESS
176178
)
177179

@@ -187,7 +189,7 @@ def test_send_build_status_404_error(self, session, mock_logger):
187189
def test_send_build_status_value_error(self, session, mock_logger):
188190
session().post.side_effect = ValueError
189191
success = self.service.send_build_status(
190-
self.external_build, BUILD_STATUS_SUCCESS
192+
self.external_build, self.external_build.commit, BUILD_STATUS_SUCCESS
191193
)
192194

193195
self.assertFalse(success)

readthedocs/rtd_tests/tests/test_projects_tasks.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,22 @@ def setUp(self):
138138

139139
@patch('readthedocs.projects.tasks.send_build_status')
140140
def test_send_external_build_status_with_external_version(self, send_build_status):
141-
send_external_build_status(self.external_version,
142-
self.external_build.id, BUILD_STATUS_SUCCESS)
141+
send_external_build_status(
142+
self.external_version.type, self.external_build.id,
143+
self.external_build.commit, BUILD_STATUS_SUCCESS
144+
)
143145

144-
send_build_status.delay.assert_called_once_with(self.external_build.id, BUILD_STATUS_SUCCESS)
146+
send_build_status.delay.assert_called_once_with(
147+
self.external_build.id,
148+
self.external_build.commit,
149+
BUILD_STATUS_SUCCESS
150+
)
145151

146152
@patch('readthedocs.projects.tasks.send_build_status')
147153
def test_send_external_build_status_with_internal_version(self, send_build_status):
148-
send_external_build_status(self.internal_version,
149-
self.internal_build.id, BUILD_STATUS_SUCCESS)
154+
send_external_build_status(
155+
self.internal_version.type, self.internal_build.id,
156+
self.external_build.commit, BUILD_STATUS_SUCCESS
157+
)
150158

151159
send_build_status.delay.assert_not_called()

0 commit comments

Comments
 (0)