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

landing_worker: add and use metadata from Revision rather than HgPatchHelper (bug 1936171) #200

Merged
merged 5 commits into from
Feb 3, 2025

Conversation

shtrom
Copy link
Member

@shtrom shtrom commented Jan 23, 2025

The ultimate goal of this change is to deprecate the back-and-forth use of the HgPatchHelper. This first change allows to confine the parsing of an Hg patch to the Revision object, which allows exposing cleaned-up attribute to the rest of the system.

The ultimate goal of this change is to deprecate the back-and-forth use
of the HgPatchHelper. This first change allows to confine the parsing of
an Hg patch to the Revision object, which allows exposing cleaned-up
attribute to the rest of the system.
@shtrom shtrom requested review from zzzeid and cgsheeh January 23, 2025 06:23
@shtrom
Copy link
Member Author

shtrom commented Jan 23, 2025

@cgsheeh this could be useful for the automation API #194, so you don't have to mess around with the HgPatchHelper when creating the Revisions.

zzzeid
zzzeid previously requested changes Jan 30, 2025
Copy link
Collaborator

@zzzeid zzzeid left a comment

Choose a reason for hiding this comment

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

Couple of small comments, otherwise looks good!

Comment on lines 129 to 151
@property
def author_name(self) -> Optional[str]:
metadata = self._parse_metadata_from_patch()
return metadata.get("author_name")

@property
def author_email(self) -> Optional[str]:
metadata = self._parse_metadata_from_patch()
return metadata.get("author_email")

@property
def commit_message(self) -> Optional[str]:
metadata = self._parse_metadata_from_patch()
return metadata.get("commit_message")

@property
def timestamp(self) -> Optional[str]:
metadata = self._parse_metadata_from_patch()
if ts := metadata.get("timestamp"):
# Some codepaths (via Phabricator) have the timestamp set as an int.
# We make sure it's always a string.
return str(ts)
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Feels a bit repetitive to call _parse_metadata_from_patch in each method. Maybe all these values should be determined at the same time, whenever self.patch_data is set.

Copy link
Member Author

Choose a reason for hiding this comment

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

The metadata is only parsed one, and cached, as part of the _parse_metadata_from_patch() method https://github.com/mozilla-conduit/lando/pull/200/files#diff-6f00d8fe4977345e9360a87afcf4861324a3b6ff012d6a6a5716640b4b44c04dR153-R168

But calling it in every method makes sure we call it the first time we ever need any metadata.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The repetitiveness aspect (i.e., calling _parse_metadata_from_patch in every method) was more so what I meant, not so much efficiency. I think changing _parse_metadata_from_patch to a property (e.g., _parsed_metadata_from_patch) and then using it directly in these methods would alleviate this issue.

This would become:

    @property
    def timestamp(self) -> Optional[str]:
        if ts := self._parsed_metadata_from_patch.get("timestamp"):
            # Some codepaths (via Phabricator) have the timestamp set as an int.
            # We make sure it's always a string.
            return str(ts)
        return None

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! Yeah, that's better.

Comment on lines +184 to +199
@property
def diff(self) -> str:
"""Return the unified diff text without any metadata"""
# The HgPatchHelper currently returns leading newline, which we don't want to
# return here, so we strip it.
return self.patch_helper.get_diff().lstrip()

@property
def patch_helper(self) -> HgPatchHelper:
"""Create and cache an HgPatchHelper to parse the raw patch with Hg metadata."""
if not self._patch_helper:
self._patch_helper = HgPatchHelper(StringIO(self.patch))
if not self._patch_helper.diff_start_line:
raise NoDiffStartLine

return self._patch_helper
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two methods are hg-specific, wonder if we should be making them more scm-agnostic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the purpose of this change is a stop gap to start hiding the HgPatchHelper.

Historically, we transformed Phab revision into a Hg Patch, which we no longer need to do.

In this first cut, we hide the patch helper from everything else, and let the interface of the Revision be sufficient, and we can change the implementation later on.

@shtrom shtrom dismissed zzzeid’s stale review February 2, 2025 23:20

@Property added; I don't think support for a GitPatchHelper is needed for now.

@shtrom shtrom requested a review from zzzeid February 2, 2025 23:20
Copy link
Collaborator

@zzzeid zzzeid left a comment

Choose a reason for hiding this comment

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

nit: the first commit message line looks a bit too long and might get clipped when landing.

@shtrom shtrom merged commit e9407cf into main Feb 3, 2025
1 check passed
@shtrom shtrom deleted the bug1936171/no-HgPatchHelper-metadata branch February 3, 2025 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants