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

Allow to configure user preferences with a configuration file #45

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
90 changes: 84 additions & 6 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,83 @@
pass


class UserPreferences:
SCHEMA = {
"upstream_remote": str,
"pr_remote": str,
"auto_pr": bool,
"push": bool,
}

def __init__(
self,
*,
pr_remote="origin",
upstream_remote=None,
push=True,
auto_pr=True,
):
self.pr_remote = pr_remote
self.upstream_remote = upstream_remote
self.push = push
self.auto_pr = auto_pr

Check warning on line 114 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L111-L114

Added lines #L111 - L114 were not covered by tests

@staticmethod
def get_preferences_path():
return os.path.join(

Check warning on line 118 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L118

Added line #L118 was not covered by tests
os.path.expanduser("~"), ".config", "cherry_picker", "preferences.toml"
)

@classmethod
def from_ctx(cls, ctx):
preferences_path = cls.get_preferences_path()
if not os.path.exists(preferences_path):
return cls()

Check warning on line 126 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L124-L126

Added lines #L124 - L126 were not covered by tests

applicable_preferences = cls._get_applicable_preferences(ctx, preferences_path)
kwargs = {}

Check warning on line 129 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L128-L129

Added lines #L128 - L129 were not covered by tests

for preferences in applicable_preferences:
for key, expected_type in cls.SCHEMA.items():
value = preferences.get(key)
if value is None:
continue
if type(value) is not expected_type:
raise ValueError(

Check warning on line 137 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L131-L137

Added lines #L131 - L137 were not covered by tests
f"User preference for {key} is of invalid type ({type(value)},"
f" {expected_type} was expected."
)
kwargs[key] = value

Check warning on line 141 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L141

Added line #L141 was not covered by tests

return cls(**kwargs)

Check warning on line 143 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L143

Added line #L143 was not covered by tests

@classmethod
def _get_applicable_preferences(cls, ctx, preferences_path):
with open(preferences_path, "rb") as fp:
data = tomllib.load(fp)
applicable_preferences = []

Check warning on line 149 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L147-L149

Added lines #L147 - L149 were not covered by tests

if "global" in data:
global_preferences = data["global"]
applicable_preferences.append(global_preferences)
cls._maybe_append_continue(ctx, applicable_preferences, global_preferences)

Check warning on line 154 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L151-L154

Added lines #L151 - L154 were not covered by tests

cwd = os.getcwd()
if "local" in data and cwd in data["local"]:
local_preferences = data["local"][cwd]
applicable_preferences.append(local_preferences[cwd])
cls._maybe_append_continue(ctx, applicable_preferences, local_preferences)

Check warning on line 160 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L156-L160

Added lines #L156 - L160 were not covered by tests

applicable_preferences.append(ctx.params)

Check warning on line 162 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L162

Added line #L162 was not covered by tests

return applicable_preferences

Check warning on line 164 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L164

Added line #L164 was not covered by tests

@staticmethod
def _maybe_append_continue(ctx, applicable_preferences, preferences):
if ctx.params["abort"] is False and "--continue" in preferences:
applicable_preferences.append(preferences["--continue"])

Check warning on line 169 in cherry_picker/cherry_picker.py

View check run for this annotation

Codecov / codecov/patch

cherry_picker/cherry_picker.py#L168-L169

Added lines #L168 - L169 were not covered by tests


class CherryPicker:
ALLOWED_STATES = WORKFLOW_STATES.BACKPORT_PAUSED, WORKFLOW_STATES.UNSET
"""The list of states expected at the start of the app."""
Expand Down Expand Up @@ -715,7 +792,7 @@
"pr_remote",
metavar="REMOTE",
help="git remote to use for PR branches",
default="origin",
default=None,
)
@click.option(
"--upstream-remote",
Expand Down Expand Up @@ -749,14 +826,14 @@
"--push/--no-push",
"push",
is_flag=True,
default=True,
default=None,
help="Changes won't be pushed to remote",
)
@click.option(
"--auto-pr/--no-auto-pr",
"auto_pr",
is_flag=True,
default=True,
default=None,
help=(
"If auto PR is enabled, cherry-picker will automatically open a PR"
" through API if GH_AUTH env var is set, or automatically open the PR"
Expand Down Expand Up @@ -795,16 +872,17 @@
click.echo("\U0001F40D \U0001F352 \u26CF")

chosen_config_path, config = load_config(config_path)
user_preferences = UserPreferences.from_ctx(ctx)

try:
cherry_picker = CherryPicker(
pr_remote,
user_preferences.pr_remote,
commit_sha1,
branches,
upstream_remote=upstream_remote,
dry_run=dry_run,
push=push,
auto_pr=auto_pr,
push=user_preferences.push,
auto_pr=user_preferences.auto_pr,
config=config,
chosen_config_path=chosen_config_path,
)
Expand Down