-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.go
More file actions
174 lines (153 loc) · 4.83 KB
/
Copy pathgithub.go
File metadata and controls
174 lines (153 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"cmp"
"context"
"fmt"
"time"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
// githubEntity holds the common profile fields for a GitHub User or Organisation.
type githubEntity struct {
Login string
Name string
URL string `graphql:"url"`
WebsiteURL string `graphql:"websiteUrl"`
AvatarURL string `graphql:"avatarUrl"`
}
func (e githubEntity) toRawSponsor(source string, monthly float64) rawSponsor {
return rawSponsor{
id: e.Login,
name: cmp.Or(e.Name, e.Login),
source: source,
website: cmp.Or(e.WebsiteURL, e.URL),
image: e.AvatarURL,
monthlyUSD: monthly,
}
}
type githubClient struct {
client *githubv4.Client
}
func newGithubClient(token string) *githubClient {
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), src)
return &githubClient{client: githubv4.NewClient(httpClient)}
}
// fetchSponsors returns the public sponsors for the given GitHub account:
// currently-active recurring sponsorships plus one-time payments from the past
// year. The account may be either a user or an organisation: both implement the
// Sponsorable interface, so we resolve it via repositoryOwner rather than the
// user field, which only resolves User accounts.
// One-time sponsorships are included with their amount divided by 12 to
// normalise to a monthly figure.
func (c *githubClient) fetchSponsors(user string) ([]rawSponsor, error) {
var q struct {
RepositoryOwner struct {
Sponsorable struct {
SponsorshipsAsMaintainer struct {
PageInfo struct {
HasNextPage bool
EndCursor githubv4.String
}
Nodes []struct {
SponsorEntity struct {
AsUser githubEntity `graphql:"... on User"`
AsOrganization githubEntity `graphql:"... on Organization"`
}
Tier struct {
MonthlyPriceInDollars int
IsOneTime bool
}
IsActive bool
PrivacyLevel string
CreatedAt githubv4.DateTime
}
} `graphql:"sponsorshipsAsMaintainer(first: 100, after: $cursor, activeOnly: false)"`
} `graphql:"... on Sponsorable"`
} `graphql:"repositoryOwner(login: $user)"`
}
variables := map[string]any{
"user": githubv4.String(user),
"cursor": (*githubv4.String)(nil),
}
oneYearAgo := time.Now().AddDate(-1, 0, 0)
var sponsors []rawSponsor
for {
if err := c.client.Query(context.Background(), &q, variables); err != nil {
return nil, err
}
for _, n := range q.RepositoryOwner.Sponsorable.SponsorshipsAsMaintainer.Nodes {
if n.PrivacyLevel != "PUBLIC" {
continue
}
entity := n.SponsorEntity.AsUser
if entity.Login == "" {
entity = n.SponsorEntity.AsOrganization
}
if entity.Login == "" {
continue
}
// We query with activeOnly: false so that one-time payments (which
// become inactive immediately after charging) are still returned.
// One-time sponsorships count for a year from their creation date;
// recurring sponsorships only count while still active.
if !n.Tier.IsOneTime && !n.IsActive {
continue
}
monthly := float64(n.Tier.MonthlyPriceInDollars)
if n.Tier.IsOneTime {
if n.CreatedAt.Before(oneYearAgo) {
continue
}
monthly /= 12
}
if monthly <= 0 {
continue
}
sponsors = append(sponsors, entity.toRawSponsor("github", monthly))
}
if !q.RepositoryOwner.Sponsorable.SponsorshipsAsMaintainer.PageInfo.HasNextPage {
break
}
variables["cursor"] = new(q.RepositoryOwner.Sponsorable.SponsorshipsAsMaintainer.PageInfo.EndCursor)
}
return sponsors, nil
}
// UserInfo holds public profile data for a GitHub user or organisation.
type UserInfo struct {
Name string
Login string
Website string
Image string
}
// FetchUserInfo fetches public profile data for the given login via GraphQL.
// repositoryOwner resolves both users and organisations without errors.
// ProfileOwner is the interface implemented by both User and Organization.
func (c *githubClient) FetchUserInfo(login string) (UserInfo, error) {
var q struct {
RepositoryOwner *struct {
Login string
URL string `graphql:"url"`
AvatarURL string `graphql:"avatarUrl"`
Profile struct {
Name string
WebsiteURL string `graphql:"websiteUrl"`
} `graphql:"... on ProfileOwner"`
} `graphql:"repositoryOwner(login: $login)"`
}
if err := c.client.Query(context.Background(), &q, map[string]any{
"login": githubv4.String(login),
}); err != nil {
return UserInfo{}, fmt.Errorf("fetch user info %q: %w", login, err)
}
if q.RepositoryOwner == nil {
return UserInfo{}, fmt.Errorf("fetch user info %q: not found", login)
}
o := q.RepositoryOwner
return UserInfo{
Name: cmp.Or(o.Profile.Name, o.Login),
Login: o.Login,
Website: cmp.Or(o.Profile.WebsiteURL, o.URL),
Image: o.AvatarURL,
}, nil
}