diff --git a/torchci/test/fixtures/webhookToDynamoExpected.json.gz b/torchci/test/fixtures/webhookToDynamoExpected.json.gz new file mode 100644 index 0000000000..5a06087875 Binary files /dev/null and b/torchci/test/fixtures/webhookToDynamoExpected.json.gz differ 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"); + }); +});