Skip to content

Commit 854d38d

Browse files
authored
Merge pull request #559 from GaetanLepage/eval
Refactor eval type choice logic
2 parents a78bda6 + fcc7b4e commit 854d38d

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

nixpkgs_review/cli/pr.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,10 @@ def parse_pr_numbers(number_args: list[str]) -> list[int]:
4040

4141
def pr_command(args: argparse.Namespace) -> str:
4242
prs: list[int] = parse_pr_numbers(args.number)
43-
match args.eval:
44-
case "ofborg":
45-
warn("Warning: `--eval=ofborg` is deprecated. Use `--eval=github` instead.")
46-
args.eval = "github"
47-
case "auto":
48-
if args.token:
49-
args.eval = "github"
50-
else:
51-
if not args.package:
52-
warn(
53-
"No GitHub token provided via GITHUB_TOKEN variable. Falling back to local evaluation.\n"
54-
"Tip: Install the `gh` command line tool and run `gh auth login` to authenticate."
55-
)
56-
args.eval = "local"
57-
case "github":
58-
if not args.token:
59-
warn("No GitHub token provided")
60-
sys.exit(1)
61-
use_github_eval = args.eval == "github"
43+
if args.eval == "ofborg":
44+
warn("Warning: `--eval=ofborg` is deprecated. Use `--eval=github` instead.")
45+
args.eval = "github"
46+
6247
checkout_option = (
6348
CheckoutOption.MERGE if args.checkout == "merge" else CheckoutOption.COMMIT
6449
)
@@ -116,7 +101,7 @@ def pr_command(args: argparse.Namespace) -> str:
116101
run=args.run,
117102
remote=args.remote,
118103
api_token=args.token,
119-
use_github_eval=use_github_eval,
104+
eval_type=args.eval,
120105
only_packages=set(args.package),
121106
additional_packages=set(args.additional_package),
122107
package_regexes=args.package_regex,

nixpkgs_review/review.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def __init__(
105105
build_graph: str,
106106
nixpkgs_config: Path,
107107
extra_nixpkgs_config: str,
108+
eval_type: str,
108109
api_token: str | None = None,
109-
use_github_eval: bool | None = True,
110110
only_packages: set[str] | None = None,
111111
additional_packages: set[str] | None = None,
112112
package_regexes: list[Pattern[str]] | None = None,
@@ -135,8 +135,9 @@ def __init__(
135135
self.no_shell = no_shell
136136
self.run = run
137137
self.remote = remote
138+
self.api_token = api_token
138139
self.github_client = GithubClient(api_token)
139-
self.use_github_eval = use_github_eval and not only_packages
140+
self.eval_type = eval_type
140141
self.checkout = checkout
141142
self.only_packages = only_packages
142143
self.additional_packages = additional_packages
@@ -164,6 +165,54 @@ def __init__(
164165
self.head_commit: str | None = None
165166
self.pr_object = pr_object
166167

168+
@property
169+
def _use_github_eval(self) -> bool:
170+
# If the user explicitly asks for local eval, just do it
171+
if self.eval_type == "local":
172+
return False
173+
174+
if self.only_packages:
175+
return False
176+
177+
# Handle the GH_TOKEN eventually not being provided
178+
if not self.api_token:
179+
warn("No GitHub token provided via GITHUB_TOKEN variable.")
180+
match self.eval_type:
181+
case "auto":
182+
warn(
183+
"Falling back to local evaluation.\n"
184+
"Tip: Install the `gh` command line tool and run `gh auth login` to authenticate."
185+
)
186+
return False
187+
case "github":
188+
sys.exit(1)
189+
190+
# GHA evaluation only evaluates nixpkgs with an empty config.
191+
# Its results might be incorrect when a non-default nixpkgs config is requested
192+
normalized_config = self.extra_nixpkgs_config.replace(" ", "")
193+
194+
if normalized_config == "{}":
195+
return True
196+
197+
warn("Non-default --extra-nixpkgs-config provided.")
198+
match self.eval_type:
199+
# By default, fall back to local evaluation
200+
case "auto":
201+
warn("Falling back to local evaluation")
202+
return False
203+
204+
# If the user explicitly requires GitHub eval, warn him, but proceed
205+
case "github":
206+
warn(
207+
"Forcing `github` evaluation -> Be warned that the evaluation results might not correspond to the provided nixpkgs config"
208+
)
209+
return True
210+
211+
# This should never happen
212+
case _:
213+
warn("Invalid eval_type")
214+
sys.exit(1)
215+
167216
def _process_aliases_for_systems(self, system: str) -> set[str]:
168217
match system:
169218
case "current":
@@ -422,16 +471,7 @@ def build_pr(self, pr_number: int) -> dict[System, list[Attr]]:
422471

423472
packages_per_system: dict[System, set[str]] | None = None
424473

425-
# GHA evaluation only evaluates nixpkgs with an empty config.
426-
# Its results should not be used when a non-default nixpkgs config is requested
427-
normalized_config = self.extra_nixpkgs_config.replace(" ", "")
428-
if self.use_github_eval and (normalized_config != "{}"):
429-
print(
430-
"Non-default --extra-nixpkgs-config provided. Falling back to local evaluation"
431-
)
432-
self.use_github_eval = False
433-
434-
if self.use_github_eval:
474+
if self._use_github_eval:
435475
assert all(system in PLATFORMS for system in self.systems)
436476
print("-> Fetching eval results from GitHub actions")
437477

@@ -911,6 +951,7 @@ def review_local_revision(
911951
run=args.run,
912952
remote=args.remote,
913953
only_packages=set(args.package),
954+
eval_type="local",
914955
additional_packages=set(args.additional_package),
915956
package_regexes=args.package_regex,
916957
skip_packages=set(args.skip_package),

0 commit comments

Comments
 (0)