Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle no runs with no error in comment_actions #16

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}