Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete tags with same commit #4915

Merged
merged 4 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions readthedocs/restapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
"""Utility functions that are used by both views and celery tasks."""

from __future__ import (
absolute_import, division, print_function, unicode_literals)
absolute_import,
division,
print_function,
unicode_literals,
)

import hashlib
import logging

from rest_framework.pagination import PageNumberPagination

from readthedocs.builds.constants import (LATEST, LATEST_VERBOSE_NAME,
NON_REPOSITORY_VERSIONS, STABLE,
STABLE_VERBOSE_NAME)
from readthedocs.builds.constants import (
BRANCH,
LATEST,
LATEST_VERBOSE_NAME,
NON_REPOSITORY_VERSIONS,
STABLE,
STABLE_VERBOSE_NAME,
TAG,
)
from readthedocs.builds.models import Version
from readthedocs.search.indexes import PageIndex, ProjectIndex, SectionIndex

Expand Down Expand Up @@ -133,15 +143,25 @@ def set_or_create_version(project, slug, version_id, verbose_name, type_):

def delete_versions(project, version_data):
"""Delete all versions not in the current repo."""
current_versions = []
if 'tags' in version_data:
for version in version_data['tags']:
current_versions.append(version['identifier'])
if 'branches' in version_data:
for version in version_data['branches']:
current_versions.append(version['identifier'])
# We use verbose_name for tags
# because several tags can point to the same identifier.
versions_tags = [
version['verbose_name']
for version in version_data.get('tags', [])
]
versions_branches = [
version['identifier']
for version in version_data.get('branches', [])
]
to_delete_qs = project.versions.all()
to_delete_qs = to_delete_qs.exclude(identifier__in=current_versions)
to_delete_qs = to_delete_qs.exclude(
type=TAG,
verbose_name__in=versions_tags,
)
to_delete_qs = to_delete_qs.exclude(
type=BRANCH,
identifier__in=versions_branches,
)
to_delete_qs = to_delete_qs.exclude(uploaded=True)
to_delete_qs = to_delete_qs.exclude(active=True)
to_delete_qs = to_delete_qs.exclude(slug__in=NON_REPOSITORY_VERSIONS)
Expand Down
100 changes: 96 additions & 4 deletions readthedocs/rtd_tests/tests/test_sync_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ def test_new_tag_update_active(self):
self.pip.get_stable_version().identifier,
)

def test_new_tag_update_inactive(self):
def test_new_tag_dont_update_inactive(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, I was able to fix and understand why this test was failing, first this test is wrong, why? Because our current logic says (and do)

https://github.com/rtfd/readthedocs.org/blob/4031b9078efba9948301e89c948a236671086d26/readthedocs/restapi/views/model_views.py#L176-L177

why the test was passing?

Because the version was created with type='unknown', so it was being skipped here

https://github.com/rtfd/readthedocs.org/blob/4031b9078efba9948301e89c948a236671086d26/readthedocs/projects/version_handling.py#L238-L240

Why the test fail now?

Because we are filtering by branches and tags only, so the unknown version was being deleted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, good catch!


Version.objects.create(
project=self.pip,
identifier='0.8.3',
verbose_name='0.8.3',
type=TAG,
active=False,
)

Expand Down Expand Up @@ -142,13 +143,13 @@ def test_new_tag_update_inactive(self):
data=json.dumps(version_post_data),
content_type='application/json',
)
# Version 0.9 becomes the stable version and active
version_9 = Version.objects.get(slug='0.9')
# Version 0.9 becomes the stable version, but it's inactive
version_9 = self.pip.versions.get(slug='0.9')
self.assertEqual(
version_9.identifier,
self.pip.get_stable_version().identifier,
)
self.assertTrue(version_9.active)
self.assertFalse(version_9.active)

# Version 0.8.3 is still inactive
version_8 = Version.objects.get(slug='0.8.3')
Expand Down Expand Up @@ -651,6 +652,97 @@ def test_machine_attr_when_user_define_latest_branch_and_delete_it(self):
)
self.assertTrue(version_latest.machine)

def test_deletes_version_with_same_identifier(self):
version_post_data = {
'branches': [
{
'identifier': 'origin/master',
'verbose_name': 'master',
},
],
'tags': [
{
'identifier': '1234',
'verbose_name': 'one',
},
],
}

resp = self.client.post(
reverse('project-sync-versions', args=[self.pip.pk]),
data=json.dumps(version_post_data),
content_type='application/json',
)
self.assertEqual(resp.status_code, 200)

# We only have one version with an identifier `1234`
self.assertEqual(
self.pip.versions.filter(identifier='1234').count(),
1
)

# We add a new tag with the same identifier
version_post_data = {
'branches': [
{
'identifier': 'origin/master',
'verbose_name': 'master',
},
],
'tags': [
{
'identifier': '1234',
'verbose_name': 'two',
},
{
'identifier': '1234',
'verbose_name': 'one',
},
],
}

resp = self.client.post(
reverse('project-sync-versions', args=[self.pip.pk]),
data=json.dumps(version_post_data),
content_type='application/json',
)
self.assertEqual(resp.status_code, 200)

# We have two versions with an identifier `1234`
self.assertEqual(
self.pip.versions.filter(identifier='1234').count(),
2
)

# We delete one version with identifier `1234`
version_post_data = {
'branches': [
{
'identifier': 'origin/master',
'verbose_name': 'master',
},
],
'tags': [
{
'identifier': '1234',
'verbose_name': 'one',
},
],
}

resp = self.client.post(
reverse('project-sync-versions', args=[self.pip.pk]),
data=json.dumps(version_post_data),
content_type='application/json',
)
self.assertEqual(resp.status_code, 200)

# We have only one version with an identifier `1234`
self.assertEqual(
self.pip.versions.filter(identifier='1234').count(),
1
)


class TestStableVersion(TestCase):
fixtures = ['eric', 'test_data']
Expand Down