Skip to content

Commit

Permalink
Merge pull request #16 from zapier/fix-nil-ref
Browse files Browse the repository at this point in the history
Handle no runs with no error in comment_actions
  • Loading branch information
davidwin93 authored Jan 10, 2023
2 parents 2071ecc + 973de35 commit d0ba5cc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/gitlab_hooks/comment_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ func (w *GitlabEventWorker) processNoteEvent(event vcs.MRCommentEvent) (projectN
return proj, nil
}
executedWorkspaces, tfError := trigger.TriggerTFCEvents()
if tfError == nil && len(executedWorkspaces.Errored) > 0 {
for _, failedWS := range executedWorkspaces.Errored {
w.postMessageToMergeRequest(event, fmt.Sprintf(":no_entry: %s could not be run because: %s", failedWS.Name, failedWS.Error))
if tfError == nil && executedWorkspaces != nil {
if len(executedWorkspaces.Errored) > 0 {
for _, failedWS := range executedWorkspaces.Errored {
w.postMessageToMergeRequest(event, fmt.Sprintf(":no_entry: %s could not be run because: %s", failedWS.Name, failedWS.Error))
}
return proj, nil
}
return proj, nil
}
return proj, tfError

Expand Down
56 changes: 56 additions & 0 deletions pkg/gitlab_hooks/comment_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,59 @@ func TestProcessNoteEventPlanFailedMultipleWorkspaces(t *testing.T) {
t.Fatal("expected a project name to be returned")
}
}

func TestProcessNoteEventNoErrorNoRuns(t *testing.T) {
os.Setenv(allow_list.GitlabProjectAllowListEnv, "zapier/")
defer os.Unsetenv(allow_list.GitlabProjectAllowListEnv)
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockGitClient := mocks.NewMockGitClient(mockCtrl)

mockApiClient := mocks.NewMockApiClient(mockCtrl)
mockStreamClient := mocks.NewMockStreamClient(mockCtrl)
mockProject := mocks.NewMockProject(mockCtrl)
mockProject.EXPECT().GetPathWithNamespace().Return("zapier/service-tf-buddy")

mockLastCommit := mocks.NewMockCommit(mockCtrl)
mockLastCommit.EXPECT().GetSHA().Return("abvc12345")

mockAttributes := mocks.NewMockMRAttributes(mockCtrl)

mockAttributes.EXPECT().GetNote().Return("tfc plan -w service-tf-buddy")
mockAttributes.EXPECT().GetType().Return("SomeNote")

mockMREvent := mocks.NewMockMRCommentEvent(mockCtrl)
mockMREvent.EXPECT().GetProject().Return(mockProject)
mockMREvent.EXPECT().GetAttributes().Return(mockAttributes).Times(2)
mockMREvent.EXPECT().GetLastCommit().Return(mockLastCommit)

mockSimpleMR := mocks.NewMockMR(mockCtrl)
mockSimpleMR.EXPECT().GetSourceBranch().Return("DTA-2009")

mockSimpleMR.EXPECT().GetInternalID().Return(101)
mockMREvent.EXPECT().GetMR().Return(mockSimpleMR).Times(2)

mockTFCTrigger := mocks.NewMockTrigger(mockCtrl)
mockTFCConfig := mocks.NewMockTriggerConfig(mockCtrl)
mockTFCConfig.EXPECT().SetAction(tfc_trigger.PlanAction)
mockTFCConfig.EXPECT().SetWorkspace("service-tf-buddy")
mockTFCTrigger.EXPECT().GetConfig().Return(mockTFCConfig).Times(2)
mockTFCTrigger.EXPECT().TriggerTFCEvents().Return(nil, nil)

client := &GitlabEventWorker{
gl: mockGitClient,
tfc: mockApiClient,
runstream: mockStreamClient,
triggerCreation: func(gl vcs.GitClient, tfc tfc_api.ApiClient, runstream runstream.StreamClient, cfg tfc_trigger.TriggerConfig) tfc_trigger.Trigger {
return mockTFCTrigger
},
}

proj, err := client.processNoteEvent(mockMREvent)
if err != nil {
t.Fatal(err)
}
if proj != "zapier/service-tf-buddy" {
t.Fatal("expected a project name to be returned")
}
}

0 comments on commit d0ba5cc

Please sign in to comment.