( runtime: PRRuntime, ref: GhPRRef, metadata: PRMetadata, )
| 722 | * Down: walk from currentPR.headBranch → leaf PRs (descendants) |
| 723 | */ |
| 724 | export async function fetchGhPRStack( |
| 725 | runtime: PRRuntime, |
| 726 | ref: GhPRRef, |
| 727 | metadata: PRMetadata, |
| 728 | ): Promise<PRStackTree | null> { |
| 729 | if (metadata.platform !== "github") return null; |
| 730 | const defaultBranch = metadata.defaultBranch; |
| 731 | if (!defaultBranch) return null; |
| 732 | |
| 733 | const currentNode: PRStackNode = { |
| 734 | branch: metadata.headBranch, |
| 735 | number: metadata.number, |
| 736 | title: metadata.title, |
| 737 | url: metadata.url, |
| 738 | isCurrent: true, |
| 739 | isDefaultBranch: false, |
| 740 | }; |
| 741 | |
| 742 | // Walk up: find the PR whose headRefName === baseBranch, repeat |
| 743 | const ancestors: PRStackNode[] = []; |
| 744 | let nextHead = metadata.baseBranch; |
| 745 | const maxDepth = 10; |
| 746 | |
| 747 | for (let i = 0; i < maxDepth; i++) { |
| 748 | if (nextHead === defaultBranch) break; |
| 749 | |
| 750 | const prs = await queryPRsByRef(runtime, ref, "head", nextHead); |
| 751 | if (prs.length === 0) { |
| 752 | ancestors.push({ branch: nextHead, isCurrent: false, isDefaultBranch: false }); |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | const pr = prs[0]; |
| 757 | ancestors.push({ |
| 758 | branch: pr.headRefName, |
| 759 | number: pr.number, |
| 760 | title: pr.title, |
| 761 | url: pr.url, |
| 762 | isCurrent: false, |
| 763 | isDefaultBranch: false, |
| 764 | state: (pr.state === 'MERGED' ? 'merged' : pr.state === 'CLOSED' ? 'closed' : 'open') as PRStackNode['state'], |
| 765 | }); |
| 766 | nextHead = pr.baseRefName; |
| 767 | } |
| 768 | |
| 769 | // Walk down: find PRs whose baseRefName === current headBranch, repeat |
| 770 | const descendants: PRStackNode[] = []; |
| 771 | let nextBase = metadata.headBranch; |
| 772 | |
| 773 | for (let i = 0; i < maxDepth; i++) { |
| 774 | const prs = await queryPRsByRef(runtime, ref, "base", nextBase); |
| 775 | if (prs.length === 0) break; |
| 776 | |
| 777 | const pr = prs[0]; |
| 778 | descendants.push({ |
| 779 | branch: pr.headRefName, |
| 780 | number: pr.number, |
| 781 | title: pr.title, |
no test coverage detected