Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ View code ownership for every file right in the status bar. You'll get the name
Quick access to the owning team's config file. Clicking on the status bar item will open a popup that includes a button that opens the team's config file. See [Code Teams](https://github.com/rubyatscale/code_teams) for more information on team config files.

## Installation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Contributor Author

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 🤷🏻

[Install from Marketplace](https://marketplace.visualstudio.com/items?itemName=Gusto.code-ownership-vscode)

## Requirements
Expand Down
201 changes: 127 additions & 74 deletions src/extension.ts
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';
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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();
}
}
Expand All @@ -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

Choose a reason for hiding this comment

The 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';
}
}

Expand Down
Loading