Skip to content

Commit fa3c5a4

Browse files
committed
feat: show suggested scopes
1 parent 585f61f commit fa3c5a4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/scopes.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,40 @@ export function getScopeMarkdown(id: string): string {
194194
content.push(
195195
`This scope is ${scope.classification.toLowerCase()}. See [OAuth2 Verification](https://support.google.com/cloud/answer/13463073) for more information.`,
196196
);
197+
198+
const suggestions: { id: string; sharedPrefix: string }[] = [];
199+
200+
for (const [altId, alt] of SCOPES) {
201+
if (
202+
alt.classification === ScopeClassification.SENSITIVE ||
203+
alt.classification === ScopeClassification.RESTRICTED
204+
) {
205+
continue;
206+
}
207+
208+
const sharedPrefix = getSharedPrefix(id, altId);
209+
210+
if (sharedPrefix.length < id.length) {
211+
continue;
212+
}
213+
214+
suggestions.push({
215+
id: altId,
216+
sharedPrefix,
217+
});
218+
}
219+
220+
suggestions.sort();
221+
222+
if (suggestions.length > 0) {
223+
content.push("Consider these scopes:");
224+
225+
for (const { id } of suggestions) {
226+
content.push(
227+
`- \`${id.replace("https://www.googleapis.com/auth/", "")}\` ${SCOPES.get(id)?.description ?? ""}`,
228+
);
229+
}
230+
}
197231
}
198232

199233
if (scope.apis.length > 0) {
@@ -208,3 +242,11 @@ export function getScopeMarkdown(id: string): string {
208242

209243
return content.join("\n\n");
210244
}
245+
246+
function getSharedPrefix(a: string, b: string): string {
247+
let i = 0;
248+
while (i < a.length && i < b.length && a[i] === b[i]) {
249+
i++;
250+
}
251+
return a.slice(0, i);
252+
}

0 commit comments

Comments
 (0)