From 3c88f8c2539ba32d72961f61b27f0b63cada518b Mon Sep 17 00:00:00 2001 From: clee2000 <44682903+clee2000@users.noreply.github.com> Date: Wed, 6 Nov 2024 12:56:51 -0800 Subject: [PATCH] [bots][ez] Add testing for webhookToDynamo bot (#5869) Adds tests for webhookToDynamo bot so I can be certain that nothing changes when I upgrade octokit The tests do not have every webhook event, but most of that bot's code is the same for each event zipped file of expected results (zipped to reduce size and to reduce confusion when grepping for things) No functional changes, just saving the current outputs/results --- .../fixtures/webhookToDynamoExpected.json.gz | Bin 0 -> 5160 bytes .../test/fixtures/workflow_job.queued.json | 154 ++++++++++++++++++ torchci/test/wehookToDynamoBot.test.ts | 97 +++++++++++ 3 files changed, 251 insertions(+) create mode 100644 torchci/test/fixtures/webhookToDynamoExpected.json.gz create mode 100644 torchci/test/fixtures/workflow_job.queued.json create mode 100644 torchci/test/wehookToDynamoBot.test.ts diff --git a/torchci/test/fixtures/webhookToDynamoExpected.json.gz b/torchci/test/fixtures/webhookToDynamoExpected.json.gz new file mode 100644 index 0000000000000000000000000000000000000000..5a06087875839ece93575d9d1c1b973d59bef4d8 GIT binary patch literal 5160 zcmZ{mS2!CC!-nlud(RpTVzyLC?X5nTdQ^yCAQe1 zb`ie!`p&-VzyAC1eomi*n>(2d@4qk{+PL{`v^C*GgqPAXM7{0y#yfpp>z*JKhQDbG zWR{umkn6MnU#&T&)tyB25=WK+GqTiGPvIuVkRKI^(cPUsljtQ_Z8dk;$O%#9%pl0} z+Tj+Yd$Kd~&U~NNWxf+>ut7H8>($TYs~VPU#yZcF%n-CjlG*eVF#PBV$%=C@@qyT} zG@aG=`(&9u60d~Ml)U)@z?1b^zOS-`xC5Q!7* z_lJm#oW6-S%M;mY_m|jxnxGL5^TP1V+7+0Dh5hDOK9L5NVBS7P*Ww~z;Jvk4e?`M+!#+5!LRXwM`q>EC$gKPj^A}?(J=rlhmB;k#YbKb) zG0dN~=kB$DbE6p81Q{u(%H zEUC8@87<+xF+_DL#Y0FF#ux)t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;z2yWLx>~Oq zn%%Txsq-bXxW1L}$X)v~4VFGO z2mTeiKHHrU^cJPlqhpPD`!JVWWU@FuM@7I=Apg}|xdT}ZuwU;JC} z&)O27(NXW2CV$hpt!~f9E8Q*&najdhgO$7*#%iTiW+N+@PJGUu>B;`sSnb@F z#&zT79Akl65tcpFx&*tt&;M!^$|`Dni60!}p7@R5R5yJzah4*(ydhgToY=#%q1RbV z*H*Iy`&$Ck{eu0|ag{t__IC6-M?&20a8l(+D!I+zqzQ{9MVvq&*Wk3+LY9Awvw@@W zP?_qXNMDS{E3ET(Ge)p!(l&zlQ-KS?2c|cOo(D462zr*PLMux16D^ zt=axHFb4XtLEn4p?EK3g0WMBv6{T)#8 z#+?b5In5ix0M}e*R0}44s>|uJqndZ1!{eHf8@lN&C}+q1UE#bN4aUiME5#2+B=yHu#i(Qp!l9&M;5N@)vQg2@H_xk*#5}}o??|0np~eU)brNX{@ZQYQS8zlyEK zb12T&mi^S2`AE@zq9uel`|RkwGXCa)pbHUR97~f;tS@?uNZh4-5Jcj>}89u`$EpZ~r`0MBR zW5D%_y0G4ASWl6X*g8msn(!5nPIxim0Tn*eY4;|tBco+2f$|LqpCkU|51U+ z^@o*$+J1l!I#{&y^1I384BvPmGc=5QbvG|SH`u$4+yzx9>o2FlT)g4 Jp(Q-L{{WZ66Gs35 literal 0 HcmV?d00001 diff --git a/torchci/test/fixtures/workflow_job.queued.json b/torchci/test/fixtures/workflow_job.queued.json new file mode 100644 index 0000000000..e90c2073db --- /dev/null +++ b/torchci/test/fixtures/workflow_job.queued.json @@ -0,0 +1,154 @@ +{ + "name": "workflow_job", + "id": "a2ca94b0-9c65-11ef-9cd5-232c915246db", + "payload": { + "action": "queued", + "workflow_job": { + "id": 32611702112, + "run_id": 11708636007, + "workflow_name": "alert-15min", + "head_branch": "main", + "run_url": "https://api.github.com/repos/clee2000/random-testing/actions/runs/11708636007", + "run_attempt": 3, + "node_id": "CR_kwDOHXT3Fc8AAAAHl88VYA", + "head_sha": "5d7a569f08736d24ffba2cee3bb6510acce4be95", + "url": "https://api.github.com/repos/clee2000/random-testing/actions/jobs/32611702112", + "html_url": "https://github.com/clee2000/random-testing/actions/runs/11708636007/job/32611702112", + "status": "queued", + "conclusion": null, + "created_at": "2024-11-06T17:36:20Z", + "started_at": "2024-11-06T17:36:20Z", + "completed_at": null, + "name": "alert-15min", + "steps": [], + "check_run_url": "https://api.github.com/repos/clee2000/random-testing/check-runs/32611702112", + "labels": ["ubuntu-latest"], + "runner_id": null, + "runner_name": null, + "runner_group_id": null, + "runner_group_name": null + }, + "repository": { + "id": 494204693, + "node_id": "R_kgDOHXT3FQ", + "name": "random-testing", + "full_name": "clee2000/random-testing", + "private": true, + "owner": { + "login": "clee2000", + "id": 44682903, + "node_id": "MDQ6VXNlcjQ0NjgyOTAz", + "avatar_url": "https://avatars.githubusercontent.com/u/44682903?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/clee2000", + "html_url": "https://github.com/clee2000", + "followers_url": "https://api.github.com/users/clee2000/followers", + "following_url": "https://api.github.com/users/clee2000/following{/other_user}", + "gists_url": "https://api.github.com/users/clee2000/gists{/gist_id}", + "starred_url": "https://api.github.com/users/clee2000/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/clee2000/subscriptions", + "organizations_url": "https://api.github.com/users/clee2000/orgs", + "repos_url": "https://api.github.com/users/clee2000/repos", + "events_url": "https://api.github.com/users/clee2000/events{/privacy}", + "received_events_url": "https://api.github.com/users/clee2000/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + }, + "html_url": "https://github.com/clee2000/random-testing", + "description": "for random testing of anything, like gha", + "fork": false, + "url": "https://api.github.com/repos/clee2000/random-testing", + "forks_url": "https://api.github.com/repos/clee2000/random-testing/forks", + "keys_url": "https://api.github.com/repos/clee2000/random-testing/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/clee2000/random-testing/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/clee2000/random-testing/teams", + "hooks_url": "https://api.github.com/repos/clee2000/random-testing/hooks", + "issue_events_url": "https://api.github.com/repos/clee2000/random-testing/issues/events{/number}", + "events_url": "https://api.github.com/repos/clee2000/random-testing/events", + "assignees_url": "https://api.github.com/repos/clee2000/random-testing/assignees{/user}", + "branches_url": "https://api.github.com/repos/clee2000/random-testing/branches{/branch}", + "tags_url": "https://api.github.com/repos/clee2000/random-testing/tags", + "blobs_url": "https://api.github.com/repos/clee2000/random-testing/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/clee2000/random-testing/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/clee2000/random-testing/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/clee2000/random-testing/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/clee2000/random-testing/statuses/{sha}", + "languages_url": "https://api.github.com/repos/clee2000/random-testing/languages", + "stargazers_url": "https://api.github.com/repos/clee2000/random-testing/stargazers", + "contributors_url": "https://api.github.com/repos/clee2000/random-testing/contributors", + "subscribers_url": "https://api.github.com/repos/clee2000/random-testing/subscribers", + "subscription_url": "https://api.github.com/repos/clee2000/random-testing/subscription", + "commits_url": "https://api.github.com/repos/clee2000/random-testing/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/clee2000/random-testing/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/clee2000/random-testing/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/clee2000/random-testing/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/clee2000/random-testing/contents/{+path}", + "compare_url": "https://api.github.com/repos/clee2000/random-testing/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/clee2000/random-testing/merges", + "archive_url": "https://api.github.com/repos/clee2000/random-testing/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/clee2000/random-testing/downloads", + "issues_url": "https://api.github.com/repos/clee2000/random-testing/issues{/number}", + "pulls_url": "https://api.github.com/repos/clee2000/random-testing/pulls{/number}", + "milestones_url": "https://api.github.com/repos/clee2000/random-testing/milestones{/number}", + "notifications_url": "https://api.github.com/repos/clee2000/random-testing/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/clee2000/random-testing/labels{/name}", + "releases_url": "https://api.github.com/repos/clee2000/random-testing/releases{/id}", + "deployments_url": "https://api.github.com/repos/clee2000/random-testing/deployments", + "created_at": "2022-05-19T19:31:21Z", + "updated_at": "2024-09-24T00:05:14Z", + "pushed_at": "2024-09-24T00:05:11Z", + "git_url": "git://github.com/clee2000/random-testing.git", + "ssh_url": "git@github.com:clee2000/random-testing.git", + "clone_url": "https://github.com/clee2000/random-testing.git", + "svn_url": "https://github.com/clee2000/random-testing", + "homepage": null, + "size": 2561, + "stargazers_count": 0, + "watchers_count": 0, + "language": "Python", + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": false, + "has_pages": false, + "has_discussions": false, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 12, + "license": null, + "allow_forking": true, + "is_template": false, + "web_commit_signoff_required": false, + "topics": [], + "visibility": "private", + "forks": 0, + "open_issues": 12, + "watchers": 0, + "default_branch": "main" + }, + "sender": { + "login": "clee2000", + "id": 44682903, + "node_id": "MDQ6VXNlcjQ0NjgyOTAz", + "avatar_url": "https://avatars.githubusercontent.com/u/44682903?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/clee2000", + "html_url": "https://github.com/clee2000", + "followers_url": "https://api.github.com/users/clee2000/followers", + "following_url": "https://api.github.com/users/clee2000/following{/other_user}", + "gists_url": "https://api.github.com/users/clee2000/gists{/gist_id}", + "starred_url": "https://api.github.com/users/clee2000/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/clee2000/subscriptions", + "organizations_url": "https://api.github.com/users/clee2000/orgs", + "repos_url": "https://api.github.com/users/clee2000/repos", + "events_url": "https://api.github.com/users/clee2000/events{/privacy}", + "received_events_url": "https://api.github.com/users/clee2000/received_events", + "type": "User", + "user_view_type": "public", + "site_admin": false + } + } +} diff --git a/torchci/test/wehookToDynamoBot.test.ts b/torchci/test/wehookToDynamoBot.test.ts new file mode 100644 index 0000000000..372bc89a26 --- /dev/null +++ b/torchci/test/wehookToDynamoBot.test.ts @@ -0,0 +1,97 @@ +import nock from "nock"; +import { Probot } from "probot"; +import zlib from "zlib"; +import * as bot from "../lib/bot/webhookToDynamo"; +import * as dynamo from "../lib/dynamo"; +import { requireDeepCopy } from "./common"; +import * as utils from "./utils"; + +nock.disableNetConnect(); +jest.mock("uuid", () => ({ v4: () => "fake-uuid" })); + +describe("webhookToDynamo tests", () => { + let probot: Probot; + let expectedResults: { [key: string]: any }; + let returnedResults: { [key: string]: any }; + const resultsFile = "./test/fixtures/webhookToDynamoExpected.json.gz"; + + function saveResult(filename: string, body: any) { + returnedResults[filename] = body; + } + + beforeAll(() => { + expectedResults = JSON.parse( + zlib.gunzipSync(require("fs").readFileSync(resultsFile)).toString() + ); + returnedResults = {}; + }); + + beforeEach(() => { + probot = utils.testProbot(); + probot.load(bot.default); + nock.cleanAll(); + jest.clearAllMocks(); + }); + + afterAll(() => { + // To save the file, uncomment the following lines + // const zipped = zlib.gzipSync(JSON.stringify(returnedResults)); + // require("fs").writeFileSync(resultsFile, zipped); + }); + + /** + * + * @param filename + * @param name Name of the webhook event, ex "pull_request", "issue", + * "workflow_job". This is the X-GitHub-Event header. Files that take the + * form {"name": something, "id": something, "payload": something} should not + * include this. + */ + async function helper( + filename: string, + name: string | undefined = undefined + ) { + const event = requireDeepCopy(filename); + const mockedPut = jest.fn(); + jest.spyOn(dynamo, "getDynamoClient").mockReturnValue({ + put: mockedPut, + } as any); + if (name) { + await probot.receive({ name: name as any, payload: event, id: "2" }); + } else { + await probot.receive(event); + } + expect(mockedPut.mock.calls.length).toBe(1); + const body = mockedPut.mock.calls[0]; + saveResult(filename, body); + expect(body).toEqual(expectedResults[filename]); + } + + test("workflow_run.completed", async () => { + await helper("./fixtures/workflow_run.completed.json"); + }); + + test("workflow_job.queued", async () => { + await helper("./fixtures/workflow_job.queued.json"); + }); + + test("pull_request.labeled", async () => { + await helper("./fixtures/pull_request.labeled.json", "pull_request"); + }); + + test("issue.opened", async () => { + await helper("./fixtures/issues.opened.json", "issues"); + }); + + test("pull_request_review", async () => { + await helper("./fixtures/pull_request_review.json"); + }); + + test("issue_comment", async () => { + await helper("./fixtures/issue_comment.json"); + }); + + test("pull_request_comment", async () => { + await helper("./fixtures/pull_request_comment.json"); + }); +});