Skip to content

Commit 588b5d4

Browse files
committed
refactor: emit actions from validator strategy
1 parent 6f6a9a2 commit 588b5d4

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/extension.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,16 @@ export function activate(context: vscode.ExtensionContext) {
5757
() => {
5858
if (owner) {
5959
const filename = owner.filepath.split(sep).slice(-1)[0];
60-
const { teamConfig, slack } = owner;
61-
62-
const items: { title: string; action: () => void }[] = [];
63-
64-
if (slack) {
65-
items.push({
66-
title: `Slack: #${slack}`,
67-
action() {
68-
const uri = vscode.Uri.parse(
69-
`https://slack.com/app_redirect?channel=${slack}`,
70-
);
71-
vscode.commands.executeCommand('vscode.open', uri);
72-
},
73-
});
74-
}
75-
76-
items.push({
77-
title: 'View team config',
78-
action() {
79-
const uri = vscode.Uri.parse(teamConfig);
80-
vscode.commands.executeCommand('vscode.open', uri);
81-
},
82-
});
8360

8461
vscode.window
8562
.showInformationMessage(
8663
`${filename} is owned by ${owner.teamName}`,
87-
...items,
64+
...owner.actions.map((action) => ({
65+
title: action.title,
66+
action() {
67+
vscode.commands.executeCommand('vscode.open', action.uri);
68+
},
69+
})),
8870
)
8971
.then((x) => x?.action());
9072
}
@@ -119,7 +101,12 @@ type Owner = {
119101
filepath: string;
120102
teamName: string;
121103
teamConfig: string;
122-
slack?: string;
104+
actions: UserAction[];
105+
};
106+
107+
type UserAction = {
108+
title: string;
109+
uri: vscode.Uri;
123110
};
124111

125112
type Validator = (filepath: string) => Promise<Owner | undefined>;
@@ -144,6 +131,7 @@ const mockValidator: Validator = (filepath) => {
144131
filepath,
145132
teamName: `Some Team`,
146133
teamConfig: filepath,
134+
actions: [],
147135
});
148136
}, ms),
149137
);
@@ -178,11 +166,29 @@ const codeownershipValidator: Validator = async (filepath) => {
178166
obj.team_yml,
179167
);
180168

169+
const actions: UserAction[] = [];
170+
171+
const slackChannel = await getSlackChannel(teamConfig);
172+
173+
if (slackChannel) {
174+
actions.push({
175+
title: `Slack: #${slackChannel}`,
176+
uri: vscode.Uri.parse(
177+
`https://slack.com/app_redirect?channel=${slackChannel}`,
178+
),
179+
});
180+
}
181+
182+
actions.push({
183+
title: 'View team config',
184+
uri: vscode.Uri.parse(teamConfig),
185+
});
186+
181187
return {
182188
filepath,
183189
teamName: obj.team_name,
184190
teamConfig,
185-
slack: await getSlackHandle(teamConfig),
191+
actions,
186192
};
187193
}
188194
} catch {
@@ -191,7 +197,9 @@ const codeownershipValidator: Validator = async (filepath) => {
191197
return undefined;
192198
};
193199

194-
async function getSlackHandle(teamConfig: string): Promise<string | undefined> {
200+
async function getSlackChannel(
201+
teamConfig: string,
202+
): Promise<string | undefined> {
195203
try {
196204
const text = (await readFile(teamConfig)).toString();
197205
const config = yaml.load(text) as any;

0 commit comments

Comments
 (0)