forked from codecov/sourcegraph-codecov
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
260 lines (242 loc) · 9.46 KB
/
extension.ts
File metadata and controls
260 lines (242 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { BehaviorSubject, combineLatest, from, Subscription } from 'rxjs'
import { concatMap, filter, map, startWith, switchMap } from 'rxjs/operators'
import * as sourcegraph from 'sourcegraph'
import { codecovToDecorations } from './decoration'
import {
getCommitCoverageRatio,
getFileCoverageRatios,
getFileLineCoverage,
} from './model'
import {
Endpoint,
resolveEndpoint,
resolveSettings,
Settings,
} from './settings'
import {
codecovParamsForRepositoryCommit,
resolveDocumentURI,
resolveRootURI,
} from './uri'
const decorationType =
sourcegraph.app.createDecorationType &&
sourcegraph.app.createDecorationType()
/** Entrypoint for the Codecov Sourcegraph extension. */
export function activate(
context: sourcegraph.ExtensionContext = {
subscriptions: new Subscription(),
}
): void {
/**
* An Observable that emits the active window's visible view components
* when the active window or its view components change.
*/
const editorsChanges = sourcegraph.app.activeWindowChanges
? from(sourcegraph.app.activeWindowChanges).pipe(
filter(
(
activeWindow
): activeWindow is Exclude<typeof activeWindow, undefined> =>
activeWindow !== undefined
),
switchMap(activeWindow =>
from(activeWindow.activeViewComponentChanges).pipe(
map(() => activeWindow.visibleViewComponents)
)
)
// Backcompat: rely on onDidOpenTextDocument if the extension host doesn't support activeWindowChanges / activeViewComponentChanges
)
: from(sourcegraph.workspace.onDidOpenTextDocument).pipe(
map(
() =>
(sourcegraph.app.activeWindow &&
sourcegraph.app.activeWindow.visibleViewComponents) ||
[]
)
)
// When the configuration or current file changes, publish new decorations.
//
// TODO: Unpublish decorations on previously (but not currently) open files when settings changes, to avoid a
// brief flicker of the old state when the file is reopened.
async function decorate(
settings: Readonly<Settings>,
editors: sourcegraph.CodeEditor[]
): Promise<void> {
const resolvedSettings = resolveSettings(settings)
for (const editor of editors) {
const decorations = await getFileLineCoverage(
resolveDocumentURI(editor.document.uri),
resolvedSettings['codecov.endpoints'][0],
sourcegraph
)
editor.setDecorations(
decorationType,
codecovToDecorations(settings, decorations)
)
}
}
/**
* A BehaviorSubject of the extension's resolved {@link Settings}.
*/
const configurationChanges = new BehaviorSubject<Readonly<Settings>>(
sourcegraph.configuration.get<Settings>().value
)
context.subscriptions.add(
sourcegraph.configuration.subscribe(() =>
configurationChanges.next(
sourcegraph.configuration.get<Settings>().value
)
)
)
context.subscriptions.add(
combineLatest([configurationChanges, editorsChanges])
.pipe(
concatMap(async ([settings, editors]) => {
try {
await decorate(settings, editors)
} catch (err) {
console.error('Codecov: decoration error', err)
}
})
)
.subscribe()
)
// Set context values referenced in template expressions in the extension manifest (e.g., to interpolate "N" in
// the "Coverage: N%" button label).
//
// The context only needs to be updated when the endpoints configuration changes.
async function updateContext(
endpoints: readonly Endpoint[] | undefined,
roots: readonly sourcegraph.WorkspaceRoot[],
editors: sourcegraph.CodeEditor[]
): Promise<void> {
// Get the current repository. Sourcegraph 3.0-preview exposes sourcegraph.workspace.roots, but earlier
// versions do not.
let uri: string
if (roots && roots.length > 0) {
uri = roots[0].uri.toString()
} else if (editors.length > 0) {
uri = editors[0].document.uri
} else {
return
}
const lastURI = resolveRootURI(uri)
const endpoint = resolveEndpoint(endpoints)
const context: {
[key: string]: string | number | boolean | null
} = {}
const p = codecovParamsForRepositoryCommit(lastURI, sourcegraph)
const repoURL = `${p.baseURL || 'https://codecov.io'}/${p.service}/${
p.owner
}/${p.repo}`
context['codecov.repoURL'] = repoURL
const baseFileURL = `${repoURL}/src/${p.sha}`
context['codecov.commitURL'] = `${repoURL}/commit/${p.sha}`
try {
// Store overall commit coverage ratio.
const commitCoverage = await getCommitCoverageRatio(
lastURI,
endpoint,
sourcegraph
)
context['codecov.commitCoverage'] = commitCoverage
? commitCoverage.toFixed(1)
: null
// Store coverage ratio (and Codecov report URL) for each file at this commit so that
// template strings in contributions can refer to these values.
const fileRatios = await getFileCoverageRatios(
lastURI,
endpoint,
sourcegraph
)
for (const [path, ratio] of Object.entries(fileRatios)) {
const uri = `git://${lastURI.repo}?${lastURI.rev}#${path}`
context[`codecov.coverageRatio.${uri}`] = ratio.toFixed(0)
context[`codecov.fileURL.${uri}`] = `${baseFileURL}/${path}`
}
} catch (err) {
console.error(`Error loading Codecov file coverage: ${err}`)
}
sourcegraph.internal.updateContext(context)
}
// Update the context when the configuration, workspace roots or active editors change.
context.subscriptions.add(
combineLatest([
configurationChanges.pipe(
map(settings => settings['codecov.endpoints'])
),
from(
// Backcompat: rely on onDidChangeRoots if the extension host doesn't support rootChanges.
sourcegraph.workspace.rootChanges ||
sourcegraph.workspace.onDidChangeRoots
).pipe(
map(() => sourcegraph.workspace.roots),
startWith(sourcegraph.workspace.roots)
),
editorsChanges,
])
.pipe(
concatMap(async ([endpoints, roots, editors]) => {
try {
await updateContext(endpoints, roots, editors)
} catch (err) {
console.error('Codecov: error updating context', err)
}
})
)
.subscribe()
)
sourcegraph.commands.registerCommand(
'codecov.setupEnterprise',
async () => {
const endpoint = resolveEndpoint(
sourcegraph.configuration
.get<Settings>()
.get('codecov.endpoints')
)
if (!sourcegraph.app.activeWindow) {
throw new Error(
'To set a Codecov Endpoint, navigate to a file and then re-run this command.'
)
}
const service = await sourcegraph.app.activeWindow.showInputBox({
prompt: `Version control type (gh/ghe/bb/gl):`,
value: endpoint.service || '',
})
const url = await sourcegraph.app.activeWindow.showInputBox({
prompt: `Codecov endpoint:`,
value: endpoint.url || '',
})
if (url !== undefined && service !== undefined) {
// TODO: Only supports setting the token of the first API endpoint.
return sourcegraph.configuration
.get<Settings>()
.update('codecov.endpoints', [
{ ...endpoint, url, service },
])
}
}
)
// Handle the "Set Codecov API token" command (show the user a prompt for their token, and save
// their input to settings).
sourcegraph.commands.registerCommand('codecov.setAPIToken', async () => {
const endpoint = resolveEndpoint(
sourcegraph.configuration.get<Settings>().get('codecov.endpoints')
)
if (!sourcegraph.app.activeWindow) {
throw new Error(
'To set a Codecov API token, navigate to a file and then re-run this command.'
)
}
const token = await sourcegraph.app.activeWindow.showInputBox({
prompt: `Codecov API token (for ${endpoint.url}):`,
value: endpoint.token || undefined,
})
if (token !== undefined) {
// TODO: Only supports setting the token of the first API endpoint.
return sourcegraph.configuration
.get<Settings>()
.update('codecov.endpoints', [{ ...endpoint, token }])
}
})
}