diff --git a/README.md b/README.md index 9c686f1..0824224 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,19 @@ sha, err = client.ResolveCommit(ctx, "https://github.com/actions/checkout", "v4. A ref that does not exist returns `forges.ErrNotFound`. A ref that is already a full 40-character SHA is normalized and returned without a network request. +Funding links live in a `FUNDING.yml` file, and the `funding` package looks for it where GitHub does: `.github/`, the repository root, `docs/`, then the owner's `.github` repository. + +```go +import "github.com/git-pkgs/forge/funding" + +f, _ := client.ForgeFor("github.com") +links, err := funding.Discover(ctx, f.Files(), "octocat", "hello-world", "") +// links.GitHub holds ["sponsors-handle"], links.Custom the URLs the file lists. + +// A caller that already has the file parses it without a forge: +links, err = funding.Parse(body) +``` + The GitHub backend also exposes a standalone resolver for callers that do not need the full `Forge` interface: ```go diff --git a/funding/funding.go b/funding/funding.go new file mode 100644 index 0000000..5530ab6 --- /dev/null +++ b/funding/funding.go @@ -0,0 +1,163 @@ +// Package funding discovers the funding links a repository advertises, the same +// way GitHub does for the Sponsor button: a FUNDING.yml file, looked up in the +// places GitHub looks. +package funding + +import ( + "context" + "errors" + "fmt" + + "go.yaml.in/yaml/v3" + + forges "github.com/git-pkgs/forge" +) + +// Links is the parsed content of a FUNDING.yml file. +type Links struct { + GitHub StringList `yaml:"github" json:"github,omitempty"` + Patreon string `yaml:"patreon" json:"patreon,omitempty"` + OpenCollective string `yaml:"open_collective" json:"open_collective,omitempty"` + KoFi string `yaml:"ko_fi" json:"ko_fi,omitempty"` + Tidelift string `yaml:"tidelift" json:"tidelift,omitempty"` + CommunityBridge string `yaml:"community_bridge" json:"community_bridge,omitempty"` + Liberapay string `yaml:"liberapay" json:"liberapay,omitempty"` + IssueHunt string `yaml:"issuehunt" json:"issuehunt,omitempty"` + LFXCrowdfunding string `yaml:"lfx_crowdfunding" json:"lfx_crowdfunding,omitempty"` + Polar string `yaml:"polar" json:"polar,omitempty"` + BuyMeACoffee string `yaml:"buy_me_a_coffee" json:"buy_me_a_coffee,omitempty"` + ThanksDev string `yaml:"thanks_dev" json:"thanks_dev,omitempty"` + Otechie string `yaml:"otechie" json:"otechie,omitempty"` + Custom StringList `yaml:"custom" json:"custom,omitempty"` +} + +// Empty reports whether the file declared no funding destination. +func (l Links) Empty() bool { + return len(l.GitHub) == 0 && len(l.Custom) == 0 && + l.Patreon == "" && l.OpenCollective == "" && l.KoFi == "" && + l.Tidelift == "" && l.CommunityBridge == "" && l.Liberapay == "" && + l.IssueHunt == "" && l.LFXCrowdfunding == "" && l.Polar == "" && + l.BuyMeACoffee == "" && l.ThanksDev == "" && l.Otechie == "" +} + +// URL returns the page GitHub links for a handle on a platform, or an empty +// string for a platform it does not know. Custom entries are already URLs. +func URL(platform, handle string) string { + switch platform { + case "github": + return "https://github.com/sponsors/" + handle + case "patreon": + return "https://www.patreon.com/" + handle + case "open_collective": + return "https://opencollective.com/" + handle + case "ko_fi": + return "https://ko-fi.com/" + handle + case "tidelift": + return "https://tidelift.com/funding/github/" + handle + case "community_bridge": + return "https://funding.communitybridge.org/projects/" + handle + case "liberapay": + return "https://liberapay.com/" + handle + case "issuehunt": + return "https://issuehunt.io/r/" + handle + case "lfx_crowdfunding": + return "https://crowdfunding.lfx.linuxfoundation.org/projects/" + handle + case "polar": + return "https://polar.sh/" + handle + case "buy_me_a_coffee": + return "https://www.buymeacoffee.com/" + handle + case "thanks_dev": + return "https://thanks.dev/" + handle + case "otechie": + return "https://otechie.com/" + handle + } + return "" +} + +// locations is where GitHub looks for a funding file, in order. +var locations = []location{ + {path: ".github/FUNDING.yml"}, + {path: "FUNDING.yml"}, + {path: "docs/FUNDING.yml"}, + {repo: ".github", path: ".github/FUNDING.yml"}, + {repo: ".github", path: "FUNDING.yml"}, +} + +// location is one file a funding file may live in. A set repo names another +// repository of the same owner, read from its own default branch. +type location struct { + repo string + path string +} + +// Discover reads the funding file GitHub would read for owner/repo; an empty +// ref reads the default branch, and a missing file moves to the next location. +func Discover(ctx context.Context, files forges.FileService, owner, repo, ref string) (Links, error) { + if files == nil { + return Links{}, errors.New("funding: nil file service") + } + for _, loc := range locations { + fileRepo, probeRef := repo, ref + if loc.repo != "" { + fileRepo, probeRef = loc.repo, "" + } + content, err := files.Get(ctx, owner, fileRepo, loc.path, probeRef) + if err != nil { + if isMissing(err) { + continue + } + return Links{}, fmt.Errorf("funding: read %s/%s %s: %w", owner, fileRepo, loc.path, err) + } + if content == nil { + continue + } + links, err := Parse(content.Content) + if err != nil { + return Links{}, fmt.Errorf("funding: parse %s/%s %s: %w", owner, fileRepo, loc.path, err) + } + return links, nil + } + return Links{}, nil +} + +// isMissing reports whether an error means there is nothing to read: the file +// is not there, or the forge has no file API at all. +func isMissing(err error) bool { + return errors.Is(err, forges.ErrNotFound) || errors.Is(err, forges.ErrNotSupported) +} + +// Parse reads the body of a FUNDING.yml file. Keys this package does not know +// are ignored, so a platform added later still parses. +func Parse(content []byte) (Links, error) { + var links Links + if err := yaml.Unmarshal(content, &links); err != nil { + return Links{}, err + } + return links, nil +} + +// StringList is a value GitHub accepts either as one entry or as a list of +// them: "github" and "custom" both allow either. +type StringList []string + +func (l *StringList) UnmarshalYAML(node *yaml.Node) error { + switch node.Kind { + case yaml.ScalarNode: + var value string + if err := node.Decode(&value); err != nil { + return err + } + if value != "" { + *l = StringList{value} + } + return nil + case yaml.SequenceNode: + var values []string + if err := node.Decode(&values); err != nil { + return err + } + *l = values + return nil + } + return fmt.Errorf("want a string or a list, got %s", node.Tag) +} diff --git a/funding/funding_test.go b/funding/funding_test.go new file mode 100644 index 0000000..7f295e1 --- /dev/null +++ b/funding/funding_test.go @@ -0,0 +1,331 @@ +package funding + +import ( + "context" + "net/http" + "reflect" + "strings" + "testing" + + forges "github.com/git-pkgs/forge" +) + +// fakeFiles answers FileService.Get from a map keyed by "owner/repo/path@ref", +// so a test can say exactly which funding file exists where. +type fakeFiles struct { + files map[string]string + errs map[string]error + calls []string +} + +func (f *fakeFiles) key(owner, repo, path, ref string) string { + return owner + "/" + repo + "/" + path + "@" + ref +} + +func (f *fakeFiles) Get(_ context.Context, owner, repo, path, ref string) (*forges.FileContent, error) { + key := f.key(owner, repo, path, ref) + f.calls = append(f.calls, key) + if err, ok := f.errs[key]; ok { + return nil, err + } + body, ok := f.files[key] + if !ok { + return nil, forges.ErrNotFound + } + return &forges.FileContent{Path: path, Content: []byte(body)}, nil +} + +func (f *fakeFiles) List(_ context.Context, _, _, _, _ string) ([]forges.FileEntry, error) { + return nil, forges.ErrNotSupported +} + +func TestParseReadsEveryPlatform(t *testing.T) { + content := `# Funding for this project +github: [alice, bob] +patreon: alice +open_collective: "my-collective" +ko_fi: 'alice' +tidelift: npm/left-pad +community_bridge: my-project +liberapay: alice +issuehunt: alice +lfx_crowdfunding: my-project +polar: alice +buy_me_a_coffee: alice +thanks_dev: gh/alice +custom: ["https://example.com/donate", "https://example.org/#giving"] +otechie: alice +unknown_platform: ignored +` + links, err := Parse([]byte(content)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + want := Links{ + GitHub: StringList{"alice", "bob"}, + Patreon: "alice", + OpenCollective: "my-collective", + KoFi: "alice", + Tidelift: "npm/left-pad", + CommunityBridge: "my-project", + Liberapay: "alice", + IssueHunt: "alice", + LFXCrowdfunding: "my-project", + Polar: "alice", + BuyMeACoffee: "alice", + ThanksDev: "gh/alice", + Otechie: "alice", + Custom: StringList{"https://example.com/donate", "https://example.org/#giving"}, + } + if !reflect.DeepEqual(links, want) { + t.Errorf("Parse:\n got %+v\nwant %+v", links, want) + } + if links.Empty() { + t.Error("Empty() = true for a file that declares funding") + } +} + +func TestParseReadsBlockListsAndScalars(t *testing.T) { + content := `github: + - alice + - bob +custom: + - https://example.com/donate + - https://example.org/docs#donate +patreon: alice # a trailing comment +` + links, err := Parse([]byte(content)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + if !reflect.DeepEqual(links.GitHub, StringList{"alice", "bob"}) { + t.Errorf("GitHub = %v", links.GitHub) + } + // A "#" inside a URL does not start a comment, so the fragment survives. + if !reflect.DeepEqual(links.Custom, StringList{"https://example.com/donate", "https://example.org/docs#donate"}) { + t.Errorf("Custom = %v", links.Custom) + } + if links.Patreon != "alice" { + t.Errorf("Patreon = %q", links.Patreon) + } +} + +func TestParseErrorsOnMalformedLines(t *testing.T) { + for name, content := range map[string]string{ + "list item without a key": "- alice\n", + "line without a colon": "github\n", + "unterminated list": "github: [alice, bob\n", + "empty list item": "github: [alice, , bob]\n", + } { + t.Run(name, func(t *testing.T) { + if _, err := Parse([]byte(content)); err == nil { + t.Errorf("Parse(%q) returned no error", content) + } + }) + } +} + +func TestParseSkipsABOMAndADocumentMarker(t *testing.T) { + content := "\ufeff---\ngithub: alice\n...\n" + links, err := Parse([]byte(content)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + if !reflect.DeepEqual(links.GitHub, StringList{"alice"}) { + t.Errorf("GitHub = %v, want [alice]", links.GitHub) + } +} + +func TestParseReadsGitHubsYAMLShapes(t *testing.T) { + for name, tc := range map[string]struct { + content string + want Links + }{ + "unknown key with a block list": { + content: "new_platform:\n - a\n - b\ngithub: alice\n", + want: Links{GitHub: StringList{"alice"}}, + }, + "flow list split across lines": { + content: "custom: [\"https://a.example\",\n \"https://b.example\"]\n", + want: Links{Custom: StringList{"https://a.example", "https://b.example"}}, + }, + } { + t.Run(name, func(t *testing.T) { + links, err := Parse([]byte(tc.content)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + if !reflect.DeepEqual(links, tc.want) { + t.Errorf("Parse:\n got %+v\nwant %+v", links, tc.want) + } + }) + } +} + +func TestParseHandlesCRLF(t *testing.T) { + content := "github: [alice, bob]\r\npatreon: alice\r\n" + links, err := Parse([]byte(content)) + if err != nil { + t.Fatalf("Parse: %v", err) + } + if !reflect.DeepEqual(links.GitHub, StringList{"alice", "bob"}) || links.Patreon != "alice" { + t.Errorf("Parse = %+v", links) + } +} + +func TestParseEmptyContentIsEmpty(t *testing.T) { + links, err := Parse(nil) + if err != nil { + t.Fatalf("Parse: %v", err) + } + if !links.Empty() { + t.Errorf("Parse(nil) = %+v, want empty", links) + } +} + +func TestURL(t *testing.T) { + for platform, want := range map[string]string{ + "github": "https://github.com/sponsors/alice", + "patreon": "https://www.patreon.com/alice", + "open_collective": "https://opencollective.com/alice", + "ko_fi": "https://ko-fi.com/alice", + "tidelift": "https://tidelift.com/funding/github/alice", + "community_bridge": "https://funding.communitybridge.org/projects/alice", + "liberapay": "https://liberapay.com/alice", + "issuehunt": "https://issuehunt.io/r/alice", + "lfx_crowdfunding": "https://crowdfunding.lfx.linuxfoundation.org/projects/alice", + "polar": "https://polar.sh/alice", + "buy_me_a_coffee": "https://www.buymeacoffee.com/alice", + "thanks_dev": "https://thanks.dev/alice", + "otechie": "https://otechie.com/alice", + } { + if got := URL(platform, "alice"); got != want { + t.Errorf("URL(%q) = %q, want %q", platform, got, want) + } + } + // Custom holds URLs already, so there is nothing to build. + if got := URL("custom", "https://example.com"); got != "" { + t.Errorf("URL(custom) = %q, want empty", got) + } +} + +func TestDiscoverPrefersTheGitHubDirectory(t *testing.T) { + files := &fakeFiles{files: map[string]string{ + "alice/project/.github/FUNDING.yml@main": "github: alice\n", + "alice/project/FUNDING.yml@main": "patreon: alice\n", + }} + links, err := Discover(context.Background(), files, "alice", "project", "main") + if err != nil { + t.Fatalf("Discover: %v", err) + } + if !reflect.DeepEqual(links.GitHub, StringList{"alice"}) || links.Patreon != "" { + t.Errorf("Discover = %+v, want the .github directory file", links) + } + if len(files.calls) != 1 { + t.Errorf("Discover made %d calls, want 1: %v", len(files.calls), files.calls) + } +} + +func TestDiscoverFallsBackInGitHubsOrder(t *testing.T) { + cases := map[string]struct { + exists string + want string + calls int + }{ + "github dir": {".github/FUNDING.yml", "github: a", 1}, + "root": {"FUNDING.yml", "github: a", 2}, + "docs": {"docs/FUNDING.yml", "github: a", 3}, + "owner .github": {"OWNER:.github/FUNDING.yml", "github: a", 4}, + "owner root": {"OWNER:FUNDING.yml", "github: a", 5}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + key := "alice/project/" + tc.exists + "@main" + if handle, ok := strings.CutPrefix(tc.exists, "OWNER:"); ok { + // The owner's .github repository is read from its own default + // branch: a ref from owner/repo does not exist there. + key = "alice/.github/" + handle + "@" + } + files := &fakeFiles{files: map[string]string{key: tc.want}} + links, err := Discover(context.Background(), files, "alice", "project", "main") + if err != nil { + t.Fatalf("Discover: %v", err) + } + if !reflect.DeepEqual(links.GitHub, StringList{"a"}) { + t.Errorf("Discover = %+v, want the %s file", links, name) + } + if len(files.calls) != tc.calls { + t.Errorf("Discover made %d calls, want %d: %v", len(files.calls), tc.calls, files.calls) + } + }) + } +} + +func TestDiscoverReturnsEmptyWhenNothingIsFunded(t *testing.T) { + files := &fakeFiles{} + links, err := Discover(context.Background(), files, "alice", "project", "") + if err != nil { + t.Fatalf("Discover: %v", err) + } + if !links.Empty() { + t.Errorf("Discover = %+v, want empty", links) + } + // With an empty ref the default branch is read, which is how the call is + // forwarded to the forge. + for _, call := range files.calls { + if !strings.HasSuffix(call, "@") { + t.Errorf("call %q does not pass the empty ref through", call) + } + } +} + +func TestDiscoverStopsAtAFileThatDeclaresNothing(t *testing.T) { + // Existence decides which file GitHub reads, so a file with a key this + // package does not know stops the walk instead of falling through to docs/. + files := &fakeFiles{files: map[string]string{ + "alice/project/FUNDING.yml@main": "unknown_platform: alice\n", + "alice/project/docs/FUNDING.yml@main": "github: alice\n", + }} + links, err := Discover(context.Background(), files, "alice", "project", "main") + if err != nil { + t.Fatalf("Discover: %v", err) + } + if !links.Empty() { + t.Errorf("Discover = %+v, want empty", links) + } + if len(files.calls) != 2 { + t.Errorf("Discover made %d calls, want 2: %v", len(files.calls), files.calls) + } +} + +func TestDiscoverTreatsAnUnsupportedForgeAsMissing(t *testing.T) { + files := &fakeFiles{errs: map[string]error{ + "alice/project/.github/FUNDING.yml@main": forges.ErrNotSupported, + "alice/project/FUNDING.yml@main": forges.ErrNotSupported, + "alice/project/docs/FUNDING.yml@main": forges.ErrNotSupported, + "alice/.github/.github/FUNDING.yml@": forges.ErrNotSupported, + "alice/.github/FUNDING.yml@": forges.ErrNotSupported, + }} + links, err := Discover(context.Background(), files, "alice", "project", "main") + if err != nil { + t.Fatalf("Discover: %v", err) + } + if !links.Empty() { + t.Errorf("Discover = %+v, want empty", links) + } +} + +func TestDiscoverReturnsOtherErrors(t *testing.T) { + files := &fakeFiles{errs: map[string]error{ + "alice/project/.github/FUNDING.yml@main": &forges.HTTPError{StatusCode: http.StatusForbidden}, + }} + if _, err := Discover(context.Background(), files, "alice", "project", "main"); err == nil { + t.Fatal("Discover returned no error for a 403") + } +} + +func TestDiscoverRequiresAFileService(t *testing.T) { + if _, err := Discover(context.Background(), nil, "alice", "project", "main"); err == nil { + t.Fatal("Discover(nil) returned no error") + } +} diff --git a/go.mod b/go.mod index 95ba697..d2e7d96 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 gitlab.com/gitlab-org/api/client-go v1.46.0 + go.yaml.in/yaml/v3 v3.0.5 golang.org/x/crypto v0.58.0 golang.org/x/term v0.47.0 ) diff --git a/go.sum b/go.sum index 994327d..21f11f4 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD gitlab.com/gitlab-org/api/client-go v1.46.0 h1:YxBWFZIFYKcGESCb9fpkwzouo+apyB9pr/XTWzNoL24= gitlab.com/gitlab-org/api/client-go v1.46.0/go.mod h1:FtgyU6g2HS5+fMhw6nLK96GBEEBx5MzntOiJWfIaiN8= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=