This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
extension.ts
184 lines (168 loc) · 7.11 KB
/
extension.ts
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
import * as sourcegraph from 'sourcegraph'
import { codecovToDecorations } from './decoration'
import {
getCommitCoverageRatio,
getFileCoverageRatios,
getFileLineCoverage,
} from './model'
import { 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(): void {
function activeEditors(): sourcegraph.CodeEditor[] {
return 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(editors = activeEditors()): Promise<void> {
const settings = resolveSettings(
sourcegraph.configuration.get<Settings>().value
)
try {
for (const editor of editors) {
const decorations = await getFileLineCoverage(
resolveDocumentURI(editor.document.uri),
settings['codecov.endpoints'][0],
sourcegraph
)
editor.setDecorations(
decorationType,
codecovToDecorations(settings, decorations)
)
}
} catch (err) {
console.error('Decoration error:', err)
}
}
sourcegraph.configuration.subscribe(() => decorate())
// TODO(sqs): Add a way to get notified when a new editor is opened (because we want to be able to pass an `editor` to `updateDecorations`/`updateContext`, but this subscription just gives us a `doc`).
sourcegraph.workspace.onDidOpenTextDocument.subscribe(() => decorate())
// 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(editors = activeEditors()): Promise<void> {
// Get the current repository. Sourcegraph 3.0-preview exposes sourcegraph.workspace.roots, but earlier
// versions do not.
let uri: string
if (
sourcegraph.workspace.roots &&
sourcegraph.workspace.roots.length > 0
) {
uri = sourcegraph.workspace.roots[0].uri.toString()
} else if (editors.length > 0) {
uri = editors[0].document.uri
} else {
return
}
const lastURI = resolveRootURI(uri)
const endpoint = resolveEndpoint(
sourcegraph.configuration.get<Settings>().get('codecov.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)
}
sourcegraph.configuration.subscribe(() => updateContext())
sourcegraph.workspace.onDidOpenTextDocument.subscribe(() => updateContext())
if (sourcegraph.workspace.onDidChangeRoots) {
sourcegraph.workspace.onDidChangeRoots.subscribe(() => updateContext())
}
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 }])
}
})
}