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

Commit

Permalink
Gate keep Code Insights app UI based on code insights API availability (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
vovakulikov authored and BolajiOlajide committed Jan 19, 2023
1 parent b29fcfb commit c87f693
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions client/web/dev/utils/create-js-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
executorsEnabled: false,
codeIntelAutoIndexingEnabled: false,
codeIntelAutoIndexingAllowGlobalPolicies: false,
codeInsightsEnabled: true,
externalServicesUserMode: 'public',
productResearchPageEnabled: true,
assetsRoot: '/.assets',
Expand Down
4 changes: 2 additions & 2 deletions client/web/src/enterprise/insights/CodeInsightsRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface CodeInsightsRouterProps extends TelemetryProps {
}

export const CodeInsightsRouter: FC<CodeInsightsRouterProps> = props => {
const { authenticatedUser, isSourcegraphDotCom, telemetryService } = props
const { authenticatedUser, telemetryService } = props

if (isSourcegraphDotCom) {
if (!window.context.codeInsightsEnabled) {
return (
<CodeInsightsDotComGetStartedLazy
telemetryService={telemetryService}
Expand Down
1 change: 1 addition & 0 deletions client/web/src/integration/jscontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
batchChangesEnabled: true,
batchChangesDisableWebhooksWarning: false,
batchChangesWebhookLogsEnabled: true,
codeInsightsEnabled: true,
executorsEnabled: true,
codeIntelAutoIndexingEnabled: true,
codeIntelAutoIndexingAllowGlobalPolicies: true,
Expand Down
3 changes: 3 additions & 0 deletions client/web/src/jscontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export interface SourcegraphContext extends Pick<Required<SiteConfiguration>, 'e
/** Whether global policies are enabled for auto-indexing. */
codeIntelAutoIndexingAllowGlobalPolicies: boolean

/** Whether code insights API is enabled on the site. */
codeInsightsEnabled: boolean

/** Whether users are allowed to add their own code and at what permission level. */
externalServicesUserMode: 'disabled' | 'public' | 'all' | 'unknown'

Expand Down
28 changes: 28 additions & 0 deletions cmd/frontend/enterprise/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"time"

"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
Expand All @@ -12,6 +14,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/auth"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/conf/deploy"
"github.com/sourcegraph/sourcegraph/internal/database"
)

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

// IsCodeInsightsEnabled tells if code insights are enabled or not.
func IsCodeInsightsEnabled() bool {
if envvar.SourcegraphDotComMode() {
return false
}
if v, _ := strconv.ParseBool(os.Getenv("DISABLE_CODE_INSIGHTS")); v {
// Code insights can always be disabled. This can be a helpful escape hatch if e.g. there
// are issues with (or connecting to) the codeinsights-db deployment and it is preventing
// the Sourcegraph frontend or repo-updater from starting.
//
// It is also useful in dev environments if you do not wish to spend resources running Code
// Insights.
return false
}
if deploy.IsDeployTypeSingleDockerContainer(deploy.Type()) {
// Code insights is not supported in single-container Docker demo deployments unless
// explicity allowed, (for example by backend integration tests.)
if v, _ := strconv.ParseBool(os.Getenv("ALLOW_SINGLE_DOCKER_CODE_INSIGHTS")); v {
return true
}
return false
}
return true
}

type stubRankingService struct{}

func (s stubRankingService) LastUpdatedAt(ctx context.Context, repoIDs []api.RepoID) (map[api.RepoID]time.Time, error) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/frontend/internal/app/jscontext/jscontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ type JSContext struct {
CodeIntelAutoIndexingEnabled bool `json:"codeIntelAutoIndexingEnabled"`
CodeIntelAutoIndexingAllowGlobalPolicies bool `json:"codeIntelAutoIndexingAllowGlobalPolicies"`

CodeInsightsEnabled bool `json:"codeInsightsEnabled"`

RedirectUnsupportedBrowser bool `json:"RedirectUnsupportedBrowser"`

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

CodeInsightsEnabled: enterprise.IsCodeInsightsEnabled(),

ProductResearchPageEnabled: conf.ProductResearchPageEnabled(),

ExperimentalFeatures: conf.ExperimentalFeatures(),
Expand Down
22 changes: 1 addition & 21 deletions enterprise/internal/insights/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package insights

import (
"context"
"os"
"strconv"

"github.com/sourcegraph/sourcegraph/cmd/frontend/enterprise"
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel"
Expand All @@ -18,26 +16,8 @@ import (
"github.com/sourcegraph/sourcegraph/lib/errors"
)

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

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

0 comments on commit c87f693

Please sign in to comment.