|
| 1 | +import { from } from 'rxjs' |
| 2 | +import { filter, switchMap } from 'rxjs/operators' |
| 3 | +import * as sourcegraph from 'sourcegraph' |
| 4 | +import { resolveSettings, Settings } from './settings' |
| 5 | + |
| 6 | +interface Params { |
| 7 | + repo: string | null |
| 8 | + file: string | null |
| 9 | + folder: string | null |
| 10 | +} |
| 11 | + |
| 12 | +// TODO: Receive code matching patterns from the Sentry extension settings |
| 13 | +const CODE_PATTERNS = [ |
| 14 | + /throw new Error+\([\'\"]([^\'\"]+)[\'\"]\)/gi, |
| 15 | + /console\.[^\'\"\`]+\([\'\"\`]([^\'\"\`]+)[\'\"\`]\)/gi, |
| 16 | + /log\.[^\'\"]+\([\'\"]([^\'\"]+)[\'\"]\)/gi, |
| 17 | +] |
| 18 | + |
| 19 | +const DECORATION_TYPE = sourcegraph.app.createDecorationType() |
| 20 | +const SETTINGSCONFIG = resolveSettings(sourcegraph.configuration.get<Settings>().value) |
| 21 | + |
| 22 | +function decorateEditor(editor: sourcegraph.CodeEditor, sentryProjects: Settings['sentry.projects']): void { |
| 23 | + const decorations: sourcegraph.TextDocumentDecoration[] = [] |
| 24 | + for (const [i, line] of editor.document.text!.split('\n').entries()) { |
| 25 | + let m: RegExpExecArray | null |
| 26 | + for (const pattern of CODE_PATTERNS) { |
| 27 | + do { |
| 28 | + m = pattern.exec(line) |
| 29 | + if (m) { |
| 30 | + decorations.push({ |
| 31 | + range: new sourcegraph.Range(i, 0, i, 0), |
| 32 | + isWholeLine: true, |
| 33 | + after: { |
| 34 | + backgroundColor: '#e03e2f', |
| 35 | + color: 'rgba(255, 255, 255, 0.8)', |
| 36 | + contentText: ' View logs in Sentry » ', |
| 37 | + linkURL: buildUrl(m[1]).toString(), |
| 38 | + }, |
| 39 | + }) |
| 40 | + } |
| 41 | + } while (m) |
| 42 | + pattern.lastIndex = 0 // reset |
| 43 | + } |
| 44 | + } |
| 45 | + editor.setDecorations(DECORATION_TYPE, decorations) |
| 46 | +} |
| 47 | + |
| 48 | +export function activate(context: sourcegraph.ExtensionContext): void { |
| 49 | + sourcegraph.workspace.onDidOpenTextDocument.subscribe(textDocument => { |
| 50 | + const params: Params = getParamsFromUriPath(textDocument.uri) |
| 51 | + const sentryProjects = SETTINGSCONFIG['sentry.projects'] |
| 52 | + |
| 53 | + if (sourcegraph.app.activeWindowChanges && sentryProjects && isSentryEnabled(params, sentryProjects)) { |
| 54 | + const activeEditor = from(sourcegraph.app.activeWindowChanges).pipe( |
| 55 | + filter((window): window is sourcegraph.Window => window !== undefined), |
| 56 | + switchMap(window => window.activeViewComponentChanges), |
| 57 | + filter((editor): editor is sourcegraph.CodeEditor => editor !== undefined) |
| 58 | + ) |
| 59 | + // When the active editor changes, publish new decorations. |
| 60 | + context.subscriptions.add( |
| 61 | + activeEditor.subscribe(editor => { |
| 62 | + decorateEditor(editor, sentryProjects) |
| 63 | + }) |
| 64 | + ) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Extract Sentry params from Document URI necessary to |
| 71 | + * build URL to the Sentry issues stream page, if the current |
| 72 | + * Document sends log events to Sentry. |
| 73 | + * |
| 74 | + * TODO: Implement regex match of params with Sentry extension settings. |
| 75 | + */ |
| 76 | +export function getParamsFromUriPath(textDocument: string): Params { |
| 77 | + const repoPattern = /github\.com\/([^\?\#]+)/gi |
| 78 | + const filePattern = /#([^\?\#\/]+)\/.*\.tsx?$/gi |
| 79 | + const repoM = repoPattern.exec(textDocument) |
| 80 | + const fileM = filePattern.exec(textDocument) |
| 81 | + return { |
| 82 | + repo: repoM![1], |
| 83 | + file: fileM![0], |
| 84 | + folder: fileM![1], |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Verify if the params from the document URI match with the repo and file formats specified |
| 90 | + * in the Sentry extension settings, we know the document is enabled to send logs |
| 91 | + * to Sentry. |
| 92 | + * @param params params extracted from the document's URI |
| 93 | + * @param projects Sentry extension projects configurations |
| 94 | + */ |
| 95 | +function isSentryEnabled(params: Params, projects: Settings['sentry.projects']): boolean { |
| 96 | + // Check if repo matches the repo specified under the Sentry extension configuration |
| 97 | + const doesRepoMatch: boolean = !!projects!.find(p => !!new RegExp(p.patternProperties.repoMatch).exec(params.repo!)) |
| 98 | + |
| 99 | + // Check if document matches the file format specified under the Sentry extension configuration |
| 100 | + const doesFileMatch: boolean = !!projects!.find(p => !!new RegExp(p.patternProperties.fileMatch).exec(params.file!)) |
| 101 | + |
| 102 | + if (doesRepoMatch && doesFileMatch) { |
| 103 | + return true |
| 104 | + } |
| 105 | + return false |
| 106 | +} |
| 107 | + |
| 108 | +// TODO receive projectId from enabled Sentry project |
| 109 | +function buildUrl(errorQuery: string): URL { |
| 110 | + const url = new URL( |
| 111 | + 'https://sentry.io/organizations/' + |
| 112 | + SETTINGSCONFIG['sentry.organization'] + |
| 113 | + '/issues/?project=1334031&query=is%3Aunresolved+' + |
| 114 | + errorQuery.split(' ').join('+') + |
| 115 | + '&statsPeriod=14d' |
| 116 | + ) |
| 117 | + return url |
| 118 | +} |
| 119 | +// Sourcegraph extension documentation: https://docs.sourcegraph.com/extensions/authoring |
0 commit comments