|
| 1 | +import { from } from 'rxjs' |
| 2 | +import { filter, switchMap } from 'rxjs/operators' |
| 3 | +import * as sourcegraph from 'sourcegraph' |
| 4 | +import { |
| 5 | + checkMissingConfig, |
| 6 | + createDecoration, |
| 7 | + getParamsFromUriPath, |
| 8 | + isFileMatched, |
| 9 | + matchSentryProject, |
| 10 | +} from './handler' |
| 11 | +import { resolveSettings, Settings } from './settings' |
| 12 | + |
| 13 | +/** |
| 14 | + * Params derived from the document's URI. |
| 15 | + */ |
| 16 | +interface Params { |
| 17 | + repo: string | null |
| 18 | + file: string | null |
| 19 | +} |
| 20 | + |
| 21 | +const DECORATION_TYPE = sourcegraph.app.createDecorationType() |
| 22 | +const SETTINGSCONFIG = resolveSettings(sourcegraph.configuration.get<Settings>().value) |
| 23 | +const SENTRYORGANIZATION = SETTINGSCONFIG['sentry.organization'] |
| 24 | + |
| 25 | +/** |
| 26 | + * Common error log patterns to use in case no line matching regexes |
| 27 | + * are set in the sentry extension settings. |
| 28 | + */ |
| 29 | +const COMMON_ERRORLOG_PATTERNS = [ |
| 30 | + /throw new Error+\(['"]([^'"]+)['"]\)/gi, |
| 31 | + /console\.(log|error|info|warn)\(['"`]([^'"`]+)['"`]\)/gi, |
| 32 | + /log\.(Printf|Print|Println)\(['"]([^'"]+)['"]\)/gi, |
| 33 | +] |
| 34 | + |
| 35 | +// TODO: Refactor to use activeEditor |
| 36 | +export function activate(context: sourcegraph.ExtensionContext): void { |
| 37 | + sourcegraph.workspace.onDidOpenTextDocument.subscribe(textDocument => { |
| 38 | + const params: Params = getParamsFromUriPath(textDocument.uri) |
| 39 | + const sentryProjects = SETTINGSCONFIG['sentry.projects'] |
| 40 | + |
| 41 | + // Retrieve the Sentry project that this document reports to. |
| 42 | + // TODO: Move this outside of activate() and into a separate, testable function. |
| 43 | + const sentryProject = sentryProjects && matchSentryProject(params, sentryProjects) |
| 44 | + let missingConfigData: string[] = [] |
| 45 | + let fileMatched: boolean | null |
| 46 | + |
| 47 | + if (sentryProject) { |
| 48 | + missingConfigData = checkMissingConfig(sentryProject) |
| 49 | + fileMatched = isFileMatched(params, sentryProject) |
| 50 | + // Do not decorate lines if the document file format does not match the |
| 51 | + // file matching patterns listed in the Sentry extension configurations. |
| 52 | + if (fileMatched === false) { |
| 53 | + return |
| 54 | + } |
| 55 | + } |
| 56 | + if (sourcegraph.app.activeWindowChanges) { |
| 57 | + const activeEditor = from(sourcegraph.app.activeWindowChanges).pipe( |
| 58 | + filter((window): window is sourcegraph.Window => window !== undefined), |
| 59 | + switchMap(window => window.activeViewComponentChanges), |
| 60 | + filter((editor): editor is sourcegraph.CodeEditor => editor !== undefined) |
| 61 | + ) |
| 62 | + // When the active editor changes, publish new decorations. |
| 63 | + context.subscriptions.add( |
| 64 | + activeEditor.subscribe(editor => { |
| 65 | + sentryProject |
| 66 | + ? decorateEditor( |
| 67 | + editor, |
| 68 | + missingConfigData, |
| 69 | + sentryProject.projectId, |
| 70 | + sentryProject.patternProperties.lineMatches |
| 71 | + ) |
| 72 | + : decorateEditor(editor, missingConfigData) |
| 73 | + }) |
| 74 | + ) |
| 75 | + } |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +// TODO: Refactor so that it calls a new function that returns TextDocumentDecoration[], |
| 80 | +// and add tests for that new function (kind of like getBlameDecorations()) |
| 81 | +function decorateEditor( |
| 82 | + editor: sourcegraph.CodeEditor, |
| 83 | + missingConfigData: string[], |
| 84 | + sentryProjectId?: string, |
| 85 | + lineMatches?: RegExp[] |
| 86 | +): void { |
| 87 | + const decorations: sourcegraph.TextDocumentDecoration[] = [] |
| 88 | + for (const [index, line] of editor.document.text!.split('\n').entries()) { |
| 89 | + let match: RegExpExecArray | null |
| 90 | + for (let pattern of lineMatches ? lineMatches : COMMON_ERRORLOG_PATTERNS) { |
| 91 | + pattern = new RegExp(pattern, 'gi') |
| 92 | + do { |
| 93 | + match = pattern.exec(line) |
| 94 | + if (match) { |
| 95 | + decorations.push(decorateLine(index, match, missingConfigData, sentryProjectId)) |
| 96 | + } |
| 97 | + } while (match) |
| 98 | + pattern.lastIndex = 0 // reset |
| 99 | + } |
| 100 | + } |
| 101 | + editor.setDecorations(DECORATION_TYPE, decorations) |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Decorate a line that matches either the line match pattern from the Sentry extension configurations |
| 106 | + * or that matches common error loggin patterns. |
| 107 | + * @param index for decoration range |
| 108 | + * @param match for a line containing an error query |
| 109 | + * @param sentryProjectId Sentry project id retrieved from Sentry extension settings |
| 110 | + */ |
| 111 | +export function decorateLine( |
| 112 | + index: number, |
| 113 | + match: RegExpExecArray, |
| 114 | + missingConfigData: string[], |
| 115 | + sentryProjectId?: string |
| 116 | +): sourcegraph.TextDocumentDecoration { |
| 117 | + const lineDecorationText = createDecoration(missingConfigData, sentryProjectId) |
| 118 | + const decoration: sourcegraph.TextDocumentDecoration = { |
| 119 | + range: new sourcegraph.Range(index, 0, index, 0), |
| 120 | + isWholeLine: true, |
| 121 | + after: { |
| 122 | + backgroundColor: missingConfigData.length === 0 ? '#e03e2f' : '#f2736d', |
| 123 | + color: 'rgba(255, 255, 255, 0.8)', |
| 124 | + contentText: lineDecorationText.content, |
| 125 | + hoverMessage: lineDecorationText.hover, |
| 126 | + // Depending on the line matching pattern the query m is indexed in position 1 or 2. |
| 127 | + // TODO: Specify which capture group should be used through configuration. |
| 128 | + // TODO: If !SENTRYORGANIZATION is missing in config, link to $USER/settings and hint |
| 129 | + // user to fill it out. |
| 130 | + linkURL: !SENTRYORGANIZATION |
| 131 | + ? '' |
| 132 | + : sentryProjectId |
| 133 | + ? buildUrl(match.length > 2 ? match[2] : match[1], sentryProjectId).toString() |
| 134 | + : buildUrl(match.length > 2 ? match[2] : match[1]).toString(), |
| 135 | + }, |
| 136 | + } |
| 137 | + return decoration |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Build URL to the Sentry issues stream page with the Sentry Org, query and, if available, Sentry project ID. |
| 142 | + * @param errorQuery extracted from the error handling code matching the config matching pattern. |
| 143 | + * @param sentryProjectId from the associated Sentry project receiving logs from the document's repo. |
| 144 | + * @return URL to the Sentry unresolved issues stream page for this kind of query. |
| 145 | + */ |
| 146 | +// TODO: Use URLSearchParams instead of encodeURIComponent |
| 147 | +function buildUrl(errorQuery: string, sentryProjectId?: string): URL { |
| 148 | + const url = new URL( |
| 149 | + 'https://sentry.io/organizations/' + |
| 150 | + encodeURIComponent(SENTRYORGANIZATION!) + |
| 151 | + '/issues/' + |
| 152 | + (sentryProjectId |
| 153 | + ? '?project=' + |
| 154 | + encodeURIComponent(sentryProjectId) + |
| 155 | + '&query=is%3Aunresolved+' + |
| 156 | + encodeURIComponent(errorQuery) + |
| 157 | + '&statsPeriod=14d' |
| 158 | + : '') |
| 159 | + ) |
| 160 | + return url |
| 161 | +} |
0 commit comments