From 4bb030ee65318b498d79a52d6210cc3bff29f20c Mon Sep 17 00:00:00 2001 From: Sam Kiessler Date: Thu, 22 Dec 2022 12:25:33 +0000 Subject: [PATCH 1/2] Extending setup method to support Azure DevOps --- .../events/events_controller_test.go | 253 ++++++++++-------- 1 file changed, 148 insertions(+), 105 deletions(-) diff --git a/server/controllers/events/events_controller_test.go b/server/controllers/events/events_controller_test.go index 8bba02213b..4a848876bc 100644 --- a/server/controllers/events/events_controller_test.go +++ b/server/controllers/events/events_controller_test.go @@ -44,6 +44,7 @@ const githubHeader = "X-Github-Event" const gitlabHeader = "X-Gitlab-Event" const azuredevopsHeader = "Request-Id" +var user = []byte("user") var secret = []byte("secret") func AnyRepo() models.Repo { @@ -53,7 +54,7 @@ func AnyRepo() models.Repo { func TestPost_NotGithubOrGitlab(t *testing.T) { t.Log("when the request is not for gitlab or github a 400 is returned") - e, _, _, _, _, _, _, _ := setup(t) + e, _, _, _, _, _, _, _, _ := setup(t) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) e.Post(w, req) @@ -62,7 +63,7 @@ func TestPost_NotGithubOrGitlab(t *testing.T) { func TestPost_UnsupportedVCSGithub(t *testing.T) { t.Log("when the request is for an unsupported vcs a 400 is returned") - e, _, _, _, _, _, _, _ := setup(t) + e, _, _, _, _, _, _, _, _ := setup(t) e.SupportedVCSHosts = nil req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "value") @@ -73,7 +74,7 @@ func TestPost_UnsupportedVCSGithub(t *testing.T) { func TestPost_UnsupportedVCSGitlab(t *testing.T) { t.Log("when the request is for an unsupported vcs a 400 is returned") - e, _, _, _, _, _, _, _ := setup(t) + e, _, _, _, _, _, _, _, _ := setup(t) e.SupportedVCSHosts = nil req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") @@ -84,7 +85,7 @@ func TestPost_UnsupportedVCSGitlab(t *testing.T) { func TestPost_InvalidGithubSecret(t *testing.T) { t.Log("when the github payload can't be validated a 400 is returned") - e, v, _, _, _, _, _, _ := setup(t) + e, v, _, _, _, _, _, _, _ := setup(t) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "value") @@ -95,7 +96,7 @@ func TestPost_InvalidGithubSecret(t *testing.T) { func TestPost_InvalidGitlabSecret(t *testing.T) { t.Log("when the gitlab payload can't be validated a 400 is returned") - e, _, gl, _, _, _, _, _ := setup(t) + e, _, gl, _, _, _, _, _, _ := setup(t) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") @@ -106,7 +107,7 @@ func TestPost_InvalidGitlabSecret(t *testing.T) { func TestPost_UnsupportedGithubEvent(t *testing.T) { t.Log("when the event type is an unsupported github event we ignore it") - e, v, _, _, _, _, _, _ := setup(t) + e, v, _, _, _, _, _, _, _ := setup(t) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "value") @@ -117,7 +118,7 @@ func TestPost_UnsupportedGithubEvent(t *testing.T) { func TestPost_UnsupportedGitlabEvent(t *testing.T) { t.Log("when the event type is an unsupported gitlab event we ignore it") - e, _, gl, _, _, _, _, _ := setup(t) + e, _, gl, _, _, _, _, _, _ := setup(t) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") @@ -129,7 +130,7 @@ func TestPost_UnsupportedGitlabEvent(t *testing.T) { // Test that if the comment comes from a commit rather than a merge request, // we give an error and ignore it. func TestPost_GitlabCommentOnCommit(t *testing.T) { - e, _, gl, _, _, _, _, _ := setup(t) + e, _, gl, _, _, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) w := httptest.NewRecorder() req.Header.Set(gitlabHeader, "value") @@ -140,7 +141,7 @@ func TestPost_GitlabCommentOnCommit(t *testing.T) { func TestPost_GithubCommentNotCreated(t *testing.T) { t.Log("when the event is a github comment but it's not a created event we ignore it") - e, v, _, _, _, _, _, _ := setup(t) + e, v, _, _, _, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "issue_comment") // comment action is deleted, not created @@ -153,7 +154,7 @@ func TestPost_GithubCommentNotCreated(t *testing.T) { func TestPost_GithubInvalidComment(t *testing.T) { t.Log("when the event is a github comment without all expected data we return a 400") - e, v, _, p, _, _, _, _ := setup(t) + e, v, _, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "issue_comment") event := `{"action": "created"}` @@ -166,7 +167,7 @@ func TestPost_GithubInvalidComment(t *testing.T) { func TestPost_GitlabCommentInvalidCommand(t *testing.T) { t.Log("when the event is a gitlab comment with an invalid command we ignore it") - e, _, gl, _, _, _, _, cp := setup(t) + e, _, gl, _, _, _, _, _, cp := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil) @@ -178,7 +179,7 @@ func TestPost_GitlabCommentInvalidCommand(t *testing.T) { func TestPost_GithubCommentInvalidCommand(t *testing.T) { t.Log("when the event is a github comment with an invalid command we ignore it") - e, v, _, p, _, _, _, cp := setup(t) + e, v, _, _, p, _, _, _, cp := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "issue_comment") event := `{"action": "created"}` @@ -319,7 +320,7 @@ func TestPost_GithubCommentNotAllowlistedWithSilenceErrors(t *testing.T) { func TestPost_GitlabCommentResponse(t *testing.T) { // When the event is a gitlab comment that warrants a comment response we comment back. - e, _, gl, _, _, _, vcsClient, cp := setup(t) + e, _, gl, _, _, _, _, vcsClient, cp := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil) @@ -332,7 +333,7 @@ func TestPost_GitlabCommentResponse(t *testing.T) { func TestPost_GithubCommentResponse(t *testing.T) { t.Log("when the event is a github comment that warrants a comment response we comment back") - e, v, _, p, _, _, vcsClient, cp := setup(t) + e, v, _, _, p, _, _, vcsClient, cp := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "issue_comment") event := `{"action": "created"}` @@ -350,7 +351,7 @@ func TestPost_GithubCommentResponse(t *testing.T) { func TestPost_GitlabCommentSuccess(t *testing.T) { t.Log("when the event is a gitlab comment with a valid command we call the command handler") - e, _, gl, _, cr, _, _, _ := setup(t) + e, _, gl, _, _, cr, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil) @@ -363,7 +364,7 @@ func TestPost_GitlabCommentSuccess(t *testing.T) { func TestPost_GithubCommentSuccess(t *testing.T) { t.Log("when the event is a github comment with a valid command we call the command handler") - e, v, _, p, cr, _, _, cp := setup(t) + e, v, _, _, p, cr, _, _, cp := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "issue_comment") event := `{"action": "created"}` @@ -382,7 +383,7 @@ func TestPost_GithubCommentSuccess(t *testing.T) { func TestPost_GithubPullRequestInvalid(t *testing.T) { t.Log("when the event is a github pull request with invalid data we return a 400") - e, v, _, p, _, _, _, _ := setup(t) + e, v, _, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "pull_request") @@ -396,7 +397,7 @@ func TestPost_GithubPullRequestInvalid(t *testing.T) { func TestPost_GitlabMergeRequestInvalid(t *testing.T) { t.Log("when the event is a gitlab merge request with invalid data we return a 400") - e, _, gl, p, _, _, _, _ := setup(t) + e, _, gl, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeEvent{}, nil) @@ -410,7 +411,7 @@ func TestPost_GitlabMergeRequestInvalid(t *testing.T) { func TestPost_GithubPullRequestNotAllowlisted(t *testing.T) { t.Log("when the event is a github pull request to a non-allowlisted repo we return a 400") - e, v, _, _, _, _, _, _ := setup(t) + e, v, _, _, _, _, _, _, _ := setup(t) var err error e.RepoAllowlistChecker, err = events.NewRepoAllowlistChecker("github.com/nevermatch") Ok(t, err) @@ -426,7 +427,7 @@ func TestPost_GithubPullRequestNotAllowlisted(t *testing.T) { func TestPost_GitlabMergeRequestNotAllowlisted(t *testing.T) { t.Log("when the event is a gitlab merge request to a non-allowlisted repo we return a 400") - e, _, gl, p, _, _, _, _ := setup(t) + e, _, gl, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") @@ -445,7 +446,7 @@ func TestPost_GitlabMergeRequestNotAllowlisted(t *testing.T) { func TestPost_GithubPullRequestUnsupportedAction(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") - e, v, _, _, _, _, _, _ := setup(t) + e, v, _, _, _, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "pull_request") @@ -460,7 +461,7 @@ func TestPost_GithubPullRequestUnsupportedAction(t *testing.T) { func TestPost_GitlabMergeRequestUnsupportedAction(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a gitlab merge request to a non-allowlisted repo we return a 400") - e, _, gl, p, _, _, _, _ := setup(t) + e, _, gl, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") var event gitlab.MergeEvent @@ -476,37 +477,8 @@ func TestPost_GitlabMergeRequestUnsupportedAction(t *testing.T) { } func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) { - u := "user" - user := []byte(u) - t.Log("when the event is an azure devops pull request update that should not trigger workflow we ignore it") - RegisterMockTestingT(t) - v := mocks.NewMockAzureDevopsRequestValidator() - p := emocks.NewMockEventParsing() - cp := emocks.NewMockCommentParsing() - cr := emocks.NewMockCommandRunner() - c := emocks.NewMockPullCleaner() - vcsmock := vcsmocks.NewMockClient() - repoAllowlistChecker, err := events.NewRepoAllowlistChecker("*") - Ok(t, err) - logger := logging.NewNoopLogger(t) - scope, _, _ := metrics.NewLoggingScope(logger, "null") - e := events_controllers.VCSEventsController{ - TestingMode: true, - Logger: logger, - Scope: scope, - ApplyDisabled: false, - AzureDevopsWebhookBasicUser: user, - AzureDevopsWebhookBasicPassword: secret, - AzureDevopsRequestValidator: v, - Parser: p, - CommentParser: cp, - CommandRunner: cr, - PullCleaner: c, - SupportedVCSHosts: []models.VCSHostType{models.AzureDevops}, - RepoAllowlistChecker: repoAllowlistChecker, - VCSClient: vcsmock, - } + e, _, _, ado, _, _, _, _, _ := setup(t) event := `{ "subscriptionId": "11111111-1111-1111-1111-111111111111", @@ -547,7 +519,7 @@ func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) { payload := fmt.Sprintf(event, c.message) req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) req.Header.Set(azuredevopsHeader, "reqID") - When(v.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) + When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) w := httptest.NewRecorder() e.Parser = &events.EventParser{} e.Post(w, req) @@ -557,37 +529,8 @@ func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) { } func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) { - u := "user" - user := []byte(u) - t.Log("when the event is an azure devops pull request deleted comment event we ignore it") - RegisterMockTestingT(t) - v := mocks.NewMockAzureDevopsRequestValidator() - p := emocks.NewMockEventParsing() - cp := emocks.NewMockCommentParsing() - cr := emocks.NewMockCommandRunner() - c := emocks.NewMockPullCleaner() - vcsmock := vcsmocks.NewMockClient() - repoAllowlistChecker, err := events.NewRepoAllowlistChecker("*") - Ok(t, err) - logger := logging.NewNoopLogger(t) - scope, _, _ := metrics.NewLoggingScope(logger, "null") - e := events_controllers.VCSEventsController{ - TestingMode: true, - Logger: logger, - Scope: scope, - ApplyDisabled: false, - AzureDevopsWebhookBasicUser: user, - AzureDevopsWebhookBasicPassword: secret, - AzureDevopsRequestValidator: v, - Parser: p, - CommentParser: cp, - CommandRunner: cr, - PullCleaner: c, - SupportedVCSHosts: []models.VCSHostType{models.AzureDevops}, - RepoAllowlistChecker: repoAllowlistChecker, - VCSClient: vcsmock, - } + e, _, _, ado, _, _, _, _, _ := setup(t) payload := `{ "subscriptionId": "11111111-1111-1111-1111-111111111111", @@ -610,7 +553,7 @@ func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) { t.Run("Dev has deleted a pull request comment", func(t *testing.T) { req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) req.Header.Set(azuredevopsHeader, "reqID") - When(v.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) + When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) w := httptest.NewRecorder() e.Parser = &events.EventParser{} e.Post(w, req) @@ -618,11 +561,106 @@ func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) { }) } +func TestPost_AzureDevopsPullRequestCommentWebhookTestIgnoreEvent(t *testing.T) { + t.Log("when the event is an azure devops webhook test we ignore it") + e, _, _, ado, _, _, _, _, _ := setup(t) + + event := `{ + "subscriptionId": "11111111-1111-1111-1111-111111111111", + "notificationId": 1, + "id": "22222222-2222-2222-2222-222222222222", + "eventType": "%s", + "publisherId": "tfs", + "message": { + "text": "%s" + }, + "resource": { + "pullRequest": { + "repository":{ + "url": "https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079" + } + }, + "comment": { + "content": "This is my comment." + } + }}` + + cases := []struct { + eventType string + message string + }{ + { + "ms.vss-code.git-pullrequest-comment-event", + "Jamal Hartnett has edited a pull request comment", + }, + } + + for _, c := range cases { + t.Run(c.message, func(t *testing.T) { + payload := fmt.Sprintf(event, c.eventType, c.message) + req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) + req.Header.Set(azuredevopsHeader, "reqID") + When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) + w := httptest.NewRecorder() + e.Parser = &events.EventParser{} + e.Post(w, req) + ResponseContains(t, w, http.StatusOK, "Ignoring Azure DevOps Test Event with Repo URL") + }) + } +} + +func TestPost_AzureDevopsPullRequestWebhookTestIgnoreEvent(t *testing.T) { + t.Log("when the event is an azure devops webhook tests we ignore it") + e, _, _, ado, _, _, _, _, _ := setup(t) + + event := `{ + "subscriptionId": "11111111-1111-1111-1111-111111111111", + "notificationId": 1, + "id": "22222222-2222-2222-2222-222222222222", + "eventType": "%s", + "publisherId": "tfs", + "message": { + "text": "%s" + }, + "resource": { + "repository":{ + "url": "https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079" + } + }}` + + cases := []struct { + eventType string + message string + }{ + { + "git.pullrequest.created", + "Jamal Hartnett created a new pull request", + }, + { + "git.pullrequest.updated", + "Jamal Hartnett marked the pull request as completed", + }, + } + + for _, c := range cases { + t.Run(c.message, func(t *testing.T) { + payload := fmt.Sprintf(event, c.eventType, c.message) + req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) + req.Header.Set(azuredevopsHeader, "reqID") + When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) + w := httptest.NewRecorder() + e.Parser = &events.EventParser{} + e.Post(w, req) + ResponseContains(t, w, http.StatusOK, "Ignoring Azure DevOps Test Event with Repo URL") + }) + } +} + func TestPost_GithubPullRequestClosedErrCleaningPull(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a closed pull request and we have an error calling CleanUpPull we return a 503") RegisterMockTestingT(t) - e, v, _, p, _, c, _, _ := setup(t) + e, v, _, _, p, _, c, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "pull_request") @@ -640,7 +678,7 @@ func TestPost_GithubPullRequestClosedErrCleaningPull(t *testing.T) { func TestPost_GitlabMergeRequestClosedErrCleaningPull(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a closed gitlab merge request and an error occurs calling CleanUpPull we return a 500") - e, _, gl, p, _, c, _, _ := setup(t) + e, _, gl, _, p, _, c, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") var event gitlab.MergeEvent @@ -658,7 +696,7 @@ func TestPost_GitlabMergeRequestClosedErrCleaningPull(t *testing.T) { func TestPost_GithubClosedPullRequestSuccess(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a pull request and everything works we return a 200") - e, v, _, p, _, c, _, _ := setup(t) + e, v, _, _, p, _, c, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(githubHeader, "pull_request") @@ -676,7 +714,7 @@ func TestPost_GithubClosedPullRequestSuccess(t *testing.T) { func TestPost_GitlabMergeRequestSuccess(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a gitlab merge request and the cleanup works we return a 200") - e, _, gl, p, _, _, _, _ := setup(t) + e, _, gl, _, p, _, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) req.Header.Set(gitlabHeader, "value") When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeEvent{}, nil) @@ -799,7 +837,7 @@ func TestPost_PullOpenedOrUpdated(t *testing.T) { for _, c := range cases { t.Run(c.Description, func(t *testing.T) { - e, v, gl, p, cr, _, _, _ := setup(t) + e, v, gl, _, p, cr, _, _, _ := setup(t) req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil)) var pullRequest models.PullRequest var repo models.Repo @@ -830,10 +868,11 @@ func TestPost_PullOpenedOrUpdated(t *testing.T) { } } -func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGithubRequestValidator, *mocks.MockGitlabRequestParserValidator, *emocks.MockEventParsing, *emocks.MockCommandRunner, *emocks.MockPullCleaner, *vcsmocks.MockClient, *emocks.MockCommentParsing) { +func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGithubRequestValidator, *mocks.MockGitlabRequestParserValidator, *mocks.MockAzureDevopsRequestValidator, *emocks.MockEventParsing, *emocks.MockCommandRunner, *emocks.MockPullCleaner, *vcsmocks.MockClient, *emocks.MockCommentParsing) { RegisterMockTestingT(t) v := mocks.NewMockGithubRequestValidator() gl := mocks.NewMockGitlabRequestParserValidator() + ado := mocks.NewMockAzureDevopsRequestValidator() p := emocks.NewMockEventParsing() cp := emocks.NewMockCommentParsing() cr := emocks.NewMockCommandRunner() @@ -844,20 +883,24 @@ func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGit logger := logging.NewNoopLogger(t) scope, _, _ := metrics.NewLoggingScope(logger, "null") e := events_controllers.VCSEventsController{ - TestingMode: true, - Logger: logger, - Scope: scope, - GithubRequestValidator: v, - Parser: p, - CommentParser: cp, - CommandRunner: cr, - PullCleaner: c, - GithubWebhookSecret: secret, - SupportedVCSHosts: []models.VCSHostType{models.Github, models.Gitlab}, - GitlabWebhookSecret: secret, - GitlabRequestParserValidator: gl, - RepoAllowlistChecker: repoAllowlistChecker, - VCSClient: vcsmock, + TestingMode: true, + Logger: logger, + Scope: scope, + ApplyDisabled: false, + AzureDevopsWebhookBasicUser: user, + AzureDevopsWebhookBasicPassword: secret, + AzureDevopsRequestValidator: ado, + GithubRequestValidator: v, + Parser: p, + CommentParser: cp, + CommandRunner: cr, + PullCleaner: c, + GithubWebhookSecret: secret, + SupportedVCSHosts: []models.VCSHostType{models.Github, models.Gitlab, models.AzureDevops}, + GitlabWebhookSecret: secret, + GitlabRequestParserValidator: gl, + RepoAllowlistChecker: repoAllowlistChecker, + VCSClient: vcsmock, } - return e, v, gl, p, cr, c, vcsmock, cp + return e, v, gl, ado, p, cr, c, vcsmock, cp } From baf6e5469674229a6338e45b7536df808af0d8d0 Mon Sep 17 00:00:00 2001 From: Sam Kiessler Date: Thu, 22 Dec 2022 12:53:55 +0000 Subject: [PATCH 2/2] Removing tests from unmerged PRs --- .../events/events_controller_test.go | 95 ------------------- 1 file changed, 95 deletions(-) diff --git a/server/controllers/events/events_controller_test.go b/server/controllers/events/events_controller_test.go index 4a848876bc..a5984d8b9c 100644 --- a/server/controllers/events/events_controller_test.go +++ b/server/controllers/events/events_controller_test.go @@ -561,101 +561,6 @@ func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) { }) } -func TestPost_AzureDevopsPullRequestCommentWebhookTestIgnoreEvent(t *testing.T) { - t.Log("when the event is an azure devops webhook test we ignore it") - e, _, _, ado, _, _, _, _, _ := setup(t) - - event := `{ - "subscriptionId": "11111111-1111-1111-1111-111111111111", - "notificationId": 1, - "id": "22222222-2222-2222-2222-222222222222", - "eventType": "%s", - "publisherId": "tfs", - "message": { - "text": "%s" - }, - "resource": { - "pullRequest": { - "repository":{ - "url": "https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079" - } - }, - "comment": { - "content": "This is my comment." - } - }}` - - cases := []struct { - eventType string - message string - }{ - { - "ms.vss-code.git-pullrequest-comment-event", - "Jamal Hartnett has edited a pull request comment", - }, - } - - for _, c := range cases { - t.Run(c.message, func(t *testing.T) { - payload := fmt.Sprintf(event, c.eventType, c.message) - req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) - req.Header.Set(azuredevopsHeader, "reqID") - When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) - w := httptest.NewRecorder() - e.Parser = &events.EventParser{} - e.Post(w, req) - ResponseContains(t, w, http.StatusOK, "Ignoring Azure DevOps Test Event with Repo URL") - }) - } -} - -func TestPost_AzureDevopsPullRequestWebhookTestIgnoreEvent(t *testing.T) { - t.Log("when the event is an azure devops webhook tests we ignore it") - e, _, _, ado, _, _, _, _, _ := setup(t) - - event := `{ - "subscriptionId": "11111111-1111-1111-1111-111111111111", - "notificationId": 1, - "id": "22222222-2222-2222-2222-222222222222", - "eventType": "%s", - "publisherId": "tfs", - "message": { - "text": "%s" - }, - "resource": { - "repository":{ - "url": "https://fabrikam.visualstudio.com/DefaultCollection/_apis/git/repositories/4bc14d40-c903-45e2-872e-0462c7748079" - } - }}` - - cases := []struct { - eventType string - message string - }{ - { - "git.pullrequest.created", - "Jamal Hartnett created a new pull request", - }, - { - "git.pullrequest.updated", - "Jamal Hartnett marked the pull request as completed", - }, - } - - for _, c := range cases { - t.Run(c.message, func(t *testing.T) { - payload := fmt.Sprintf(event, c.eventType, c.message) - req, _ := http.NewRequest("GET", "", strings.NewReader(payload)) - req.Header.Set(azuredevopsHeader, "reqID") - When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil) - w := httptest.NewRecorder() - e.Parser = &events.EventParser{} - e.Post(w, req) - ResponseContains(t, w, http.StatusOK, "Ignoring Azure DevOps Test Event with Repo URL") - }) - } -} - func TestPost_GithubPullRequestClosedErrCleaningPull(t *testing.T) { t.Skip("relies too much on mocks, should use real event parser") t.Log("when the event is a closed pull request and we have an error calling CleanUpPull we return a 503")