|
1 | 1 | from datetime import datetime, timedelta
|
| 2 | +from textwrap import dedent |
2 | 3 | from unittest import mock
|
3 | 4 |
|
| 5 | +from django.contrib.auth.models import User |
4 | 6 | from django.test import TestCase, override_settings
|
5 | 7 | from django.utils import timezone
|
6 | 8 | from django_dynamic_fixture import get
|
7 | 9 |
|
8 | 10 | from readthedocs.builds.constants import (
|
9 | 11 | BRANCH,
|
| 12 | + BUILD_STATE_FINISHED, |
10 | 13 | EXTERNAL,
|
11 | 14 | EXTERNAL_VERSION_STATE_CLOSED,
|
12 | 15 | EXTERNAL_VERSION_STATE_OPEN,
|
| 16 | + LATEST, |
13 | 17 | TAG,
|
14 | 18 | )
|
15 | 19 | from readthedocs.builds.models import Build, BuildCommandResult, Version
|
16 | 20 | from readthedocs.builds.tasks import (
|
17 | 21 | archive_builds_task,
|
18 | 22 | delete_closed_external_versions,
|
| 23 | + post_build_overview, |
19 | 24 | )
|
| 25 | +from readthedocs.filetreediff.dataclasses import FileTreeDiff, FileTreeDiffFileStatus |
| 26 | +from readthedocs.oauth.constants import GITHUB_APP |
| 27 | +from readthedocs.oauth.models import ( |
| 28 | + GitHubAccountType, |
| 29 | + GitHubAppInstallation, |
| 30 | + RemoteRepository, |
| 31 | +) |
| 32 | +from readthedocs.oauth.services import GitHubAppService |
20 | 33 | from readthedocs.projects.models import Project
|
21 | 34 |
|
22 | 35 |
|
@@ -110,3 +123,217 @@ def test_archive_builds(self, build_commands_storage):
|
110 | 123 | self.assertEqual(Build.objects.count(), 10)
|
111 | 124 | self.assertEqual(Build.objects.filter(cold_storage=True).count(), 5)
|
112 | 125 | self.assertEqual(BuildCommandResult.objects.count(), 50)
|
| 126 | + |
| 127 | + |
| 128 | +@override_settings( |
| 129 | + PRODUCTION_DOMAIN="readthedocs.org", |
| 130 | + PUBLIC_DOMAIN="readthedocs.io", |
| 131 | + RTD_EXTERNAL_VERSION_DOMAIN="readthedocs.build", |
| 132 | +) |
| 133 | +class TestPostBuildOverview(TestCase): |
| 134 | + |
| 135 | + def setUp(self): |
| 136 | + self.user = get(User) |
| 137 | + self.github_app_installation = get( |
| 138 | + GitHubAppInstallation, |
| 139 | + installation_id=1111, |
| 140 | + target_id=1111, |
| 141 | + target_type=GitHubAccountType.USER, |
| 142 | + ) |
| 143 | + self.remote_repository = get( |
| 144 | + RemoteRepository, |
| 145 | + name="repo", |
| 146 | + full_name="user/repo", |
| 147 | + vcs_provider=GITHUB_APP, |
| 148 | + github_app_installation=self.github_app_installation, |
| 149 | + ) |
| 150 | + |
| 151 | + self.project = get( |
| 152 | + Project, |
| 153 | + name="My project", |
| 154 | + slug="my-project", |
| 155 | + users=[self.user], |
| 156 | + remote_repository=self.remote_repository, |
| 157 | + ) |
| 158 | + self.base_version = self.project.versions.get(slug=LATEST) |
| 159 | + self.base_version.built = True |
| 160 | + self.base_version.save() |
| 161 | + self.base_version_build = get( |
| 162 | + Build, |
| 163 | + project=self.project, |
| 164 | + version=self.base_version, |
| 165 | + commit="1234abcd", |
| 166 | + state=BUILD_STATE_FINISHED, |
| 167 | + success=True, |
| 168 | + ) |
| 169 | + self.current_version = get( |
| 170 | + Version, |
| 171 | + project=self.project, |
| 172 | + verbose_name="1", |
| 173 | + slug="1", |
| 174 | + type=EXTERNAL, |
| 175 | + active=True, |
| 176 | + built=True, |
| 177 | + ) |
| 178 | + self.current_version_build = get( |
| 179 | + Build, |
| 180 | + project=self.project, |
| 181 | + version=self.current_version, |
| 182 | + commit="5678abcd", |
| 183 | + state=BUILD_STATE_FINISHED, |
| 184 | + success=True, |
| 185 | + ) |
| 186 | + |
| 187 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 188 | + @mock.patch("readthedocs.builds.reporting.get_diff") |
| 189 | + def test_post_build_overview(self, get_diff, post_comment): |
| 190 | + get_diff.return_value = FileTreeDiff( |
| 191 | + current_version=self.current_version, |
| 192 | + current_version_build=self.current_version_build, |
| 193 | + base_version=self.base_version, |
| 194 | + base_version_build=self.base_version_build, |
| 195 | + files=[ |
| 196 | + ("index.html", FileTreeDiffFileStatus.modified), |
| 197 | + ("changes.html", FileTreeDiffFileStatus.added), |
| 198 | + ("deleteme.html", FileTreeDiffFileStatus.deleted), |
| 199 | + ], |
| 200 | + outdated=False, |
| 201 | + ) |
| 202 | + post_build_overview(build_pk=self.current_version_build.pk) |
| 203 | + expected_comment = dedent( |
| 204 | + f""" |
| 205 | + ## Documentation build overview |
| 206 | +
|
| 207 | + > 📚 [My project](https://readthedocs.org/projects/my-project/) | 🛠️ build [#{self.current_version_build.id}](https://readthedocs.org/projects/my-project/builds/{self.current_version_build.id}/) (5678abcd) | 🔍 [preview](http://my-project--1.readthedocs.build/en/1/) |
| 208 | +
|
| 209 | + ### Files changed |
| 210 | +
|
| 211 | + > Comparing with [latest](http://my-project.readthedocs.io/en/latest/) (1234abcd) |
| 212 | +
|
| 213 | +
|
| 214 | + <details> |
| 215 | + <summary>Show files (3) | 1 modified | 1 added | 1 deleted</summary> |
| 216 | +
|
| 217 | + | File | Status | |
| 218 | + | --- | --- | |
| 219 | + | [changes.html](http://my-project--1.readthedocs.build/en/1/changes.html) | ➕ added | |
| 220 | + | [deleteme.html](http://my-project--1.readthedocs.build/en/1/deleteme.html) | ❌ deleted | |
| 221 | + | [index.html](http://my-project--1.readthedocs.build/en/1/index.html) | 📝 modified | |
| 222 | +
|
| 223 | +
|
| 224 | + </details> |
| 225 | +
|
| 226 | + """ |
| 227 | + ) |
| 228 | + post_comment.assert_called_once_with( |
| 229 | + build=self.current_version_build, |
| 230 | + comment=expected_comment, |
| 231 | + ) |
| 232 | + |
| 233 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 234 | + @mock.patch("readthedocs.builds.reporting.get_diff") |
| 235 | + def test_post_build_overview_more_than_5_files(self, get_diff, post_comment): |
| 236 | + get_diff.return_value = FileTreeDiff( |
| 237 | + current_version=self.current_version, |
| 238 | + current_version_build=self.current_version_build, |
| 239 | + base_version=self.base_version, |
| 240 | + base_version_build=self.base_version_build, |
| 241 | + files=[ |
| 242 | + ("index.html", FileTreeDiffFileStatus.modified), |
| 243 | + ("changes.html", FileTreeDiffFileStatus.added), |
| 244 | + ("deleteme.html", FileTreeDiffFileStatus.deleted), |
| 245 | + ("one.html", FileTreeDiffFileStatus.modified), |
| 246 | + ("three.html", FileTreeDiffFileStatus.modified), |
| 247 | + ("two.html", FileTreeDiffFileStatus.modified), |
| 248 | + ], |
| 249 | + outdated=False, |
| 250 | + ) |
| 251 | + post_build_overview(build_pk=self.current_version_build.pk) |
| 252 | + expected_comment = dedent( |
| 253 | + f""" |
| 254 | + ## Documentation build overview |
| 255 | +
|
| 256 | + > 📚 [My project](https://readthedocs.org/projects/my-project/) | 🛠️ build [#{self.current_version_build.id}](https://readthedocs.org/projects/my-project/builds/{self.current_version_build.id}/) (5678abcd) | 🔍 [preview](http://my-project--1.readthedocs.build/en/1/) |
| 257 | +
|
| 258 | + ### Files changed |
| 259 | +
|
| 260 | + > Comparing with [latest](http://my-project.readthedocs.io/en/latest/) (1234abcd) |
| 261 | +
|
| 262 | +
|
| 263 | + <details> |
| 264 | + <summary>Show files (6) | 4 modified | 1 added | 1 deleted</summary> |
| 265 | +
|
| 266 | + | File | Status | |
| 267 | + | --- | --- | |
| 268 | + | [changes.html](http://my-project--1.readthedocs.build/en/1/changes.html) | ➕ added | |
| 269 | + | [deleteme.html](http://my-project--1.readthedocs.build/en/1/deleteme.html) | ❌ deleted | |
| 270 | + | [index.html](http://my-project--1.readthedocs.build/en/1/index.html) | 📝 modified | |
| 271 | + | [one.html](http://my-project--1.readthedocs.build/en/1/one.html) | 📝 modified | |
| 272 | + | [three.html](http://my-project--1.readthedocs.build/en/1/three.html) | 📝 modified | |
| 273 | + | [two.html](http://my-project--1.readthedocs.build/en/1/two.html) | 📝 modified | |
| 274 | +
|
| 275 | +
|
| 276 | + </details> |
| 277 | +
|
| 278 | + """ |
| 279 | + ) |
| 280 | + post_comment.assert_called_once_with( |
| 281 | + build=self.current_version_build, |
| 282 | + comment=expected_comment, |
| 283 | + ) |
| 284 | + |
| 285 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 286 | + @mock.patch("readthedocs.builds.reporting.get_diff") |
| 287 | + def test_post_build_overview_no_files_changed(self, get_diff, post_comment): |
| 288 | + get_diff.return_value = FileTreeDiff( |
| 289 | + current_version=self.current_version, |
| 290 | + current_version_build=self.current_version_build, |
| 291 | + base_version=self.base_version, |
| 292 | + base_version_build=self.base_version_build, |
| 293 | + files=[], |
| 294 | + outdated=False, |
| 295 | + ) |
| 296 | + post_build_overview(build_pk=self.current_version_build.pk) |
| 297 | + expected_comment = dedent( |
| 298 | + f""" |
| 299 | + ## Documentation build overview |
| 300 | +
|
| 301 | + > 📚 [My project](https://readthedocs.org/projects/my-project/) | 🛠️ build [#{self.current_version_build.id}](https://readthedocs.org/projects/my-project/builds/{self.current_version_build.id}/) (5678abcd) | 🔍 [preview](http://my-project--1.readthedocs.build/en/1/) |
| 302 | +
|
| 303 | + ### Files changed |
| 304 | +
|
| 305 | + > Comparing with [latest](http://my-project.readthedocs.io/en/latest/) (1234abcd) |
| 306 | +
|
| 307 | +
|
| 308 | + No files changed. |
| 309 | +
|
| 310 | + """ |
| 311 | + ) |
| 312 | + post_comment.assert_called_once_with( |
| 313 | + build=self.current_version_build, |
| 314 | + comment=expected_comment, |
| 315 | + ) |
| 316 | + |
| 317 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 318 | + def test_post_build_overview_no_external_version(self, post_comment): |
| 319 | + assert not self.base_version.is_external |
| 320 | + post_build_overview(build_pk=self.base_version_build.pk) |
| 321 | + post_comment.assert_not_called() |
| 322 | + |
| 323 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 324 | + def test_post_build_overview_no_github_app_project(self, post_comment): |
| 325 | + self.project.remote_repository = None |
| 326 | + self.project.save() |
| 327 | + |
| 328 | + assert not self.project.is_github_app_project |
| 329 | + assert self.current_version.is_external |
| 330 | + post_build_overview(build_pk=self.current_version_build.pk) |
| 331 | + post_comment.assert_not_called() |
| 332 | + |
| 333 | + @mock.patch.object(GitHubAppService, "post_comment") |
| 334 | + @mock.patch("readthedocs.builds.reporting.get_diff") |
| 335 | + def test_post_build_overview_no_diff_available(self, get_diff, post_comment): |
| 336 | + get_diff.return_value = None |
| 337 | + assert self.current_version.is_external |
| 338 | + post_build_overview(build_pk=self.current_version_build.pk) |
| 339 | + post_comment.assert_not_called() |
0 commit comments