|
1 | 1 | import { relative, resolve, sep } from 'path'; |
2 | 2 | import * as cp from 'child_process'; |
3 | 3 | import { readFile } from 'fs/promises'; |
| 4 | +import { existsSync } from 'fs'; |
4 | 5 |
|
5 | 6 | import * as yaml from 'js-yaml'; |
6 | 7 | import * as vscode from 'vscode'; |
@@ -252,111 +253,122 @@ class StatusProvider implements vscode.Disposable { |
252 | 253 | } |
253 | 254 |
|
254 | 255 | class Worker implements vscode.Disposable { |
| 256 | + private isConfigured: boolean | null = null; |
| 257 | + |
255 | 258 | constructor( |
256 | 259 | private readonly workspace: vscode.WorkspaceFolder, |
257 | 260 | private readonly statusProvider: StatusProvider, |
258 | | - ) { } |
| 261 | + ) { |
| 262 | + this.checkConfiguration(); |
| 263 | + } |
| 264 | + |
| 265 | + private async checkConfiguration(): Promise<void> { |
| 266 | + const binaryPath = resolve(this.workspace.uri.fsPath, 'bin/codeownership'); |
| 267 | + this.isConfigured = existsSync(binaryPath); |
| 268 | + |
| 269 | + if (!this.isConfigured) { |
| 270 | + log('info', `No code ownership binary found in workspace: ${this.workspace.name}`); |
| 271 | + } else { |
| 272 | + log('info', `Code ownership binary found in workspace: ${this.workspace.name}`); |
| 273 | + } |
| 274 | + } |
259 | 275 |
|
260 | 276 | workspaceHas(file: vscode.Uri): boolean { |
261 | 277 | return file.fsPath.startsWith(this.workspace.uri.fsPath); |
262 | 278 | } |
263 | 279 |
|
264 | 280 | async run(file: vscode.Uri): Promise<void> { |
265 | | - if (this.workspaceHas(file)) { |
266 | | - this.statusProvider.status = 'working'; |
267 | | - await new Promise((r) => setTimeout(r, 50)); |
268 | | - |
269 | | - const cwd = this.workspace.uri.fsPath; |
270 | | - const relativePath = relative(cwd, file.fsPath); |
271 | | - |
272 | | - logSpace(); |
273 | | - log('info', `Checking ownership for ${relativePath}`); |
274 | | - log('debug', `cwd: ${cwd}`); |
275 | | - log('debug', `workspace: ${this.workspace.uri.fsPath}`); |
276 | | - log('debug', `file path: ${file.fsPath}`); |
277 | | - |
278 | | - // Check if binary exists |
279 | | - const checkBinary = await runCommand( |
280 | | - cwd, |
281 | | - 'test -f bin/codeownership && echo "exists"', |
282 | | - this.statusProvider |
283 | | - ); |
284 | | - |
285 | | - if (!checkBinary) { |
286 | | - log('info', 'No code ownership binary found in project'); |
| 281 | + if (!this.workspaceHas(file)) return; |
| 282 | + |
| 283 | + if (this.isConfigured === null) { |
| 284 | + await this.checkConfiguration(); |
| 285 | + } |
| 286 | + |
| 287 | + if (!this.isConfigured) { |
| 288 | + this.statusProvider.owner = undefined; |
| 289 | + this.statusProvider.status = 'idle'; |
| 290 | + return; |
| 291 | + } |
| 292 | + |
| 293 | + this.statusProvider.status = 'working'; |
| 294 | + await new Promise((r) => setTimeout(r, 50)); |
| 295 | + |
| 296 | + const cwd = this.workspace.uri.fsPath; |
| 297 | + const relativePath = relative(cwd, file.fsPath); |
| 298 | + |
| 299 | + logSpace(); |
| 300 | + log('info', `Checking ownership for ${relativePath}`); |
| 301 | + log('debug', `cwd: ${cwd}`); |
| 302 | + log('debug', `workspace: ${this.workspace.uri.fsPath}`); |
| 303 | + log('debug', `file path: ${file.fsPath}`); |
| 304 | + |
| 305 | + // Run ownership check |
| 306 | + const output = await runCommand( |
| 307 | + cwd, |
| 308 | + `bin/codeownership for_file "${relativePath}" --json`, |
| 309 | + this.statusProvider, |
| 310 | + ); |
| 311 | + |
| 312 | + if (!output) { |
| 313 | + log('info', 'Code ownership check returned no output'); |
| 314 | + this.statusProvider.owner = undefined; |
| 315 | + this.statusProvider.status = 'idle'; |
| 316 | + return; |
| 317 | + } |
| 318 | + |
| 319 | + try { |
| 320 | + const obj = JSON.parse(output); |
| 321 | + |
| 322 | + if (!obj.team_name) { |
| 323 | + log('info', 'No team name found in ownership data'); |
287 | 324 | this.statusProvider.owner = undefined; |
288 | 325 | this.statusProvider.status = 'idle'; |
289 | 326 | return; |
290 | 327 | } |
291 | 328 |
|
292 | | - // Run ownership check |
293 | | - const output = await runCommand( |
294 | | - cwd, |
295 | | - `bin/codeownership for_file "${relativePath}" --json`, |
296 | | - this.statusProvider, |
297 | | - ); |
298 | | - |
299 | | - if (!output) { |
300 | | - log('info', 'Code ownership check returned no output'); |
| 329 | + if (!obj.team_yml) { |
| 330 | + log('info', 'No team config file found in ownership data'); |
301 | 331 | this.statusProvider.owner = undefined; |
302 | 332 | this.statusProvider.status = 'idle'; |
303 | 333 | return; |
304 | 334 | } |
305 | 335 |
|
306 | | - try { |
307 | | - const obj = JSON.parse(output); |
308 | | - |
309 | | - if (!obj.team_name) { |
310 | | - log('info', 'No team name found in ownership data'); |
311 | | - this.statusProvider.owner = undefined; |
312 | | - this.statusProvider.status = 'idle'; |
313 | | - return; |
314 | | - } |
315 | | - |
316 | | - if (!obj.team_yml) { |
317 | | - log('info', 'No team config file found in ownership data'); |
318 | | - this.statusProvider.owner = undefined; |
319 | | - this.statusProvider.status = 'idle'; |
320 | | - return; |
321 | | - } |
322 | | - |
323 | | - if (obj.team_name === 'Unowned') { |
324 | | - log('info', 'File is explicitly unowned'); |
325 | | - this.statusProvider.owner = undefined; |
326 | | - this.statusProvider.status = 'idle'; |
327 | | - return; |
328 | | - } |
| 336 | + if (obj.team_name === 'Unowned') { |
| 337 | + log('info', 'File is explicitly unowned'); |
| 338 | + this.statusProvider.owner = undefined; |
| 339 | + this.statusProvider.status = 'idle'; |
| 340 | + return; |
| 341 | + } |
329 | 342 |
|
330 | | - const teamConfig = resolve(this.workspace.uri.fsPath, obj.team_yml); |
331 | | - const actions: UserAction[] = []; |
332 | | - |
333 | | - const slackChannel = await getSlackChannel(teamConfig); |
334 | | - if (slackChannel) { |
335 | | - actions.push({ |
336 | | - title: `Slack: #${slackChannel}`, |
337 | | - uri: vscode.Uri.parse( |
338 | | - `https://slack.com/app_redirect?channel=${slackChannel}`, |
339 | | - ), |
340 | | - }); |
341 | | - } |
| 343 | + const teamConfig = resolve(this.workspace.uri.fsPath, obj.team_yml); |
| 344 | + const actions: UserAction[] = []; |
342 | 345 |
|
| 346 | + const slackChannel = await getSlackChannel(teamConfig); |
| 347 | + if (slackChannel) { |
343 | 348 | actions.push({ |
344 | | - title: 'View team config', |
345 | | - uri: vscode.Uri.parse(teamConfig), |
| 349 | + title: `Slack: #${slackChannel}`, |
| 350 | + uri: vscode.Uri.parse( |
| 351 | + `https://slack.com/app_redirect?channel=${slackChannel}`, |
| 352 | + ), |
346 | 353 | }); |
347 | | - |
348 | | - this.statusProvider.owner = { |
349 | | - filepath: file.fsPath, |
350 | | - teamName: obj.team_name, |
351 | | - teamConfig, |
352 | | - actions, |
353 | | - }; |
354 | | - this.statusProvider.status = 'idle'; |
355 | | - } catch (error) { |
356 | | - log('info', `Invalid ownership data format: ${error.message}`); |
357 | | - this.statusProvider.owner = undefined; |
358 | | - this.statusProvider.status = 'idle'; |
359 | 354 | } |
| 355 | + |
| 356 | + actions.push({ |
| 357 | + title: 'View team config', |
| 358 | + uri: vscode.Uri.parse(teamConfig), |
| 359 | + }); |
| 360 | + |
| 361 | + this.statusProvider.owner = { |
| 362 | + filepath: file.fsPath, |
| 363 | + teamName: obj.team_name, |
| 364 | + teamConfig, |
| 365 | + actions, |
| 366 | + }; |
| 367 | + this.statusProvider.status = 'idle'; |
| 368 | + } catch (error) { |
| 369 | + log('info', `Invalid ownership data format: ${error.message}`); |
| 370 | + this.statusProvider.owner = undefined; |
| 371 | + this.statusProvider.status = 'idle'; |
360 | 372 | } |
361 | 373 | } |
362 | 374 |
|
|
0 commit comments