-
Notifications
You must be signed in to change notification settings - Fork 1
Handle cases when a project doesn't use codeownership #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e020ed
Fix 'Maximum call stack size exceeded' during `run` function
technicalpickles b81fc69
first pass at handling. at least it's not an error
technicalpickles 0986bd3
check for binary/configuration much earlier
technicalpickles 09019a5
show when it's not configured vs no owner
technicalpickles 6ab9cae
run prettier
technicalpickles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { relative, resolve, sep } from 'path'; | ||
| import * as cp from 'child_process'; | ||
| import { readFile } from 'fs/promises'; | ||
| import { existsSync } from 'fs'; | ||
|
|
||
| import * as yaml from 'js-yaml'; | ||
| import * as vscode from 'vscode'; | ||
|
|
@@ -9,7 +10,8 @@ let channel: vscode.OutputChannel; | |
|
|
||
| function run(file: string | vscode.Uri | null | undefined) { | ||
| if (!file) { | ||
| run(vscode.window.activeTextEditor?.document.uri); | ||
| if (!vscode.window.activeTextEditor) return; | ||
| run(vscode.window.activeTextEditor.document.uri); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -200,6 +202,7 @@ class StatusProvider implements vscode.Disposable { | |
|
|
||
| private _status: Status = 'idle'; | ||
| private _owner: Owner | undefined = undefined; | ||
| private _isConfigured: boolean | null = null; | ||
|
|
||
| get status(): Status { | ||
| return this._status; | ||
|
|
@@ -217,6 +220,14 @@ class StatusProvider implements vscode.Disposable { | |
| this.update(); | ||
| } | ||
|
|
||
| get isConfigured(): boolean | null { | ||
| return this._isConfigured; | ||
| } | ||
| set isConfigured(value: boolean | null) { | ||
| this._isConfigured = value; | ||
| this.update(); | ||
| } | ||
|
|
||
| private update() { | ||
| if (this.status === 'error') { | ||
| this.statusBarItem.command = 'code-ownership-vscode.showOutputChannel'; | ||
|
|
@@ -237,9 +248,14 @@ class StatusProvider implements vscode.Disposable { | |
| this.statusBarItem.text = `$(account) Owner: ${this.owner.teamName}`; | ||
| this.statusBarItem.tooltip = undefined; | ||
| this.statusBarItem.show(); | ||
| } else if (this.isConfigured === false) { | ||
| this.statusBarItem.text = `$(info) Ownership: not configured`; | ||
| this.statusBarItem.tooltip = | ||
| 'This workspace is not configured for code ownership'; | ||
| this.statusBarItem.show(); | ||
| } else { | ||
| this.statusBarItem.text = `$(warning) Owner: none`; | ||
| this.statusBarItem.tooltip = undefined; | ||
| this.statusBarItem.tooltip = 'This file has no assigned team ownership'; | ||
| this.statusBarItem.show(); | ||
| } | ||
| } | ||
|
|
@@ -251,92 +267,129 @@ class StatusProvider implements vscode.Disposable { | |
| } | ||
|
|
||
| class Worker implements vscode.Disposable { | ||
| private isConfigured: boolean | null = null; | ||
|
|
||
| constructor( | ||
| private readonly workspace: vscode.WorkspaceFolder, | ||
| private readonly statusProvider: StatusProvider, | ||
| ) {} | ||
| ) { | ||
| this.checkConfiguration(); | ||
| } | ||
|
|
||
| private async checkConfiguration(): Promise<void> { | ||
| const binaryPath = resolve(this.workspace.uri.fsPath, 'bin/codeownership'); | ||
| this.isConfigured = existsSync(binaryPath); | ||
| this.statusProvider.isConfigured = this.isConfigured; | ||
|
|
||
| if (!this.isConfigured) { | ||
| log( | ||
| 'info', | ||
| `No code ownership binary found in workspace: ${this.workspace.name}`, | ||
| ); | ||
| } else { | ||
| log( | ||
| 'info', | ||
| `Code ownership binary found in workspace: ${this.workspace.name}`, | ||
| ); | ||
| } | ||
| } | ||
|
Comment on lines
+279
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 |
||
|
|
||
| workspaceHas(file: vscode.Uri): boolean { | ||
| return file.fsPath.startsWith(this.workspace.uri.fsPath); | ||
| } | ||
|
|
||
| async run(file: vscode.Uri): Promise<void> { | ||
| if (this.workspaceHas(file)) { | ||
| this.statusProvider.status = 'working'; | ||
|
|
||
| await new Promise((r) => setTimeout(r, 50)); | ||
|
|
||
| // bin/codeownership currenlty wants relative paths | ||
| const cwd = this.workspace.uri.fsPath; | ||
| const relativePath = relative(cwd, file.fsPath); | ||
|
|
||
| logSpace(); | ||
| log('debug', `cwd: ${cwd}`); | ||
| log('debug', `workspace: ${this.workspace.uri.fsPath}`); | ||
| log('debug', `file path: ${file.fsPath}`); | ||
| log('debug', `relative path: ${relativePath}`); | ||
|
|
||
| try { | ||
| const output = await runCommand( | ||
| cwd, | ||
| `bin/codeownership for_file "${relativePath}" --json`, | ||
| this.statusProvider, | ||
| ); | ||
| if (!this.workspaceHas(file)) return; | ||
|
|
||
| const obj = JSON.parse(output); | ||
| if (this.isConfigured === null) { | ||
| await this.checkConfiguration(); | ||
| } | ||
|
|
||
| if (typeof obj.team_name !== 'string') { | ||
| log( | ||
| 'warning', | ||
| 'Missing expected property `team_name` in command output', | ||
| ); | ||
| } | ||
| if (typeof obj.team_yml !== 'string') { | ||
| log( | ||
| 'warning', | ||
| 'Missing expected property `team_yml` in command output', | ||
| ); | ||
| } | ||
| if (!this.isConfigured) { | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| typeof obj.team_name === 'string' && | ||
| typeof obj.team_yml === 'string' && | ||
| obj.team_name !== 'Unowned' | ||
| ) { | ||
| const teamConfig = resolve(this.workspace.uri.fsPath, obj.team_yml); | ||
|
|
||
| const actions: UserAction[] = []; | ||
|
|
||
| const slackChannel = await getSlackChannel(teamConfig); | ||
|
|
||
| if (slackChannel) { | ||
| actions.push({ | ||
| title: `Slack: #${slackChannel}`, | ||
| uri: vscode.Uri.parse( | ||
| `https://slack.com/app_redirect?channel=${slackChannel}`, | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| actions.push({ | ||
| title: 'View team config', | ||
| uri: vscode.Uri.parse(teamConfig), | ||
| }); | ||
|
|
||
| this.statusProvider.owner = { | ||
| filepath: file.fsPath, | ||
| teamName: obj.team_name, | ||
| teamConfig, | ||
| actions, | ||
| }; | ||
| } else { | ||
| this.statusProvider.owner = undefined; | ||
| } | ||
| this.statusProvider.status = 'working'; | ||
| await new Promise((r) => setTimeout(r, 50)); | ||
|
|
||
| const cwd = this.workspace.uri.fsPath; | ||
| const relativePath = relative(cwd, file.fsPath); | ||
|
|
||
| logSpace(); | ||
| log('info', `Checking ownership for ${relativePath}`); | ||
| log('debug', `cwd: ${cwd}`); | ||
| log('debug', `workspace: ${this.workspace.uri.fsPath}`); | ||
| log('debug', `file path: ${file.fsPath}`); | ||
|
|
||
| // Run ownership check | ||
| const output = await runCommand( | ||
| cwd, | ||
| `bin/codeownership for_file "${relativePath}" --json`, | ||
| this.statusProvider, | ||
| ); | ||
|
|
||
| if (!output) { | ||
| log('info', 'Code ownership check returned no output'); | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const obj = JSON.parse(output); | ||
|
|
||
| if (!obj.team_name) { | ||
| log('info', 'No team name found in ownership data'); | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| } catch { | ||
| this.statusProvider.status = 'error'; | ||
| log('error', 'Error parsing command output'); | ||
| return; | ||
| } | ||
|
|
||
| if (!obj.team_yml) { | ||
| log('info', 'No team config file found in ownership data'); | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| return; | ||
| } | ||
|
|
||
| if (obj.team_name === 'Unowned') { | ||
| log('info', 'File is explicitly unowned'); | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| return; | ||
| } | ||
|
|
||
| const teamConfig = resolve(this.workspace.uri.fsPath, obj.team_yml); | ||
| const actions: UserAction[] = []; | ||
|
|
||
| const slackChannel = await getSlackChannel(teamConfig); | ||
| if (slackChannel) { | ||
| actions.push({ | ||
| title: `Slack: #${slackChannel}`, | ||
| uri: vscode.Uri.parse( | ||
| `https://slack.com/app_redirect?channel=${slackChannel}`, | ||
| ), | ||
| }); | ||
| } | ||
|
|
||
| actions.push({ | ||
| title: 'View team config', | ||
| uri: vscode.Uri.parse(teamConfig), | ||
| }); | ||
|
|
||
| this.statusProvider.owner = { | ||
| filepath: file.fsPath, | ||
| teamName: obj.team_name, | ||
| teamConfig, | ||
| actions, | ||
| }; | ||
| this.statusProvider.status = 'idle'; | ||
| } catch (error) { | ||
| log('info', `Invalid ownership data format: ${error.message}`); | ||
| this.statusProvider.owner = undefined; | ||
| this.statusProvider.status = 'idle'; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier did this on its own 🤷🏻