|
| 1 | +import logUploader, { |
| 2 | + isRepoEnabled, |
| 3 | + parseRepoAllowlist, |
| 4 | +} from "lib/bot/logUploader"; |
| 5 | +import * as lambda from "lib/lambda"; |
| 6 | +import nock from "nock"; |
| 7 | +import { Probot } from "probot"; |
| 8 | +import * as utils from "./utils"; |
| 9 | + |
| 10 | +nock.disableNetConnect(); |
| 11 | + |
| 12 | +function workflowJobPayload({ |
| 13 | + owner = "meta-pytorch", |
| 14 | + name = "torchcomms", |
| 15 | + action = "completed", |
| 16 | + id = 12345, |
| 17 | + conclusion = "failure" as string | null, |
| 18 | +} = {}) { |
| 19 | + return { |
| 20 | + action, |
| 21 | + workflow_job: { id, conclusion }, |
| 22 | + repository: { |
| 23 | + full_name: `${owner}/${name}`, |
| 24 | + name, |
| 25 | + owner: { login: owner }, |
| 26 | + }, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe("parseRepoAllowlist", () => { |
| 31 | + test("an unset value disables the handler", () => { |
| 32 | + expect(parseRepoAllowlist(undefined).size).toBe(0); |
| 33 | + expect(parseRepoAllowlist("").size).toBe(0); |
| 34 | + }); |
| 35 | + |
| 36 | + test("entries are trimmed and lowercased", () => { |
| 37 | + expect(parseRepoAllowlist(" Pytorch/PyTorch , meta-pytorch/* ")).toEqual( |
| 38 | + new Set(["pytorch/pytorch", "meta-pytorch/*"]) |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test("empty entries from a trailing comma are dropped", () => { |
| 43 | + expect(parseRepoAllowlist("pytorch/pytorch,,").size).toBe(1); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("isRepoEnabled", () => { |
| 48 | + test("matches an exact repo", () => { |
| 49 | + const allowlist = parseRepoAllowlist("pytorch/pytorch"); |
| 50 | + expect(isRepoEnabled(allowlist, "pytorch", "pytorch")).toBe(true); |
| 51 | + expect(isRepoEnabled(allowlist, "pytorch", "executorch")).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + test("matches a whole org via a wildcard", () => { |
| 55 | + const allowlist = parseRepoAllowlist("meta-pytorch/*"); |
| 56 | + expect(isRepoEnabled(allowlist, "meta-pytorch", "torchcomms")).toBe(true); |
| 57 | + expect(isRepoEnabled(allowlist, "meta-pytorch", "monarch")).toBe(true); |
| 58 | + expect(isRepoEnabled(allowlist, "pytorch", "pytorch")).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + test("an org wildcard does not leak into a similarly named org", () => { |
| 62 | + const allowlist = parseRepoAllowlist("pytorch/*"); |
| 63 | + expect(isRepoEnabled(allowlist, "meta-pytorch", "torchcomms")).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + test("comparison is case insensitive", () => { |
| 67 | + const allowlist = parseRepoAllowlist("PyTorch/PyTorch"); |
| 68 | + expect(isRepoEnabled(allowlist, "pytorch", "pytorch")).toBe(true); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("logUploader", () => { |
| 73 | + let probot: Probot; |
| 74 | + let invoke: jest.SpyInstance; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + probot = utils.testProbot(); |
| 78 | + probot.load(logUploader); |
| 79 | + invoke = jest.spyOn(lambda, "invokeLogUploader").mockResolvedValue(); |
| 80 | + process.env.LOG_UPLOADER_REPOS = "meta-pytorch/*,pytorch/pytorch"; |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + jest.restoreAllMocks(); |
| 85 | + nock.cleanAll(); |
| 86 | + delete process.env.LOG_UPLOADER_REPOS; |
| 87 | + }); |
| 88 | + |
| 89 | + async function receive(payload: any) { |
| 90 | + await probot.receive({ name: "workflow_job", payload, id: "1" } as any); |
| 91 | + } |
| 92 | + |
| 93 | + test("queues an upload for a completed job on an allowlisted repo", async () => { |
| 94 | + await receive(workflowJobPayload()); |
| 95 | + |
| 96 | + expect(invoke).toHaveBeenCalledWith({ |
| 97 | + repo: "meta-pytorch/torchcomms", |
| 98 | + job_id: 12345, |
| 99 | + conclusion: "failure", |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test("ignores anything but the completed action", async () => { |
| 104 | + // workflow_job also fires on queued and in_progress, where there is no log |
| 105 | + // to fetch yet. Uploading then would archive a truncated log. |
| 106 | + await receive(workflowJobPayload({ action: "queued" })); |
| 107 | + await receive(workflowJobPayload({ action: "in_progress" })); |
| 108 | + |
| 109 | + expect(invoke).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + test("skips a repo that is not on the allowlist", async () => { |
| 113 | + await receive(workflowJobPayload({ owner: "pytorch", name: "executorch" })); |
| 114 | + |
| 115 | + expect(invoke).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + test("skips an org the bot does not serve, even if allowlisted", async () => { |
| 119 | + // The allowlist narrows the org gate, it must not widen it. |
| 120 | + process.env.LOG_UPLOADER_REPOS = "someoneelse/*"; |
| 121 | + await receive(workflowJobPayload({ owner: "someoneelse", name: "repo" })); |
| 122 | + |
| 123 | + expect(invoke).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + test("does nothing when the allowlist is unset", async () => { |
| 127 | + delete process.env.LOG_UPLOADER_REPOS; |
| 128 | + await receive(workflowJobPayload()); |
| 129 | + |
| 130 | + expect(invoke).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + test("passes a null conclusion through rather than dropping the job", async () => { |
| 134 | + await receive(workflowJobPayload({ conclusion: null })); |
| 135 | + |
| 136 | + expect(invoke).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ conclusion: null }) |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + test.each([ |
| 142 | + ["the role cannot invoke the function", "AccessDeniedException"], |
| 143 | + ["credentials are missing entirely", "MissingAwsCredentialsError"], |
| 144 | + ["the Lambda API is unreachable", "TimeoutError"], |
| 145 | + ["Lambda throttles us", "TooManyRequestsException"], |
| 146 | + ])("a failed invoke does not fail the webhook when %s", async (_l, name) => { |
| 147 | + // Throwing here would make GitHub redeliver the event and re-run every other |
| 148 | + // handler, to retry something Dr.CI repairs on its own. |
| 149 | + const error = new Error(name); |
| 150 | + error.name = name; |
| 151 | + invoke.mockRejectedValue(error); |
| 152 | + |
| 153 | + await expect(receive(workflowJobPayload())).resolves.not.toThrow(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments