Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(import): send import results to discord #6662

Merged
merged 13 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions editor/src/components/editor/actions/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,10 @@ import { canCondenseJSXElementChild } from '../../../utils/can-condense'
import { getNavigatorTargetsFromEditorState } from '../../navigator/navigator-utils'
import { getParseCacheOptions } from '../../../core/shared/parse-cache-utils'
import { styleP } from '../../inspector/inspector-common'
import { getUpdateOperationResult } from '../../../core/shared/import/import-operation-service'
import {
getUpdateOperationResult,
notifyImportStatusToDiscord,
} from '../../../core/shared/import/import-operation-service'
import { updateRequirements } from '../../../core/shared/import/project-health-check/utopia-requirements-service'
import {
applyValuesAtPath,
Expand Down Expand Up @@ -2191,12 +2194,15 @@ export const UPDATE_FNS = {
}
},
UPDATE_IMPORT_STATUS: (action: UpdateImportStatus, editor: EditorModel): EditorModel => {
const newImportState = {
...editor.importState,
importStatus: action.importStatus,
}
// side effect ☢️
notifyImportStatusToDiscord(newImportState, editor.projectName)
return {
...editor,
importState: {
...editor.importState,
importStatus: action.importStatus,
},
importState: newImportState,
}
},
UPDATE_PROJECT_REQUIREMENTS: (
Expand Down
49 changes: 2 additions & 47 deletions editor/src/components/editor/import-wizard/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type {
ImportOperation,
} from '../../../core/shared/import/import-operation-types'
import { ImportOperationResult } from '../../../core/shared/import/import-operation-types'
import { assertNever } from '../../../core/shared/utils'
import { Icn, Icons, useColorTheme } from '../../../uuiui'
import { GithubSpinner } from '../../../components/navigator/left-pane/github-pane/github-spinner'
import { getImportOperationTextAsJsx } from './import-wizard-helpers'

export function OperationLine({ operation }: { operation: ImportOperation }) {
const operationRunningStatus = React.useMemo(() => {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function OperationLine({ operation }: { operation: ImportOperation }) {
>
<OperationLineContent textColor={textColor}>
<OperationIcon runningStatus={operationRunningStatus} result={operation.result} />
<div>{getImportOperationText(operation)}</div>
<div>{getImportOperationTextAsJsx(operation)}</div>
<div>
<TimeFromInSeconds operation={operation} runningStatus={operationRunningStatus} />
</div>
Expand Down Expand Up @@ -246,48 +246,3 @@ function OperationLineContent({
</div>
)
}

function getImportOperationText(operation: ImportOperation): React.ReactNode {
if (operation.text != null) {
return operation.text
}
switch (operation.type) {
case 'loadBranch':
if (operation.branchName != null) {
return (
<span>
Fetching branch{' '}
<strong>
{operation.githubRepo?.owner}/{operation.githubRepo?.repository}@
{operation.branchName}
</strong>
</span>
)
} else {
return (
<span>
Fetching repository{' '}
<strong>
{operation.githubRepo?.owner}/{operation.githubRepo?.repository}
</strong>
</span>
)
}
case 'fetchDependency':
return `Fetching ${operation.dependencyName}@${operation.dependencyVersion}`
case 'parseFiles':
return 'Parsing files'
case 'refreshDependencies':
return 'Fetching dependencies'
case 'checkRequirementsPreParse':
return 'Validating code'
case 'checkRequirementsPostParse':
return 'Checking Utopia requirements'
case 'checkRequirementAndFixPreParse':
return operation.text
case 'checkRequirementAndFixPostParse':
return operation.text
default:
assertNever(operation)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react'
import {
ImportOperationResult,
type ImportOperation,
} from '../../../core/shared/import/import-operation-types'
import { assertNever } from '../../../core/shared/utils'

export function getImportOperationText(operation: ImportOperation): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved here to be shared

if (operation.text != null) {
return operation.text
}
switch (operation.type) {
case 'loadBranch':
const action =
operation.result == ImportOperationResult.Error ||
operation.result == ImportOperationResult.CriticalError
? 'Error Fetching'
: 'Fetching'
if (operation.branchName != null) {
return `${action} branch **${operation.githubRepo?.owner}/${operation.githubRepo?.repository}@${operation.branchName}**`
} else {
return `${action} repository **${operation.githubRepo?.owner}/${operation.githubRepo?.repository}**`
}
case 'fetchDependency':
return `Fetching ${operation.dependencyName}@${operation.dependencyVersion}`
case 'parseFiles':
return 'Parsing files'
case 'refreshDependencies':
return 'Fetching dependencies'
case 'checkRequirementsPreParse':
return 'Validating code'
case 'checkRequirementsPostParse':
return 'Checking Utopia requirements'
case 'checkRequirementAndFixPreParse':
return operation.text
case 'checkRequirementAndFixPostParse':
return operation.text
default:
assertNever(operation)
}
}

export function getImportOperationTextAsJsx(operation: ImportOperation): React.ReactNode {
const text = getImportOperationText(operation)
const nodes = text.split('**').map((part, index) => {
return index % 2 == 0 ? part : <strong key={part}>{part}</strong>
})
return <span>{nodes}</span>
}
32 changes: 24 additions & 8 deletions editor/src/components/editor/import-wizard/import-wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { unless, when } from '../../../utils/react-conditionals'
import {
getTotalImportStatusAndResult,
hideImportWizard,
notifyImportStatusToDiscord,
updateProjectImportStatus,
} from '../../../core/shared/import/import-operation-service'
import { OperationLine } from './components'
Expand Down Expand Up @@ -45,11 +46,6 @@ export const ImportWizard = React.memo(() => {
e.stopPropagation()
}, [])

const totalImportResult: TotalImportResult = React.useMemo(
() => getTotalImportStatusAndResult(importState),
[importState],
)

if (projectId == null) {
return null
}
Expand Down Expand Up @@ -130,7 +126,7 @@ export const ImportWizard = React.memo(() => {
gap: 10,
}}
>
<ActionButtons importResult={totalImportResult} />
<ActionButtons />
</div>
</div>,
)}
Expand All @@ -139,7 +135,23 @@ export const ImportWizard = React.memo(() => {
})
ImportWizard.displayName = 'ImportWizard'

function ActionButtons({ importResult }: { importResult: TotalImportResult }) {
function ActionButtons() {
const importState = useEditorState(
Substores.github,
(store) => store.editor.importState,
'ImportWizard importState',
)

const projectName = useEditorState(
Substores.restOfEditor,
(store) => store.editor.projectName,
'ImportWizard projectName',
)

const importResult: TotalImportResult = React.useMemo(
() => getTotalImportStatusAndResult(importState),
[importState],
)
const colorTheme = useColorTheme()
const dispatch = useDispatch()
const result = importResult.result
Expand Down Expand Up @@ -174,6 +186,10 @@ function ActionButtons({ importResult }: { importResult: TotalImportResult }) {
}
}, [dispatch, hideWizard, importResult.importStatus])
const importADifferentProject = React.useCallback(() => {
if (importResult.importStatus.status !== 'done') {
// force a notification to discord that the import was exited in the middle
notifyImportStatusToDiscord(importState, projectName, true)
}
dispatch(
[
setImportWizardOpen(false),
Expand All @@ -182,7 +198,7 @@ function ActionButtons({ importResult }: { importResult: TotalImportResult }) {
],
'everyone',
)
}, [dispatch])
}, [dispatch, importResult.importStatus.status, importState, projectName])
const textStyle = {
color: textColor,
fontSize: 14,
Expand Down
17 changes: 17 additions & 0 deletions editor/src/components/editor/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { assertNever } from '../../core/shared/utils'
import { checkOnlineState } from './online-status'
import type { GithubOperationContext } from '../../core/shared/github/operations/github-operation-context'
import { GithubEndpoints } from '../../core/shared/github/endpoints'
import type { DiscordEndpointPayload } from 'utopia-shared/src/types'

export { fetchProjectList, fetchShowcaseProjects, getLoginState } from '../../common/server'

Expand Down Expand Up @@ -761,3 +762,19 @@ export function getBranchProjectContents(operationContext: GithubOperationContex
return response.json()
}
}

export async function sendDiscordMessage(payload: DiscordEndpointPayload) {
try {
const response = await fetch(`/internal/discord/webhook`, {
method: 'POST',
credentials: 'include',
mode: MODE,
body: JSON.stringify(payload),
})
if (!response.ok) {
console.error(`Send Discord message failed (${response.status}): ${response.statusText}`)
}
} catch (e) {
console.error(`Send Discord message failed: ${e}`)
}
}
12 changes: 7 additions & 5 deletions editor/src/components/github/github-repository-clone-flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import { OperationContext } from '../../core/shared/github/operations/github-ope
import { notice } from '../common/notice'
import { isFeatureEnabled } from '../../utils/feature-switches'
import {
notifyOperationCriticalError,
notifyOperationFinished,
startImportProcess,
updateProjectImportStatus,
} from '../../core/shared/import/import-operation-service'
import { ImportOperationResult } from '../../core/shared/import/import-operation-types'

Expand Down Expand Up @@ -131,11 +133,11 @@ async function cloneGithubRepo(
if (repositoryEntry == null) {
if (isFeatureEnabled('Import Wizard')) {
startImportProcess(dispatch)
notifyOperationFinished(
dispatch,
{ type: 'loadBranch', branchName: githubRepo.branch ?? undefined, githubRepo: githubRepo },
ImportOperationResult.CriticalError,
)
notifyOperationCriticalError(dispatch, {
type: 'loadBranch',
branchName: githubRepo.branch ?? undefined,
githubRepo: githubRepo,
})
} else {
dispatch([showToast(notice('Cannot find repository', 'ERROR'))])
}
Expand Down
14 changes: 4 additions & 10 deletions editor/src/core/shared/github/operations/load-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ import { GithubOperations } from '.'
import { assertNever } from '../../utils'
import { updateProjectContentsWithParseResults } from '../../parser-projectcontents-utils'
import {
notifyOperationCriticalError,
notifyOperationFinished,
notifyOperationStarted,
pauseImport,
startImportProcess,
updateProjectImportStatus,
} from '../../import/import-operation-service'
import {
RequirementResolutionResult,
Expand Down Expand Up @@ -165,21 +167,13 @@ export const updateProjectWithBranchContent =
switch (responseBody.type) {
case 'FAILURE':
if (isFeatureEnabled('Import Wizard')) {
notifyOperationFinished(
dispatch,
{ type: 'loadBranch' },
ImportOperationResult.CriticalError,
)
notifyOperationCriticalError(dispatch, { type: 'loadBranch' })
}
throw githubAPIError(operation, responseBody.failureReason)
case 'SUCCESS':
if (responseBody.branch == null) {
if (isFeatureEnabled('Import Wizard')) {
notifyOperationFinished(
dispatch,
{ type: 'loadBranch' },
ImportOperationResult.CriticalError,
)
notifyOperationCriticalError(dispatch, { type: 'loadBranch' })
}
throw githubAPIError(operation, `Could not find branch ${branchName}`)
}
Expand Down
Loading
Loading