Skip to content

Commit

Permalink
fix: displayed orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Nov 27, 2024
1 parent c2c587a commit 262c3a0
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions static/scripts/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createClient, SupabaseClient, Session } from "@supabase/supabase-js";
import { Octokit } from "@octokit/rest";
import { GitHubUser } from "../types/github";
import { CONFIG_ORG_REPO } from "@ubiquity-os/plugin-sdk/constants";

declare const SUPABASE_URL: string;
declare const SUPABASE_ANON_KEY: string;
Expand Down Expand Up @@ -131,9 +132,28 @@ export class AuthService {
}

public async getGitHubUserOrgs(): Promise<string[]> {
const octokit = await this.getOctokit();
const response = await octokit.rest.orgs.listForAuthenticatedUser();
return response.data.map((org: { login: string }) => org.login);
const user = await this.octokit?.rest.users.getAuthenticated();
const listForAuthUser = await this.octokit?.rest.orgs.listForAuthenticatedUser();
if (!user || !listForAuthUser) return [];
const listForUserPublic = await this.octokit?.rest.orgs.listForUser({ username: user?.data.login });
const allOrgs = [...(listForAuthUser?.data || []), ...(listForUserPublic?.data || [])].map((org) => org.login);

const orgConfigPermissions: Record<string, string> = {};

const getOrgConfigRepoUserPermissions = async (org: string) => {
try {
const p = await this.octokit?.rest.repos.getCollaboratorPermissionLevel({ owner: org, repo: CONFIG_ORG_REPO, username: user?.data.login });
orgConfigPermissions[org] = p?.data.permission || "none";
} catch (er) {
console.error(`[getOrgConfigPermissions] - ${org}::`, er);
}
};

for (const org of allOrgs) {
await getOrgConfigRepoUserPermissions(org);
}

return Object.keys(orgConfigPermissions).filter((org) => orgConfigPermissions[org] === "admin" || orgConfigPermissions[org] === "write");
}

public async getOctokit(): Promise<Octokit> {
Expand Down

0 comments on commit 262c3a0

Please sign in to comment.