Skip to content

Commit

Permalink
batches: remove rollout windows config from jscontext and DropdownBut…
Browse files Browse the repository at this point in the history
…ton (sourcegraph#51752)

Co-authored-by: Kelli Rockwell <kelli@sourcegraph.com>
  • Loading branch information
BolajiOlajide and courier-new committed May 17, 2023
1 parent b9f5136 commit 9140aa2
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 49 deletions.
2 changes: 1 addition & 1 deletion client/web/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/web/dev/utils/create-js-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
},
embeddingsEnabled: false,
primaryLoginProvidersCount: 5,
batchChangesRolloutWindows: null,
// Site-config overrides default JS context
...siteConfig,
}
Expand Down
67 changes: 65 additions & 2 deletions client/web/src/enterprise/batches/DropdownButton.story.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { DecoratorFn, Meta, Story } from '@storybook/react'

import { getDocumentNode } from '@sourcegraph/http-client'
import { MockedTestProvider } from '@sourcegraph/shared/src/testing/apollo'

import { WebStory } from '../../components/WebStory'

import { BATCH_CHANGES_SITE_CONFIGURATION } from './backend'
import { Action, DropdownButton } from './DropdownButton'
import { rolloutWindowConfigMockResult, noRolloutWindowMockResult } from './mocks'

// eslint-disable-next-line @typescript-eslint/require-await
const onTrigger = async (onDone: () => void) => onDone()
Expand Down Expand Up @@ -33,6 +38,15 @@ const experimentalAction: Action = {
experimental: true,
}

const publishAction: Action = {
type: 'publish',
buttonLabel: 'Publish Changeset',
dropdownTitle: 'Publish Changeset',
dropdownDescription: 'Attempt to publish all changesets to the code hosts.',
onTrigger,
experimental: false,
}

const decorator: DecoratorFn = story => <div className="p-3 container">{story()}</div>

const config: Meta = {
Expand Down Expand Up @@ -64,15 +78,64 @@ export const SingleAction: Story = args => <WebStory>{() => <DropdownButton acti
SingleAction.storyName = 'Single action'

export const MultipleActionsWithoutDefault: Story = args => (
<WebStory>{() => <DropdownButton actions={[action, disabledAction, experimentalAction]} {...args} />}</WebStory>
<WebStory>
{() => (
<MockedTestProvider
mocks={[
{
request: {
query: getDocumentNode(BATCH_CHANGES_SITE_CONFIGURATION),
},
result: noRolloutWindowMockResult,
},
]}
>
<DropdownButton actions={[action, disabledAction, experimentalAction, publishAction]} {...args} />
</MockedTestProvider>
)}
</WebStory>
)

MultipleActionsWithoutDefault.storyName = 'Multiple actions without default'

export const MultipleActionsWithDefault: Story = args => (
<WebStory>
{() => <DropdownButton actions={[action, disabledAction, experimentalAction]} defaultAction={0} {...args} />}
{() => (
<MockedTestProvider
mocks={[
{
request: {
query: getDocumentNode(BATCH_CHANGES_SITE_CONFIGURATION),
},
result: noRolloutWindowMockResult,
},
]}
>
<DropdownButton actions={[action, disabledAction, experimentalAction]} defaultAction={0} {...args} />
</MockedTestProvider>
)}
</WebStory>
)

MultipleActionsWithDefault.storyName = 'Multiple actions with default'

export const PublishActionWithRolloutWindowConfigured: Story = args => (
<WebStory>
{() => (
<MockedTestProvider
mocks={[
{
request: {
query: getDocumentNode(BATCH_CHANGES_SITE_CONFIGURATION),
},
result: rolloutWindowConfigMockResult,
},
]}
>
<DropdownButton actions={[action, publishAction]} defaultAction={0} {...args} />
</MockedTestProvider>
)}
</WebStory>
)

PublishActionWithRolloutWindowConfigured.storyName = 'Publish Action with rollout window configured'
29 changes: 16 additions & 13 deletions client/web/src/enterprise/batches/DropdownButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
Icon,
} from '@sourcegraph/wildcard'

import { useBatchChangesRolloutWindowConfig } from './backend'

import styles from './DropdownButton.module.scss'

export interface Action {
Expand Down Expand Up @@ -169,11 +171,11 @@ const DropdownItem: React.FunctionComponent<React.PropsWithChildren<DropdownItem
action,
setSelectedType,
}) => {
const { rolloutWindowConfig, loading } = useBatchChangesRolloutWindowConfig()
const onSelect = useCallback(() => {
setSelectedType(action.type)
}, [setSelectedType, action.type])

const { batchChangesRolloutWindows } = window.context
const shouldDisplayRolloutInfo = action.type === 'publish' && rolloutWindowConfig && rolloutWindowConfig.length > 0

return (
<MenuItem className={styles.menuListItem} onSelect={onSelect} disabled={action.disabled}>
Expand All @@ -187,17 +189,18 @@ const DropdownItem: React.FunctionComponent<React.PropsWithChildren<DropdownItem
)}
</H4>
<Text className="text-wrap text-muted mb-0">
{action.type === 'publish' && batchChangesRolloutWindows && batchChangesRolloutWindows.length > 0 ? (
<small>
{action.dropdownDescription} <br />
<b>
Note: Rollout windows have been set up by the admin. This means that some of the selected
changesets won't be processed until a time in the future.
</b>{' '}
</small>
) : (
<small>{action.dropdownDescription}</small>
)}
<small>
{action.dropdownDescription}
{!loading && shouldDisplayRolloutInfo && (
<>
<br />
<strong>
Note: Rollout windows have been set up by the admin. This means that some of the
selected changesets won't be processed until a time in the future.
</strong>
</>
)}
</small>
</Text>
</MenuItem>
)
Expand Down
33 changes: 27 additions & 6 deletions client/web/src/enterprise/batches/backend.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { QueryResult } from '@apollo/client'
import { useMemo } from 'react'

import { ApolloError } from '@apollo/client'
import * as jsonc from 'jsonc-parser'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { dataOrThrowErrors, gql, useQuery } from '@sourcegraph/http-client'
import { BatchChangeRolloutWindow, SiteConfiguration } from '@sourcegraph/shared/src/schema/site.schema'

import { requestGraphQL } from '../../backend/graphql'
import {
Expand Down Expand Up @@ -212,10 +216,27 @@ export const BATCH_CHANGES_SITE_CONFIGURATION = gql`
}
`

export const useGetBatchChangesSiteConfiguration = (): QueryResult<
BatchChangesSiteConfigurationResult,
BatchChangesSiteConfigurationVariables
> =>
useQuery(BATCH_CHANGES_SITE_CONFIGURATION, {
interface useGetBatchChangesSiteConfigurationResult {
loading: boolean
error: ApolloError | undefined
rolloutWindowConfig: BatchChangeRolloutWindow[]
}

export const useBatchChangesRolloutWindowConfig = (): useGetBatchChangesSiteConfigurationResult => {
const { loading, error, data } = useQuery<
BatchChangesSiteConfigurationResult,
BatchChangesSiteConfigurationVariables
>(BATCH_CHANGES_SITE_CONFIGURATION, {
fetchPolicy: 'cache-first',
})

const rolloutWindowConfig: BatchChangeRolloutWindow[] = useMemo(() => {
if (!data) {
return []
}
const siteConfig = jsonc.parse(data.site.configuration.effectiveContents) as SiteConfiguration
return siteConfig['batchChanges.rolloutWindows'] || []
}, [data])

return { loading, error, rolloutWindowConfig }
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
UserBatchChangesCodeHostsResult,
} from '../../../graphql-operations'
import { BATCH_CHANGES_SITE_CONFIGURATION } from '../backend'
import { noRolloutWindowMockResult, rolloutWindowConfigMockResult } from '../mocks'

import { USER_CODE_HOSTS } from './backend'
import { BatchChangesSettingsArea } from './BatchChangesSettingsArea'
import { noRolloutWindowMockResult, rolloutWindowConfigMockResult } from './mocks'

const decorator: DecoratorFn = story => <div className="p-3 container">{story()}</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { MockedTestProvider } from '@sourcegraph/shared/src/testing/apollo'

import { WebStory } from '../../../components/WebStory'
import { BATCH_CHANGES_SITE_CONFIGURATION } from '../backend'
import { noRolloutWindowMockResult, rolloutWindowConfigMockResult } from '../mocks'

import { noRolloutWindowMockResult, rolloutWindowConfigMockResult } from './mocks'
import { RolloutWindowsConfiguration } from './RolloutWindowsConfiguration'

const decorator: DecoratorFn = story => <div className="p-3 container">{story()}</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import React, { useEffect, useState } from 'react'
import React from 'react'

import { mdiPulse } from '@mdi/js'
import * as jsonc from 'jsonc-parser'

import { BatchChangeRolloutWindow } from '@sourcegraph/shared/src/schema/site.schema'
import { Text, H3, Container, Icon, LoadingSpinner, ErrorAlert, Link } from '@sourcegraph/wildcard'

import { useGetBatchChangesSiteConfiguration } from '../backend'
import { useBatchChangesRolloutWindowConfig } from '../backend'

import { formatRate, formatDays } from './format'

import styles from './RolloutWindowsConfiguration.module.scss'

// Displays the rollout window configuration.
export const RolloutWindowsConfiguration: React.FunctionComponent = () => {
const [rolloutWindows, setRolloutWindows] = useState<BatchChangeRolloutWindow[]>([])
const { loading, error, data } = useGetBatchChangesSiteConfiguration()
useEffect(() => {
if (data) {
const siteConfig = jsonc.parse(data.site.configuration.effectiveContents)
setRolloutWindows(siteConfig['batchChanges.rolloutWindows'] || [])
}
}, [data])
const { loading, error, rolloutWindowConfig } = useBatchChangesRolloutWindowConfig()
return (
<Container className="mb-3">
<H3>Rollout Windows</H3>
{loading && <LoadingSpinner />}
{error && <ErrorAlert error={error} />}
{!loading &&
data &&
(rolloutWindows.length === 0 ? (
rolloutWindowConfig &&
(rolloutWindowConfig.length === 0 ? (
<Text className="mb-0">
No rollout windows configured for changesets. Learn how to configure them in{' '}
<Link to="/help/admin/config/batch_changes#rollout-windows" target="_blank">
Expand All @@ -48,7 +39,7 @@ export const RolloutWindowsConfiguration: React.FunctionComponent = () => {
</Link>
</Text>
<ul className={styles.rolloutWindowList}>
{rolloutWindows.map((rolloutWindow, index) => (
{rolloutWindowConfig.map((rolloutWindow, index) => (
<li key={index} className={styles.rolloutWindowListItem}>
<Text className={styles.rolloutWindowListItemFrequency}>
<Icon
Expand Down
1 change: 0 additions & 1 deletion client/web/src/integration/jscontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ export const createJsContext = ({ sourcegraphBaseUrl }: { sourcegraphBaseUrl: st
runningOnMacOS: true,
srcServeGitUrl: 'http://127.0.0.1:3434',
primaryLoginProvidersCount: 5,
batchChangesRolloutWindows: null,
})
4 changes: 1 addition & 3 deletions client/web/src/jscontext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthenticatedUser } from '@sourcegraph/shared/src/auth'
import { SiteConfiguration, BatchChangeRolloutWindow } from '@sourcegraph/shared/src/schema/site.schema'
import { SiteConfiguration } from '@sourcegraph/shared/src/schema/site.schema'

import { TemporarySettingsResult } from './graphql-operations'

Expand Down Expand Up @@ -174,8 +174,6 @@ export interface SourcegraphContext extends Pick<Required<SiteConfiguration>, 'e

batchChangesWebhookLogsEnabled: boolean

batchChangesRolloutWindows: BatchChangeRolloutWindow[] | null

/** Whether cody is enabled for the user. */
codyEnabled: boolean

Expand Down
8 changes: 3 additions & 5 deletions cmd/frontend/internal/app/jscontext/jscontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ type JSContext struct {
// the request is an admin and thus can access batch changes
// It does NOT reflect whether or not the site license has batch changes available.
// Use LicenseInfo for that.
BatchChangesEnabled bool `json:"batchChangesEnabled"`
BatchChangesDisableWebhooksWarning bool `json:"batchChangesDisableWebhooksWarning"`
BatchChangesWebhookLogsEnabled bool `json:"batchChangesWebhookLogsEnabled"`
BatchChangesRolloutWindows *[]*schema.BatchChangeRolloutWindow `json:"batchChangesRolloutWindows"`
BatchChangesEnabled bool `json:"batchChangesEnabled"`
BatchChangesDisableWebhooksWarning bool `json:"batchChangesDisableWebhooksWarning"`
BatchChangesWebhookLogsEnabled bool `json:"batchChangesWebhookLogsEnabled"`

CodyEnabled bool `json:"codyEnabled"`

Expand Down Expand Up @@ -360,7 +359,6 @@ func NewJSContextFromRequest(req *http.Request, db database.DB) JSContext {
BatchChangesEnabled: enterprise.BatchChangesEnabledForUser(ctx, db) == nil,
BatchChangesDisableWebhooksWarning: conf.Get().BatchChangesDisableWebhooksWarning,
BatchChangesWebhookLogsEnabled: webhooks.LoggingEnabled(conf.Get()),
BatchChangesRolloutWindows: conf.Get().BatchChangesRolloutWindows,

CodyEnabled: cody.IsCodyEnabled(ctx),

Expand Down

0 comments on commit 9140aa2

Please sign in to comment.