Skip to content

Commit

Permalink
Merge pull request #3128 from ytausch/reapply-git-6-again
Browse files Browse the repository at this point in the history
Reapply (Git-6) again
  • Loading branch information
beckermr authored Nov 11, 2024
2 parents 2e0a67e + 878dfd4 commit 6bcaee7
Show file tree
Hide file tree
Showing 17 changed files with 1,852 additions and 543 deletions.
129 changes: 61 additions & 68 deletions conda_forge_tick/auto_tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
import typing
from dataclasses import dataclass
from typing import Literal, MutableMapping, cast
from typing import Literal, cast
from urllib.error import URLError
from uuid import uuid4

Expand All @@ -31,15 +31,13 @@
from conda_forge_tick.feedstock_parser import BOOTSTRAP_MAPPINGS
from conda_forge_tick.git_utils import (
DryRunBackend,
DuplicatePullRequestError,
GitCli,
GitCliError,
GitPlatformBackend,
RepositoryNotFoundError,
comment_on_pr,
github3_client,
github_backend,
is_github_api_limit_reached,
push_repo,
)
from conda_forge_tick.lazy_json_backends import (
LazyJson,
Expand Down Expand Up @@ -72,6 +70,7 @@
)

from .migrators_types import MigrationUidTypedDict
from .models.pr_json import PullRequestData, PullRequestInfoSpecial, PullRequestState

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -423,13 +422,20 @@ def _check_and_process_solvability(
return False


def get_spoofed_closed_pr_info() -> PullRequestInfoSpecial:
return PullRequestInfoSpecial(
id=str(uuid4()),
merged_at="never issued",
state="closed",
)


def run_with_tmpdir(
context: FeedstockContext,
migrator: Migrator,
git_backend: GitPlatformBackend,
rerender: bool = True,
base_branch: str = "main",
dry_run: bool = False,
**kwargs: typing.Any,
) -> tuple[MigrationUidTypedDict, dict] | tuple[Literal[False], Literal[False]]:
"""
Expand All @@ -448,7 +454,6 @@ def run_with_tmpdir(
git_backend=git_backend,
rerender=rerender,
base_branch=base_branch,
dry_run=dry_run,
**kwargs,
)

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

# This is needed because we want to migrate to the new backend step-by-step
repo: github3.repos.Repository | None = github3_client().repository(
context.git_repo_owner, context.git_repo_name
)

assert repo is not None

feedstock_dir = str(context.local_clone_dir.resolve())

# TODO: Better annotation here
pr_json: typing.Union[MutableMapping, None, bool]
pr_data: PullRequestData | PullRequestInfoSpecial | None
"""
The PR data for the PR that was created. The contents of this variable will be stored in the bot's database.
None means: We don't update the PR data.
"""
if (
isinstance(migrator, MigrationYaml)
and not rerender_info.nontrivial_changes
and context.attrs["name"] != "conda-forge-pinning"
):
# spoof this so it looks like the package is done
pr_json = {
"state": "closed",
"merged_at": "never issued",
"id": str(uuid4()),
}
pr_data = get_spoofed_closed_pr_info()
else:
# push up
# push and PR
git_backend.push_to_repository(
owner=git_backend.user,
repo_name=context.git_repo_name,
git_dir=context.local_clone_dir,
branch=branch_name,
)
try:
pr_json = push_repo(
fctx=context,
feedstock_dir=feedstock_dir,
body=migration_run_data["pr_body"],
repo=repo,
title=migration_run_data["pr_title"],
branch=branch_name,
pr_data = git_backend.create_pull_request(
target_owner=context.git_repo_owner,
target_repo=context.git_repo_name,
base_branch=base_branch,
dry_run=dry_run,
head_branch=branch_name,
title=migration_run_data["pr_title"],
body=migration_run_data["pr_body"],
)
except DuplicatePullRequestError:
# This shouldn't happen too often anymore since we won't double PR
logger.warning(
f"Attempted to create a duplicate PR for merging {git_backend.user}:{branch_name} "
f"into {context.git_repo_owner}:{base_branch}. Ignoring."
)
# Don't update the PR data
pr_data = None

# This shouldn't happen too often any more since we won't double PR
except github3.GitHubError as e:
if e.msg != "Validation Failed":
raise
else:
print(f"Error during push {e}")
# If we just push to the existing PR then do nothing to the json
pr_json = False
ljpr = False

if pr_json and pr_json["state"] != "closed" and rerender_info.rerender_comment:
comment_on_pr(
pr_json,
rerender_info.rerender_comment,
repo,
if (
pr_data
and pr_data.state != PullRequestState.CLOSED
and rerender_info.rerender_comment
):
git_backend.comment_on_pull_request(
repo_owner=context.git_repo_owner,
repo_name=context.git_repo_name,
pr_number=pr_data.number,
comment=rerender_info.rerender_comment,
)

if pr_json:
ljpr = LazyJson(
os.path.join("pr_json", str(pr_json["id"]) + ".json"),
if pr_data:
pr_lazy_json = LazyJson(
os.path.join("pr_json", f"{pr_data.id}.json"),
)
with ljpr as __ljpr:
__ljpr.update(**pr_json)
with pr_lazy_json as __edit_pr_lazy_json:
__edit_pr_lazy_json.update(**pr_data.model_dump(mode="json"))
else:
ljpr = False
pr_lazy_json = False

# If we've gotten this far then the node is good
with context.attrs["pr_info"] as pri:
Expand All @@ -623,11 +624,10 @@ def run(
context.attrs, migrator_name, is_version=is_version_migration
)

logger.info("Removing feedstock dir")
return migration_run_data["migrate_return_value"], ljpr
return migration_run_data["migrate_return_value"], pr_lazy_json


def _compute_time_per_migrator(mctx, migrators):
def _compute_time_per_migrator(migrators):
# we weight each migrator by the number of available nodes to migrate
num_nodes = []
for migrator in tqdm.tqdm(migrators, ncols=80, desc="computing time per migrator"):
Expand Down Expand Up @@ -707,7 +707,6 @@ def _run_migrator_on_feedstock_branch(
migrator,
fctx: FeedstockContext,
git_backend: GitPlatformBackend,
dry_run,
mctx,
migrator_name,
good_prs,
Expand All @@ -723,9 +722,8 @@ def _run_migrator_on_feedstock_branch(
migrator=migrator,
git_backend=git_backend,
rerender=migrator.rerender,
hash_type=attrs.get("hash_type", "sha256"),
base_branch=base_branch,
dry_run=dry_run,
hash_type=attrs.get("hash_type", "sha256"),
)
finally:
fctx.attrs.pop("new_version", None)
Expand Down Expand Up @@ -758,6 +756,7 @@ def _run_migrator_on_feedstock_branch(
)

except (github3.GitHubError, github.GithubException) as e:
# TODO: pull this down into run() - also check the other exceptions
if hasattr(e, "msg") and e.msg == "Repository was archived so is read-only.":
attrs["archived"] = True
else:
Expand Down Expand Up @@ -886,9 +885,7 @@ def _is_migrator_done(_mg_start, good_prs, time_per, pr_limit):
return False


def _run_migrator(
migrator, mctx, temp, time_per, dry_run, git_backend: GitPlatformBackend
):
def _run_migrator(migrator, mctx, temp, time_per, git_backend: GitPlatformBackend):
_mg_start = time.time()

migrator_name = get_migrator_name(migrator)
Expand Down Expand Up @@ -1011,7 +1008,6 @@ def _run_migrator(
migrator=migrator,
fctx=fctx,
git_backend=git_backend,
dry_run=dry_run,
mctx=mctx,
migrator_name=migrator_name,
good_prs=good_prs,
Expand All @@ -1030,8 +1026,7 @@ def _run_migrator(
os.chdir(BOT_HOME_DIR)

# Write graph partially through
if not dry_run:
dump_graph(mctx.graph)
dump_graph(mctx.graph)

with filter_reprinted_lines("rm-tmp"):
for f in glob.glob("/tmp/*"):
Expand Down Expand Up @@ -1231,7 +1226,6 @@ def main(ctx: CliContext) -> None:
graph=gx,
smithy_version=smithy_version,
pinning_version=pinning_version,
dry_run=ctx.dry_run,
)
migrators = load_migrators()

Expand All @@ -1243,7 +1237,6 @@ def main(ctx: CliContext) -> None:
time_per_migrator,
tot_time_per_migrator,
) = _compute_time_per_migrator(
mctx,
migrators,
)
for i, migrator in enumerate(migrators):
Expand All @@ -1267,7 +1260,7 @@ def main(ctx: CliContext) -> None:

for mg_ind, migrator in enumerate(migrators):
good_prs = _run_migrator(
migrator, mctx, temp, time_per_migrator[mg_ind], ctx.dry_run, git_backend
migrator, mctx, temp, time_per_migrator[mg_ind], git_backend
)
if good_prs > 0:
pass
Expand Down
8 changes: 7 additions & 1 deletion conda_forge_tick/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MigratorSessionContext:
graph: DiGraph = None
smithy_version: str = ""
pinning_version: str = ""
dry_run: bool = True


@dataclass(frozen=True)
Expand Down Expand Up @@ -55,6 +54,13 @@ def git_repo_owner(self) -> str:
def git_repo_name(self) -> str:
return f"{self.feedstock_name}-feedstock"

@property
def git_http_ref(self) -> str:
"""
A link to the feedstock's GitHub repository.
"""
return f"https://github.com/{self.git_repo_owner}/{self.git_repo_name}"

@property
def automerge(self) -> bool | str:
"""
Expand Down
Loading

0 comments on commit 6bcaee7

Please sign in to comment.