|
| 1 | +import { hasApprovedPullRuns } from "lib/bot/utils"; |
| 2 | +import nock from "nock"; |
| 3 | +import { Probot } from "probot"; |
| 4 | +import triggerInductorTestsBot from "../lib/bot/triggerInductorTestsBot"; |
| 5 | +import * as utils from "./utils"; |
| 6 | + |
| 7 | +nock.disableNetConnect(); |
| 8 | + |
| 9 | +describe("utils: hasApprovedPullRuns", () => { |
| 10 | + let probot: Probot; |
| 11 | + let octokit = utils.testOctokit(); |
| 12 | + let REPO = "pytorch/pytorch"; |
| 13 | + let SHA = "random sha"; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + probot = utils.testProbot(); |
| 17 | + probot.load(triggerInductorTestsBot); |
| 18 | + }); |
| 19 | + |
| 20 | + function mockRuns( |
| 21 | + runs: { conclusion: string; created_at?: string; updated_at?: string }[] |
| 22 | + ) { |
| 23 | + return nock("https://api.github.com") |
| 24 | + .get(`/repos/${REPO}/actions/runs?head_sha=${SHA}`) |
| 25 | + .reply(200, { |
| 26 | + workflow_runs: runs.map((run) => ({ |
| 27 | + event: "pull_request", |
| 28 | + ...run, |
| 29 | + })), |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + async function checkhasApprovedPullRunsReturns(value: boolean) { |
| 34 | + expect(await hasApprovedPullRuns(octokit, "pytorch", "pytorch", SHA)).toBe( |
| 35 | + value |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + nock.cleanAll(); |
| 41 | + jest.restoreAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("successful runs = good", async () => { |
| 45 | + mockRuns([{ conclusion: "success" }, { conclusion: "success" }]); |
| 46 | + await checkhasApprovedPullRunsReturns(true); |
| 47 | + }); |
| 48 | + |
| 49 | + test("at least 1 action required run = bad", async () => { |
| 50 | + mockRuns([{ conclusion: "action_required" }, { conclusion: "success" }]); |
| 51 | + await checkhasApprovedPullRunsReturns(false); |
| 52 | + }); |
| 53 | + |
| 54 | + test("no runs = bad", async () => { |
| 55 | + mockRuns([]); |
| 56 | + await checkhasApprovedPullRunsReturns(false); |
| 57 | + }); |
| 58 | + |
| 59 | + test("one startup failure = bad", async () => { |
| 60 | + mockRuns([ |
| 61 | + { |
| 62 | + conclusion: "failure", |
| 63 | + created_at: "time", |
| 64 | + updated_at: "time", |
| 65 | + }, |
| 66 | + ]); |
| 67 | + await checkhasApprovedPullRunsReturns(false); |
| 68 | + }); |
| 69 | + |
| 70 | + test("one startup failure and one action required = bad", async () => { |
| 71 | + mockRuns([ |
| 72 | + { |
| 73 | + conclusion: "failure", |
| 74 | + created_at: "time", |
| 75 | + updated_at: "time", |
| 76 | + }, |
| 77 | + { conclusion: "action_required" }, |
| 78 | + ]); |
| 79 | + await checkhasApprovedPullRunsReturns(false); |
| 80 | + }); |
| 81 | + |
| 82 | + test("one startup failure and one success = bad", async () => { |
| 83 | + mockRuns([ |
| 84 | + { |
| 85 | + conclusion: "failure", |
| 86 | + created_at: "time", |
| 87 | + updated_at: "time", |
| 88 | + }, |
| 89 | + { conclusion: "success" }, |
| 90 | + ]); |
| 91 | + await checkhasApprovedPullRunsReturns(false); |
| 92 | + }); |
| 93 | +}); |
0 commit comments