Skip to content

Commit b79eb05

Browse files
committed
fix: improve relative file handling
1 parent 33674c0 commit b79eb05

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

src/extension.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,35 @@ import { readFile } from 'fs/promises';
55
import * as yaml from 'js-yaml';
66
import * as vscode from 'vscode';
77

8+
let ranWithError = false;
89
let statusBarItem: vscode.StatusBarItem;
910
let channel: vscode.OutputChannel;
1011
let owner: Owner | undefined = undefined;
1112

13+
function rootWorkspace(): vscode.WorkspaceFolder | undefined {
14+
const count = vscode.workspace.workspaceFolders?.length || 0;
15+
if (count === 1) {
16+
return vscode.workspace.workspaceFolders?.[0];
17+
} else {
18+
log('warning', `Detected ${count} workspaces, but currently requires 1.`);
19+
return undefined;
20+
}
21+
}
22+
1223
function updateStatus(active: vscode.TextEditor | undefined) {
13-
if (active) {
24+
if (ranWithError) {
25+
statusBarItem.text = '$(error) Owner: Error!';
26+
statusBarItem.tooltip = `See "${channel.name}" output channel for details`;
27+
statusBarItem.show();
28+
} else if (active) {
1429
statusBarItem.text = '$(loading~spin) Owner: running...';
30+
statusBarItem.tooltip = undefined;
1531
statusBarItem.show();
1632
setTimeout(() => {
1733
try {
1834
codeownershipValidator(active.document.uri.fsPath).then((result) => {
1935
owner = result;
20-
log('debug', JSON.stringify(owner));
36+
log('debug', `owner: ${JSON.stringify(owner)}`);
2137
if (
2238
active.document.uri.fsPath ===
2339
vscode.window.activeTextEditor?.document.uri.fsPath
@@ -139,14 +155,21 @@ const mockValidator: Validator = (filepath) => {
139155

140156
const codeownershipValidator: Validator = async (filepath) => {
141157
// bin/codeownership currenlty wants relative paths
142-
const relativePath = relative(process.cwd(), filepath);
158+
const cwd = rootWorkspace()?.uri.fsPath;
159+
const relativePath = relative(cwd || process.cwd(), filepath);
143160

144-
const output = runCommand(
145-
process.cwd(),
146-
`bin/codeownership for_file "${relativePath}" --json`,
147-
);
161+
logSpace();
162+
log('debug', `cwd: ${cwd}`);
163+
log('debug', `workspace: ${rootWorkspace()?.uri.fsPath}`);
164+
log('debug', `file path: ${filepath}`);
165+
log('debug', `relative path: ${relativePath}`);
148166

149167
try {
168+
const output = runCommand(
169+
cwd,
170+
`bin/codeownership for_file "${relativePath}" --json`,
171+
);
172+
150173
const obj = JSON.parse(output);
151174

152175
if (typeof obj.team_name !== 'string') {
@@ -162,7 +185,7 @@ const codeownershipValidator: Validator = async (filepath) => {
162185
obj.team_name !== 'Unowned'
163186
) {
164187
const teamConfig = resolve(
165-
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || '',
188+
rootWorkspace()?.uri.fsPath || '',
166189
obj.team_yml,
167190
);
168191

@@ -191,7 +214,9 @@ const codeownershipValidator: Validator = async (filepath) => {
191214
actions,
192215
};
193216
}
217+
ranWithError = false;
194218
} catch {
219+
ranWithError = true;
195220
log('error', 'Error parsing command output');
196221
}
197222
return undefined;
@@ -215,23 +240,37 @@ async function getSlackChannel(
215240
}
216241
}
217242

218-
function runCommand(cwd: string, command: string, stdin?: string): string {
243+
function runCommand(
244+
cwd: string | undefined,
245+
command: string,
246+
stdin?: string,
247+
): string {
219248
let stdout: string = '';
220249
log('info', `command: ${command}`);
221250

222-
stdout = cp
223-
.execSync(command, {
224-
cwd,
225-
input: stdin,
226-
})
227-
.toString()
228-
.trim();
229-
230-
log('info', `stdout: ${stdout}`);
251+
try {
252+
stdout = cp
253+
.execSync(command, {
254+
cwd,
255+
input: stdin,
256+
})
257+
.toString()
258+
.trim();
259+
260+
log('info', `stdout: ${stdout}`);
261+
ranWithError = false;
262+
} catch (ex) {
263+
ranWithError = true;
264+
log('error', ex.message);
265+
}
231266

232267
return stdout;
233268
}
234269

270+
function logSpace() {
271+
channel.appendLine('');
272+
}
273+
235274
function log(level: 'debug' | 'info' | 'warning' | 'error', msg: string) {
236275
channel.appendLine(`[${date()}] [${level}] ${msg}`);
237276
}

0 commit comments

Comments
 (0)