Browse Source

fix: error handling for gitlab variable fetch (#4177)

Gergely Brautigam 1 year ago
parent
commit
5350b03308
1 changed files with 10 additions and 15 deletions
  1. 10 15
      pkg/provider/gitlab/gitlab.go

+ 10 - 15
pkg/provider/gitlab/gitlab.go

@@ -225,21 +225,16 @@ func (g *gitlabBase) GetSecret(_ context.Context, ref esv1beta1.ExternalSecretDa
 	data, resp, err := g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts)
 	metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err)
 	if err != nil {
-		return nil, err
-	}
-
-	if resp == nil {
-		return nil, errors.New("gitlab response is nil")
-	}
-
-	if !isEmptyOrWildcard(g.store.Environment) && resp.StatusCode == http.StatusNotFound {
-		vopts.Filter.EnvironmentScope = "*"
-		data, resp, err = g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts)
-		metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err)
-	}
-
-	if resp.StatusCode >= 400 && resp.StatusCode != http.StatusNotFound {
-		return nil, fmt.Errorf("gitlab response status code was not OK: %d", resp.StatusCode)
+		if resp != nil && resp.StatusCode == http.StatusNotFound && !isEmptyOrWildcard(g.store.Environment) {
+			vopts.Filter.EnvironmentScope = "*"
+			data, resp, err = g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts)
+			metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err)
+			if err != nil || resp == nil {
+				return nil, fmt.Errorf("error getting variable %s from GitLab: %w", ref.Key, err)
+			}
+		} else {
+			return nil, err
+		}
 	}
 
 	err = g.ResolveGroupIds()