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

Webhooks: refactor branch/tag building #12014

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Feb 20, 2025

  • Webhooks can trigger builds to tags, not just branches
  • Since webhooks already know the type of the version, we use it to filter by results.
  • verbose_name is the value we use to match branches or tags.

Extracted from #11942

@stsewd stsewd requested a review from a team as a code owner February 20, 2025 18:57
@stsewd stsewd requested a review from humitos February 20, 2025 18:57
Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

I left some questions since I have some doubts, but I think it's getting close.

Comment on lines +486 to +488
return ref.removeprefix(heads_prefix), BRANCH
if ref.startswith(tags_prefix):
return ref.removeprefix(tags_prefix), TAG
Copy link
Member

Choose a reason for hiding this comment

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

Adding parenthesis as it's easier to read.

Suggested change
return ref.removeprefix(heads_prefix), BRANCH
if ref.startswith(tags_prefix):
return ref.removeprefix(tags_prefix), TAG
return (ref.removeprefix(heads_prefix), BRANCH)
if ref.startswith(tags_prefix):
return (ref.removeprefix(tags_prefix), TAG)

Copy link
Member Author

Choose a reason for hiding this comment

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

Think black will just remove these?

@@ -15,7 +15,7 @@
log = structlog.get_logger(__name__)


def _build_version(project, slug, already_built=()):
def _build_version(project, version):
"""
Where we actually trigger builds for a project and slug.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Where we actually trigger builds for a project and slug.
Where we actually trigger builds for a project and version.

@@ -27,44 +27,43 @@ def _build_version(project, slug, already_built=()):
# Previously we were building the latest version (inactive or active)
# when building the default version,
# some users may have relied on this to update the version list #4450
version = project.versions.filter(active=True, slug=slug).first()
if version and slug not in already_built:
if version.active:
log.info(
"Building.",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Building.",
"Triggering build.",

@@ -15,7 +15,7 @@
log = structlog.get_logger(__name__)


def _build_version(project, slug, already_built=()):
def _build_version(project, version):
Copy link
Member

Choose a reason for hiding this comment

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

If we are passing the version object, project is not needed anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

We usually have the project already loaded outside the version, relying on the version only will result in another queryset, we pass the project and version in several parts of the codebase.

to_build.add(ret)
if version.slug in to_build:
continue
if _build_version(project, version):
Copy link
Member

Choose a reason for hiding this comment

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

I don't like too much when we use a function in a conditional that trigger things behind the scenes. I would split the boolean check and trigger the build inside the conditional instead. We mentioned something similar in other opportunity cc @ericholscher

I'd rewrite this as:

if should_build_version(project, version):
  trigger_build(project=project, version=version)
  to_build.add(version.slug)

This is a lot clearer, easier to read and simple to follow.

Copy link
Member Author

Choose a reason for hiding this comment

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

That would just mean to extract the whole _build_version function into the caller or have a one-liner helper function... Not really follow what's the problem here? The return value hints if the version was built or not, we already use this pattern in the codebase to hint if an operation was successful or not.

# so we don't have a way to match it by name.
# We should do another lookup to get the original stable version,
# or change our logic to store the tags name in the identifier of stable.
| Q(identifier=name, machine=True)
Copy link
Member

Choose a reason for hiding this comment

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

Why we were using remotes/origin/%s and origin/%s previously and we are not now? I'm confused with this method and I'm not sure the logic will be the same. Do we have enough tests for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

We used to save the branches with remote/origin as the identifier, and verbose_name without it, identifier and verbose name are always the same for branches now (this was changed a long time ago when we switched to gitpython...). Anyway, this is always using the verbose name, so even if there are ancient versions that include remote/origin in the identifier, this won't be a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

2 participants