Skip to content

Commit b6bf932

Browse files
committed
chore: iaw uses org setting insted of feature flag
1 parent 08863bf commit b6bf932

File tree

6 files changed

+88
-8
lines changed

6 files changed

+88
-8
lines changed

internal/api/api.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ApiClient interface {
2525
GetUserMe() (string, error)
2626
GetSelf() (contract.SelfResponse, error)
2727
GetSastSettings(orgId string) (*sast_contract.SastResponse, error)
28+
GetOrgSettings(orgId string) (*contract.OrgSettingsResponse, error)
2829
}
2930

3031
var _ ApiClient = (*snykApiClient)(nil)
@@ -247,6 +248,28 @@ func (a *snykApiClient) GetSastSettings(orgId string) (*sast_contract.SastRespon
247248
return &response, err
248249
}
249250

251+
func (a *snykApiClient) GetOrgSettings(orgId string) (*contract.OrgSettingsResponse, error) {
252+
endpoint := a.url + fmt.Sprintf("/v1/org/%s/settings", url.QueryEscape(orgId))
253+
res, err := a.client.Get(endpoint)
254+
if err != nil {
255+
return nil, fmt.Errorf("unable to retrieve org settings: %w", err)
256+
}
257+
//goland:noinspection GoUnhandledErrorResult
258+
defer res.Body.Close()
259+
260+
body, err := io.ReadAll(res.Body)
261+
if err != nil {
262+
return nil, fmt.Errorf("unable to retrieve org settings: %w", err)
263+
}
264+
265+
var response contract.OrgSettingsResponse
266+
if err = json.Unmarshal(body, &response); err != nil {
267+
return nil, fmt.Errorf("unable to retrieve org settings (status: %d): %w", res.StatusCode, err)
268+
}
269+
270+
return &response, err
271+
}
272+
250273
// clientGet performs an HTTP GET request to the Snyk API, handling query parameters,
251274
// API versioning, and basic error checking.
252275
//
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package contract
2+
3+
type OrgIgnoreSettings struct {
4+
ReasonRequired bool `json:"reasonRequired,omitempty"`
5+
AutoApproveIgnores bool `json:"autoApproveIgnores,omitempty"`
6+
ApprovalWorkflowEnabled bool `json:"approvalWorkflowEnabled,omitempty"`
7+
}
8+
9+
type OrgSettingsResponse struct {
10+
Ignores OrgIgnoreSettings `json:"ignores"`
11+
}

internal/mocks/api.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/local_workflows/ignore_workflow/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/uuid"
88

99
"github.com/snyk/error-catalog-golang-public/cli"
10+
"github.com/snyk/go-application-framework/internal/api"
1011
policyApi "github.com/snyk/go-application-framework/internal/api/policy/2024-10-15"
1112
"github.com/snyk/go-application-framework/pkg/configuration"
1213
"github.com/snyk/go-application-framework/pkg/local_workflows/local_models"
@@ -43,6 +44,28 @@ func addCreateIgnoreDefaultConfigurationValues(invocationCtx workflow.Invocation
4344
})
4445
}
4546

47+
func getOrgIgnoreApprovalEnabled(engine workflow.Engine) configuration.DefaultValueFunction {
48+
return func(existingValue interface{}) (interface{}, error) {
49+
if existingValue != nil {
50+
return existingValue, nil
51+
}
52+
53+
config := engine.GetConfiguration()
54+
org := config.GetString(configuration.ORGANIZATION)
55+
client := engine.GetNetworkAccess().GetHttpClient()
56+
url := config.GetString(configuration.API_URL)
57+
apiClient := api.NewApi(url, client)
58+
59+
settings, err := apiClient.GetOrgSettings(org)
60+
if err != nil {
61+
engine.GetLogger().Err(err).Msg("Failed to access settings.")
62+
return nil, err
63+
}
64+
65+
return settings.Ignores.ApprovalWorkflowEnabled, nil
66+
}
67+
}
68+
4669
func remoteRepoUrlDefaultFunc(existingValue interface{}, config configuration.Configuration) (interface{}, error) {
4770
if existingValue != nil && existingValue != "" {
4871
return existingValue, nil

pkg/local_workflows/ignore_workflow/ignore_workflow.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
"github.com/snyk/code-client-go/sarif"
1616
"github.com/snyk/error-catalog-golang-public/cli"
17+
"github.com/snyk/error-catalog-golang-public/snyk_errors"
1718

1819
policyApi "github.com/snyk/go-application-framework/internal/api/policy/2024-10-15"
1920
"github.com/snyk/go-application-framework/pkg/configuration"
20-
"github.com/snyk/go-application-framework/pkg/local_workflows"
21-
"github.com/snyk/go-application-framework/pkg/local_workflows/config_utils"
21+
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
2222
"github.com/snyk/go-application-framework/pkg/local_workflows/local_models"
2323
"github.com/snyk/go-application-framework/pkg/utils/git"
2424
"github.com/snyk/go-application-framework/pkg/workflow"
@@ -60,6 +60,8 @@ const (
6060

6161
interactiveEnsureVersionControlMessage = "👉🏼 Ensure the code containing the issue is committed and pushed to remote origin, so approvers can review it."
6262
interactiveIgnoreRequestSubmissionMessage = "✅ Your ignore request has been submitted."
63+
64+
configIgnoreApprovalEnabled = "internal_iaw_enabled"
6365
)
6466

6567
var reasonPromptHelpMap = map[string]string{
@@ -88,7 +90,7 @@ func InitIgnoreWorkflows(engine workflow.Engine) error {
8890
return err
8991
}
9092

91-
config_utils.AddFeatureFlagToConfig(engine, configuration.FF_IAW_ENABLED, "ignoreApprovalWorkflow")
93+
engine.GetConfiguration().AddDefaultValue(configIgnoreApprovalEnabled, getOrgIgnoreApprovalEnabled(engine))
9294

9395
return nil
9496
}
@@ -100,8 +102,14 @@ func ignoreCreateWorkflowEntryPoint(invocationCtx workflow.InvocationContext, _
100102
config := invocationCtx.GetConfiguration()
101103
id := invocationCtx.GetWorkflowIdentifier()
102104

103-
if !config.GetBool(configuration.FF_IAW_ENABLED) {
104-
return nil, cli.NewFeatureUnderDevelopmentError("")
105+
if !config.GetBool(configIgnoreApprovalEnabled) {
106+
return nil, snyk_errors.Error{
107+
ID: "SNYK-CLI-0014",
108+
Title: "Organization setting not enabled",
109+
Description: "The feature you are trying to use is not enabled you the current organization. Enable it in the settings or try switching the organization.",
110+
Detail: "",
111+
Level: "fatal",
112+
}
105113
}
106114

107115
interactive := config.GetBool(InteractiveKey)

pkg/local_workflows/ignore_workflow/ignore_workflow_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func setupMockIgnoreContext(t *testing.T, payload string, statusCode int) *mocks
5151
config := configuration.New()
5252
config.Set(configuration.API_URL, "https://api.snyk.io")
5353
config.Set(configuration.ORGANIZATION, uuid.New().String())
54-
config.Set(configuration.FF_IAW_ENABLED, true)
54+
config.Set(configIgnoreApprovalEnabled, true)
5555
// setup mocks
5656
ctrl := gomock.NewController(t)
5757
networkAccessMock := mocks.NewMockNetworkAccess(ctrl)
@@ -306,7 +306,7 @@ func Test_ignoreCreateWorkflowEntryPoint(t *testing.T) {
306306

307307
invocationContext := setupMockIgnoreContext(t, "{}", http.StatusCreated)
308308
config := invocationContext.GetConfiguration()
309-
config.Set(configuration.FF_IAW_ENABLED, false)
309+
config.Set(configIgnoreApprovalEnabled, false)
310310

311311
config.Set(InteractiveKey, false)
312312

@@ -330,7 +330,7 @@ func setupInteractiveMockContext(t *testing.T, mockApiResponse string, mockApiSt
330330
config := configuration.New()
331331
config.Set(configuration.API_URL, "https://api.snyk.io")
332332
config.Set(configuration.ORGANIZATION, uuid.New().String())
333-
config.Set(configuration.FF_IAW_ENABLED, true)
333+
config.Set(configIgnoreApprovalEnabled, true)
334334
config.Set(InteractiveKey, true) // Always interactive
335335
config.Set(EnrichResponseKey, true)
336336
config.Set(configuration.ORGANIZATION_SLUG, "some-org")

0 commit comments

Comments
 (0)