diff options
| author | Priyansh <[email protected]> | 2025-08-26 15:31:16 +0530 |
|---|---|---|
| committer | Priyansh <[email protected]> | 2025-08-26 15:31:16 +0530 |
| commit | 2b42c3666467a26043e92982cf4fa0eb11280c70 (patch) | |
| tree | 906efabe7a477339ffda56344120afb028760691 /utils | |
| parent | 0c66e0b7dedda5aab5a5848513ccafffdad66d6b (diff) | |
| download | eda-2b42c3666467a26043e92982cf4fa0eb11280c70.tar.xz eda-2b42c3666467a26043e92982cf4fa0eb11280c70.zip | |
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/github/github.go | 16 | ||||
| -rw-r--r-- | utils/github/types.go | 21 | ||||
| -rw-r--r-- | utils/github/user.go | 72 |
3 files changed, 109 insertions, 0 deletions
diff --git a/utils/github/github.go b/utils/github/github.go new file mode 100644 index 0000000..081a49a --- /dev/null +++ b/utils/github/github.go @@ -0,0 +1,16 @@ +package github + +import ( + "context" + "eda/config" + + "github.com/google/go-github/v74/github" + "github.com/shurcooL/githubv4" + "golang.org/x/oauth2" +) + +var GithubClient = github.NewClient(nil).WithAuthToken(config.GitHub.GitHubToken) + +var GraphQLClient = githubv4.NewClient(oauth2.NewClient(context.TODO(), oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: config.GitHub.GitHubToken}, +))) diff --git a/utils/github/types.go b/utils/github/types.go new file mode 100644 index 0000000..8e3fe9a --- /dev/null +++ b/utils/github/types.go @@ -0,0 +1,21 @@ +package github + +type ContributionDay struct { + ContributionCount int `json:"contributionCount"` + Date string `json:"date"` + Color string `json:"color"` +} + +type ContributionWeek struct { + ContributionDays []ContributionDay `json:"contributionDays"` + FirstDay string `json:"firstDay"` +} + +type ContributionCalendar struct { + TotalContributions int `json:"totalContributions"` + Weeks []ContributionWeek `json:"weeks"` +} + +type ContributionGraph struct { + ContributionCalendar ContributionCalendar `json:"contributionCalendar"` +} diff --git a/utils/github/user.go b/utils/github/user.go new file mode 100644 index 0000000..cfd2ae3 --- /dev/null +++ b/utils/github/user.go @@ -0,0 +1,72 @@ +package github + +import ( + "context" + "eda/config" + + "github.com/google/go-github/v74/github" + "github.com/shurcooL/githubv4" +) + +func GetProfile() *github.User { + user, _, err := GithubClient.Users.Get(context.TODO(), config.GitHub.GitHubUsername) + if err != nil { + return nil + } + return user +} + +func GetContributionGraph() (*ContributionGraph, error) { + var query struct { + User struct { + ContributionsCollection struct { + ContributionCalendar struct { + TotalContributions int `graphql:"totalContributions"` + Weeks []struct { + ContributionDays []struct { + ContributionCount int `graphql:"contributionCount"` + Date string `graphql:"date"` + Color string `graphql:"color"` + } `graphql:"contributionDays"` + FirstDay string `graphql:"firstDay"` + } `graphql:"weeks"` + } `graphql:"contributionCalendar"` + } `graphql:"contributionsCollection"` + } `graphql:"user(login: $username)"` + } + + variables := map[string]interface{}{ + "username": githubv4.String(config.GitHub.GitHubUsername), + } + + err := GraphQLClient.Query(context.Background(), &query, variables) + if err != nil { + return nil, err + } + + contributionGraph := &ContributionGraph{ + ContributionCalendar: ContributionCalendar{ + TotalContributions: query.User.ContributionsCollection.ContributionCalendar.TotalContributions, + Weeks: make([]ContributionWeek, len(query.User.ContributionsCollection.ContributionCalendar.Weeks)), + }, + } + + for i, week := range query.User.ContributionsCollection.ContributionCalendar.Weeks { + contributionWeek := ContributionWeek{ + FirstDay: week.FirstDay, + ContributionDays: make([]ContributionDay, len(week.ContributionDays)), + } + + for j, day := range week.ContributionDays { + contributionWeek.ContributionDays[j] = ContributionDay{ + ContributionCount: day.ContributionCount, + Date: day.Date, + Color: day.Color, + } + } + + contributionGraph.ContributionCalendar.Weeks[i] = contributionWeek + } + + return contributionGraph, nil +} |
