|
| 1 | +// Copyright (c) Fensak, LLC. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later OR BUSL-1.1 |
| 3 | + |
| 4 | +import { Octokit } from "../deps.ts"; |
| 5 | + |
| 6 | +import { |
| 7 | + FensakConfigSource, |
| 8 | + getBitBucketWorkspace, |
| 9 | + getComputedFensakConfig, |
| 10 | + getGitHubOrgRecord, |
| 11 | +} from "../svcdata/mod.ts"; |
| 12 | +import { isOrgManager } from "../ghstd/mod.ts"; |
| 13 | + |
| 14 | +export interface Account { |
| 15 | + source: "github" | "bitbucket"; |
| 16 | + slug: string; |
| 17 | + app_is_installed: boolean; |
| 18 | + dotfensak_ready: boolean; |
| 19 | + subscription_id: string | null; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Filters down the given GitHub Orgs (identified by slug) based on whether the authenticated user of the Octokit client |
| 24 | + * is an admin of the org. |
| 25 | + */ |
| 26 | +export async function filterAllowedGitHubOrgsForAuthenticatedUser( |
| 27 | + octokit: Octokit, |
| 28 | + slugs: string[], |
| 29 | +): Promise<Account[]> { |
| 30 | + const orgData = await Promise.all(slugs.map((sl) => getGitHubOrgRecord(sl))); |
| 31 | + const allowedOrgs = await Promise.all( |
| 32 | + orgData.map(async (od): Promise<Account | null> => { |
| 33 | + if (od.value == null) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const isAllowed = await isOrgManager(octokit, od.value.name); |
| 38 | + if (!isAllowed) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + const maybeCfg = await getComputedFensakConfig( |
| 43 | + FensakConfigSource.GitHub, |
| 44 | + od.value.name, |
| 45 | + ); |
| 46 | + |
| 47 | + return { |
| 48 | + source: "github", |
| 49 | + slug: od.value.name, |
| 50 | + app_is_installed: od.value.installationID != null, |
| 51 | + dotfensak_ready: maybeCfg != null, |
| 52 | + subscription_id: od.value.subscriptionID, |
| 53 | + }; |
| 54 | + }), |
| 55 | + ); |
| 56 | + const out: Account[] = []; |
| 57 | + for (const o of allowedOrgs) { |
| 58 | + if (!o) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + out.push(o); |
| 62 | + } |
| 63 | + return out; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Filters down the given BitBucket Workspaces (identified by slug) based on whether the authenticated user |
| 68 | + * is an admin of the workspace. |
| 69 | + */ |
| 70 | +export async function filterAllowedBitBucketWorkspacesForAuthenticatedUser( |
| 71 | + token: string, |
| 72 | + slugs: string[], |
| 73 | +): Promise<Account[]> { |
| 74 | + const wsLookup = await getWorkspacePermissionLookup(token); |
| 75 | + const wsData = await Promise.all( |
| 76 | + slugs.map((sl) => getBitBucketWorkspace(sl)), |
| 77 | + ); |
| 78 | + const allowedWS = await Promise.all( |
| 79 | + wsData.map(async (ws): Promise<Account | null> => { |
| 80 | + if (ws.value == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + const isAllowed = wsLookup[ws.value.name] === "owner"; |
| 85 | + if (!isAllowed) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + const maybeCfg = await getComputedFensakConfig( |
| 90 | + FensakConfigSource.BitBucket, |
| 91 | + ws.value.name, |
| 92 | + ); |
| 93 | + |
| 94 | + return { |
| 95 | + source: "bitbucket", |
| 96 | + slug: ws.value.name, |
| 97 | + app_is_installed: ws.value.securityContext != null, |
| 98 | + dotfensak_ready: maybeCfg != null, |
| 99 | + subscription_id: ws.value.subscriptionID, |
| 100 | + }; |
| 101 | + }), |
| 102 | + ); |
| 103 | + const out: Account[] = []; |
| 104 | + for (const w of allowedWS) { |
| 105 | + if (!w) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + out.push(w); |
| 109 | + } |
| 110 | + return out; |
| 111 | +} |
| 112 | + |
| 113 | +export async function getWorkspacePermissionLookup( |
| 114 | + token: string, |
| 115 | +): Promise<Record<string, "owner" | "member">> { |
| 116 | + const wsUPath = "/user/permissions/workspaces"; |
| 117 | + const wsResp = await fetch( |
| 118 | + `https://api.bitbucket.org/2.0${wsUPath}`, |
| 119 | + { |
| 120 | + method: "GET", |
| 121 | + headers: { |
| 122 | + Authorization: `Bearer ${token}`, |
| 123 | + Accept: "application/json", |
| 124 | + }, |
| 125 | + }, |
| 126 | + ); |
| 127 | + if (!wsResp.ok) { |
| 128 | + const rtext = await wsResp.text(); |
| 129 | + throw new Error( |
| 130 | + `BitBucket API Error for url path ${wsUPath} (${wsResp.status}): ${rtext}`, |
| 131 | + ); |
| 132 | + } |
| 133 | + const data = await wsResp.json(); |
| 134 | + |
| 135 | + const wsPermissionLookup: Record<string, "owner" | "member"> = {}; |
| 136 | + for (const wm of data.values) { |
| 137 | + wsPermissionLookup[wm.workspace.slug] = wm.permission as "owner" | "member"; |
| 138 | + } |
| 139 | + return wsPermissionLookup; |
| 140 | +} |
0 commit comments