Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit c87f693

Browse files
vovakulikovBolajiOlajide
authored andcommitted
Gate keep Code Insights app UI based on code insights API availability (#46630)
* Add codeInsights enabled var to the jscontext * Gate keep code insights app-like UI based on code insights API availability * Update insights.IsEnabled usage * Fix integration tests and static js context mocks * Fix import order * Add proxy helper isEnabled back in insight package
1 parent b29fcfb commit c87f693

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

client/web/dev/utils/create-js-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
3131
executorsEnabled: false,
3232
codeIntelAutoIndexingEnabled: false,
3333
codeIntelAutoIndexingAllowGlobalPolicies: false,
34+
codeInsightsEnabled: true,
3435
externalServicesUserMode: 'public',
3536
productResearchPageEnabled: true,
3637
assetsRoot: '/.assets',

client/web/src/enterprise/insights/CodeInsightsRouter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export interface CodeInsightsRouterProps extends TelemetryProps {
1818
}
1919

2020
export const CodeInsightsRouter: FC<CodeInsightsRouterProps> = props => {
21-
const { authenticatedUser, isSourcegraphDotCom, telemetryService } = props
21+
const { authenticatedUser, telemetryService } = props
2222

23-
if (isSourcegraphDotCom) {
23+
if (!window.context.codeInsightsEnabled) {
2424
return (
2525
<CodeInsightsDotComGetStartedLazy
2626
telemetryService={telemetryService}

client/web/src/integration/jscontext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
1919
batchChangesEnabled: true,
2020
batchChangesDisableWebhooksWarning: false,
2121
batchChangesWebhookLogsEnabled: true,
22+
codeInsightsEnabled: true,
2223
executorsEnabled: true,
2324
codeIntelAutoIndexingEnabled: true,
2425
codeIntelAutoIndexingAllowGlobalPolicies: true,

client/web/src/jscontext.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export interface SourcegraphContext extends Pick<Required<SiteConfiguration>, 'e
121121
/** Whether global policies are enabled for auto-indexing. */
122122
codeIntelAutoIndexingAllowGlobalPolicies: boolean
123123

124+
/** Whether code insights API is enabled on the site. */
125+
codeInsightsEnabled: boolean
126+
124127
/** Whether users are allowed to add their own code and at what permission level. */
125128
externalServicesUserMode: 'disabled' | 'public' | 'all' | 'unknown'
126129

cmd/frontend/enterprise/enterprise.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"os"
8+
"strconv"
79
"time"
810

911
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
@@ -12,6 +14,7 @@ import (
1214
"github.com/sourcegraph/sourcegraph/internal/api"
1315
"github.com/sourcegraph/sourcegraph/internal/auth"
1416
"github.com/sourcegraph/sourcegraph/internal/conf"
17+
"github.com/sourcegraph/sourcegraph/internal/conf/deploy"
1518
"github.com/sourcegraph/sourcegraph/internal/database"
1619
)
1720

@@ -163,6 +166,31 @@ func BatchChangesEnabledForUser(ctx context.Context, db database.DB) error {
163166
return nil
164167
}
165168

169+
// IsCodeInsightsEnabled tells if code insights are enabled or not.
170+
func IsCodeInsightsEnabled() bool {
171+
if envvar.SourcegraphDotComMode() {
172+
return false
173+
}
174+
if v, _ := strconv.ParseBool(os.Getenv("DISABLE_CODE_INSIGHTS")); v {
175+
// Code insights can always be disabled. This can be a helpful escape hatch if e.g. there
176+
// are issues with (or connecting to) the codeinsights-db deployment and it is preventing
177+
// the Sourcegraph frontend or repo-updater from starting.
178+
//
179+
// It is also useful in dev environments if you do not wish to spend resources running Code
180+
// Insights.
181+
return false
182+
}
183+
if deploy.IsDeployTypeSingleDockerContainer(deploy.Type()) {
184+
// Code insights is not supported in single-container Docker demo deployments unless
185+
// explicity allowed, (for example by backend integration tests.)
186+
if v, _ := strconv.ParseBool(os.Getenv("ALLOW_SINGLE_DOCKER_CODE_INSIGHTS")); v {
187+
return true
188+
}
189+
return false
190+
}
191+
return true
192+
}
193+
166194
type stubRankingService struct{}
167195

168196
func (s stubRankingService) LastUpdatedAt(ctx context.Context, repoIDs []api.RepoID) (map[api.RepoID]time.Time, error) {

cmd/frontend/internal/app/jscontext/jscontext.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ type JSContext struct {
107107
CodeIntelAutoIndexingEnabled bool `json:"codeIntelAutoIndexingEnabled"`
108108
CodeIntelAutoIndexingAllowGlobalPolicies bool `json:"codeIntelAutoIndexingAllowGlobalPolicies"`
109109

110+
CodeInsightsEnabled bool `json:"codeInsightsEnabled"`
111+
110112
RedirectUnsupportedBrowser bool `json:"RedirectUnsupportedBrowser"`
111113

112114
ProductResearchPageEnabled bool `json:"productResearchPageEnabled"`
@@ -247,6 +249,8 @@ func NewJSContextFromRequest(req *http.Request, db database.DB) JSContext {
247249
CodeIntelAutoIndexingEnabled: conf.CodeIntelAutoIndexingEnabled(),
248250
CodeIntelAutoIndexingAllowGlobalPolicies: conf.CodeIntelAutoIndexingAllowGlobalPolicies(),
249251

252+
CodeInsightsEnabled: enterprise.IsCodeInsightsEnabled(),
253+
250254
ProductResearchPageEnabled: conf.ProductResearchPageEnabled(),
251255

252256
ExperimentalFeatures: conf.ExperimentalFeatures(),

enterprise/internal/insights/insights.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package insights
22

33
import (
44
"context"
5-
"os"
6-
"strconv"
75

86
"github.com/sourcegraph/sourcegraph/cmd/frontend/enterprise"
97
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel"
@@ -18,26 +16,8 @@ import (
1816
"github.com/sourcegraph/sourcegraph/lib/errors"
1917
)
2018

21-
// IsEnabled tells if code insights are enabled or not.
2219
func IsEnabled() bool {
23-
if v, _ := strconv.ParseBool(os.Getenv("DISABLE_CODE_INSIGHTS")); v {
24-
// Code insights can always be disabled. This can be a helpful escape hatch if e.g. there
25-
// are issues with (or connecting to) the codeinsights-db deployment and it is preventing
26-
// the Sourcegraph frontend or repo-updater from starting.
27-
//
28-
// It is also useful in dev environments if you do not wish to spend resources running Code
29-
// Insights.
30-
return false
31-
}
32-
if deploy.IsDeployTypeSingleDockerContainer(deploy.Type()) {
33-
// Code insights is not supported in single-container Docker demo deployments unless
34-
// explicity allowed, (for example by backend integration tests.)
35-
if v, _ := strconv.ParseBool(os.Getenv("ALLOW_SINGLE_DOCKER_CODE_INSIGHTS")); v {
36-
return true
37-
}
38-
return false
39-
}
40-
return true
20+
return enterprise.IsCodeInsightsEnabled()
4121
}
4222

4323
// Init initializes the given enterpriseServices to include the required resolvers for insights.

0 commit comments

Comments
 (0)