refactor(coderd): extract git provider abstraction from chats.go#22511
Open
refactor(coderd): extract git provider abstraction from chats.go#22511
Conversation
Contributor
Documentation CheckUpdates Needed
Automated review via Coder Tasks |
88b49f7 to
0203146
Compare
Introduces a new gitprovider package under coderd/externalauth/ that provides a provider-agnostic interface for git hosting operations (PR status, diffs, branch resolution, URL parsing). This refactor: - Moves ~475 lines of GitHub-specific code from chats.go into coderd/externalauth/gitprovider/github.go - Defines a Provider interface with 8 methods designed to minimize API round-trips per provider (e.g. GitHub GraphQL can batch into 1 call when extended later) - Adds a Git() method on externalauth.Config that returns a *GitProvider for supported git hosting providers, following the existing pattern of type-switching on Config methods - Adds APIBaseURL field to externalauth.Config with defaults for GitHub, GitLab, and Gitea - Updates chats.go to resolve providers dynamically via ExternalAuthConfigs regex matching instead of hardcoded GitHub checks - Adds 30 unit tests covering URL parsing, normalization, and branch URL construction The interface is designed to support future providers (GitLab, Bitbucket, Azure DevOps, Gitea) and future capabilities (CI status via GitHub GraphQL statusCheckRollup) without changing callers.
…BaseURL The GitHub provider had hardcoded github.com in regex patterns and URL construction, breaking GitHub Enterprise instances. Changes: - Regex patterns are now compiled per-instance using the host derived from the configured APIBaseURL - Added webBaseURL field derived from apiBaseURL (api.github.com → github.com, ghes.corp.com/api/v3 → ghes.corp.com) - All URL construction (PR URLs, branch URLs, normalized origins) uses webBaseURL instead of hardcoded github.com - Added BuildPullRequestURL to the Provider interface - Removed last hardcoded github.com URL from chats.go - Added 8 GHE-specific tests verifying all operations work with a custom host and that github.com URLs don't match a GHE instance
…terface directly The GitProvider struct was a pure pass-through wrapper adding no value. New() now returns the Provider interface directly, and all callers use the interface type.
…n, missing RemoteOrigin Fixes from code review: 1. Fixed malformed else-if indentation in resolveChatDiffReference that caused the PR URL assignment to be inside the error branch. 2. Restored original EnhancedExternalAuthProvider.Git() check in manifest.go for counting git auth configs. The cfg.Git() != nil check was wrong because gitprovider.New() only returns non-nil for GitHub, silently breaking the git auth count for GitLab, Bitbucket, Azure DevOps, and Gitea providers. 3. Added BuildRepositoryURL to the Provider interface and set RemoteOrigin when deriving a repo ref from a PR URL, so downstream resolveGitProvider calls can match against external auth config regex patterns. 4. Renamed resolveChatGitHubAccessToken to resolveChatGitAccessToken since it's now used for all git providers. 5. Consolidated httpClient nil fallback into newGitHub constructor instead of checking in every method. 6. Fixed comment referencing removed GitProvider type.
…ent interface with *http.Client
0203146 to
0141c43
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces a
gitproviderpackage undercoderd/externalauth/that provides a provider-agnostic interface for git hosting operations (PR status, diffs, branch resolution, URL parsing).Motivation
The diff/PR status code in
chats.gowas tightly coupled to GitHub — ~475 lines of hardcoded GitHub API calls, URL parsing, and regex patterns. This blocked:Changes
New package:
coderd/externalauth/gitprovider/gitprovider.go— Core types (PRState,PRRef,BranchRef,DiffStats,PRStatus) and theProviderinterface with 8 methods designed to minimize API round-trips per providergithub.go— GitHub implementation of theProviderinterface, translated from the existingchats.gocodegithub_test.go— 30 unit tests covering URL parsing, normalization, and branch URL constructionexternalauth.ConfigadditionsAPIBaseURLfield — Base URL for provider API calls (defaults:https://api.github.comfor GitHub, etc.). Enables GitHub Enterprise support.Git()method — Returns a*gitprovider.GitProviderfor git hosting providers,nilfor non-git providers (Slack, JFrog). Follows the existing type-switch pattern used byTokenRevocationRequestetc.chats.gorefactorresolveGitProvider(origin)helper that matches against configuredExternalAuthConfigsregex patternsgitproviderpackage"github"checksDesign decisions
FetchPRStatusreturns everything in one shot (state, reviews, CI, diff stats) — this lets GitHub use a single GraphQL query when we upgrade from REST later, and lets other providers batch internallyGitProviderwraps aProviderinterface — the interface is the extension point for new providers; callers use the concrete wrapperexternalauth.Configsystem rather than introducing a separate provider registryNew()factory returnsnilfor unsupported providers; GitLab/Bitbucket/etc. can be added incrementallyNext steps (not in this PR)
statusCheckRolluppull_request_statein sidebar (field already in API)