Skip to content

Commit d0adcb3

Browse files
authored
Merge pull request #3124 from ytausch/reapply-git-backend-6
Reapply (Git-6) Use new Git Backend in Entire auto_tick flow
2 parents c77cf4a + c54001a commit d0adcb3

17 files changed

+1835
-527
lines changed

conda_forge_tick/auto_tick.py

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import traceback
1010
import typing
1111
from dataclasses import dataclass
12-
from typing import Literal, MutableMapping, cast
12+
from typing import Literal, cast
1313
from urllib.error import URLError
1414
from uuid import uuid4
1515

@@ -31,15 +31,13 @@
3131
from conda_forge_tick.feedstock_parser import BOOTSTRAP_MAPPINGS
3232
from conda_forge_tick.git_utils import (
3333
DryRunBackend,
34+
DuplicatePullRequestError,
3435
GitCli,
3536
GitCliError,
3637
GitPlatformBackend,
3738
RepositoryNotFoundError,
38-
comment_on_pr,
39-
github3_client,
4039
github_backend,
4140
is_github_api_limit_reached,
42-
push_repo,
4341
)
4442
from conda_forge_tick.lazy_json_backends import (
4543
LazyJson,
@@ -72,6 +70,7 @@
7270
)
7371

7472
from .migrators_types import MigrationUidTypedDict
73+
from .models.pr_json import PullRequestData, PullRequestInfoSpecial, PullRequestState
7574

7675
logger = logging.getLogger(__name__)
7776

@@ -423,13 +422,20 @@ def _check_and_process_solvability(
423422
return False
424423

425424

425+
def get_spoofed_closed_pr_info() -> PullRequestInfoSpecial:
426+
return PullRequestInfoSpecial(
427+
id=str(uuid4()),
428+
merged_at="never issued",
429+
state="closed",
430+
)
431+
432+
426433
def run_with_tmpdir(
427434
context: FeedstockContext,
428435
migrator: Migrator,
429436
git_backend: GitPlatformBackend,
430437
rerender: bool = True,
431438
base_branch: str = "main",
432-
dry_run: bool = False,
433439
**kwargs: typing.Any,
434440
) -> tuple[MigrationUidTypedDict, dict] | tuple[Literal[False], Literal[False]]:
435441
"""
@@ -448,7 +454,6 @@ def run_with_tmpdir(
448454
git_backend=git_backend,
449455
rerender=rerender,
450456
base_branch=base_branch,
451-
dry_run=dry_run,
452457
**kwargs,
453458
)
454459

@@ -459,7 +464,6 @@ def run(
459464
git_backend: GitPlatformBackend,
460465
rerender: bool = True,
461466
base_branch: str = "main",
462-
dry_run: bool = False,
463467
**kwargs: typing.Any,
464468
) -> tuple[MigrationUidTypedDict, dict] | tuple[Literal[False], Literal[False]]:
465469
"""For a given feedstock and migration run the migration
@@ -554,67 +558,64 @@ def run(
554558
logger.warning("Skipping migration due to solvability check failure")
555559
return False, False
556560

557-
# This is needed because we want to migrate to the new backend step-by-step
558-
repo: github3.repos.Repository | None = github3_client().repository(
559-
context.git_repo_owner, context.git_repo_name
560-
)
561-
562-
assert repo is not None
563-
564-
feedstock_dir = str(context.local_clone_dir.resolve())
565-
566-
# TODO: Better annotation here
567-
pr_json: typing.Union[MutableMapping, None, bool]
561+
pr_data: PullRequestData | PullRequestInfoSpecial | None
562+
"""
563+
The PR data for the PR that was created. The contents of this variable will be stored in the bot's database.
564+
None means: We don't update the PR data.
565+
"""
568566
if (
569567
isinstance(migrator, MigrationYaml)
570568
and not rerender_info.nontrivial_changes
571569
and context.attrs["name"] != "conda-forge-pinning"
572570
):
573571
# spoof this so it looks like the package is done
574-
pr_json = {
575-
"state": "closed",
576-
"merged_at": "never issued",
577-
"id": str(uuid4()),
578-
}
572+
pr_data = get_spoofed_closed_pr_info()
579573
else:
580-
# push up
574+
# push and PR
575+
git_backend.push_to_repository(
576+
owner=git_backend.user,
577+
repo_name=context.git_repo_name,
578+
git_dir=context.local_clone_dir,
579+
branch=branch_name,
580+
)
581581
try:
582-
pr_json = push_repo(
583-
fctx=context,
584-
feedstock_dir=feedstock_dir,
585-
body=migration_run_data["pr_body"],
586-
repo=repo,
587-
title=migration_run_data["pr_title"],
588-
branch=branch_name,
582+
pr_data = git_backend.create_pull_request(
583+
target_owner=context.git_repo_owner,
584+
target_repo=context.git_repo_name,
589585
base_branch=base_branch,
590-
dry_run=dry_run,
586+
head_branch=branch_name,
587+
title=migration_run_data["pr_title"],
588+
body=migration_run_data["pr_body"],
589+
)
590+
except DuplicatePullRequestError:
591+
# This shouldn't happen too often anymore since we won't double PR
592+
logger.warning(
593+
f"Attempted to create a duplicate PR for merging {git_backend.user}:{branch_name} "
594+
f"into {context.git_repo_owner}:{base_branch}. Ignoring."
591595
)
596+
# Don't update the PR data
597+
pr_data = None
592598

593-
# This shouldn't happen too often any more since we won't double PR
594-
except github3.GitHubError as e:
595-
if e.msg != "Validation Failed":
596-
raise
597-
else:
598-
print(f"Error during push {e}")
599-
# If we just push to the existing PR then do nothing to the json
600-
pr_json = False
601-
ljpr = False
602-
603-
if pr_json and pr_json["state"] != "closed" and rerender_info.rerender_comment:
604-
comment_on_pr(
605-
pr_json,
606-
rerender_info.rerender_comment,
607-
repo,
599+
if (
600+
pr_data
601+
and pr_data.state != PullRequestState.CLOSED
602+
and rerender_info.rerender_comment
603+
):
604+
git_backend.comment_on_pull_request(
605+
repo_owner=context.git_repo_owner,
606+
repo_name=context.git_repo_name,
607+
pr_number=pr_data.number,
608+
comment=rerender_info.rerender_comment,
608609
)
609610

610-
if pr_json:
611-
ljpr = LazyJson(
612-
os.path.join("pr_json", str(pr_json["id"]) + ".json"),
611+
if pr_data:
612+
pr_lazy_json = LazyJson(
613+
os.path.join("pr_json", f"{pr_data.id}.json"),
613614
)
614-
with ljpr as __ljpr:
615-
__ljpr.update(**pr_json)
615+
with pr_lazy_json as __edit_pr_lazy_json:
616+
__edit_pr_lazy_json.update(**pr_data.model_dump(mode="json"))
616617
else:
617-
ljpr = False
618+
pr_lazy_json = False
618619

619620
# If we've gotten this far then the node is good
620621
with context.attrs["pr_info"] as pri:
@@ -623,8 +624,7 @@ def run(
623624
context.attrs, migrator_name, is_version=is_version_migration
624625
)
625626

626-
logger.info("Removing feedstock dir")
627-
return migration_run_data["migrate_return_value"], ljpr
627+
return migration_run_data["migrate_return_value"], pr_lazy_json
628628

629629

630630
def _compute_time_per_migrator(mctx, migrators):
@@ -707,7 +707,6 @@ def _run_migrator_on_feedstock_branch(
707707
migrator,
708708
fctx: FeedstockContext,
709709
git_backend: GitPlatformBackend,
710-
dry_run,
711710
mctx,
712711
migrator_name,
713712
good_prs,
@@ -723,9 +722,8 @@ def _run_migrator_on_feedstock_branch(
723722
migrator=migrator,
724723
git_backend=git_backend,
725724
rerender=migrator.rerender,
726-
hash_type=attrs.get("hash_type", "sha256"),
727725
base_branch=base_branch,
728-
dry_run=dry_run,
726+
hash_type=attrs.get("hash_type", "sha256"),
729727
)
730728
finally:
731729
fctx.attrs.pop("new_version", None)
@@ -758,6 +756,7 @@ def _run_migrator_on_feedstock_branch(
758756
)
759757

760758
except (github3.GitHubError, github.GithubException) as e:
759+
# TODO: pull this down into run() - also check the other exceptions
761760
if hasattr(e, "msg") and e.msg == "Repository was archived so is read-only.":
762761
attrs["archived"] = True
763762
else:
@@ -1011,7 +1010,6 @@ def _run_migrator(
10111010
migrator=migrator,
10121011
fctx=fctx,
10131012
git_backend=git_backend,
1014-
dry_run=dry_run,
10151013
mctx=mctx,
10161014
migrator_name=migrator_name,
10171015
good_prs=good_prs,

conda_forge_tick/contexts.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def git_repo_owner(self) -> str:
5555
def git_repo_name(self) -> str:
5656
return f"{self.feedstock_name}-feedstock"
5757

58+
@property
59+
def git_http_ref(self) -> str:
60+
"""
61+
A link to the feedstock's GitHub repository.
62+
"""
63+
return f"https://github.com/{self.git_repo_owner}/{self.git_repo_name}"
64+
5865
@property
5966
def automerge(self) -> bool | str:
6067
"""

0 commit comments

Comments
 (0)