Skip to content

Commit 99db910

Browse files
rjmsilveiraricardosilveiraolxjamengual
authored
fix: API Controller CommentCommand.Name defaults to Apply instead of Plan (#6090)
Signed-off-by: Ricardo Silveira <ricardo.silveira@olx.com> Co-authored-by: Ricardo Silveira <ricardo.silveira@olx.com> Co-authored-by: PePe Amengual <2208324+jamengual@users.noreply.github.com>
1 parent 8654305 commit 99db910

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

server/controllers/api_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ type APIRequest struct {
5656
}
5757
}
5858

59-
func (a *APIRequest) getCommands(ctx *command.Context, cmdBuilder func(*command.Context, *events.CommentCommand) ([]command.ProjectContext, error)) ([]command.ProjectContext, []*events.CommentCommand, error) {
59+
func (a *APIRequest) getCommands(ctx *command.Context, cmdName command.Name, cmdBuilder func(*command.Context, *events.CommentCommand) ([]command.ProjectContext, error)) ([]command.ProjectContext, []*events.CommentCommand, error) {
6060
cc := make([]*events.CommentCommand, 0)
6161

6262
for _, project := range a.Projects {
6363
cc = append(cc, &events.CommentCommand{
64+
Name: cmdName,
6465
ProjectName: project,
6566
})
6667
}
6768
for _, path := range a.Paths {
6869
cc = append(cc, &events.CommentCommand{
70+
Name: cmdName,
6971
RepoRelDir: strings.TrimRight(path.Directory, "/"),
7072
Workspace: path.Workspace,
7173
})
@@ -236,7 +238,7 @@ func (a *APIController) apiSetup(ctx *command.Context, cmdName command.Name) err
236238
}
237239

238240
func (a *APIController) apiPlan(request *APIRequest, ctx *command.Context) (*command.Result, error) {
239-
cmds, cc, err := request.getCommands(ctx, a.ProjectCommandBuilder.BuildPlanCommands)
241+
cmds, cc, err := request.getCommands(ctx, command.Plan, a.ProjectCommandBuilder.BuildPlanCommands)
240242
if err != nil {
241243
return nil, err
242244
}
@@ -284,7 +286,7 @@ func (a *APIController) apiPlan(request *APIRequest, ctx *command.Context) (*com
284286
}
285287

286288
func (a *APIController) apiApply(request *APIRequest, ctx *command.Context) (*command.Result, error) {
287-
cmds, cc, err := request.getCommands(ctx, a.ProjectCommandBuilder.BuildApplyCommands)
289+
cmds, cc, err := request.getCommands(ctx, command.Apply, a.ProjectCommandBuilder.BuildApplyCommands)
288290
if err != nil {
289291
return nil, err
290292
}

server/controllers/api_controller_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,79 @@ func TestAPIController_Apply(t *testing.T) {
203203
projectCommandRunner.VerifyWasCalled(Times(expectedCalls)).Apply(Any[command.ProjectContext]())
204204
}
205205

206+
// TestAPIController_Plan_PreWorkflowHooksReceiveCorrectCommand verifies that when
207+
// calling the Plan API endpoint, the pre-workflow hooks receive a CommentCommand
208+
// with Name set to command.Plan (not the zero value which would be command.Apply).
209+
func TestAPIController_Plan_PreWorkflowHooksReceiveCorrectCommand(t *testing.T) {
210+
ac, _, _ := setup(t)
211+
212+
// Get access to the pre-workflow hooks mock for verification
213+
preWorkflowHooksRunner := ac.PreWorkflowHooksCommandRunner.(*MockPreWorkflowHooksCommandRunner)
214+
215+
body, _ := json.Marshal(controllers.APIRequest{
216+
Repository: "Repo",
217+
Ref: "main",
218+
Type: "Gitlab",
219+
Projects: []string{"default"},
220+
})
221+
222+
req, _ := http.NewRequest("POST", "", bytes.NewBuffer(body))
223+
req.Header.Set(atlantisTokenHeader, atlantisToken)
224+
w := httptest.NewRecorder()
225+
ac.Plan(w, req)
226+
ResponseContains(t, w, http.StatusOK, "")
227+
228+
// Capture the CommentCommand passed to RunPreHooks and verify Name is Plan
229+
_, capturedCmd := preWorkflowHooksRunner.VerifyWasCalled(Times(1)).
230+
RunPreHooks(Any[*command.Context](), Any[*events.CommentCommand]()).
231+
GetCapturedArguments()
232+
233+
Assert(t, capturedCmd.Name == command.Plan,
234+
"expected CommentCommand.Name to be Plan (%d), got %s (%d)",
235+
command.Plan, capturedCmd.Name.String(), capturedCmd.Name)
236+
}
237+
238+
// TestAPIController_Apply_PreWorkflowHooksReceiveCorrectCommand verifies that when
239+
// calling the Apply API endpoint, the pre-workflow hooks receive a CommentCommand
240+
// with Name set to command.Apply for the apply phase (and command.Plan for the
241+
// plan phase that runs first).
242+
func TestAPIController_Apply_PreWorkflowHooksReceiveCorrectCommand(t *testing.T) {
243+
ac, _, _ := setup(t)
244+
245+
// Get access to the pre-workflow hooks mock for verification
246+
preWorkflowHooksRunner := ac.PreWorkflowHooksCommandRunner.(*MockPreWorkflowHooksCommandRunner)
247+
248+
body, _ := json.Marshal(controllers.APIRequest{
249+
Repository: "Repo",
250+
Ref: "main",
251+
Type: "Gitlab",
252+
Projects: []string{"default"},
253+
})
254+
255+
req, _ := http.NewRequest("POST", "", bytes.NewBuffer(body))
256+
req.Header.Set(atlantisTokenHeader, atlantisToken)
257+
w := httptest.NewRecorder()
258+
ac.Apply(w, req)
259+
ResponseContains(t, w, http.StatusOK, "")
260+
261+
// Apply calls apiPlan first (which runs pre-hooks with Plan), then apiApply (which runs pre-hooks with Apply)
262+
// So we expect 2 calls: first with Plan, second with Apply
263+
_, capturedCmds := preWorkflowHooksRunner.VerifyWasCalled(Times(2)).
264+
RunPreHooks(Any[*command.Context](), Any[*events.CommentCommand]()).
265+
GetAllCapturedArguments()
266+
267+
Assert(t, len(capturedCmds) == 2,
268+
"expected 2 pre-workflow hook calls, got %d", len(capturedCmds))
269+
270+
Assert(t, capturedCmds[0].Name == command.Plan,
271+
"expected first CommentCommand.Name to be Plan (%d), got %s (%d)",
272+
command.Plan, capturedCmds[0].Name.String(), capturedCmds[0].Name)
273+
274+
Assert(t, capturedCmds[1].Name == command.Apply,
275+
"expected second CommentCommand.Name to be Apply (%d), got %s (%d)",
276+
command.Apply, capturedCmds[1].Name.String(), capturedCmds[1].Name)
277+
}
278+
206279
func TestAPIController_ListLocks(t *testing.T) {
207280
ac, _, _ := setup(t)
208281
time := time.Now()

0 commit comments

Comments
 (0)