Skip to content

Commit

Permalink
Adding a check for deleted comments in AzureDevOps event handler (run…
Browse files Browse the repository at this point in the history
…atlantis#2804)

* Adding a check for deleted comments in AzureDevOps event handler

* adding test for ignoring azure devops deleted comments

Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
SSKLCP and jamengual authored Dec 17, 2022
1 parent 8d74878 commit a96a88e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/controllers/events/events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,12 @@ func (e *VCSEventsController) HandleAzureDevopsPullRequestCommentedEvent(w http.
e.respond(w, logging.Debug, http.StatusOK, "Ignoring comment event since no comment is linked to payload; %s", azuredevopsReqID)
return
}

if *resource.Comment.IsDeleted {
e.respond(w, logging.Debug, http.StatusOK, "Ignoring comment event since it is linked to deleting a pull request comment; %s", azuredevopsReqID)
return
}

strippedComment := bluemonday.StrictPolicy().SanitizeBytes([]byte(*resource.Comment.Content))

if resource.PullRequest == nil {
Expand Down
62 changes: 62 additions & 0 deletions server/controllers/events/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,68 @@ 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,
}

payload := `{
"subscriptionId": "11111111-1111-1111-1111-111111111111",
"notificationId": 1,
"id": "22222222-2222-2222-2222-222222222222",
"eventType": "ms.vss-code.git-pullrequest-comment-event",
"publisherId": "tfs",
"message": {
"text": "Dev has deleted a pull request comment"
},
"resource": {
"comment": {
"id": 1,
"isDeleted": true,
"commentType": "text"
}
}
}`

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)
w := httptest.NewRecorder()
e.Parser = &events.EventParser{}
e.Post(w, req)
ResponseContains(t, w, http.StatusOK, "Ignoring comment event since it is linked to deleting a pull request comment")
})
}

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")
Expand Down

0 comments on commit a96a88e

Please sign in to comment.