Skip to content

Commit

Permalink
added commit to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 committed Jul 25, 2019
1 parent 49716a7 commit af20054
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 40 deletions.
4 changes: 3 additions & 1 deletion readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ def get_external_version_response(self, project):
project, identifier, verbose_name
)
# returns external version verbose_name (pull/merge request number)
to_build = build_external_version(project, external_version)
to_build = build_external_version(
project=project, version=external_version, commit=identifier
)

return {
'build_triggered': True,
Expand Down
11 changes: 8 additions & 3 deletions readthedocs/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def broadcast(type, task, args, kwargs=None, callback=None): # pylint: disable=
def prepare_build(
project,
version=None,
commit=None,
record=True,
force=False,
immutable=True,
Expand All @@ -72,6 +73,7 @@ def prepare_build(
:param project: project's documentation to be built
:param version: version of the project to be built. Default: ``project.get_default_version()``
:param commit: commit sha of the version required for sending build status reports
:param record: whether or not record the build in a new Build object
:param force: build the HTML documentation even if the files haven't changed
:param immutable: whether or not create an immutable Celery signature
Expand Down Expand Up @@ -102,6 +104,7 @@ def prepare_build(
kwargs = {
'record': record,
'force': force,
'commit': commit,
}

if record:
Expand Down Expand Up @@ -131,11 +134,11 @@ def prepare_build(
options['soft_time_limit'] = time_limit
options['time_limit'] = int(time_limit * 1.2)

if build:
if build and commit:
# Send pending Build Status using Git Status API for External Builds.
send_external_build_status(
version_type=version.type, build_pk=build.id,
commit=version.identifier, status=BUILD_STATUS_PENDING
commit=commit, status=BUILD_STATUS_PENDING
)

return (
Expand All @@ -149,7 +152,7 @@ def prepare_build(
)


def trigger_build(project, version=None, record=True, force=False):
def trigger_build(project, version=None, commit=None, record=True, force=False):
"""
Trigger a Build.
Expand All @@ -158,6 +161,7 @@ def trigger_build(project, version=None, record=True, force=False):
:param project: project's documentation to be built
:param version: version of the project to be built. Default: ``latest``
:param commit: commit sha of the version required for sending build status reports
:param record: whether or not record the build in a new Build object
:param force: build the HTML documentation even if the files haven't changed
:returns: Celery AsyncResult promise and Build instance
Expand All @@ -166,6 +170,7 @@ def trigger_build(project, version=None, record=True, force=False):
update_docs_task, build = prepare_build(
project,
version,
commit,
record,
force,
immutable=True,
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/core/views/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def delete_external_version(project, identifier, verbose_name):
return None


def build_external_version(project, version):
def build_external_version(project, version, commit):
"""
Where we actually trigger builds for external versions.
Expand All @@ -173,6 +173,6 @@ def build_external_version(project, version):
project.slug,
version.slug,
)
trigger_build(project=project, version=version, force=True)
trigger_build(project=project, version=version, commit=commit, force=True)

return version.verbose_name
66 changes: 38 additions & 28 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def __init__(
build=None,
project=None,
version=None,
commit=None,
task=None,
):
self.build_env = build_env
Expand All @@ -332,6 +333,7 @@ def __init__(
self.version = {}
if version is not None:
self.version = version
self.commit = commit
self.project = {}
if project is not None:
self.project = project
Expand All @@ -342,7 +344,7 @@ def __init__(

# pylint: disable=arguments-differ
def run(
self, version_pk, build_pk=None, record=True, docker=None,
self, version_pk, build_pk=None, commit=None, record=True, docker=None,
force=False, **__
):
"""
Expand All @@ -363,6 +365,7 @@ def run(
:param version_pk int: Project Version id
:param build_pk int: Build id (if None, commands are not recorded)
:param commit: commit sha of the version required for sending build status reports
:param record bool: record a build object in the database
:param docker bool: use docker to build the project (if ``None``,
``settings.DOCKER_ENABLE`` is used)
Expand All @@ -379,6 +382,7 @@ def run(
self.project = self.version.project
self.build = self.get_build(build_pk)
self.build_force = force
self.commit = commit
self.config = None

# Build process starts here
Expand Down Expand Up @@ -577,36 +581,42 @@ def run_build(self, docker, record):
log.warning('No build ID, not syncing files')

if self.build_env.failed:
# TODO: Send RTD Webhook notification for build failure.
self.send_notifications(self.version.pk, self.build['id'])
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.build['commit'],
status=BUILD_STATUS_FAILURE
)

if self.commit:
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.commit,
status=BUILD_STATUS_FAILURE
)
elif self.build_env.successful:
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.build['commit'],
status=BUILD_STATUS_SUCCESS
)
# TODO: Send RTD Webhook notification for build success.
if self.commit:
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.commit,
status=BUILD_STATUS_SUCCESS
)
else:
msg = 'Unhandled Build Status'
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.build['commit'],
status=BUILD_STATUS_FAILURE
)
log.warning(
LOG_TEMPLATE,
{
'project': self.project.slug,
'version': self.version.slug,
'msg': msg,
}
)
if self.commit:
msg = 'Unhandled Build Status'
send_external_build_status(
version_type=self.version.type,
build_pk=self.build['id'],
commit=self.commit,
status=BUILD_STATUS_FAILURE
)
log.warning(
LOG_TEMPLATE,
{
'project': self.project.slug,
'version': self.version.slug,
'msg': msg,
}
)

build_complete.send(sender=Build, build=self.build_env.build)

Expand Down
12 changes: 8 additions & 4 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,13 @@ def setUp(self):
self.github_payload = {
'ref': 'master',
}
self.commit = "ec26de721c3235aad62de7213c562f8c821"
self.github_pull_request_payload = {
"action": "opened",
"number": 2,
"pull_request": {
"head": {
"sha": "ec26de721c3235aad62de7213c562f8c821"
"sha": self.commit
}
}
}
Expand Down Expand Up @@ -932,7 +933,8 @@ def test_github_pull_request_opened_event(self, trigger_build, core_trigger_buil
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project, version=external_version
force=True, project=self.project,
version=external_version, commit=self.commit
)
self.assertTrue(external_version)

Expand Down Expand Up @@ -963,7 +965,8 @@ def test_github_pull_request_reopened_event(self, trigger_build, core_trigger_bu
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project, version=external_version
force=True, project=self.project,
version=external_version, commit=self.commit
)
self.assertTrue(external_version)

Expand Down Expand Up @@ -1007,7 +1010,8 @@ def test_github_pull_request_synchronize_event(self, trigger_build, core_trigger
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project, version=external_version
force=True, project=self.project,
version=external_version, commit=self.commit
)
# `synchronize` webhook event updated the identifier (commit hash)
self.assertNotEqual(prev_identifier, external_version.identifier)
Expand Down
3 changes: 1 addition & 2 deletions readthedocs/rtd_tests/tests/test_config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ def get_update_docs_task(self):
build={
'id': 99,
'state': BUILD_STATE_TRIGGERED,
'commit': 'abc859dada4faf'
}
},
)
return update_docs

Expand Down
6 changes: 6 additions & 0 deletions readthedocs/rtd_tests/tests/test_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_trigger_build_when_version_not_provided_default_version_exist(self, upd
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}

update_docs_task.signature.assert_called_with(
Expand All @@ -74,6 +75,7 @@ def test_trigger_build_when_version_not_provided_default_version_doesnt_exist(se
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}

update_docs_task.signature.assert_called_with(
Expand All @@ -92,6 +94,7 @@ def test_trigger_custom_queue(self, update_docs):
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}
options = {
'queue': 'build03',
Expand All @@ -113,6 +116,7 @@ def test_trigger_build_time_limit(self, update_docs):
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}
options = {
'queue': mock.ANY,
Expand All @@ -135,6 +139,7 @@ def test_trigger_build_invalid_time_limit(self, update_docs):
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}
options = {
'queue': mock.ANY,
Expand All @@ -157,6 +162,7 @@ def test_trigger_build_rounded_time_limit(self, update_docs):
'record': True,
'force': False,
'build_pk': mock.ANY,
'commit': None
}
options = {
'queue': mock.ANY,
Expand Down

0 comments on commit af20054

Please sign in to comment.