Skip to content

Commit 844bae6

Browse files
jpoehneltCopilot
andauthored
feat: suggest scopes (#10)
* fix: additional sensitive/restricted scopes * feat: show suggested scopes * chore: add changeset * fix: use comparator Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 0be2843 commit 844bae6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

.changeset/eager-colts-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"google-workspace-developer-tools": minor
3+
---
4+
5+
Show alternative nonsensitive scopes.

src/scopes.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ const SENSITIVE_SCOPES = [
140140
"https://www.googleapis.com/auth/chat.app.spaces.create",
141141
"https://www.googleapis.com/auth/chat.customemojis",
142142
"https://www.googleapis.com/auth/chat.customemojis.readonly",
143+
"https://www.googleapis.com/auth/documents",
144+
"https://www.googleapis.com/auth/documents.readonly",
143145
"https://www.googleapis.com/auth/chat.memberships",
144146
"https://www.googleapis.com/auth/chat.memberships.app",
145147
"https://www.googleapis.com/auth/chat.memberships.readonly",
@@ -192,6 +194,40 @@ export function getScopeMarkdown(id: string): string {
192194
content.push(
193195
`This scope is ${scope.classification.toLowerCase()}. See [OAuth2 Verification](https://support.google.com/cloud/answer/13463073) for more information.`,
194196
);
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((a, b) => b.sharedPrefix.length - a.sharedPrefix.length);
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+
}
195231
}
196232

197233
if (scope.apis.length > 0) {
@@ -206,3 +242,11 @@ export function getScopeMarkdown(id: string): string {
206242

207243
return content.join("\n\n");
208244
}
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)