Skip to content

Sync versions: filter by type on update #9019

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

Merged
merged 4 commits into from
Apr 6, 2022
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
6 changes: 4 additions & 2 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Utility functions that are used by both views and celery tasks."""

import itertools
import structlog

import structlog
from rest_framework.pagination import PageNumberPagination

from readthedocs.builds.constants import (
Expand Down Expand Up @@ -79,9 +79,11 @@ def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-b
Version.objects.filter(
project=project,
verbose_name=version_name,
# Always filter by type, a tag and a branch
# can share the same verbose_name.
type=type,
).update(
identifier=version_id,
type=type,
machine=False,
)

Expand Down
8 changes: 4 additions & 4 deletions readthedocs/rtd_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def test_git_lsremote(self):
repo_branches, repo_tags = repo.lsremote

self.assertEqual(
set(branches + default_branches),
{branch.verbose_name for branch in repo_branches},
{branch: branch for branch in default_branches + branches},
{branch.verbose_name: branch.identifier for branch in repo_branches},
)

self.assertEqual(
Expand Down Expand Up @@ -111,8 +111,8 @@ def test_git_branches(self, checkout_path):
repo.clone()

self.assertEqual(
set(branches + default_branches),
{branch.verbose_name for branch in repo.branches},
{branch: branch for branch in default_branches + branches},
{branch.verbose_name: branch.identifier for branch in repo.branches},
)

@patch('readthedocs.projects.models.Project.checkout_path')
Expand Down
53 changes: 51 additions & 2 deletions readthedocs/rtd_tests/tests/test_sync_versions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from unittest import mock

from django.conf import settings
Expand All @@ -14,7 +13,6 @@
)
from readthedocs.builds.tasks import sync_versions_task
from readthedocs.organizations.models import Organization, OrganizationOwner
from readthedocs.projects.constants import PUBLIC
from readthedocs.projects.models import Project


Expand Down Expand Up @@ -726,6 +724,57 @@ def test_deletes_version_with_same_identifier(self):
1,
)

def test_versions_with_same_verbose_name(self):
get(
Version,
project=self.pip,
identifier="v2",
verbose_name="v2",
active=True,
type=BRANCH,
)
get(
Version,
project=self.pip,
identifier="1234abc",
verbose_name="v2",
active=True,
type=TAG,
)
branches_data = [
{
"identifier": "v2",
"verbose_name": "v2",
},
]
tags_data = [
{
# THe identifier has changed!
"identifier": "12345abc",
"verbose_name": "v2",
},
]

sync_versions_task(
self.pip.pk,
branches_data=branches_data,
tags_data=tags_data,
)

self.assertEqual(
self.pip.versions.filter(
verbose_name="v2", identifier="v2", type=BRANCH
).count(),
1,
)
self.assertEqual(
self.pip.versions.filter(
verbose_name="v2", identifier="12345abc", type=TAG
).count(),
1,
)


@mock.patch('readthedocs.builds.tasks.run_automation_rules')
def test_automation_rules_are_triggered_for_new_versions(self, run_automation_rules):
Version.objects.create(
Expand Down
14 changes: 10 additions & 4 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,17 @@ def branches(self):

for branch in branches:
verbose_name = branch.name
if verbose_name.startswith('origin/'):
verbose_name = verbose_name.replace('origin/', '')
if verbose_name == 'HEAD':
if verbose_name.startswith("origin/"):
verbose_name = verbose_name.replace("origin/", "", 1)
if verbose_name == "HEAD":
continue
versions.append(VCSVersion(self, str(branch), verbose_name))
versions.append(
VCSVersion(
repository=self,
identifier=verbose_name,
verbose_name=verbose_name,
)
)
return versions

@property
Expand Down