From e683e183bd18bba6cea1e5d97dcfdbde1897d5f0 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 24 Jul 2020 01:40:12 +0200 Subject: [PATCH] Sort sponsors and wrap them in a Sponsor type --- README.md | 9 +++++++-- sponsors.go | 29 +++++++++++++++++++---------- templates/github-profile.tpl | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 97006d3..197dae4 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ This function requires GitHub authentication! ``` {{range followers 5}} Username: {{.Login}} +Name: {{.Name}} +Avatar: {{.AvatarURL}} URL: {{.URL}} {{end}} ``` @@ -92,8 +94,11 @@ This function requires GitHub authentication! ``` {{range sponsors 5}} -Username: {{.Login}} -URL: {{.URL}} +Username: {{.User.Login}} +Name: {{.User.Name}} +Avatar: {{.User.AvatarURL}} +URL: {{.User.URL}} +Created: {{humanize .CreatedAt}} {{end}} ``` diff --git a/sponsors.go b/sponsors.go index 165edce..ce1d7a4 100644 --- a/sponsors.go +++ b/sponsors.go @@ -2,10 +2,16 @@ package main import ( "context" + "time" "github.com/shurcooL/githubv4" ) +type Sponsor struct { + User User + CreatedAt time.Time +} + var sponsorsQuery struct { User struct { Login githubv4.String @@ -25,14 +31,14 @@ var sponsorsQuery struct { } } } - } `graphql:"sponsorshipsAsMaintainer(first: $count)"` + } `graphql:"sponsorshipsAsMaintainer(first: $count, orderBy: {field: CREATED_AT, direction: DESC})"` } `graphql:"user(login:$username)"` } -func sponsors(count int) []User { +func sponsors(count int) []Sponsor { // fmt.Printf("Finding sponsors...\n") - var users []User + var sponsors []Sponsor variables := map[string]interface{}{ "username": githubv4.String(username), "count": githubv4.Int(count), @@ -45,17 +51,20 @@ func sponsors(count int) []User { // fmt.Printf("%+v\n", query) for _, v := range sponsorsQuery.User.SponsorshipsAsMaintainer.Edges { - u := User{ - Login: string(v.Node.SponsorEntity.SponsorUser.Login), - Name: string(v.Node.SponsorEntity.SponsorUser.Name), - AvatarURL: string(v.Node.SponsorEntity.SponsorUser.AvatarURL), - URL: string(v.Node.SponsorEntity.SponsorUser.URL), + s := Sponsor{ + User: User{ + Login: string(v.Node.SponsorEntity.SponsorUser.Login), + Name: string(v.Node.SponsorEntity.SponsorUser.Name), + AvatarURL: string(v.Node.SponsorEntity.SponsorUser.AvatarURL), + URL: string(v.Node.SponsorEntity.SponsorUser.URL), + }, + CreatedAt: v.Node.CreatedAt.Time, } - users = append(users, u) + sponsors = append(sponsors, s) } // fmt.Printf("Found %d sponsors!\n", len(users)) - return users + return sponsors } /* diff --git a/templates/github-profile.tpl b/templates/github-profile.tpl index 2159688..5a5d07e 100644 --- a/templates/github-profile.tpl +++ b/templates/github-profile.tpl @@ -22,7 +22,7 @@ #### ❤️ These awesome people sponsor me (thank you!) {{range sponsors 5}} -- [{{.Login}}]({{.URL}}) +- [{{.User.Login}}]({{.User.URL}}) ({{humanize .CreatedAt}}) {{- end}} #### 👯 Check out some of my recent followers