Skip to content

Commit

Permalink
Merge branch 'zeid/bug-1944341-legacy-repo-linking' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeid committed Jan 30, 2025
2 parents ee31efb + 2bcd3f0 commit 26cf188
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ env
staticfiles
src/lando/version.py
.test-use-suite
.coverage
.coverage*
5 changes: 5 additions & 0 deletions src/lando/api/legacy/api/transplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ def _assess_transplant_request(
repo = stack_data.repositories[to_land[0][0]["fields"]["repositoryPHID"]]
landing_repo = landable_repos[repo["phid"]]

# If the landing repo on the current revision is set as a legacy repo
# of another repo, then change the landing repo to match the target.
if landing_repo.is_legacy:
landing_repo = landing_repo.legacy_target

involved_phids = set()
for revision, _ in to_land:
involved_phids.update(gather_involved_phids(revision))
Expand Down
104 changes: 104 additions & 0 deletions src/lando/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import os
import pathlib
from collections.abc import Callable
from datetime import datetime, timezone
from pathlib import Path

import py
import pytest
import redis
import requests_mock
Expand All @@ -25,6 +28,7 @@
from lando.api.tests.mocks import PhabricatorDouble, TreeStatusDouble
from lando.main.models import SCM_LEVEL_1, SCM_LEVEL_3, Repo
from lando.main.scm import SCM_TYPE_HG
from lando.main.scm.consts import SCM_TYPE_GIT
from lando.main.support import LegacyAPIException
from lando.main.tests.conftest import git_repo, git_repo_seed
from lando.utils.phabricator import PhabricatorClient
Expand Down Expand Up @@ -576,3 +580,103 @@ def put(self, path, **kwargs):
def authenticated_client(user, user_plaintext_password, client):
client.login(username=user.username, password=user_plaintext_password)
return client


@pytest.mark.django_db
def hg_repo_mc(
hg_server: str,
hg_clone: py.path,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
params = {
"required_permission": SCM_LEVEL_3,
"url": hg_server,
"push_path": hg_server,
"pull_path": hg_server,
"system_path": hg_clone.strpath,
# The option below can be overriden in the parameters
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}
repo = Repo.objects.create(
scm_type=SCM_TYPE_HG,
name="mozilla-central-hg",
**params,
)
repo.save()
return repo


@pytest.mark.django_db
def git_repo_mc(
git_repo: pathlib.Path,
tmp_path: pathlib.Path,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
repos_dir = tmp_path / "repos"
repos_dir.mkdir()

params = {
"required_permission": SCM_LEVEL_3,
"url": str(git_repo),
"push_path": str(git_repo),
"pull_path": str(git_repo),
"system_path": repos_dir / "git_repo",
# The option below can be overriden in the parameters
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}

repo = Repo.objects.create(
scm_type=SCM_TYPE_GIT,
name="mozilla-central-git",
**params,
)
repo.save()
repo.scm.prepare_repo(repo.pull_path)
return repo


@pytest.fixture()
def repo_mc(
# Git
git_repo: pathlib.Path,
tmp_path: pathlib.Path,
# Hg
hg_server: str,
hg_clone: py.path,
) -> Callable:
def factory(
scm_type: str,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
params = {
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}

if scm_type == SCM_TYPE_GIT:
return git_repo_mc(git_repo, tmp_path, **params)
elif scm_type == SCM_TYPE_HG:
return hg_repo_mc(hg_server, hg_clone, **params)
raise Exception(f"Unknown SCM Type {scm_type=}")

return factory
2 changes: 1 addition & 1 deletion src/lando/api/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_app_wide_headers_set(client):


# See bug 1927163.
@pytest.mark.xfail
@pytest.mark.xfail(strict=True)
@pytest.mark.django_db
def test_app_wide_headers_set_for_api_endpoints(client):
response = client.get("/__version__")
Expand Down
107 changes: 1 addition & 106 deletions src/lando/api/tests/test_landings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import io
import pathlib
import unittest.mock as mock
from collections.abc import Callable

import py
import pytest

from lando.api.legacy.workers.landing_worker import (
AUTOFORMAT_COMMIT_MESSAGE,
LandingWorker,
)
from lando.main.models import SCM_LEVEL_3, Repo
from lando.main.models import Repo
from lando.main.models.landing_job import (
LandingJob,
LandingJobStatus,
Expand Down Expand Up @@ -238,106 +235,6 @@ def _create_patch_revision(number, patch=normal_patch_0):
""".lstrip()


@pytest.mark.django_db
def hg_repo_mc(
hg_server: str,
hg_clone: py.path,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
params = {
"required_permission": SCM_LEVEL_3,
"url": hg_server,
"push_path": hg_server,
"pull_path": hg_server,
"system_path": hg_clone.strpath,
# The option below can be overriden in the parameters
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}
repo = Repo.objects.create(
scm_type=SCM_TYPE_HG,
name="mozilla-central-hg",
**params,
)
repo.save()
return repo


@pytest.mark.django_db
def git_repo_mc(
git_repo: pathlib.Path,
tmp_path: pathlib.Path,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
repos_dir = tmp_path / "repos"
repos_dir.mkdir()

params = {
"required_permission": SCM_LEVEL_3,
"url": str(git_repo),
"push_path": str(git_repo),
"pull_path": str(git_repo),
"system_path": repos_dir / "git_repo",
# The option below can be overriden in the parameters
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}

repo = Repo.objects.create(
scm_type=SCM_TYPE_GIT,
name="mozilla-central-git",
**params,
)
repo.save()
repo.scm.prepare_repo(repo.pull_path)
return repo


@pytest.fixture()
def repo_mc(
# Git
git_repo: pathlib.Path,
tmp_path: pathlib.Path,
# Hg
hg_server: str,
hg_clone: py.path,
) -> Callable:
def factory(
scm_type: str,
*,
approval_required: bool = False,
autoformat_enabled: bool = False,
force_push: bool = False,
push_target: str = "",
) -> Repo:
params = {
"approval_required": approval_required,
"autoformat_enabled": autoformat_enabled,
"force_push": force_push,
"push_target": push_target,
}

if scm_type == SCM_TYPE_GIT:
return git_repo_mc(git_repo, tmp_path, **params)
elif scm_type == SCM_TYPE_HG:
return hg_repo_mc(hg_server, hg_clone, **params)
raise Exception(f"Unknown SCM Type {scm_type=}")

return factory


@pytest.mark.parametrize(
"repo_type,revisions_params",
[
Expand Down Expand Up @@ -1032,8 +929,6 @@ def test_format_patch_no_landoini(
), "Successful landing should trigger Phab repo update."


# bug 1893453
@pytest.mark.xfail
@pytest.mark.django_db
def test_landing_job_revisions_sorting(
create_patch_revision,
Expand Down
66 changes: 61 additions & 5 deletions src/lando/api/tests/test_transplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
add_job_with_revisions,
)
from lando.main.models.revision import Revision
from lando.main.scm import SCM_TYPE_HG
from lando.main.scm import SCM_TYPE_GIT, SCM_TYPE_HG
from lando.utils.phabricator import PhabricatorRevisionStatus, ReviewerStatus
from lando.utils.tasks import admin_remove_phab_project

Expand Down Expand Up @@ -327,8 +327,6 @@ def test_dryrun_outside_codefreeze(
assert not response.json["warnings"]


# auth related issue, blockers empty.
@pytest.mark.xfail
@pytest.mark.parametrize(
"permissions,status,blocker",
[
Expand Down Expand Up @@ -645,8 +643,6 @@ def test_confirmation_token_warning_order():
)


# bug 1893453.
@pytest.mark.xfail
@pytest.mark.django_db(transaction=True)
def test_integrated_transplant_simple_stack_saves_data_in_db(
app,
Expand Down Expand Up @@ -1346,3 +1342,63 @@ def test_unresolved_comment_stack(
assert (
response.json["warnings"][0]["id"] == 9
), "the warning ID should match the ID for warning_unresolved_comments"


@pytest.mark.django_db(transaction=True)
def test_transplant_on_linked_legacy_repo(
app,
proxy_client,
phabdouble,
treestatusdouble,
register_codefreeze_uri,
mocked_repo_config,
mock_permissions,
repo_mc,
):
new_repo = repo_mc(SCM_TYPE_GIT)
new_repo.legacy_source = Repo.objects.get(name="mozilla-central")
new_repo.save()
phabrepo = phabdouble.repo(name="mozilla-central")
user = phabdouble.user(username="reviewer")

d1 = phabdouble.diff()
r1 = phabdouble.revision(diff=d1, repo=phabrepo)
phabdouble.reviewer(r1, user)

d2 = phabdouble.diff()
r2 = phabdouble.revision(diff=d2, repo=phabrepo, depends_on=[r1])
phabdouble.reviewer(r2, user)

d3 = phabdouble.diff()
r3 = phabdouble.revision(diff=d3, repo=phabrepo, depends_on=[r2])

phabdouble.reviewer(r3, user)
response = proxy_client.post(
"/transplants",
json={
"landing_path": [
{"revision_id": "D{}".format(r1["id"]), "diff_id": d1["id"]},
{"revision_id": "D{}".format(r2["id"]), "diff_id": d2["id"]},
{"revision_id": "D{}".format(r3["id"]), "diff_id": d3["id"]},
]
},
permissions=mock_permissions,
)
assert response.status_code == 202
assert response.content_type == "application/json"
assert "id" in response.json
job_id = response.json["id"]

# Get LandingJob object by its id
job = LandingJob.objects.get(pk=job_id)
assert job.id == job_id
assert [
(revision.revision_id, revision.diff_id) for revision in job.revisions.all()
] == [
(r1["id"], d1["id"]),
(r2["id"], d2["id"]),
(r3["id"], d3["id"]),
]
assert job.status == LandingJobStatus.SUBMITTED
assert job.target_repo == new_repo
assert job.landed_revisions == {1: 1, 2: 2, 3: 3}
2 changes: 1 addition & 1 deletion src/lando/api/tests/test_uplift.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_strip_depends_on_from_commit_message():
), "`Depends on` line should be stripped from commit message."


@pytest.mark.xfail
@pytest.mark.xfail(strict=True)
def test_uplift_creation(
db,
monkeypatch,
Expand Down
Loading

0 comments on commit 26cf188

Please sign in to comment.