Repository navigation
Add a factory package that builds a Client from any repo URL #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NopeCharon
wants to merge
9
commits into
git-pkgs:main
Choose a base branch
from
NopeCharon:repo-factory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
97c2cba
Add a factory package that builds a Client from any repo URL
NopeCharon 2392d0a
Always detect unregistered domains over https
NopeCharon 037535e
Return an error when the detected forge type has no builder
NopeCharon 85e420c
Match forge domains case-insensitively
NopeCharon 1cd40c4
Lowercase config domains and build forges outside the client lock
NopeCharon 7c66f9e
Merge branch 'main' into repo-factory
NopeCharon 5bc3f86
Keep a registration made while first-use detection is running
NopeCharon 4c14d3b
Merge branch 'main' into repo-factory
NopeCharon 74a2d19
Normalize detection lookups and merge case-variant config sections in…
NopeCharon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Package factory builds forges.Client values wired up with every provider, so | ||
| // callers can pass any repository URL without choosing a backend themselves. | ||
| package factory | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/git-pkgs/forge" | ||
| "github.com/git-pkgs/forge/bitbucket" | ||
| "github.com/git-pkgs/forge/gerrit" | ||
| "github.com/git-pkgs/forge/gitea" | ||
| "github.com/git-pkgs/forge/github" | ||
| "github.com/git-pkgs/forge/gitlab" | ||
| "github.com/git-pkgs/forge/radicle" | ||
| "github.com/git-pkgs/forge/tangled" | ||
| ) | ||
|
|
||
| // Builders returns the constructors for every provider that can be detected on | ||
| // a self-hosted domain. | ||
| func Builders() forges.ForgeBuilders { | ||
| return forges.ForgeBuilders{ | ||
| GitHub: github.NewWithBase, | ||
| GitLab: gitlab.New, | ||
| Gitea: gitea.New, | ||
| Gerrit: gerrit.New, | ||
| Tangled: tangled.New, | ||
| Radicle: radicle.New, | ||
| } | ||
| } | ||
|
|
||
| // DefaultForges returns a Forge for each well-known public domain, keyed by | ||
| // domain. The token function supplies the token for each domain and may return | ||
| // an empty string for anonymous access. | ||
| func DefaultForges(token func(domain string) string, hc *http.Client) map[string]forges.Forge { | ||
| return map[string]forges.Forge{ | ||
| "github.com": github.New(token("github.com"), hc), | ||
| "gitlab.com": gitlab.New("https://gitlab.com", token("gitlab.com"), hc), | ||
| "codeberg.org": gitea.New("https://codeberg.org", token("codeberg.org"), hc), | ||
| "bitbucket.org": bitbucket.New(token("bitbucket.org"), hc), | ||
| "tangled.org": tangled.New("https://tangled.org", token("tangled.org"), hc), | ||
| } | ||
| } | ||
|
|
||
| // NewClient returns a Client with the default forges registered and detection | ||
| // enabled for every other domain. Options apply first, so WithToken and | ||
| // WithHTTPClient reach the default forges, and a domain registered with | ||
| // WithForge keeps that registration. | ||
| func NewClient(opts ...forges.Option) *forges.Client { | ||
| c := forges.NewClient(append([]forges.Option{forges.WithDetection(Builders())}, opts...)...) | ||
| tokens := c.Tokens() | ||
| defaults := DefaultForges(func(domain string) string { return tokens[domain] }, c.HTTPClient()) | ||
| for domain, f := range defaults { | ||
| if _, err := c.ForgeFor(domain); err != nil { | ||
| forges.WithForge(domain, f)(c) | ||
| } | ||
| } | ||
| return c | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.