gitinfo

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 5 Imported by: 0

README

gitinfo

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

Go Reference Go Version


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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetReposInfo

func GetReposInfo(user string, token string) ([]utils.RepoNode, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	repos, err := gitinfo.GetReposInfo("reinanbr", token)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(len(repos) > 0)
}
Output:
true

func GetReposName

func GetReposName(user string, token string) ([]utils.RepoNode, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	repos, err := gitinfo.GetReposName("reinanbr", token)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(len(repos) > 0)
}
Output:
true

func GetUserInfo added in v0.3.1

func GetUserInfo(username string, token string) (utils.UserInfo, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	result, err := gitinfo.GetUserInfo("reinanbr", token)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(result.Login != "")
	fmt.Println(result.URL != "")
}
Output:
true
true

Types

type CommitByDate

type CommitByDate struct {
	Date         string `json:"date"`
	CountCommits int    `json:"countCommits"`
}

type CommitsResponse

type CommitsResponse struct {
	User          string         `json:"user"`
	TotalCommits  int            `json:"totalCommits"`
	CommitsByYear []CommitsYear  `json:"commitsByYear"`
	CommitsByDay  []CommitByDate `json:"commitsByDay"`
}

func GetCommits

func GetCommits(username string, token string) (CommitsResponse, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	result, err := gitinfo.GetCommits("reinanbr", token)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(result.User)
	fmt.Println(result.TotalCommits > 0)
	fmt.Println(len(result.CommitsByYear) > 0)
	fmt.Println(len(result.CommitsByDay) > 0)
}
Output:
reinanbr
true
true
true

type CommitsYear

type CommitsYear struct {
	Year    int            `json:"year"`
	Commits []CommitByDate `json:"commits"`
}

type LangPercentage

type LangPercentage struct {
	Lang       string
	Percentage float64
}

type ResponseLangs

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

func GetLangPercents

func GetLangPercents(username string, token string, ignoreLangs []string) (ResponseLangs, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	ignoreLangs := []string{"Jupyter Notebook", "TeX"}

	result, err := gitinfo.GetLangPercents("reinanbr", token, ignoreLangs)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println(result.TotalRepos > 0)
	fmt.Println(result.TotalBytes > 0)
	fmt.Println(len(result.LangPercentages) > 0)
}
Output:
true
true
true

type StreakData added in v0.2.0

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 added in v0.2.0

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

type StreakResponse added in v0.2.0

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

func GetStreaks

func GetStreaks(username string, token string) (StreakResponse, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/reinanbr/gitinfo"
)

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

	result, err := gitinfo.GetStreaks("reinanbr", token)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	hasUser := result.User != ""
	hasStreak := result.Streak.MaxStreak >= 0

	fmt.Println(hasUser)
	fmt.Println(hasStreak)
}
Output:
true
true

Directories

Path Synopsis
cmd
smoke command
examples
repos command
user command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL