Skip to content

Commit

Permalink
Order sponsors, link to website when available
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Jan 29, 2024
1 parent 0ac24a6 commit f04673e
Showing 1 changed file with 60 additions and 15 deletions.
75 changes: 60 additions & 15 deletions script/sponsors/sponsors.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package main

import (
"cmp"
"context"
"fmt"
"html/template"
"net/url"
"os"
"slices"

"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

const sponsorsOutput = `{{range .}}
<a href="https://github.com/{{.Login}}">
<a href="{{.LinkURL}}">
<img src="{{.AvatarURL}}" class="img github-avatar" alt="{{.Name}}">
</a>
{{end}}`
Expand All @@ -35,14 +38,16 @@ func main() {
Edges []struct {
Node struct {
User struct {
Login string
Name string
AvatarURL string
Login string
Name string
AvatarURL string
WebsiteURL string
} `graphql:"... on User"`
Organization struct {
Login string
Name string
AvatarURL string
Login string
Name string
AvatarURL string
WebsiteURL string
} `graphql:"... on Organization"`
Sponsorable struct {
Sponsorship struct {
Expand All @@ -65,23 +70,32 @@ func main() {
"cursor": (*githubv4.String)(nil),
}

sponsors := []map[string]any{}
type sponsor struct {
Amount int
Name string
Login string
AvatarURL string
LinkURL string
}

var sponsors []sponsor

for {
if err := client.Query(context.Background(), &query, vars); err != nil {
fmt.Println(err)
return
}

for _, sponsor := range query.Organization.Sponsors.Edges {
for _, sponsorship := range sponsor.Node.Sponsorable.Sponsorship.Edges {
for _, spons := range query.Organization.Sponsors.Edges {
for _, sponsorship := range spons.Node.Sponsorable.Sponsorship.Edges {
if sponsorship.Node.Tier.MonthlyPriceInCents >= 100*100 {
s := map[string]any{
"Name": sponsor.Node.User.Name,
"Login": sponsor.Node.User.Login,
"AvatarURL": sponsor.Node.User.AvatarURL,
"Amount": sponsorship.Node.Tier.MonthlyPriceInCents / 100,
s := sponsor{
Name: spons.Node.User.Name,
Login: spons.Node.User.Login,
AvatarURL: spons.Node.User.AvatarURL,
Amount: sponsorship.Node.Tier.MonthlyPriceInCents,
}
s.LinkURL = urlFrom(spons.Node.User.WebsiteURL, "https://github.com/"+spons.Node.User.Login+"/")
sponsors = append(sponsors, s)
}
}
Expand All @@ -93,5 +107,36 @@ func main() {
vars["cursor"] = githubv4.NewString(query.Organization.Sponsors.PageInfo.EndCursor)
}

// Sort by amount, highest first, then by name
slices.SortFunc(sponsors, func(a, b sponsor) int {
if v := cmp.Compare(b.Amount, a.Amount); v != 0 {
return v
}
return cmp.Compare(a.Name, b.Name)
})

sponsorsOutputTpl.Execute(os.Stdout, sponsors)
}

// urlFrom returns the first reasonable, working URL in the list
func urlFrom(urls ...string) string {
for _, alt := range urls {
if alt == "" {
continue
}
if u, err := url.Parse(alt); err == nil {
if u.Scheme == "" {
u.Scheme = "https"
}
if u.Host == "" && u.Path != "" {
u.Host = u.Path
u.Path = ""
}
if u.Path == "" {
u.Path = "/"
}
return u.String()
}
}
return ""
}

0 comments on commit f04673e

Please sign in to comment.