Skip to content

Commit 8e8d2d2

Browse files
authored
transplants: replace s3 uploads with Revision model (bug 1753728) (#296)
- remove S3 functionality + tests - add Revision model - add support for storing patches in new model
1 parent 39f78a3 commit 8e8d2d2

File tree

15 files changed

+182
-516
lines changed

15 files changed

+182
-516
lines changed

landoapi/api/transplants.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
from landoapi import auth
1515
from landoapi.commit_message import format_commit_message
1616
from landoapi.decorators import require_phabricator_api_key
17-
from landoapi.hgexports import build_patch_for_revision
1817
from landoapi.models.landing_job import LandingJob, LandingJobStatus
19-
from landoapi.patches import upload
18+
from landoapi.models.revisions import Revision
2019
from landoapi.phabricator import PhabricatorClient
2120
from landoapi.projects import (
2221
CHECKIN_PROJ_SLUG,
@@ -309,7 +308,6 @@ def post(phab: PhabricatorClient, data: dict):
309308
}
310309

311310
# Build the patches to land.
312-
patch_urls = []
313311
for revision, diff in to_land:
314312
reviewers = get_collated_reviewers(revision)
315313
accepted_reviewers = reviewers_for_commit_message(
@@ -341,23 +339,28 @@ def post(phab: PhabricatorClient, data: dict):
341339
author_name, author_email = select_diff_author(diff)
342340
timestamp = int(datetime.now().timestamp())
343341

344-
# Construct the patch that will be sent to transplant.
345-
raw_diff = phab.call_conduit("differential.getrawdiff", diffID=diff["id"])
346-
patch = build_patch_for_revision(
347-
raw_diff, author_name, author_email, commit_message, timestamp
348-
)
342+
# Construct the patch that will be transplanted.
343+
revision_id = revision["id"]
344+
diff_id = diff["id"]
349345

350-
# Upload the patch to S3
351-
patch_url = upload(
352-
revision["id"],
353-
diff["id"],
354-
patch,
355-
current_app.config["PATCH_BUCKET_NAME"],
356-
aws_access_key=current_app.config["AWS_ACCESS_KEY"],
357-
aws_secret_key=current_app.config["AWS_SECRET_KEY"],
358-
endpoint_url=current_app.config["S3_ENDPOINT_URL"],
359-
)
360-
patch_urls.append(patch_url)
346+
lando_revision = Revision.get_from_revision_id(revision_id)
347+
if not lando_revision:
348+
lando_revision = Revision(revision_id=revision_id)
349+
db.session.add(lando_revision)
350+
351+
lando_revision.diff_id = diff_id
352+
db.session.commit()
353+
354+
patch_data = {
355+
"author_name": author_name,
356+
"author_email": author_email,
357+
"commit_message": commit_message,
358+
"timestamp": timestamp,
359+
}
360+
361+
raw_diff = phab.call_conduit("differential.getrawdiff", diffID=diff["id"])
362+
lando_revision.set_patch(raw_diff, patch_data)
363+
db.session.commit()
361364

362365
ldap_username = g.auth0_user.email
363366
revision_to_diff_id = {str(r["id"]): d["id"] for r, d in to_land}

landoapi/app.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from landoapi.dockerflow import dockerflow
1919
from landoapi.hooks import initialize_hooks
2020
from landoapi.logging import logging_subsystem
21-
from landoapi.patches import patches_s3_subsystem
2221
from landoapi.phabricator import phabricator_subsystem
2322
from landoapi.repos import repo_clone_subsystem
2423
from landoapi.sentry import sentry_subsystem
@@ -41,7 +40,6 @@
4140
celery_subsystem,
4241
db_subsystem,
4342
lando_ui_subsystem,
44-
patches_s3_subsystem,
4543
phabricator_subsystem,
4644
smtp_subsystem,
4745
treestatus_subsystem,
@@ -90,7 +88,6 @@ def load_config() -> dict[str, Any]:
9088
"PHABRICATOR_URL",
9189
"REPO_CLONES_PATH",
9290
"REPOS_TO_LAND",
93-
"S3_ENDPOINT_URL",
9491
"SENTRY_DSN",
9592
"TRANSPLANT_PASSWORD",
9693
"TRANSPLANT_API_KEY",

landoapi/cli.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import connexion
1414
from flask.cli import FlaskGroup
1515

16-
from landoapi import (
17-
patches,
18-
)
1916
from landoapi.models.configuration import (
2017
ConfigurationVariable,
2118
ConfigurationKey,
@@ -57,18 +54,6 @@ def cli():
5754
"""Lando API cli."""
5855

5956

60-
@cli.command()
61-
def init_s3():
62-
"""Initialize fake S3 bucket for development purposes."""
63-
# Create a fake S3 bucket, ie for moto.
64-
s3 = patches.create_s3(
65-
aws_access_key=os.environ["AWS_ACCESS_KEY"],
66-
aws_secret_key=os.environ["AWS_SECRET_KEY"],
67-
endpoint_url=os.environ["S3_ENDPOINT_URL"],
68-
)
69-
s3.create_bucket(Bucket=os.environ["PATCH_BUCKET_NAME"])
70-
71-
7257
@cli.command(context_settings=dict(ignore_unknown_options=True))
7358
@click.argument("celery_arguments", nargs=-1, type=click.UNPROCESSED)
7459
def worker(celery_arguments):

landoapi/models/revisions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
Phabricator diff that is associated with a particular revision.
1010
"""
1111

12+
from __future__ import annotations
13+
1214
import enum
1315
import logging
1416

1517
from sqlalchemy.dialects.postgresql.json import JSONB
1618

19+
from landoapi.hgexports import build_patch_for_revision
1720
from landoapi.models.base import Base
1821
from landoapi.storage import db
1922

@@ -32,6 +35,40 @@ class DiffWarningGroup(enum.Enum):
3235
LINT = "LINT"
3336

3437

38+
class Revision(Base):
39+
"""
40+
A representation of a revision in the database referencing a Phabricator revision.
41+
"""
42+
43+
# revision_id and diff_id map to Phabricator IDs (integers).
44+
revision_id = db.Column(db.Integer, nullable=False, unique=True)
45+
diff_id = db.Column(db.Integer, nullable=False)
46+
47+
# The actual patch.
48+
patch_bytes = db.Column(db.LargeBinary, nullable=False, default=b"")
49+
50+
# Patch metadata, such as author, timestamp, etc...
51+
patch_data = db.Column(JSONB, nullable=False, default=dict)
52+
53+
def __repr__(self):
54+
"""Return a human-readable representation of the instance."""
55+
return (
56+
f"<{self.__class__.__name__}: {self.id} "
57+
f"[D{self.revision_id}-{self.diff_id}]>"
58+
)
59+
60+
@classmethod
61+
def get_from_revision_id(cls, revision_id: int) -> "Revision" | None:
62+
"""Return a Revision object from a given ID."""
63+
return cls.query.filter(Revision.revision_id == revision_id).one_or_none()
64+
65+
def set_patch(self, raw_diff: bytes, patch_data: dict[str, str]):
66+
"""Given a raw_diff and patch data, build the patch and store it."""
67+
self.patch_data = patch_data
68+
patch = build_patch_for_revision(raw_diff, **self.patch_data)
69+
self.patch_bytes = patch.encode("utf-8")
70+
71+
3572
class DiffWarning(Base):
3673
"""Represents a warning message associated with a particular diff and revision."""
3774

landoapi/patches.py

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)