Verified Commit aadece3c authored by Radek Antoniuk's avatar Radek Antoniuk Committed by GitLab
Browse files

fix(feature_flags): omit ProjectFeatureFlagScope.ID when unset

Changelog: Improvements
parent ac110524
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ type ProjectFeatureFlag struct {
//
// GitLab API docs: https://docs.gitlab.com/api/feature_flags/
type ProjectFeatureFlagScope struct {
	ID               int64  `json:"id"`
	ID               int64  `json:"id,omitempty"`
	EnvironmentScope string `json:"environment_scope"`

	// Destroy removes the scope identified by ID when set to true. Used
+62 −0
Original line number Diff line number Diff line
@@ -339,6 +339,68 @@ func TestUpdateProjectFeatureFlag_ScopeDestroy(t *testing.T) {
	assert.Equal(t, expected, actual)
}

func TestUpdateProjectFeatureFlag_ScopeCreate(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/projects/1/feature_flags/testing", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPut)
		testBodyJSON(t, r, map[string]any{
			"strategies": []any{
				map[string]any{
					"id": float64(36),
					"scopes": []any{
						map[string]any{
							"environment_scope": "staging",
						},
					},
				},
			},
		})
		mustWriteHTTPResponse(t, w, "testdata/update_project_feature_flag_scope_create.json")
	})

	opt := &UpdateProjectFeatureFlagOptions{
		Strategies: &[]*FeatureFlagStrategyOptions{
			{
				ID: Ptr(int64(36)),
				Scopes: &[]*ProjectFeatureFlagScope{
					{
						EnvironmentScope: "staging",
					},
				},
			},
		},
	}

	actual, _, err := client.ProjectFeatureFlags.UpdateProjectFeatureFlag(1, "testing", opt)
	require.NoError(t, err)

	expected := &ProjectFeatureFlag{
		Name:      "awesome_feature",
		Active:    true,
		Version:   "new_version_flag",
		CreatedAt: Ptr(time.Date(2020, time.May, 13, 20, 10, 32, 0, time.UTC)),
		UpdatedAt: Ptr(time.Date(2020, time.May, 13, 20, 10, 32, 0, time.UTC)),
		Scopes:    []*ProjectFeatureFlagScope{},
		Strategies: []*ProjectFeatureFlagStrategy{
			{
				ID:         36,
				Name:       "default",
				Parameters: &ProjectFeatureFlagStrategyParameter{},
				Scopes: []*ProjectFeatureFlagScope{
					{
						ID:               41,
						EnvironmentScope: "staging",
					},
				},
			},
		},
	}

	assert.Equal(t, expected, actual)
}

func TestDeleteProjectFeatureFlag(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
+22 −0
Original line number Diff line number Diff line
{
  "name": "awesome_feature",
  "description": null,
  "active": true,
  "version": "new_version_flag",
  "created_at": "2020-05-13T20:10:32Z",
  "updated_at": "2020-05-13T20:10:32Z",
  "scopes": [],
  "strategies": [
    {
      "id": 36,
      "name": "default",
      "parameters": {},
      "scopes": [
        {
          "id": 41,
          "environment_scope": "staging"
        }
      ]
    }
  ]
}