def apply_prs()

in sync/landing.py [0:0]


    def apply_prs(self, prev_wpt_head: str, landable_commits: LandableCommits) -> None:
        """Main entry point to setting the commits for landing.

        For each upstream PR we want to create a separate commit in the
        gecko repository so that we are preserving a useful subset of the history.
        We also want to prevent divergence from upstream. So for each PR that landed
        upstream since our last sync, we take the following steps:
        1) Copy the state of upstream at the commit where the PR landed over to
           the gecko repo
        2) Reapply any commits that have been made to gecko on the integration branch
           but which are not yet landed upstream on top of the PR
        3) Apply any updated metadata from the downstream sync for the PR.
        """

        last_pr = None
        has_metadata = False
        have_prs = set()
        # Find the last gecko commit containing a PR
        if len(self.gecko_commits):
            head_commit = self.gecko_commits.head
            if TYPE_CHECKING:
                head = cast(GeckoCommit, head_commit)
            else:
                head = head_commit
            if head.is_landing:
                return

            for commit in list(self.gecko_commits):
                assert isinstance(commit, GeckoCommit)
                if commit.metadata.get("wpt-pr") is not None:
                    last_pr = int(commit.metadata["wpt-pr"])
                    has_metadata = commit.metadata.get("wpt-type") == "metadata"
                    have_prs.add(last_pr)

        pr_count_applied = len(have_prs)

        gecko_commits_landed = set()

        def update_gecko_landed(sync: DownstreamSync | UpstreamSync,
                                commits: list[WptCommit]) -> None:
            if isinstance(sync, upstream.UpstreamSync):
                for commit in commits:
                    gecko_commit = commit.metadata.get("gecko-commit")
                    if gecko_commit:
                        gecko_commits_landed.add(gecko_commit)

        unapplied_commits = []
        pr_count_upstream_empty = 0
        last_applied_seen = last_pr is None
        for i, (pr, sync, commits) in enumerate(landable_commits):
            if last_applied_seen:
                unapplied_commits.append((i, (pr, sync, commits, False)))
            else:
                prev_wpt_head = commits[-1].sha1
                try:
                    have_prs.remove(pr)
                except KeyError:
                    if isinstance(sync, downstream.DownstreamSync):
                        # This could be wrong if the changes already landed in gecko for some reason
                        raise AbortError("Expected an existing gecko commit for PR %s, "
                                         "but not found" % (pr,))
                    pr_count_upstream_empty += 1
                    continue
                if pr == last_pr:
                    last_applied_seen = True
                    if not has_metadata:
                        unapplied_commits.append((i, (pr, sync, commits, True)))
            update_gecko_landed(sync, commits)

        if have_prs:
            raise AbortError("Found unexpected gecko commit for PRs %s"
                             % (", ".join(str(item) for item in have_prs),))

        pr_count_unapplied = len(unapplied_commits)
        if pr_count_applied and not has_metadata:
            # If we have seen the commit but not the metadata it will both be in
            # have_prs and unapplied_commits, so avoid double counting
            pr_count_unapplied -= 1

        if (pr_count_applied + pr_count_upstream_empty + pr_count_unapplied !=
            len(landable_commits)):
            raise AbortError("PR counts don't match; got %d applied, %d unapplied %d upstream"
                             "(total %s), expected total %d" %
                             (pr_count_applied,
                              pr_count_unapplied,
                              pr_count_upstream_empty,
                              pr_count_unapplied + pr_count_applied + pr_count_upstream_empty,
                              len(landable_commits)))

        for i, (pr, sync, commits, meta_only) in unapplied_commits:
            logger.info("Applying PR %i of %i" % (i + 1, len(landable_commits)))
            update_gecko_landed(sync, commits)

            # If copy is set then we copy the commits and reapply in-progress upstream
            # syncs. This is currently always disabled, but the intent was to do this for
            # the first commit to ensure that the possible drift from upstream was limited.
            # However there were some difficulties reapplying all the right commits, so it's
            # disabled until this is worked out.
            # To reenable it change the below line to
            # copy = i == 0
            copy = False
            pr_commit: Commit | None = None
            if not meta_only:
                # If we haven't applied it before then create the initial commit
                pr_commit = self.add_pr(pr, sync, commits, prev_wpt_head=prev_wpt_head,
                                        copy=copy)
            prev_wpt_head = commits[-1].sha1
            if pr_commit:
                if copy:
                    self.reapply_local_commits(gecko_commits_landed)
            if isinstance(sync, downstream.DownstreamSync):
                self.add_metadata(sync)