(sourceBranch?: string)
| 671 | }; |
| 672 | |
| 673 | async getPullRequests(sourceBranch?: string) { |
| 674 | const sourceQuery = sourceBranch |
| 675 | ? `source.branch.name = "${sourceBranch}"` |
| 676 | : `source.branch.name ~ "${CMS_BRANCH_PREFIX}/"`; |
| 677 | |
| 678 | const pages = await this.requestAllValues<BitBucketPullRequest>({ |
| 679 | url: `${this.repoURL}/pullrequests`, |
| 680 | params: { |
| 681 | pagelen: 50, |
| 682 | q: oneLine` |
| 683 | source.repository.full_name = "${this.repo}" |
| 684 | AND state = "${BitBucketPullRequestState.OPEN}" |
| 685 | AND destination.branch.name = "${this.branch}" |
| 686 | AND comment_count > 0 |
| 687 | AND ${sourceQuery} |
| 688 | `, |
| 689 | // The default order is by last update, so an edit made while the pages |
| 690 | // load could move a pull request onto a page that was already read. |
| 691 | // Sorting by id keeps each one in place and adds new ones at the end. |
| 692 | sort: 'id', |
| 693 | }, |
| 694 | }); |
| 695 | // The list can still change between page requests, so list each pull request |
| 696 | // once. Then put the most recently updated first, like Bitbucket's default |
| 697 | // order: the Workflow view shows drafts in this order, and |
| 698 | // `getBranchPullRequest` takes the first one. |
| 699 | const pullRequests = orderBy(uniqBy(pages, 'id'), 'updated_on', 'desc'); |
| 700 | |
| 701 | // Each pull request needs its own label request. Limit how many run at once, |
| 702 | // so many drafts don't send hundreds of requests at the same time. |
| 703 | const sem = semaphore(MAX_CONCURRENT_LABEL_REQUESTS); |
| 704 | const labels = await Promise.all( |
| 705 | pullRequests.map( |
| 706 | pr => |
| 707 | new Promise<string>((resolve, reject) => |
| 708 | sem.take(() => |
| 709 | this.getPullRequestLabel(pr.id) |
| 710 | .then(resolve, reject) |
| 711 | .finally(() => sem.leave()), |
| 712 | ), |
| 713 | ), |
| 714 | ), |
| 715 | ); |
| 716 | |
| 717 | return pullRequests.filter((_, index) => isCMSLabel(labels[index], this.cmsLabelPrefix)); |
| 718 | } |
| 719 | |
| 720 | async getBranchPullRequest(branch: string) { |
| 721 | const pullRequests = await this.getPullRequests(branch); |
no test coverage detected