|
34 | 34 | LATEST,
|
35 | 35 | LATEST_VERBOSE_NAME,
|
36 | 36 | STABLE_VERBOSE_NAME,
|
| 37 | + EXTERNAL, |
| 38 | + BUILD_STATUS_SUCCESS, |
| 39 | + BUILD_STATUS_FAILURE, |
37 | 40 | )
|
38 | 41 | from readthedocs.builds.models import APIVersion, Build, Version
|
39 | 42 | from readthedocs.builds.signals import build_complete
|
|
59 | 62 | )
|
60 | 63 | from readthedocs.doc_builder.loader import get_builder_class
|
61 | 64 | from readthedocs.doc_builder.python_environments import Conda, Virtualenv
|
| 65 | +from readthedocs.oauth.models import RemoteRepository |
| 66 | +from readthedocs.oauth.services.github import GitHubService |
62 | 67 | from readthedocs.projects.models import APIProject, Feature
|
63 | 68 | from readthedocs.search.utils import index_new_files, remove_indexed_files
|
64 | 69 | from readthedocs.sphinx_domains.models import SphinxDomain
|
@@ -573,6 +578,25 @@ def run_build(self, docker, record):
|
573 | 578 |
|
574 | 579 | if self.build_env.failed:
|
575 | 580 | self.send_notifications(self.version.pk, self.build['id'])
|
| 581 | + # send build failure status to git Status API |
| 582 | + send_external_build_status( |
| 583 | + self.build['id'], BUILD_STATUS_FAILURE |
| 584 | + ) |
| 585 | + elif self.build_env.successful: |
| 586 | + # send build successful status to git Status API |
| 587 | + send_external_build_status( |
| 588 | + self.build['id'], BUILD_STATUS_SUCCESS |
| 589 | + ) |
| 590 | + else: |
| 591 | + msg = 'Unhandled Build State' |
| 592 | + log.warning( |
| 593 | + LOG_TEMPLATE, |
| 594 | + { |
| 595 | + 'project': self.project.slug, |
| 596 | + 'version': self.version.slug, |
| 597 | + 'msg': msg, |
| 598 | + } |
| 599 | + ) |
576 | 600 |
|
577 | 601 | build_complete.send(sender=Build, build=self.build_env.build)
|
578 | 602 |
|
@@ -1513,8 +1537,11 @@ def _manage_imported_files(version, path, commit, build):
|
1513 | 1537 | @app.task(queue='web')
|
1514 | 1538 | def send_notifications(version_pk, build_pk):
|
1515 | 1539 | version = Version.objects.get_object_or_log(pk=version_pk)
|
1516 |
| - if not version: |
| 1540 | + |
| 1541 | + # only send notification for Internal versions |
| 1542 | + if not version or version.type == EXTERNAL: |
1517 | 1543 | return
|
| 1544 | + |
1518 | 1545 | build = Build.objects.get(pk=build_pk)
|
1519 | 1546 |
|
1520 | 1547 | for hook in version.project.webhook_notifications.all():
|
@@ -1773,3 +1800,45 @@ def retry_domain_verification(domain_pk):
|
1773 | 1800 | sender=domain.__class__,
|
1774 | 1801 | domain=domain,
|
1775 | 1802 | )
|
| 1803 | + |
| 1804 | + |
| 1805 | +@app.task(queue='web') |
| 1806 | +def send_build_status(build, state): |
| 1807 | + """ |
| 1808 | + Send Build Status to Git Status API for project external versions. |
| 1809 | +
|
| 1810 | + :param build: Build |
| 1811 | + :param state: build state failed, pending, or success to be sent. |
| 1812 | + """ |
| 1813 | + try: |
| 1814 | + if build.project.remote_repository.account.provider == 'github': |
| 1815 | + service = GitHubService( |
| 1816 | + build.project.remote_repository.users.first(), |
| 1817 | + build.project.remote_repository.account |
| 1818 | + ) |
| 1819 | + |
| 1820 | + # send Status report using the API. |
| 1821 | + service.send_build_status(build, state) |
| 1822 | + |
| 1823 | + except RemoteRepository.DoesNotExist: |
| 1824 | + log.info('Remote repository does not exist for %s', build.project) |
| 1825 | + |
| 1826 | + except Exception: |
| 1827 | + log.exception('Send build status task failed for %s', build.project) |
| 1828 | + |
| 1829 | + # TODO: Send build status for other providers. |
| 1830 | + |
| 1831 | + |
| 1832 | +def send_external_build_status(build_pk, state): |
| 1833 | + """ |
| 1834 | + Check if build is external and Send Build Status for project external versions. |
| 1835 | +
|
| 1836 | + :param build_pk: Build pk |
| 1837 | + :param state: build state failed, pending, or success to be sent. |
| 1838 | + """ |
| 1839 | + build = Build.objects.get(pk=build_pk) |
| 1840 | + |
| 1841 | + # Send status reports for only External (pull/merge request) Versions. |
| 1842 | + if build.version.type == EXTERNAL: |
| 1843 | + # call the task that actually send the build status. |
| 1844 | + send_build_status.delay(build, state) |
0 commit comments