From de52ecc13916e6ae3eec2fdcb625d1dec6ea2249 Mon Sep 17 00:00:00 2001 From: Sai Sindhur Malleni Date: Thu, 13 Apr 2023 23:14:23 -0500 Subject: [PATCH] Add Contacts for each Repo Adds an additional field to each repo dictionary in the JSON consisting of top 3 contributor's GitHub id and the URL - which can both be used to render contacts on the frontend. This partially implements #5 Signed-off-by: Sai Sindhur Malleni --- scraper/go.mod | 5 ++++- scraper/go.sum | 2 ++ scraper/main.go | 14 +++++++++++++- scraper/pkg/github/github.go | 11 +++++++++++ scraper/pkg/types/repodata.go | 21 ++++++++++++++++----- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/scraper/go.mod b/scraper/go.mod index 4de8a43..e972082 100644 --- a/scraper/go.mod +++ b/scraper/go.mod @@ -4,4 +4,7 @@ go 1.19 require github.com/google/go-github v17.0.0+incompatible -require github.com/google/go-querystring v1.1.0 // indirect +require ( + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-querystring v1.1.0 // indirect +) diff --git a/scraper/go.sum b/scraper/go.sum index 6f582c8..684a969 100644 --- a/scraper/go.sum +++ b/scraper/go.sum @@ -1,4 +1,6 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= diff --git a/scraper/main.go b/scraper/main.go index 417fd2c..0f4e2da 100644 --- a/scraper/main.go +++ b/scraper/main.go @@ -15,6 +15,7 @@ const ( repositoriesFile = "../public/repositories.json" ignoredTopicsFile = "../public/ignored-topics.json" ignoreRepositoriesFile = "../public/ignored-repositories.json" + topContributorsCount = 3 ) var ( @@ -69,6 +70,7 @@ func main() { loadConfiguration() var repoData types.RepoData + var contactData []types.Contact ir := *ignoredRepositories for _, o := range *sOrgs { ghrepos := github.GitHubRepositories(ctx, o) @@ -88,8 +90,17 @@ func main() { } if !ignored { topics := r.Topics - repo := types.Repo{Org: r.Owner.GetLogin(), Name: r.GetName(), URL: r.GetHTMLURL(), Description: r.GetDescription(), Labels: topics} + contributors := github.ListContrib(ctx, r.Owner.GetLogin(), r.GetName()) + for n, contributor := range contributors { + if n > topContributorsCount-1 { + break + } + contacts := types.Contact{Username: *contributor.Login, URL: *contributor.HTMLURL} + contactData = append(contactData, contacts) + } + repo := types.Repo{Org: r.Owner.GetLogin(), Name: r.GetName(), URL: r.GetHTMLURL(), Description: r.GetDescription(), Labels: topics, Contacts: contactData} repoData.Repos = append(repoData.Repos, repo) + } } } @@ -98,5 +109,6 @@ func main() { if err != nil { log.Fatalf("Error marshaling Repositories: %s", err) } + // fmt.Println(repoData.Repos) os.WriteFile(repositoriesFile, reposJson, 0666) } diff --git a/scraper/pkg/github/github.go b/scraper/pkg/github/github.go index 2ddc51c..23a22a0 100644 --- a/scraper/pkg/github/github.go +++ b/scraper/pkg/github/github.go @@ -16,3 +16,14 @@ func GitHubRepositories(ctx context.Context, org string) []*github.Repository { } return ghrepos } + +func ListContrib(ctx context.Context, org string, repository string) []*github.Contributor { + //Using Unauthenticated client + client := github.NewClient(nil) + opts := &github.ListContributorsOptions{Anon: "false"} + contributors, _, err := client.Repositories.ListContributors(ctx, org, repository, opts) + if err != nil { + log.Fatalf("Error getting contributors: %s", err) + } + return contributors +} diff --git a/scraper/pkg/types/repodata.go b/scraper/pkg/types/repodata.go index ebad3e9..b00d1eb 100644 --- a/scraper/pkg/types/repodata.go +++ b/scraper/pkg/types/repodata.go @@ -1,13 +1,24 @@ package types type Repo struct { - Org string `json:"org"` - Name string `json:"name"` - Description string `json:"description"` - URL string `json:"url"` - Labels []string `json:"labels"` + Org string `json:"org"` + Name string `json:"name"` + Description string `json:"description"` + URL string `json:"url"` + Labels []string `json:"labels"` + Contacts []Contact `json:"contacts"` } type RepoData struct { Repos []Repo `json:"repos"` } + +type Contact struct { + Username string `json:"username"` + URL string `json:"htmlurl"` +} + +type ContactData struct { + Contacts []Contact `json:"contact"` +} +