( github: EventsGitHub, subscriber: EventsGitHub, repo: string, workDir: string, env: NodeJS.ProcessEnv, journal: Journal, journalPath: string, )
| 58 | * request gets none: a listener matching a seeded subscription would reuse it and succeed at the limit. |
| 59 | */ |
| 60 | export async function seedSubscriptions( |
| 61 | github: EventsGitHub, subscriber: EventsGitHub, repo: string, workDir: string, env: NodeJS.ProcessEnv, |
| 62 | journal: Journal, journalPath: string, |
| 63 | ): Promise<SeededSubscriptions> { |
| 64 | const seeded: SeededSubscriptions = { stale: [], active: [] }; |
| 65 | const pulls: Array<{ pr: PullRequest; stale: boolean; sets: EventType[][] }> = []; |
| 66 | const open = async (stale: boolean) => { |
| 67 | const branch = `${stale ? "abandoned" : "parallel"}-change-${pulls.length + 1}`; |
| 68 | await git(workDir, env, "switch", "-c", branch, "main"); |
| 69 | await writeFile(join(workDir, `${branch}.txt`), `${branch}\n`); |
| 70 | await git(workDir, env, "add", `${branch}.txt`); |
| 71 | await git(workDir, env, "commit", "-m", `Add ${branch}`); |
| 72 | await git(workDir, env, "push", "--set-upstream", "origin", branch); |
| 73 | await git(workDir, env, "switch", "main"); |
| 74 | const pr = await github.api<PullRequest>("POST", `/repos/${repo}/pulls`, { |
| 75 | title: stale ? `Abandoned change ${pulls.length + 1}` : `Parallel change ${pulls.length + 1}`, |
| 76 | head: branch, base: "main", body: "Another change in this repository.", |
| 77 | }); |
| 78 | pulls.push({ pr, stale, sets: [...eventTypeSets] }); |
| 79 | }; |
| 80 | for (let count = 0; ; count++) { |
| 81 | if (count >= maxSeeded) throw new Error(`The subscription limit was not reached after ${maxSeeded} subscriptions`); |
| 82 | let next = pulls.filter(pull => pull.sets.length).sort((a, b) => b.sets.length - a.sets.length)[0]; |
| 83 | if (!next) { |
| 84 | await open(true); |
| 85 | await open(false); |
| 86 | await open(true); |
| 87 | next = pulls.at(-3)!; |
| 88 | } |
| 89 | const subscription = await subscriber.subscribe(next.pr.html_url, next.sets.shift()!); |
| 90 | if (!subscription) break; |
| 91 | await journalResource(journal, journalPath, subscriptionResources, subscription.id); |
| 92 | (next.stale ? seeded.stale : seeded.active).push(subscription.id); |
| 93 | } |
| 94 | if (!seeded.stale.length || !seeded.active.length) { |
| 95 | throw new Error("The subscription limit is too low to seed both stale and active subscriptions"); |
| 96 | } |
| 97 | // Closing after subscribing keeps seeding independent of whether the API accepts closed pull requests. |
| 98 | for (const { pr } of pulls.filter(pull => pull.stale)) { |
| 99 | await github.api("PATCH", `/repos/${repo}/pulls/${pr.number}`, { state: "closed" }); |
| 100 | } |
| 101 | return seeded; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Deletes the trial's event subscriptions, then its sandbox repository. Subscriptions are created inside the agent's gh |
no test coverage detected