Skip to content

Commit

Permalink
fix: reduce risk of double-read from GitLab when finding scm-engine c…
Browse files Browse the repository at this point in the history
…onfig file
  • Loading branch information
jippi committed Aug 30, 2024
1 parent b01bf9b commit fee1b08
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/gitlab_server_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"strconv"

"github.com/jippi/scm-engine/pkg/config"
"github.com/jippi/scm-engine/pkg/state"
slogctx "github.com/veqryn/slog-context"
)
Expand Down Expand Up @@ -112,14 +113,22 @@ func GitLabWebhookHandler(ctx context.Context, webhookSecret string) http.Handle
}

// Check if there exists scm-config file in the repo before moving forward
if _, err := client.MergeRequests().GetRemoteConfig(ctx, state.ConfigFilePath(ctx), ""); err != nil {
file, err := client.MergeRequests().GetRemoteConfig(ctx, state.ConfigFilePath(ctx), "")
if err != nil {
errHandler(ctx, w, http.StatusOK, err)

return
}

// Try to parse the config file
//
// In case of a parse error cfg remains "nil" and ProcessMR will try to read-and-parse it
// (but obviously also fail), but will surface the error within the GitLab External Pipeline (if enabled)
// which will surface the issue to the end-user directly
cfg, _ := config.ParseFile(file)

// Process the MR
if err := ProcessMR(ctx, client, nil, fullEventPayload); err != nil {
if err := ProcessMR(ctx, client, cfg, fullEventPayload); err != nil {
errHandler(ctx, w, http.StatusOK, err)

return
Expand Down

0 comments on commit fee1b08

Please sign in to comment.