diff --git a/test/feature/PullRequest/pullRequestClosed.test.ts b/test/feature/PullRequest/pullRequestClosed.test.ts new file mode 100644 index 0000000..6565a11 --- /dev/null +++ b/test/feature/PullRequest/pullRequestClosed.test.ts @@ -0,0 +1,100 @@ +import {describe, test, afterEach, vi, beforeAll} from "vitest"; +import {CaseSlugs} from "../../../src/enums/CaseSlug"; +import {StrategyTestSetup} from "../strategyTestSetup"; +import PullRequestClosedStrategy from "../../../src/ActionHandler/PullRequest/PullRequestClosedStrategy"; + + + +import pullRequestClosedMergedPayload from '../../fixtures/events/pull_request/closed/merged/pull_request.closed.merged.json'; +import pullRequestClosedMergedNoCommentsPayload from '../../fixtures/events/pull_request/closed/merged/pull_request.closed.merged.no_comments.json'; +import pullRequestClosedNotMergedPayload from '../../fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.json'; +import pullRequestClosedNotMergedNoCommentsPayload from '../../fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.no_comments.json'; + +import pullRequestReviewsListMany from '../../fixtures/data/pull_request_reviews/pull_request_reviews.list.many.json'; +import pullRequestReviewsListFew from '../../fixtures/data/pull_request_reviews/pull_request_reviews.list.few.json'; + +import pullRequestListNotMerged from '../../fixtures/data/pulls/pulls.list.not-merged.json'; + + + +describe("Pull Request Opened Tests", () => { + const strategyTestSetup = new StrategyTestSetup(); + + beforeAll(() => { + strategyTestSetup.beforeAll(); + }); + + afterEach(() => { + strategyTestSetup.afterEach(); + }); + + describe.each([ + { + description: "Merged, many reviews", + previousPrs: [], + pullRequestReviews: pullRequestReviewsListMany, + expectedCaseSlug: CaseSlugs.PullRequest.Closed.MergedManyReviews, + payload:pullRequestClosedMergedPayload + }, + { + description: "Merged, few reviews", + previousPrs: [], + pullRequestReviews: pullRequestReviewsListFew, + expectedCaseSlug: CaseSlugs.PullRequest.Closed.MergedFewReviews, + payload:pullRequestClosedMergedPayload + }, + { + description: "Merged, no reviews", + previousPrs: [], + pullRequestReviews: [], + expectedCaseSlug: CaseSlugs.PullRequest.Closed.MergedNoReviews, + payload:pullRequestClosedMergedNoCommentsPayload + }, + { + description: "Not merged, many reviews", + previousPrs: [], + pullRequestReviews: pullRequestReviewsListMany, + expectedCaseSlug: CaseSlugs.PullRequest.Closed.NotMergedManyReviews, + payload:pullRequestClosedNotMergedPayload + }, + { + description: "Not merged, few reviews", + previousPrs: [], + pullRequestReviews: pullRequestReviewsListFew, + expectedCaseSlug: CaseSlugs.PullRequest.Closed.NotMergedFewReviews, + payload:pullRequestClosedNotMergedPayload + }, + { + description: "Not merged, no reviews", + previousPrs: [], + pullRequestReviews: [], + expectedCaseSlug: CaseSlugs.PullRequest.Closed.NotMergedNoReviews, + payload:pullRequestClosedNotMergedNoCommentsPayload + }, + { + description: "Not merged, previously closed", + previousPrs: pullRequestListNotMerged, + pullRequestReviews: pullRequestReviewsListFew, + expectedCaseSlug: CaseSlugs.PullRequest.Closed.NotMergedPreviouslyClosed, + payload:pullRequestClosedNotMergedPayload + }, + ])('$description', ({ previousPrs,pullRequestReviews, expectedCaseSlug ,payload}) => { + test('Creates a comment after receiving the event', async () => { + strategyTestSetup.actionStrategyHandleSpy = vi.spyOn(PullRequestClosedStrategy.prototype as any, 'handle'); + + strategyTestSetup.pullRequestIndexResponseMock.mockImplementation(() => previousPrs); + strategyTestSetup.pullRequestReviewIndexResponseMock.mockImplementation(() => pullRequestReviews); + + await strategyTestSetup.probot.receive({ + id: '123', + name: 'pull_request', + payload: payload as any, + }); + + + strategyTestSetup.performCommonAssertions(expectedCaseSlug); + + + }); + }); +}); \ No newline at end of file diff --git a/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.few.json b/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.few.json new file mode 100644 index 0000000..45b7c53 --- /dev/null +++ b/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.few.json @@ -0,0 +1,50 @@ +[ + { + "id": 1, + "user": { + "login": "reviewer1", + "id": 101, + "avatar_url": "https://avatars.githubusercontent.com/u/101?v=4", + "url": "https://api.github.com/users/reviewer1" + }, + "body": "Great work!", + "state": "APPROVED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-1", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-01T00:00:00Z", + "commit_id": "commit-sha-1", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-1" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 2, + "user": { + "login": "reviewer2", + "id": 102, + "avatar_url": "https://avatars.githubusercontent.com/u/102?v=4", + "url": "https://api.github.com/users/reviewer2" + }, + "body": "Needs some changes.", + "state": "CHANGES_REQUESTED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-2", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-02T00:00:00Z", + "commit_id": "commit-sha-2", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-2" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + } +] diff --git a/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.many.json b/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.many.json new file mode 100644 index 0000000..10ee0ed --- /dev/null +++ b/test/fixtures/data/pull_request_reviews/pull_request_reviews.list.many.json @@ -0,0 +1,146 @@ +[ + { + "id": 1, + "user": { + "login": "reviewer1", + "id": 101, + "avatar_url": "https://avatars.githubusercontent.com/u/101?v=4", + "url": "https://api.github.com/users/reviewer1" + }, + "body": "Great work!", + "state": "APPROVED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-1", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-01T00:00:00Z", + "commit_id": "commit-sha-1", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-1" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 2, + "user": { + "login": "reviewer2", + "id": 102, + "avatar_url": "https://avatars.githubusercontent.com/u/102?v=4", + "url": "https://api.github.com/users/reviewer2" + }, + "body": "Needs some changes.", + "state": "CHANGES_REQUESTED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-2", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-02T00:00:00Z", + "commit_id": "commit-sha-2", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-2" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 3, + "user": { + "login": "reviewer3", + "id": 103, + "avatar_url": "https://avatars.githubusercontent.com/u/103?v=4", + "url": "https://api.github.com/users/reviewer3" + }, + "body": "Looks good to me.", + "state": "APPROVED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-3", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-03T00:00:00Z", + "commit_id": "commit-sha-3", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-3" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 4, + "user": { + "login": "reviewer4", + "id": 104, + "avatar_url": "https://avatars.githubusercontent.com/u/104?v=4", + "url": "https://api.github.com/users/reviewer4" + }, + "body": "I have some suggestions.", + "state": "COMMENTED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-4", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-04T00:00:00Z", + "commit_id": "commit-sha-4", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-4" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 5, + "user": { + "login": "reviewer5", + "id": 105, + "avatar_url": "https://avatars.githubusercontent.com/u/105?v=4", + "url": "https://api.github.com/users/reviewer5" + }, + "body": "Looks good to me!", + "state": "APPROVED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-5", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-05T00:00:00Z", + "commit_id": "commit-sha-5", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-5" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + }, + { + "id": 6, + "user": { + "login": "reviewer6", + "id": 106, + "avatar_url": "https://avatars.githubusercontent.com/u/106?v=4", + "url": "https://api.github.com/users/reviewer6" + }, + "body": "Needs minor tweaks.", + "state": "COMMENTED", + "html_url": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-6", + "pull_request_url": "https://api.github.com/repos/test-owner/test-repo/pulls/1", + "submitted_at": "2023-01-06T00:00:00Z", + "commit_id": "commit-sha-6", + "author_association": "COLLABORATOR", + "_links": { + "html": { + "href": "https://github.com/test-owner/test-repo/pull/1#pullrequestreview-6" + }, + "pull_request": { + "href": "https://api.github.com/repos/test-owner/test-repo/pulls/1" + } + } + } +] diff --git a/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.json b/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.json new file mode 100644 index 0000000..fba2bed --- /dev/null +++ b/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.json @@ -0,0 +1,39 @@ +{ + "action": "closed", + "pull_request": { + "id": 1, + "number": 555444, + "state": "closed", + "title": "Test Pull Request", + "user": { + "login": "test-user" + }, + "head": { + "ref": "some-side-branch-123" + }, + "base": { + "ref": "master" + }, + "body": "This is a test pull request", + "created_at": "2024-01-01T00:00:00Z", + "updated_at": "2024-01-01T00:00:00Z", + "merged_at": "2024-01-01T00:00:00Z", + "closed_at": "2024-01-01T00:00:00Z", + "merged": true, + "review_comments": 5 + }, + "repository": { + "name": "test-repo", + "owner": { + "login": "test-owner" + } + }, + "sender": { + "login": "test-user", + "type": "User" + }, + "installation": { + "id": 1 + }, + "isBot": false +} \ No newline at end of file diff --git a/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.no_comments.json b/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.no_comments.json new file mode 100644 index 0000000..5ecfd2b --- /dev/null +++ b/test/fixtures/events/pull_request/closed/merged/pull_request.closed.merged.no_comments.json @@ -0,0 +1,39 @@ +{ + "action": "closed", + "pull_request": { + "id": 1, + "number": 555444, + "state": "closed", + "title": "Test Pull Request", + "user": { + "login": "test-user" + }, + "head": { + "ref": "some-side-branch-123" + }, + "base": { + "ref": "master" + }, + "body": "This is a test pull request", + "created_at": "2024-01-01T00:00:00Z", + "updated_at": "2024-01-01T00:00:00Z", + "merged_at": "2024-01-01T00:00:00Z", + "closed_at": "2024-01-01T00:00:00Z", + "merged": true, + "review_comments": 0 + }, + "repository": { + "name": "test-repo", + "owner": { + "login": "test-owner" + } + }, + "sender": { + "login": "test-user", + "type": "User" + }, + "installation": { + "id": 1 + }, + "isBot": false +} \ No newline at end of file diff --git a/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.json b/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.json new file mode 100644 index 0000000..6c88f58 --- /dev/null +++ b/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.json @@ -0,0 +1,39 @@ +{ + "action": "closed", + "pull_request": { + "id": 1, + "number": 555444, + "state": "closed", + "title": "Test Pull Request", + "user": { + "login": "test-user" + }, + "head": { + "ref": "some-side-branch-123" + }, + "base": { + "ref": "master" + }, + "body": "This is a test pull request", + "created_at": "2024-01-01T00:00:00Z", + "updated_at": "2024-01-01T00:00:00Z", + "merged_at": null, + "closed_at": "2024-01-01T00:00:00Z", + "merged": false, + "review_comments": 5 + }, + "repository": { + "name": "test-repo", + "owner": { + "login": "test-owner" + } + }, + "sender": { + "login": "test-user", + "type": "User" + }, + "installation": { + "id": 1 + }, + "isBot": false +} \ No newline at end of file diff --git a/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.no_comments.json b/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.no_comments.json new file mode 100644 index 0000000..87def26 --- /dev/null +++ b/test/fixtures/events/pull_request/closed/not_merged/pull_request.closed.not_merged.no_comments.json @@ -0,0 +1,39 @@ +{ + "action": "closed", + "pull_request": { + "id": 1, + "number": 555444, + "state": "closed", + "title": "Test Pull Request", + "user": { + "login": "test-user" + }, + "head": { + "ref": "some-side-branch-123" + }, + "base": { + "ref": "master" + }, + "body": "This is a test pull request", + "created_at": "2024-01-01T00:00:00Z", + "updated_at": "2024-01-01T00:00:00Z", + "merged_at": null, + "closed_at": "2024-01-01T00:00:00Z", + "merged": false, + "review_comments": 0 + }, + "repository": { + "name": "test-repo", + "owner": { + "login": "test-owner" + } + }, + "sender": { + "login": "test-user", + "type": "User" + }, + "installation": { + "id": 1 + }, + "isBot": false +} \ No newline at end of file diff --git a/test/fixtures/events/pull_request/pull_request.opened.json b/test/fixtures/events/pull_request/pull_request.opened.json index 1e532e1..a83daf7 100644 --- a/test/fixtures/events/pull_request/pull_request.opened.json +++ b/test/fixtures/events/pull_request/pull_request.opened.json @@ -2,7 +2,7 @@ "action": "opened", "pull_request": { "id": 1, - "number": 1, + "number": 555444, "state": "open", "title": "Test Pull Request", "user": {