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

draft: Add support for branch in the channel name #1253

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions juju/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ async def resolve(
architecture=architecture,
risk=ch.risk,
track=ch.track,
branch=ch.branch,
base=base,
revision=revision,
)
Expand Down
21 changes: 17 additions & 4 deletions juju/origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ class Channel:

"""

def __init__(self, track=None, risk=None):
def __init__(self, track=None, risk=None, branch=None):
if not Risk.valid(risk):
raise JujuError(f"unexpected risk {risk}")

self.track = track or ""
self.risk = risk
self.branch = branch or ""

@staticmethod
def parse(s: str):
Expand All @@ -83,6 +84,7 @@ def parse(s: str):

risk = None
track = None
branch = None
if len(p) == 1:
if Risk.valid(p[0]):
risk = p[0]
Expand All @@ -92,30 +94,39 @@ def parse(s: str):
elif len(p) == 2:
track = p[0]
risk = p[1]
elif len(p) == 3:
track = p[0]
risk = p[1]
branch = p[2]
Comment on lines +97 to +100
Copy link
Contributor

Choose a reason for hiding this comment

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

Python built-in would be more elegant:

Suggested change
elif len(p) == 3:
track = p[0]
risk = p[1]
branch = p[2]
elif len(p) == 3:
track, risk, branch = p

Copy link
Contributor

Choose a reason for hiding this comment

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

Or, alternatively something like:

match parts:
    ...
    case [track, risk, branch]:
        pass

Ofc. refactoring is only possible if the function is covered by unit tests.

else:
raise JujuError(f"channel is malformed and has too many components {s}")

if risk is not None and not Risk.valid(risk):
raise JujuError(f"risk in channel {s} is not valid")
if track is not None and track == "":
raise JujuError(f"track in channel {s} is not valid")
if branch is not None and branch == "":
raise JujuError(f"branch in channel {s} is not valid")

return Channel(track, risk)
return Channel(track, risk, branch)

def normalize(self):
track = self.track if self.track != "" else ""
risk = self.risk if self.risk != "" else ""
return Channel(track, risk)
branch = self.branch if self.branch != "" else ""
return Channel(track, risk, branch)

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.track == other.track and self.risk == other.risk
return self.track == other.track and self.risk == other.risk and self.branch == other.branch
return False

def __str__(self):
path = self.risk
if self.track != "":
path = f"{self.track}/{path}"
if self.branch != "":
path = f"{path}/{self.branch}"
return path

def compute_base_channel(self, series):
Expand All @@ -124,6 +135,8 @@ def compute_base_channel(self, series):

"""
_ch = [self.risk]
if self.branch:
_ch.append(self.branch)
tr = utils.get_series_version(series)
if tr:
_ch = [tr, *_ch]
Expand Down