From 32a5a32ad4681b8663daf2bbb7558a4a521f1f86 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 30 Aug 2024 17:10:46 +0200 Subject: [PATCH] fix: more resilient check for missing configs --- cmd/gitlab_server_handlers.go | 7 +++++++ pkg/scm/gitlab/client_merge_request.go | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/gitlab_server_handlers.go b/cmd/gitlab_server_handlers.go index 6dca1e8..e314e92 100644 --- a/cmd/gitlab_server_handlers.go +++ b/cmd/gitlab_server_handlers.go @@ -111,6 +111,13 @@ func GitLabWebhookHandler(ctx context.Context, webhookSecret string) http.Handle return } + // Check if there exists scm-config file in the repo before moving forward + if _, err := client.MergeRequests().GetRemoteConfig(ctx, state.ConfigFilePath(ctx), ""); err != nil { + errHandler(ctx, w, http.StatusOK, err) + + return + } + // Process the MR if err := ProcessMR(ctx, client, nil, fullEventPayload); err != nil { errHandler(ctx, w, http.StatusOK, err) diff --git a/pkg/scm/gitlab/client_merge_request.go b/pkg/scm/gitlab/client_merge_request.go index f7b9f36..397d8be 100644 --- a/pkg/scm/gitlab/client_merge_request.go +++ b/pkg/scm/gitlab/client_merge_request.go @@ -54,7 +54,13 @@ func (client *MergeRequestClient) GetRemoteConfig(ctx context.Context, filename, return nil, fmt.Errorf("could not parse project id: %w", err) } - file, _, err := client.client.wrapped.RepositoryFiles.GetRawFile(project, filename, &go_gitlab.GetRawFileOptions{Ref: scm.Ptr(ref)}) + var refPtr *string + + if len(ref) != 0 { + refPtr = scm.Ptr(ref) + } + + file, _, err := client.client.wrapped.RepositoryFiles.GetRawFile(project, filename, &go_gitlab.GetRawFileOptions{Ref: refPtr}) if err != nil { return nil, fmt.Errorf("failed to read remote raw file: %w", err) }