Skip to content

Commit

Permalink
models: various fixes (bug 1912967)
Browse files Browse the repository at this point in the history
- fix import of Worker model
- filter repos by name
- add __str__ methods to various models to improve readability
- add enabled_repo_names shim
- fix related_name on unsorted_revisions field
- add temporary placeholder for phabricator_api_key
- update migrations
  • Loading branch information
zzzeid committed Aug 13, 2024
1 parent 9ca51bb commit e5f43e3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lando/main/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from time import sleep

from lando.main.models import Worker
from lando.main.models.base import Worker


class WorkerMixin:
Expand Down
5 changes: 4 additions & 1 deletion src/lando/main/management/commands/landing_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from django.core.management.base import BaseCommand
from django.db import transaction
from lando.main.management.commands import WorkerMixin
from lando.main.models import LandingJob, LandingJobStatus
from lando.main.models.base import Repo
from lando.main.models.landing_job import LandingJob, LandingJobStatus

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,6 +70,8 @@ def loop(self):

def run_job(self, job: LandingJob) -> bool:
repo = job.target_repo
if not repo:
repo = Repo.objects.get(name=job.repository_name)
repo.reset()
repo.pull()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.7 on 2024-08-13 17:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("main", "0003_alter_profile_options"),
]

operations = [
migrations.AlterField(
model_name="landingjob",
name="unsorted_revisions",
field=models.ManyToManyField(
related_name="landing_jobs",
through="main.RevisionLandingJob",
to="main.revision",
),
),
]
10 changes: 10 additions & 0 deletions src/lando/main/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Repo(BaseModel):
default="",
)

def __str__(self):
return f"{self.name} ({self.default_branch})"

def _run(self, *args, cwd=None):
cwd = cwd or self.system_path
command = ["git"] + list(args)
Expand Down Expand Up @@ -143,6 +146,13 @@ class Worker(BaseModel):
throttle_seconds = models.IntegerField(default=10)
sleep_seconds = models.IntegerField(default=10)

def __str__(self):
return self.name

@property
def enabled_repos(self) -> list[Repo]:
return self.applicable_repos.all()

@property
def enabled_repo_names(self) -> list[str]:
return self.enabled_repos.values_list("name", flat=True)
4 changes: 3 additions & 1 deletion src/lando/main/models/landing_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def __str__(self):
# Identifier of the published commit which this job should land on top of.
target_commit_hash = models.TextField(blank=True, default="")

unsorted_revisions = models.ManyToManyField(Revision, through="RevisionLandingJob")
unsorted_revisions = models.ManyToManyField(
Revision, through="RevisionLandingJob", related_name="landing_jobs"
)

# These are automatically set, deprecated fields, but kept for compatibility.
repository_name = models.TextField(default="", blank=True)
Expand Down
6 changes: 6 additions & 0 deletions src/lando/main/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class Meta:
# User info fetched from SSO.
userinfo = models.JSONField(default=dict, blank=True)

@property
def phabricator_api_key(self):
# Temporary placeholder for phabricator_api_key field.
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1899397.
return ""

def _has_scm_permission_groups(self, codename, groups):
"""Return whether the group membership provides the correct permission.
Expand Down
5 changes: 4 additions & 1 deletion src/lando/main/models/revision.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Revision(BaseModel):
Includes a reference to the related Phabricator revision and diff ID if one exists.
"""

def __str__(self):
return f"Revision {self.revision_id} Diff {self.diff_id}"

# revision_id and diff_id map to Phabricator IDs (integers).
revision_id = models.IntegerField(blank=True, null=True, unique=True)

Expand Down Expand Up @@ -96,7 +99,7 @@ def serialize(self) -> dict[str, Any]:
"id": self.id,
"revision_id": self.revision_id,
"diff_id": self.diff_id,
"landing_jobs": [job.id for job in self.landing_jobs],
"landing_jobs": [job.id for job in self.landing_jobs.all()],
"created_at": self.created_at,
"updated_at": self.updated_at,
}
Expand Down

0 comments on commit e5f43e3

Please sign in to comment.