Skip to content

Commit

Permalink
Merge pull request #34 from sbv-world-health-org-metrics/add-more-fet…
Browse files Browse the repository at this point in the history
…chers

add more fetchers
  • Loading branch information
hasan-dot authored Jun 29, 2023
2 parents 38f4af2 + ce3d9b4 commit 6396868
Show file tree
Hide file tree
Showing 41 changed files with 489 additions and 8,452 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.env
tmp/*
tmp/*
sbv-world-health-org-metrics.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package fetchers

import (
"context"
"fmt"

"github.com/shurcooL/githubv4"
)

type ContributionInfoFetcher struct {
client *githubv4.Client
orgName string
}

func NewContributionInfoFetcher(client *githubv4.Client, orgName string) *ContributionInfoFetcher {
return &ContributionInfoFetcher{client: client, orgName: orgName}
}

type contributionInfo struct {
Node struct {
NameWithOwner githubv4.String
}
}

type contributionInfoQuery struct {
Organization struct {
Repositories struct {
Edges []contributionInfo
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"repositories(first: 100, privacy: PUBLIC, after: $reposCursor)"`
} `graphql:"organization(login:$organizationLogin)"`
}

func (c *ContributionInfoFetcher) Fetch(ctx context.Context) (string, error) {
variables := map[string]interface{}{
"organizationLogin": githubv4.String(c.orgName),
"reposCursor": (*githubv4.String)(nil), // Null after argument to get first page.
}

var q contributionInfoQuery

var allRepos []contributionInfo

for {
err := c.client.Query(ctx, &q, variables)
if err != nil {
return "", err
}
allRepos = append(allRepos, q.Organization.Repositories.Edges...)
if !q.Organization.Repositories.PageInfo.HasNextPage {
break
}
variables["reposCursor"] = githubv4.NewString(q.Organization.Repositories.PageInfo.EndCursor)
}

csvString, err := c.formatCSV(allRepos)
if err != nil {
return "", err
}
return csvString, nil
}

func (f *ContributionInfoFetcher) formatCSV(data []contributionInfo) (string, error) {
csvString := "repo_name,issues_opened\n"
for _, edge := range data {
csvString += fmt.Sprintf("%s\n",
edge.Node.NameWithOwner,
// edge.Node.OpenIssues.TotalCount,
// edge.Node.ClosedIssues.TotalCount,
// edge.Node.OpenPullRequests.TotalCount,
// edge.Node.MergedPullRequests.TotalCount,
)
}
return csvString, nil
}
82 changes: 49 additions & 33 deletions backend/business/core/collectors/github/fetchers/org_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,77 @@ package fetchers

import (
"context"
"fmt"

"github.com/shurcooL/githubv4"
"github.com/who-metrics/business/helpers"
)

type OrgInfo struct {
client *githubv4.Client
orgName string
}

type orgInfo struct {
Login githubv4.String
Name githubv4.String
Description githubv4.String
CreatedAt githubv4.DateTime
MembersWithRole struct {
TotalCount githubv4.Int
}
Projects struct {
TotalCount githubv4.Int
}
ProjectsV2 struct {
TotalCount githubv4.Int
}
Repositories struct {
TotalCount githubv4.Int
}
Teams struct {
TotalCount githubv4.Int
}
}

type orgInfoQuery struct {
Organization orgInfo `graphql:"organization(login:$organizationLogin)"`
}

func NewOrgInfo(client *githubv4.Client, orgName string) *OrgInfo {
return &OrgInfo{client: client, orgName: orgName}
}

func (f *OrgInfo) Fetch(ctx context.Context) (string, error) {
var q struct {
Organization struct {
Login githubv4.String
Name githubv4.String
Description githubv4.String
IsVerified githubv4.Boolean
CreatedAt githubv4.DateTime
MembersWithRole struct {
TotalCount githubv4.Int
}
Projects struct {
TotalCount githubv4.Int
}
EnterpriseOwners struct {
TotalCount githubv4.Int
}
ProjectsV2 struct {
TotalCount githubv4.Int
}
Repositories struct {
TotalCount githubv4.Int
}
Teams struct {
TotalCount githubv4.Int
}
} `graphql:"organization(login:$organizationLogin)"`
}
func (o *OrgInfo) Fetch(ctx context.Context) (string, error) {
variables := map[string]interface{}{
"organizationLogin": githubv4.String(f.orgName),
"organizationLogin": githubv4.String(o.orgName),
}

err := f.client.Query(ctx, &q, variables)
var q orgInfoQuery

err := o.client.Query(ctx, &q, variables)
if err != nil {
return "", err
}

jsonData, err := helpers.FormatJSON(q)
csvString, err := o.formatCSV(q.Organization)

if err != nil {
return "", err
}
return jsonData, nil
return csvString, nil
}

func (f *OrgInfo) formatCSV(data orgInfo) (string, error) {
csvString := "login,name,description,createdAt,totalMembers,totalProjects,totalRepositories,totalTeams\n"
csvString += fmt.Sprintf("%s,%s,%s,%s,%d,%d,%d,%d\n",
data.Login,
data.Name,
data.Description,
data.CreatedAt,
data.MembersWithRole.TotalCount,
data.Projects.TotalCount+data.ProjectsV2.TotalCount,
data.Repositories.TotalCount,
data.Teams.TotalCount,
)
return csvString, nil
}
109 changes: 0 additions & 109 deletions backend/business/core/collectors/github/fetchers/org_repos_info.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ type repoInfo struct {
Issues struct {
TotalCount githubv4.Int
}
OpenIssues struct {
TotalCount githubv4.Int
} `graphql:"openIssues: issues(states: OPEN)"`
ClosedIssues struct {
TotalCount githubv4.Int
} `graphql:"closedIssues: issues(states: CLOSED)"`
OpenPullRequests struct {
TotalCount githubv4.Int
} `graphql:"openPullRequests: pullRequests(states: OPEN)"`
MergedPullRequests struct {
TotalCount githubv4.Int
} `graphql:"mergedPullRequests: pullRequests(states: MERGED)"`
LicenseInfo struct {
Name githubv4.String
}
Expand All @@ -46,7 +58,7 @@ type repoInfo struct {
}
}

type collaboratorsQuery struct {
type reposInfoQuery struct {
Organization struct {
Repositories struct {
Edges []repoInfo
Expand All @@ -64,7 +76,7 @@ func (f *ReposInfoFetcher) Fetch(ctx context.Context) (string, error) {
"reposCursor": (*githubv4.String)(nil), // Null after argument to get first page.
}

var q collaboratorsQuery
var q reposInfoQuery

var allRepos []repoInfo

Expand All @@ -88,15 +100,19 @@ func (f *ReposInfoFetcher) Fetch(ctx context.Context) (string, error) {
}

func (f *ReposInfoFetcher) formatCSV(data []repoInfo) (string, error) {
csvString := "repo_name,collaborators_count,projects_count,discussions_count,forks_count,issues_count,licenseInfo,watchers_count\n"
csvString := "repo_name,collaborators_count,projects_count,discussions_count,forks_count,issues_count,open_issues_count,closed_issues_count,open_pull_requests_count,merged_pull_requests_count,license_name,watchers_count\n"
for _, edge := range data {
csvString += fmt.Sprintf("%s,%d,%d,%d,%d,%d,%s,%d\n",
csvString += fmt.Sprintf("%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%d\n",
edge.Node.NameWithOwner,
edge.Node.Collaborators.TotalCount,
edge.Node.Projects.TotalCount+edge.Node.ProjectsV2.TotalCount,
edge.Node.Discussions.TotalCount,
edge.Node.Forks.TotalCount,
edge.Node.Issues.TotalCount,
edge.Node.OpenIssues.TotalCount,
edge.Node.ClosedIssues.TotalCount,
edge.Node.OpenPullRequests.TotalCount,
edge.Node.MergedPullRequests.TotalCount,
edge.Node.LicenseInfo.Name,
edge.Node.Watchers.TotalCount,
)
Expand Down
3 changes: 2 additions & 1 deletion backend/business/core/collectors/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func buildFetchers(client *githubv4.Client) []Fetcher {
&fetchers.OrgInfo{client: client, orgName: vars.orgName},
}
*/
// TODO: map the fetchers to keys so we can easily access them
return []Fetcher{
// fetchers.NewOrgInfo(client, "WorldHealthOrganization"),
fetchers.NewReposInfoFetcher(client, "WorldHealthOrganization"),
// fetchers.NewOrgReposInfo(client, "WorldHealthOrganization"),
// fetchers.NewContributionInfoFetcher(client, "WorldHealthOrganization"),
}
}
9 changes: 0 additions & 9 deletions next-beta/.env.local.example

This file was deleted.

3 changes: 0 additions & 3 deletions next-beta/.eslintrc.json

This file was deleted.

Loading

0 comments on commit 6396868

Please sign in to comment.