def load_patches_stack()

in libmozdata/phabricator.py [0:0]


    def load_patches_stack(self, diff_id, diff=None):
        """
        Load a stack of patches with related commits, to reach a given Phabricator Diff
        without hitting a local mercurial repository
        If the full diff details are provided, they can be used directly in the stack
        """

        # Diff PHIDs from our patch to its base
        def add_patch(diff):
            # Build a nicer Diff instance with associated commit & patch
            assert isinstance(diff, dict)
            assert "id" in diff
            assert "phid" in diff
            assert "baseRevision" in diff
            patch = self.load_raw_diff(diff["id"])
            diffs = self.search_diffs(
                diff_phid=diff["phid"], attachments={"commits": True}
            )
            commits = diffs[0]["attachments"]["commits"].get("commits", [])
            logger.info("Adding patch #{} to stack".format(diff["id"]))
            return PhabricatorPatch(
                diff["id"], diff["phid"], patch, diff["baseRevision"], commits
            )

        # Load full diff when not provided by user
        if diff is None:
            diffs = self.search_diffs(diff_id=diff_id)
            if not diffs:
                raise Exception("Diff not found")
            diff = diffs[0]
        else:
            assert isinstance(diff, dict)
            assert "revisionPHID" in diff

        # Stack always has the top diff
        stack = [add_patch(diff)]

        parents = self.load_parents(diff["revisionPHID"])
        if parents:
            # Load all parent diffs
            for parent in parents:
                logger.info("Loading parent diff {}".format(parent))

                # Sort parent diffs by their id to load the most recent patch
                parent_diffs = sorted(
                    self.search_diffs(revision_phid=parent), key=lambda x: x["id"]
                )
                last_diff = parent_diffs[-1]

                # Add most recent patch to stack
                stack.insert(0, add_patch(last_diff))

        return stack