Skip to content

reinanbr/gitinfo

Repository files navigation

gitinfo

A Go library to fetch GitHub profile insights via the GitHub GraphQL API.

Go Reference Go Version License: MIT


Overview

gitinfo gives you a clean Go API over GitHub's GraphQL endpoint to pull user and repository data without dealing with pagination, query building, or response parsing yourself.

Function What it returns
GetReposInfo Full repository data for a user
GetReposName Repository names only
GetUserInfo Basic user profile data and counts
GetLangPercents Language usage percentages across all repos
GetCommits Total commits grouped by year and by day
GetStreaks Max and current contribution streak with date ranges

Installation

go get github.com/reinanbr/gitinfo

Requirements: Go 1.21+ · GitHub Personal Access Token · Internet access to GitHub GraphQL API


Quick Start

package main

import (
    "fmt"
    "os"

    "github.com/reinanbr/gitinfo"
)

func main() {
    user  := "reinanbr"
    token := os.Getenv("GITHUB_TOKEN")

    info, _ := gitinfo.GetUserInfo(user, token)
    fmt.Println("name:", info.Name)
    fmt.Println("followers:", info.Followers.TotalCount)

    repos, _ := gitinfo.GetReposInfo(user, token)
    fmt.Println("repos:", len(repos))

    langs, _ := gitinfo.GetLangPercents(user, token, []string{"Jupyter Notebook", "TeX"})
    fmt.Printf("top language: %s (%.1f%%)\n", langs.LangPercentages[0].Lang, langs.LangPercentages[0].Percentage)

    commits, _ := gitinfo.GetCommits(user, token)
    fmt.Println("total commits:", commits.TotalCommits)

    streaks, _ := gitinfo.GetStreaks(user, token)
    fmt.Println("max streak:", streaks.Streak.MaxStreak)
    fmt.Println("current streak:", streaks.Streak.CurrentStreak)
}

API Reference

GetReposInfo(user, token string) ([]RepoNode, error)

Returns full repository metadata for a user.

repos, err := gitinfo.GetReposInfo("reinanbr", token)
if err != nil {
    log.Fatal(err)
}
fmt.Println(len(repos), "repositories found")

GetReposName(user, token string) ([]RepoNode, error)

Returns only repository names — lighter call for when you just need the list.

names, err := gitinfo.GetReposName("reinanbr", token)

GetUserInfo(username, token string) (UserInfo, error)

Returns user profile data (name, bio, avatar, created date) and basic counts.

info, err := gitinfo.GetUserInfo("reinanbr", token)
if err != nil {
    log.Fatal(err)
}
fmt.Println(info.Login, info.URL)

Response type:

type UserInfo struct {
    ID        string
    Name      string
    Login     string
    Bio       string
    AvatarUrl string
    CreatedAt string
    URL       string
    Followers struct {
        TotalCount int
    }
    Following struct {
        TotalCount int
    }
    Repositories struct {
        TotalCount int
    }
}

GetLangPercents(username, token string, ignoreLangs []string) (ResponseLangs, error)

Returns language distribution across all repositories, sorted by usage.

result, err := gitinfo.GetLangPercents("reinanbr", token, []string{"TeX", "Jupyter Notebook"})

fmt.Println("repos analyzed:", result.TotalRepos)
fmt.Println("total bytes:   ", result.TotalBytes)

for _, lp := range result.LangPercentages {
    fmt.Printf("  %-20s %.2f%%\n", lp.Lang, lp.Percentage)
}

Response type:

type ResponseLangs struct {
    LangPercentages []LangPercentage
    TotalBytes      int
    TotalRepos      int
}

type LangPercentage struct {
    Lang       string
    Percentage float64
}

GetCommits(username, token string) (CommitsResponse, error)

Returns total commits with two views: grouped by year and flat daily list.
Future calendar days are automatically filtered out.

commits, err := gitinfo.GetCommits("reinanbr", token)

fmt.Println("total:", commits.TotalCommits)
for _, year := range commits.CommitsByYear {
    fmt.Printf("  %d: %d commits\n", year.Year, len(year.Commits))
}

Response type:

type CommitsResponse struct {
    User          string
    TotalCommits  int
    CommitsByYear []CommitsYear
    CommitsByDay  []CommitByDate
}

GetStreaks(username, token string) (StreakResponse, error)

Returns max and current contribution streaks with start/end dates as a typed struct — no type assertions needed.

result, err := gitinfo.GetStreaks("reinanbr", token)
if err != nil {
    log.Fatal(err)
}

fmt.Println("max streak:    ", result.Streak.MaxStreak)
fmt.Println("current streak:", result.Streak.CurrentStreak)
fmt.Println("max period:    ", result.Streak.MaxStreakPeriod.Start, "->", result.Streak.MaxStreakPeriod.End)
fmt.Println("current period:", result.Streak.CurrentStreakPeriod.Start, "->", result.Streak.CurrentStreakPeriod.End)

Response type:

type StreakResponse struct {
    User   string     `json:"user"`
    Streak StreakData `json:"streak"`
}

type StreakData struct {
    MaxStreak           int          `json:"max_streak"`
    CurrentStreak       int          `json:"current_streak"`
    MaxStreakPeriod     StreakPeriod `json:"max_streak_period"`
    CurrentStreakPeriod StreakPeriod `json:"current_streak_period"`
}

type StreakPeriod struct {
    Start string `json:"start"`
    End   string `json:"end"`
}

Testing

Tests are integration tests and require a valid token. Create a .env file first:

GITHUB_TOKEN=your_token_here
# Run all tests
go test -v

# Run a specific test
go test -v -run TestGetCommits

Smoke Test CLI

A CLI at cmd/smoke validates all endpoints end-to-end before you publish or deploy.

# Run with env token
go run ./cmd/smoke -user reinanbr

# Run with explicit token
go run ./cmd/smoke -user reinanbr -token YOUR_TOKEN

# All flags
go run ./cmd/smoke -user reinanbr -ignore "Jupyter Notebook,TeX" -show-days
Flag Description Default
-user GitHub username (required)
-token GitHub token (falls back to GITHUB_TOKEN)
-ignore Comma-separated languages to ignore Jupyter Notebook,TeX
-show-days Print all commitsByDay entries false

Notes

  • Responses depend on GitHub API availability and your token's rate limits.
  • Never commit your .env file — add it to .gitignore.
  • Data is fetched from 2015 onwards by default.

License

MIT © reinanbr# bump

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages