The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as developerconnect from "@distilled.cloud/gcp/developerconnect_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | compact, |
| 13 | fieldMask, |
| 14 | fingerprint, |
| 15 | listAtLocation, |
| 16 | listLabeledPages, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | sameBool, |
| 23 | toPhysicalId, |
| 24 | userAnnotations, |
| 25 | userLabels, |
| 26 | waitForOperation, |
| 27 | waitUntilExists, |
| 28 | waitUntilGone, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | export type ServiceDirectoryConfig = { |
| 32 | /** |
| 33 | * Service Directory service used to reach a private host, as |
| 34 | * `projects/{project}/locations/{location}/namespaces/{namespace}/services/{service}`. |
| 35 | */ |
| 36 | service?: string; |
| 37 | }; |
| 38 | |
| 39 | export type UserCredential = { |
| 40 | /** |
| 41 | * Secret Manager resource containing the user token, as |
| 42 | * `projects/{project}/secrets/{secret}/versions/{version}`. |
| 43 | */ |
| 44 | userTokenSecretVersion?: string; |
| 45 | }; |
| 46 | |
| 47 | export type OAuthCredential = { |
| 48 | /** |
| 49 | * Secret Manager resource containing the OAuth token that authorizes |
| 50 | * the connection. |
| 51 | */ |
| 52 | oauthTokenSecretVersion?: string; |
| 53 | }; |
| 54 | |
| 55 | export type GitHubConfig = { |
| 56 | /** |
| 57 | * GitHub App that was installed (`DEVELOPER_CONNECT`, `FIREBASE`, |
| 58 | * `GEMINI_CODE_ASSIST`, `DATAFORM`). Immutable — changing it |
| 59 | * replaces the connection. |
| 60 | * @default "DEVELOPER_CONNECT" |
| 61 | */ |
| 62 | githubApp?: developerconnect.GitHubConfigGithubAppEnum | (string & {}); |
| 63 | /** |
| 64 | * OAuth credential of the account that authorized the GitHub App. |
| 65 | */ |
| 66 | authorizerCredential?: OAuthCredential; |
| 67 | /** |
| 68 | * GitHub App installation id. |
| 69 | */ |
| 70 | appInstallationId?: string; |
| 71 | }; |
| 72 | |
| 73 | export type GitHubEnterpriseConfig = { |
| 74 | /** |
| 75 | * URI of the GitHub Enterprise host. Immutable — changing it |
| 76 | * replaces the connection. |
| 77 | */ |
| 78 | hostUri?: string; |
| 79 | /** |
| 80 | * Installation id of the GitHub App. |
| 81 | */ |
| 82 | appInstallationId?: string; |
| 83 | /** |
| 84 | * SSL certificate used for requests to GitHub Enterprise. |
| 85 | */ |
| 86 | sslCaCertificate?: string; |
| 87 | /** |
| 88 | * GitHub Enterprise organization in which the GitHub App is created. |
| 89 | * Immutable. |
| 90 | */ |
| 91 | organization?: string; |
| 92 | /** |
| 93 | * Secret Manager resource containing the GitHub App private key. |
| 94 | */ |
| 95 | privateKeySecretVersion?: string; |
| 96 | /** |
| 97 | * Id of the GitHub App created from the manifest. |
| 98 | */ |
| 99 | appId?: string; |
| 100 | /** |
| 101 | * Secret Manager resource containing the webhook secret. |
| 102 | */ |
| 103 | webhookSecretSecretVersion?: string; |
| 104 | /** |
| 105 | * Service Directory config for a private GitHub Enterprise host. |
| 106 | */ |
| 107 | serviceDirectoryConfig?: ServiceDirectoryConfig; |
| 108 | }; |
| 109 | |
| 110 | export type GitLabConfig = { |
| 111 | /** |
| 112 | * GitLab personal access token with `api` scope. |
| 113 | */ |
| 114 | authorizerCredential?: UserCredential; |
| 115 | /** |
| 116 | * Secret Manager resource containing the webhook secret. Immutable — |
| 117 | * changing it replaces the connection. |
| 118 | */ |
| 119 | webhookSecretSecretVersion?: string; |
| 120 | /** |
| 121 | * GitLab personal access token with `read_api` scope. |
| 122 | */ |
| 123 | readAuthorizerCredential?: UserCredential; |
| 124 | }; |
| 125 | |
| 126 | export type GitLabEnterpriseConfig = { |
| 127 | /** |
| 128 | * URI of the GitLab Enterprise host. Immutable — changing it |
| 129 | * replaces the connection. |
| 130 | */ |
| 131 | hostUri?: string; |
| 132 | /** |
| 133 | * Secret Manager resource containing the webhook secret. Immutable — |
| 134 | * changing it replaces the connection. |
| 135 | */ |
| 136 | webhookSecretSecretVersion?: string; |
| 137 | /** |
| 138 | * GitLab personal access token with `read_api` scope. |
| 139 | */ |
| 140 | readAuthorizerCredential?: UserCredential; |
| 141 | /** |
| 142 | * GitLab personal access token with `api` scope. |
| 143 | */ |
| 144 | authorizerCredential?: UserCredential; |
| 145 | /** |
| 146 | * Service Directory config for a private GitLab Enterprise host. |
| 147 | */ |
| 148 | serviceDirectoryConfig?: ServiceDirectoryConfig; |
| 149 | /** |
| 150 | * SSL certificate used for requests to GitLab Enterprise. |
| 151 | */ |
| 152 | sslCaCertificate?: string; |
| 153 | }; |
| 154 | |
| 155 | export type BitbucketCloudConfig = { |
| 156 | /** |
| 157 | * Bitbucket Cloud workspace id. Immutable — changing it replaces the |
| 158 | * connection. |
| 159 | */ |
| 160 | workspace?: string; |
| 161 | /** |
| 162 | * Secret Manager resource containing the webhook secret. Immutable — |
| 163 | * changing it replaces the connection. |
| 164 | */ |
| 165 | webhookSecretSecretVersion?: string; |
| 166 | /** |
| 167 | * Access token with `repository` access. |
| 168 | */ |
| 169 | readAuthorizerCredential?: UserCredential; |
| 170 | /** |
| 171 | * Access token with `webhook`, `repository`, and `pullrequest` scope. |
| 172 | */ |
| 173 | authorizerCredential?: UserCredential; |
| 174 | }; |
| 175 | |
| 176 | export type BitbucketDataCenterConfig = { |
| 177 | /** |
| 178 | * URI of the Bitbucket Data Center host. Immutable — changing it |
| 179 | * replaces the connection. |
| 180 | */ |
| 181 | hostUri?: string; |
| 182 | /** |
| 183 | * Secret Manager resource containing the webhook secret. Immutable — |
| 184 | * changing it replaces the connection. |
| 185 | */ |
| 186 | webhookSecretSecretVersion?: string; |
| 187 | /** |
| 188 | * HTTP access token with `Repository read` access. |
| 189 | */ |
| 190 | readAuthorizerCredential?: UserCredential; |
| 191 | /** |
| 192 | * HTTP access token with `Repository admin` access. |
| 193 | */ |
| 194 | authorizerCredential?: UserCredential; |
| 195 | /** |
| 196 | * Service Directory config for a private Bitbucket Data Center host. |
| 197 | */ |
| 198 | serviceDirectoryConfig?: ServiceDirectoryConfig; |
| 199 | /** |
| 200 | * SSL certificate used for requests to Bitbucket Data Center. |
| 201 | */ |
| 202 | sslCaCertificate?: string; |
| 203 | }; |
| 204 | |
| 205 | export type SecureSourceManagerInstanceConfig = { |
| 206 | /** |
| 207 | * Secure Source Manager instance, as |
| 208 | * `projects/{project}/locations/{location}/instances/{instance}`. |
| 209 | * Immutable — changing it replaces the connection. |
| 210 | */ |
| 211 | instance?: string; |
| 212 | }; |
| 213 | |
| 214 | export type GitProxyConfig = { |
| 215 | /** |
| 216 | * Enable the git proxy for repositories linked on this connection. |
| 217 | * @default false |
| 218 | */ |
| 219 | enabled?: boolean; |
| 220 | }; |
| 221 | |
| 222 | export type CryptoKeyConfig = { |
| 223 | /** |
| 224 | * Cloud KMS key used to encrypt customer data, as |
| 225 | * `projects/{project}/locations/{location}/keyRings/{ring}/cryptoKeys/{key}`. |
| 226 | */ |
| 227 | keyReference?: string; |
| 228 | }; |
| 229 | |
| 230 | export type InstallationState = { |
| 231 | /** Current step of the installation process. */ |
| 232 | stage?: string; |
| 233 | /** Message describing the next user action, if any. */ |
| 234 | message?: string; |
| 235 | /** Link for the next user action, if any. */ |
| 236 | actionUri?: string; |
| 237 | }; |
| 238 | |
| 239 | export type ConnectionProps = { |
| 240 | /** |
| 241 | * Connection id (the `{connection}` segment of |
| 242 | * `projects/{project}/locations/{location}/connections/{connection}`). |
| 243 | * If omitted, a unique RFC1035 name is generated. Immutable — |
| 244 | * changing it replaces the connection. |
| 245 | */ |
| 246 | connectionId?: string; |
| 247 | /** |
| 248 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 249 | * replaces the connection. `US-CENTRAL1` is accepted and normalized |
| 250 | * to `us-central1`. |
| 251 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 252 | * region, else `us-central1`) |
| 253 | */ |
| 254 | location?: string; |
| 255 | /** |
| 256 | * When true, repository APIs and webhook processing for this |
| 257 | * connection are disabled. |
| 258 | * @default false |
| 259 | */ |
| 260 | disabled?: boolean; |
| 261 | /** |
| 262 | * User labels. Alchemy ownership labels are merged in automatically. |
| 263 | */ |
| 264 | labels?: Record<string, string>; |
| 265 | /** |
| 266 | * User annotations (AIP-148). |
| 267 | */ |
| 268 | annotations?: Record<string, string>; |
| 269 | /** |
| 270 | * Configuration for github.com. If no SCM config is set, Alchemy |
| 271 | * defaults to `githubApp: "DEVELOPER_CONNECT"` on create. |
| 272 | */ |
| 273 | githubConfig?: GitHubConfig; |
| 274 | /** |
| 275 | * Configuration for a GitHub Enterprise host. Mutually exclusive |
| 276 | * with the other SCM configs. |
| 277 | */ |
| 278 | githubEnterpriseConfig?: GitHubEnterpriseConfig; |
| 279 | /** |
| 280 | * Configuration for gitlab.com. Mutually exclusive with the other |
| 281 | * SCM configs. |
| 282 | */ |
| 283 | gitlabConfig?: GitLabConfig; |
| 284 | /** |
| 285 | * Configuration for GitLab Enterprise. Mutually exclusive with the |
| 286 | * other SCM configs. |
| 287 | */ |
| 288 | gitlabEnterpriseConfig?: GitLabEnterpriseConfig; |
| 289 | /** |
| 290 | * Configuration for Bitbucket Cloud. Mutually exclusive with the |
| 291 | * other SCM configs. |
| 292 | */ |
| 293 | bitbucketCloudConfig?: BitbucketCloudConfig; |
| 294 | /** |
| 295 | * Configuration for Bitbucket Data Center. Mutually exclusive with |
| 296 | * the other SCM configs. |
| 297 | */ |
| 298 | bitbucketDataCenterConfig?: BitbucketDataCenterConfig; |
| 299 | /** |
| 300 | * Configuration for a Secure Source Manager instance. Mutually |
| 301 | * exclusive with the other SCM configs. |
| 302 | */ |
| 303 | secureSourceManagerInstanceConfig?: SecureSourceManagerInstanceConfig; |
| 304 | /** |
| 305 | * Git proxy configuration. |
| 306 | */ |
| 307 | gitProxyConfig?: GitProxyConfig; |
| 308 | /** |
| 309 | * Customer-managed encryption. |
| 310 | */ |
| 311 | cryptoKeyConfig?: CryptoKeyConfig; |
| 312 | }; |
| 313 | |
| 314 | export type Connection = Resource< |
| 315 | "GCP.DeveloperConnect.Connection", |
| 316 | ConnectionProps, |
| 317 | { |
| 318 | /** Full resource name. */ |
| 319 | name: string; |
| 320 | /** Connection id (last path segment). */ |
| 321 | connectionId: string; |
| 322 | /** Project id. */ |
| 323 | project: string; |
| 324 | /** Location id. */ |
| 325 | location: string; |
| 326 | /** Whether repository APIs and webhooks are disabled. */ |
| 327 | disabled: boolean; |
| 328 | /** User labels (Alchemy ownership labels stripped). */ |
| 329 | labels: Record<string, string>; |
| 330 | /** User annotations. */ |
| 331 | annotations: Record<string, string>; |
| 332 | /** github.com configuration, if this is a GitHub connection. */ |
| 333 | githubConfig: GitHubConfig | undefined; |
| 334 | /** GitHub Enterprise configuration, if any. */ |
| 335 | githubEnterpriseConfig: GitHubEnterpriseConfig | undefined; |
| 336 | /** GitLab configuration, if any. */ |
| 337 | gitlabConfig: GitLabConfig | undefined; |
| 338 | /** GitLab Enterprise configuration, if any. */ |
| 339 | gitlabEnterpriseConfig: GitLabEnterpriseConfig | undefined; |
| 340 | /** Bitbucket Cloud configuration, if any. */ |
| 341 | bitbucketCloudConfig: BitbucketCloudConfig | undefined; |
| 342 | /** Bitbucket Data Center configuration, if any. */ |
| 343 | bitbucketDataCenterConfig: BitbucketDataCenterConfig | undefined; |
| 344 | /** Secure Source Manager configuration, if any. */ |
| 345 | secureSourceManagerInstanceConfig: SecureSourceManagerInstanceConfig | undefined; |
| 346 | /** Git proxy configuration. */ |
| 347 | gitProxyConfig: GitProxyConfig | undefined; |
| 348 | /** Customer-managed encryption, if any. */ |
| 349 | cryptoKeyConfig: CryptoKeyConfig | undefined; |
| 350 | /** Installation progress for GitHub-based connections. */ |
| 351 | installationState: InstallationState | undefined; |
| 352 | /** True while Developer Connect is applying the connection. */ |
| 353 | reconciling: boolean; |
| 354 | /** Server checksum of the resource. */ |
| 355 | etag: string | undefined; |
| 356 | /** Server-generated resource uid. */ |
| 357 | uid: string | undefined; |
| 358 | /** RFC3339 creation timestamp. */ |
| 359 | createTime: string | undefined; |
| 360 | /** RFC3339 last-update timestamp. */ |
| 361 | updateTime: string | undefined; |
| 362 | }, |
| 363 | never, |
| 364 | Providers |
| 365 | >; |
| 366 | |
| 367 | /** |
| 368 | * A Developer Connect connection to a source-control host (GitHub, |
| 369 | * GitHub Enterprise, GitLab, Bitbucket, or Secure Source Manager). |
| 370 | * |
| 371 | * Changing `connectionId`, `location`, SCM kind, `githubApp`, or |
| 372 | * immutable host fields (`hostUri`, `workspace`, |
| 373 | * `webhookSecretSecretVersion`, SSM `instance`) replaces the |
| 374 | * connection. `disabled`, labels, annotations, credentials, and |
| 375 | * `gitProxyConfig` update in place. |
| 376 | * |
| 377 | * Developer Connect stores OAuth tokens in Secret Manager as its service |
| 378 | * agent, so the project must grant that agent Secret Manager Admin once |
| 379 | * (the console does this for you; the API does not). Without it, create |
| 380 | * fails with `Permission 'secretmanager.secrets.create' denied`: |
| 381 | * |
| 382 | * ```sh |
| 383 | * gcloud beta services identity create --service=developerconnect.googleapis.com --project=PROJECT_ID |
| 384 | * gcloud projects add-iam-policy-binding PROJECT_ID \ |
| 385 | * --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-devconnect.iam.gserviceaccount.com \ |
| 386 | * --role=roles/secretmanager.admin |
| 387 | * ``` |
| 388 | * |
| 389 | * ### Creating a Connection |
| 390 | * **Example:** Generated GitHub connection |
| 391 | * ```typescript |
| 392 | * const github = yield* GCP.DeveloperConnect.Connection("Github", { |
| 393 | * githubConfig: { githubApp: "DEVELOPER_CONNECT" }, |
| 394 | * }); |
| 395 | * ``` |
| 396 | * |
| 397 | * **Example:** Named GitHub connection with labels |
| 398 | * ```typescript |
| 399 | * const github = yield* GCP.DeveloperConnect.Connection("Github", { |
| 400 | * connectionId: "app-github", |
| 401 | * location: "us-central1", |
| 402 | * labels: { env: "prod" }, |
| 403 | * githubConfig: { githubApp: "DEVELOPER_CONNECT" }, |
| 404 | * }); |
| 405 | * ``` |
| 406 | * |
| 407 | * ### Disabling a Connection |
| 408 | * **Example:** Disable webhook processing |
| 409 | * ```typescript |
| 410 | * const github = yield* GCP.DeveloperConnect.Connection("Github", { |
| 411 | * connectionId: "app-github", |
| 412 | * githubConfig: { githubApp: "DEVELOPER_CONNECT" }, |
| 413 | * disabled: true, |
| 414 | * }); |
| 415 | * ``` |
| 416 | * |
| 417 | * @resource |
| 418 | * @category DeveloperConnect |
| 419 | */ |
| 420 | export const Connection = Resource<Connection>("GCP.DeveloperConnect.Connection"); |
| 421 | |
| 422 | type ScmKind = |
| 423 | | "github" |
| 424 | | "githubEnterprise" |
| 425 | | "gitlab" |
| 426 | | "gitlabEnterprise" |
| 427 | | "bitbucketCloud" |
| 428 | | "bitbucketDataCenter" |
| 429 | | "secureSourceManager"; |
| 430 | |
| 431 | const resourceName = (project: string, location: string, connectionId: string) => |
| 432 | `projects/${project}/locations/${location}/connections/${connectionId}`; |
| 433 | |
| 434 | const toUserCredential = ( |
| 435 | credential: UserCredential | undefined, |
| 436 | ): developerconnect.UserCredential | undefined => { |
| 437 | if (credential === undefined) return undefined; |
| 438 | return compact({ userTokenSecretVersion: credential.userTokenSecretVersion }); |
| 439 | }; |
| 440 | |
| 441 | const toOAuthCredential = ( |
| 442 | credential: OAuthCredential | undefined, |
| 443 | ): developerconnect.OAuthCredential | undefined => { |
| 444 | if (credential === undefined) return undefined; |
| 445 | return compact({ |
| 446 | oauthTokenSecretVersion: credential.oauthTokenSecretVersion, |
| 447 | }); |
| 448 | }; |
| 449 | |
| 450 | const toServiceDirectory = ( |
| 451 | config: ServiceDirectoryConfig | undefined, |
| 452 | ): developerconnect.ServiceDirectoryConfig | undefined => { |
| 453 | if (config === undefined) return undefined; |
| 454 | return compact({ service: config.service }); |
| 455 | }; |
| 456 | |
| 457 | const toGithub = (config: GitHubConfig | undefined): developerconnect.GitHubConfig | undefined => { |
| 458 | if (config === undefined) return undefined; |
| 459 | return compact({ |
| 460 | githubApp: config.githubApp ?? "DEVELOPER_CONNECT", |
| 461 | authorizerCredential: toOAuthCredential(config.authorizerCredential), |
| 462 | appInstallationId: config.appInstallationId, |
| 463 | }); |
| 464 | }; |
| 465 | |
| 466 | const toGithubEnterprise = ( |
| 467 | config: GitHubEnterpriseConfig | undefined, |
| 468 | ): developerconnect.GitHubEnterpriseConfig | undefined => { |
| 469 | if (config === undefined) return undefined; |
| 470 | return compact({ |
| 471 | hostUri: config.hostUri, |
| 472 | appInstallationId: config.appInstallationId, |
| 473 | sslCaCertificate: config.sslCaCertificate, |
| 474 | organization: config.organization, |
| 475 | privateKeySecretVersion: config.privateKeySecretVersion, |
| 476 | appId: config.appId, |
| 477 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 478 | serviceDirectoryConfig: toServiceDirectory(config.serviceDirectoryConfig), |
| 479 | }); |
| 480 | }; |
| 481 | |
| 482 | const toGitlab = (config: GitLabConfig | undefined): developerconnect.GitLabConfig | undefined => { |
| 483 | if (config === undefined) return undefined; |
| 484 | return compact({ |
| 485 | authorizerCredential: toUserCredential(config.authorizerCredential), |
| 486 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 487 | readAuthorizerCredential: toUserCredential(config.readAuthorizerCredential), |
| 488 | }); |
| 489 | }; |
| 490 | |
| 491 | const toGitlabEnterprise = ( |
| 492 | config: GitLabEnterpriseConfig | undefined, |
| 493 | ): developerconnect.GitLabEnterpriseConfig | undefined => { |
| 494 | if (config === undefined) return undefined; |
| 495 | return compact({ |
| 496 | hostUri: config.hostUri, |
| 497 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 498 | readAuthorizerCredential: toUserCredential(config.readAuthorizerCredential), |
| 499 | authorizerCredential: toUserCredential(config.authorizerCredential), |
| 500 | serviceDirectoryConfig: toServiceDirectory(config.serviceDirectoryConfig), |
| 501 | sslCaCertificate: config.sslCaCertificate, |
| 502 | }); |
| 503 | }; |
| 504 | |
| 505 | const toBitbucketCloud = ( |
| 506 | config: BitbucketCloudConfig | undefined, |
| 507 | ): developerconnect.BitbucketCloudConfig | undefined => { |
| 508 | if (config === undefined) return undefined; |
| 509 | return compact({ |
| 510 | workspace: config.workspace, |
| 511 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 512 | readAuthorizerCredential: toUserCredential(config.readAuthorizerCredential), |
| 513 | authorizerCredential: toUserCredential(config.authorizerCredential), |
| 514 | }); |
| 515 | }; |
| 516 | |
| 517 | const toBitbucketDataCenter = ( |
| 518 | config: BitbucketDataCenterConfig | undefined, |
| 519 | ): developerconnect.BitbucketDataCenterConfig | undefined => { |
| 520 | if (config === undefined) return undefined; |
| 521 | return compact({ |
| 522 | hostUri: config.hostUri, |
| 523 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 524 | readAuthorizerCredential: toUserCredential(config.readAuthorizerCredential), |
| 525 | authorizerCredential: toUserCredential(config.authorizerCredential), |
| 526 | serviceDirectoryConfig: toServiceDirectory(config.serviceDirectoryConfig), |
| 527 | sslCaCertificate: config.sslCaCertificate, |
| 528 | }); |
| 529 | }; |
| 530 | |
| 531 | const toSsm = ( |
| 532 | config: SecureSourceManagerInstanceConfig | undefined, |
| 533 | ): developerconnect.SecureSourceManagerInstanceConfig | undefined => { |
| 534 | if (config === undefined) return undefined; |
| 535 | return compact({ instance: config.instance }); |
| 536 | }; |
| 537 | |
| 538 | const fromGithub = ( |
| 539 | config: developerconnect.GitHubConfig | undefined, |
| 540 | ): GitHubConfig | undefined => { |
| 541 | if (config === undefined) return undefined; |
| 542 | return compact({ |
| 543 | githubApp: config.githubApp, |
| 544 | authorizerCredential: config.authorizerCredential |
| 545 | ? compact({ |
| 546 | oauthTokenSecretVersion: config.authorizerCredential.oauthTokenSecretVersion, |
| 547 | }) |
| 548 | : undefined, |
| 549 | appInstallationId: config.appInstallationId, |
| 550 | }); |
| 551 | }; |
| 552 | |
| 553 | const fromGithubEnterprise = ( |
| 554 | config: developerconnect.GitHubEnterpriseConfig | undefined, |
| 555 | ): GitHubEnterpriseConfig | undefined => { |
| 556 | if (config === undefined) return undefined; |
| 557 | return compact({ |
| 558 | hostUri: config.hostUri, |
| 559 | appInstallationId: config.appInstallationId, |
| 560 | sslCaCertificate: config.sslCaCertificate, |
| 561 | organization: config.organization, |
| 562 | privateKeySecretVersion: config.privateKeySecretVersion, |
| 563 | appId: config.appId, |
| 564 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 565 | serviceDirectoryConfig: config.serviceDirectoryConfig |
| 566 | ? compact({ service: config.serviceDirectoryConfig.service }) |
| 567 | : undefined, |
| 568 | }); |
| 569 | }; |
| 570 | |
| 571 | const fromGitlab = ( |
| 572 | config: developerconnect.GitLabConfig | undefined, |
| 573 | ): GitLabConfig | undefined => { |
| 574 | if (config === undefined) return undefined; |
| 575 | return compact({ |
| 576 | authorizerCredential: config.authorizerCredential |
| 577 | ? compact({ |
| 578 | userTokenSecretVersion: config.authorizerCredential.userTokenSecretVersion, |
| 579 | }) |
| 580 | : undefined, |
| 581 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 582 | readAuthorizerCredential: config.readAuthorizerCredential |
| 583 | ? compact({ |
| 584 | userTokenSecretVersion: config.readAuthorizerCredential.userTokenSecretVersion, |
| 585 | }) |
| 586 | : undefined, |
| 587 | }); |
| 588 | }; |
| 589 | |
| 590 | const fromGitlabEnterprise = ( |
| 591 | config: developerconnect.GitLabEnterpriseConfig | undefined, |
| 592 | ): GitLabEnterpriseConfig | undefined => { |
| 593 | if (config === undefined) return undefined; |
| 594 | return compact({ |
| 595 | hostUri: config.hostUri, |
| 596 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 597 | readAuthorizerCredential: config.readAuthorizerCredential |
| 598 | ? compact({ |
| 599 | userTokenSecretVersion: config.readAuthorizerCredential.userTokenSecretVersion, |
| 600 | }) |
| 601 | : undefined, |
| 602 | authorizerCredential: config.authorizerCredential |
| 603 | ? compact({ |
| 604 | userTokenSecretVersion: config.authorizerCredential.userTokenSecretVersion, |
| 605 | }) |
| 606 | : undefined, |
| 607 | serviceDirectoryConfig: config.serviceDirectoryConfig |
| 608 | ? compact({ service: config.serviceDirectoryConfig.service }) |
| 609 | : undefined, |
| 610 | sslCaCertificate: config.sslCaCertificate, |
| 611 | }); |
| 612 | }; |
| 613 | |
| 614 | const fromBitbucketCloud = ( |
| 615 | config: developerconnect.BitbucketCloudConfig | undefined, |
| 616 | ): BitbucketCloudConfig | undefined => { |
| 617 | if (config === undefined) return undefined; |
| 618 | return compact({ |
| 619 | workspace: config.workspace, |
| 620 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 621 | readAuthorizerCredential: config.readAuthorizerCredential |
| 622 | ? compact({ |
| 623 | userTokenSecretVersion: config.readAuthorizerCredential.userTokenSecretVersion, |
| 624 | }) |
| 625 | : undefined, |
| 626 | authorizerCredential: config.authorizerCredential |
| 627 | ? compact({ |
| 628 | userTokenSecretVersion: config.authorizerCredential.userTokenSecretVersion, |
| 629 | }) |
| 630 | : undefined, |
| 631 | }); |
| 632 | }; |
| 633 | |
| 634 | const fromBitbucketDataCenter = ( |
| 635 | config: developerconnect.BitbucketDataCenterConfig | undefined, |
| 636 | ): BitbucketDataCenterConfig | undefined => { |
| 637 | if (config === undefined) return undefined; |
| 638 | return compact({ |
| 639 | hostUri: config.hostUri, |
| 640 | webhookSecretSecretVersion: config.webhookSecretSecretVersion, |
| 641 | readAuthorizerCredential: config.readAuthorizerCredential |
| 642 | ? compact({ |
| 643 | userTokenSecretVersion: config.readAuthorizerCredential.userTokenSecretVersion, |
| 644 | }) |
| 645 | : undefined, |
| 646 | authorizerCredential: config.authorizerCredential |
| 647 | ? compact({ |
| 648 | userTokenSecretVersion: config.authorizerCredential.userTokenSecretVersion, |
| 649 | }) |
| 650 | : undefined, |
| 651 | serviceDirectoryConfig: config.serviceDirectoryConfig |
| 652 | ? compact({ service: config.serviceDirectoryConfig.service }) |
| 653 | : undefined, |
| 654 | sslCaCertificate: config.sslCaCertificate, |
| 655 | }); |
| 656 | }; |
| 657 | |
| 658 | const fromSsm = ( |
| 659 | config: developerconnect.SecureSourceManagerInstanceConfig | undefined, |
| 660 | ): SecureSourceManagerInstanceConfig | undefined => { |
| 661 | if (config === undefined) return undefined; |
| 662 | return compact({ instance: config.instance }); |
| 663 | }; |
| 664 | |
| 665 | const scmKindOf = (value: { |
| 666 | githubConfig?: unknown; |
| 667 | githubEnterpriseConfig?: unknown; |
| 668 | gitlabConfig?: unknown; |
| 669 | gitlabEnterpriseConfig?: unknown; |
| 670 | bitbucketCloudConfig?: unknown; |
| 671 | bitbucketDataCenterConfig?: unknown; |
| 672 | secureSourceManagerInstanceConfig?: unknown; |
| 673 | }): ScmKind | undefined => { |
| 674 | if (value.githubEnterpriseConfig !== undefined) return "githubEnterprise"; |
| 675 | if (value.gitlabEnterpriseConfig !== undefined) return "gitlabEnterprise"; |
| 676 | if (value.gitlabConfig !== undefined) return "gitlab"; |
| 677 | if (value.bitbucketDataCenterConfig !== undefined) return "bitbucketDataCenter"; |
| 678 | if (value.bitbucketCloudConfig !== undefined) return "bitbucketCloud"; |
| 679 | if (value.secureSourceManagerInstanceConfig !== undefined) return "secureSourceManager"; |
| 680 | if (value.githubConfig !== undefined) return "github"; |
| 681 | return undefined; |
| 682 | }; |
| 683 | |
| 684 | const desiredScmKind = (news: ConnectionProps): ScmKind | undefined => scmKindOf(news); |
| 685 | |
| 686 | const toScmBody = (news: ConnectionProps): Partial<developerconnect.Connection> => { |
| 687 | const kind = desiredScmKind(news) ?? "github"; |
| 688 | switch (kind) { |
| 689 | case "githubEnterprise": |
| 690 | return { |
| 691 | githubEnterpriseConfig: toGithubEnterprise(news.githubEnterpriseConfig), |
| 692 | }; |
| 693 | case "gitlab": |
| 694 | return { gitlabConfig: toGitlab(news.gitlabConfig) }; |
| 695 | case "gitlabEnterprise": |
| 696 | return { |
| 697 | gitlabEnterpriseConfig: toGitlabEnterprise(news.gitlabEnterpriseConfig), |
| 698 | }; |
| 699 | case "bitbucketCloud": |
| 700 | return { |
| 701 | bitbucketCloudConfig: toBitbucketCloud(news.bitbucketCloudConfig), |
| 702 | }; |
| 703 | case "bitbucketDataCenter": |
| 704 | return { |
| 705 | bitbucketDataCenterConfig: toBitbucketDataCenter(news.bitbucketDataCenterConfig), |
| 706 | }; |
| 707 | case "secureSourceManager": |
| 708 | return { |
| 709 | secureSourceManagerInstanceConfig: toSsm(news.secureSourceManagerInstanceConfig), |
| 710 | }; |
| 711 | default: |
| 712 | return { githubConfig: toGithub(news.githubConfig ?? {}) }; |
| 713 | } |
| 714 | }; |
| 715 | |
| 716 | const scmField = (kind: ScmKind | undefined) => { |
| 717 | switch (kind) { |
| 718 | case "githubEnterprise": |
| 719 | return "githubEnterpriseConfig"; |
| 720 | case "gitlab": |
| 721 | return "gitlabConfig"; |
| 722 | case "gitlabEnterprise": |
| 723 | return "gitlabEnterpriseConfig"; |
| 724 | case "bitbucketCloud": |
| 725 | return "bitbucketCloudConfig"; |
| 726 | case "bitbucketDataCenter": |
| 727 | return "bitbucketDataCenterConfig"; |
| 728 | case "secureSourceManager": |
| 729 | return "secureSourceManagerInstanceConfig"; |
| 730 | case "github": |
| 731 | return "githubConfig"; |
| 732 | default: |
| 733 | return undefined; |
| 734 | } |
| 735 | }; |
| 736 | |
| 737 | const immutableHostOf = (value: { |
| 738 | githubEnterpriseConfig?: { hostUri?: string }; |
| 739 | gitlabEnterpriseConfig?: { hostUri?: string }; |
| 740 | bitbucketDataCenterConfig?: { hostUri?: string }; |
| 741 | bitbucketCloudConfig?: { workspace?: string }; |
| 742 | secureSourceManagerInstanceConfig?: { instance?: string }; |
| 743 | githubConfig?: { githubApp?: string }; |
| 744 | }) => |
| 745 | value.githubEnterpriseConfig?.hostUri ?? |
| 746 | value.gitlabEnterpriseConfig?.hostUri ?? |
| 747 | value.bitbucketDataCenterConfig?.hostUri ?? |
| 748 | value.bitbucketCloudConfig?.workspace ?? |
| 749 | value.secureSourceManagerInstanceConfig?.instance ?? |
| 750 | value.githubConfig?.githubApp; |
| 751 | |
| 752 | const immutableWebhookOf = (value: { |
| 753 | githubEnterpriseConfig?: { webhookSecretSecretVersion?: string }; |
| 754 | gitlabConfig?: { webhookSecretSecretVersion?: string }; |
| 755 | gitlabEnterpriseConfig?: { webhookSecretSecretVersion?: string }; |
| 756 | bitbucketDataCenterConfig?: { webhookSecretSecretVersion?: string }; |
| 757 | bitbucketCloudConfig?: { webhookSecretSecretVersion?: string }; |
| 758 | }) => |
| 759 | value.githubEnterpriseConfig?.webhookSecretSecretVersion ?? |
| 760 | value.gitlabConfig?.webhookSecretSecretVersion ?? |
| 761 | value.gitlabEnterpriseConfig?.webhookSecretSecretVersion ?? |
| 762 | value.bitbucketDataCenterConfig?.webhookSecretSecretVersion ?? |
| 763 | value.bitbucketCloudConfig?.webhookSecretSecretVersion; |
| 764 | |
| 765 | const toAttrs = (item: developerconnect.Connection, project: string, region: string) => { |
| 766 | const name = item.name ?? ""; |
| 767 | const parsed = parseName(name, "connections", region); |
| 768 | return { |
| 769 | name, |
| 770 | connectionId: parsed.id, |
| 771 | project: parsed.project || project, |
| 772 | location: parsed.location, |
| 773 | disabled: item.disabled === true, |
| 774 | labels: userLabels(item.labels), |
| 775 | annotations: userAnnotations(item.annotations), |
| 776 | githubConfig: fromGithub(item.githubConfig), |
| 777 | githubEnterpriseConfig: fromGithubEnterprise(item.githubEnterpriseConfig), |
| 778 | gitlabConfig: fromGitlab(item.gitlabConfig), |
| 779 | gitlabEnterpriseConfig: fromGitlabEnterprise(item.gitlabEnterpriseConfig), |
| 780 | bitbucketCloudConfig: fromBitbucketCloud(item.bitbucketCloudConfig), |
| 781 | bitbucketDataCenterConfig: fromBitbucketDataCenter(item.bitbucketDataCenterConfig), |
| 782 | secureSourceManagerInstanceConfig: fromSsm(item.secureSourceManagerInstanceConfig), |
| 783 | gitProxyConfig: item.gitProxyConfig |
| 784 | ? compact({ enabled: item.gitProxyConfig.enabled === true }) |
| 785 | : undefined, |
| 786 | cryptoKeyConfig: item.cryptoKeyConfig |
| 787 | ? compact({ keyReference: item.cryptoKeyConfig.keyReference }) |
| 788 | : undefined, |
| 789 | installationState: item.installationState |
| 790 | ? compact({ |
| 791 | stage: item.installationState.stage, |
| 792 | message: item.installationState.message, |
| 793 | actionUri: item.installationState.actionUri, |
| 794 | }) |
| 795 | : undefined, |
| 796 | reconciling: item.reconciling === true, |
| 797 | etag: item.etag, |
| 798 | uid: item.uid, |
| 799 | createTime: item.createTime, |
| 800 | updateTime: item.updateTime, |
| 801 | }; |
| 802 | }; |
| 803 | |
| 804 | const getByName = (name: string) => |
| 805 | developerconnect |
| 806 | .getProjectsLocationsConnections({ name }) |
| 807 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 808 | |
| 809 | const listOwned = (project: string, region: string) => |
| 810 | listAtLocation(project, region, (parent) => |
| 811 | listLabeledPages( |
| 812 | developerconnect.listProjectsLocationsConnections.pages({ |
| 813 | parent, |
| 814 | pageSize: 1000, |
| 815 | }), |
| 816 | (page) => page.connections, |
| 817 | (item) => item.labels, |
| 818 | ), |
| 819 | ); |
| 820 | |
| 821 | export const ConnectionProvider = () => |
| 822 | Provider.succeed(Connection, { |
| 823 | stables: ["name", "connectionId", "project", "location", "uid", "createTime"], |
| 824 | |
| 825 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 826 | if (!isResolved(news)) return undefined; |
| 827 | const env = yield* GcpEnvironment.current; |
| 828 | const previousKind = |
| 829 | desiredScmKind(olds ?? {}) ?? (output === undefined ? undefined : scmKindOf(output)); |
| 830 | const nextKind = desiredScmKind(news); |
| 831 | const previousHost = immutableHostOf(olds ?? {}) ?? immutableHostOf(output ?? {}); |
| 832 | const nextHost = immutableHostOf(news); |
| 833 | const previousWebhook = immutableWebhookOf(olds ?? {}) ?? immutableWebhookOf(output ?? {}); |
| 834 | const nextWebhook = immutableWebhookOf(news); |
| 835 | return replaceOnIdentity({ |
| 836 | previousId: olds?.connectionId ?? output?.connectionId, |
| 837 | nextId: news.connectionId ?? olds?.connectionId ?? output?.connectionId, |
| 838 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 839 | nextLocation: normalizeLocation( |
| 840 | news.location ?? olds?.location ?? output?.location, |
| 841 | env.region, |
| 842 | ), |
| 843 | extra: |
| 844 | (previousKind !== undefined && nextKind !== undefined && nextKind !== previousKind) || |
| 845 | (previousHost !== undefined && nextHost !== undefined && nextHost !== previousHost) || |
| 846 | (previousWebhook !== undefined && |
| 847 | nextWebhook !== undefined && |
| 848 | nextWebhook !== previousWebhook), |
| 849 | }); |
| 850 | }), |
| 851 | |
| 852 | read: Effect.fn(function* ({ id, olds, output }) { |
| 853 | const env = yield* GcpEnvironment.current; |
| 854 | const connectionId = yield* toPhysicalId( |
| 855 | id, |
| 856 | olds?.connectionId, |
| 857 | output?.connectionId, |
| 858 | "connection", |
| 859 | ); |
| 860 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 861 | const name = output?.name ?? resourceName(env.project, location, connectionId); |
| 862 | const existing = yield* getByName(name); |
| 863 | if (existing === undefined) return undefined; |
| 864 | const attrs = toAttrs(existing, env.project, env.region); |
| 865 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 866 | }), |
| 867 | |
| 868 | list: () => |
| 869 | Effect.gen(function* () { |
| 870 | const env = yield* GcpEnvironment.current; |
| 871 | const items = yield* listOwned(env.project, env.region); |
| 872 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 873 | }), |
| 874 | |
| 875 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 876 | const env = yield* GcpEnvironment.current; |
| 877 | const connectionId = yield* toPhysicalId( |
| 878 | id, |
| 879 | news.connectionId, |
| 880 | output?.connectionId, |
| 881 | "connection", |
| 882 | ); |
| 883 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 884 | const name = resourceName(env.project, location, connectionId); |
| 885 | const desiredLabels = { |
| 886 | ...toLabels(news.labels), |
| 887 | ...(yield* createInternalLabels(id)), |
| 888 | }; |
| 889 | const annotations = news.annotations; |
| 890 | const desiredDisabled = news.disabled === true; |
| 891 | const scmBody = toScmBody(news); |
| 892 | const body = compact({ |
| 893 | ...scmBody, |
| 894 | disabled: desiredDisabled ? true : undefined, |
| 895 | labels: desiredLabels, |
| 896 | annotations, |
| 897 | gitProxyConfig: news.gitProxyConfig |
| 898 | ? compact({ enabled: news.gitProxyConfig.enabled }) |
| 899 | : undefined, |
| 900 | cryptoKeyConfig: news.cryptoKeyConfig |
| 901 | ? compact({ keyReference: news.cryptoKeyConfig.keyReference }) |
| 902 | : undefined, |
| 903 | }); |
| 904 | |
| 905 | let current = yield* getByName(output?.name ?? name); |
| 906 | |
| 907 | if (current === undefined) { |
| 908 | const created = yield* developerconnect |
| 909 | .createProjectsLocationsConnections({ |
| 910 | parent: parentOf(env.project, location), |
| 911 | connectionId, |
| 912 | body, |
| 913 | }) |
| 914 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 915 | if (created !== undefined) { |
| 916 | yield* waitForOperation(created); |
| 917 | } |
| 918 | current = yield* waitUntilExists(getByName(name), name); |
| 919 | } |
| 920 | |
| 921 | if (current === undefined) { |
| 922 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 923 | } |
| 924 | |
| 925 | const observedLabels = tagRecord(current.labels); |
| 926 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 927 | const nextKind = desiredScmKind(news) ?? "github"; |
| 928 | const observedKind = scmKindOf(toAttrs(current, env.project, env.region)); |
| 929 | const observedScm = compact({ |
| 930 | githubConfig: fromGithub(current.githubConfig), |
| 931 | githubEnterpriseConfig: fromGithubEnterprise(current.githubEnterpriseConfig), |
| 932 | gitlabConfig: fromGitlab(current.gitlabConfig), |
| 933 | gitlabEnterpriseConfig: fromGitlabEnterprise(current.gitlabEnterpriseConfig), |
| 934 | bitbucketCloudConfig: fromBitbucketCloud(current.bitbucketCloudConfig), |
| 935 | bitbucketDataCenterConfig: fromBitbucketDataCenter(current.bitbucketDataCenterConfig), |
| 936 | secureSourceManagerInstanceConfig: fromSsm(current.secureSourceManagerInstanceConfig), |
| 937 | }); |
| 938 | const desiredScm = compact({ |
| 939 | githubConfig: nextKind === "github" ? toGithub(news.githubConfig ?? {}) : undefined, |
| 940 | githubEnterpriseConfig: toGithubEnterprise(news.githubEnterpriseConfig), |
| 941 | gitlabConfig: toGitlab(news.gitlabConfig), |
| 942 | gitlabEnterpriseConfig: toGitlabEnterprise(news.gitlabEnterpriseConfig), |
| 943 | bitbucketCloudConfig: toBitbucketCloud(news.bitbucketCloudConfig), |
| 944 | bitbucketDataCenterConfig: toBitbucketDataCenter(news.bitbucketDataCenterConfig), |
| 945 | secureSourceManagerInstanceConfig: toSsm(news.secureSourceManagerInstanceConfig), |
| 946 | }); |
| 947 | const scmChanged = fingerprint(observedScm) !== fingerprint(desiredScm); |
| 948 | const mask = fieldMask([ |
| 949 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 950 | fingerprint(userAnnotations(current.annotations)) !== |
| 951 | fingerprint(userAnnotations(annotations)) && "annotations", |
| 952 | !sameBool(current.disabled, desiredDisabled) && "disabled", |
| 953 | scmChanged && scmField(nextKind ?? observedKind), |
| 954 | !sameBool(current.gitProxyConfig?.enabled, news.gitProxyConfig?.enabled) && |
| 955 | "gitProxyConfig", |
| 956 | fingerprint(current.cryptoKeyConfig) !== fingerprint(news.cryptoKeyConfig) && |
| 957 | "cryptoKeyConfig", |
| 958 | ]); |
| 959 | |
| 960 | if (mask.length > 0) { |
| 961 | const operation = yield* developerconnect |
| 962 | .patchProjectsLocationsConnections({ |
| 963 | name: current.name ?? name, |
| 964 | updateMask: mask, |
| 965 | body: compact({ |
| 966 | etag: current.etag, |
| 967 | ...body, |
| 968 | disabled: desiredDisabled, |
| 969 | }), |
| 970 | }) |
| 971 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 972 | if (operation === undefined) { |
| 973 | const created = yield* developerconnect |
| 974 | .createProjectsLocationsConnections({ |
| 975 | parent: parentOf(env.project, location), |
| 976 | connectionId, |
| 977 | body, |
| 978 | }) |
| 979 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 980 | if (created !== undefined) { |
| 981 | yield* waitForOperation(created); |
| 982 | } |
| 983 | } else { |
| 984 | yield* waitForOperation(operation); |
| 985 | } |
| 986 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 987 | } |
| 988 | |
| 989 | return toAttrs(current, env.project, env.region); |
| 990 | }), |
| 991 | |
| 992 | delete: Effect.fn(function* ({ output }) { |
| 993 | const operation = yield* developerconnect |
| 994 | .deleteProjectsLocationsConnections({ name: output.name }) |
| 995 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 996 | if (operation !== undefined) { |
| 997 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 998 | } |
| 999 | yield* waitUntilGone(getByName(output.name), output.name); |
| 1000 | }), |
| 1001 | }); |