Getting Started¶
Installation¶
Prerequisites¶
- Go 1.21 or later
- A GitHub personal access token (for most operations)
Creating a GitHub Token¶
- Go to GitHub Settings > Developer settings > Personal access tokens
- Click "Generate new token (classic)" or "Fine-grained tokens"
-
Select the scopes you need:
Scope Required For repoPrivate repository access public_repoPublic repository write access (no scopes) Public read-only access -
Copy the token and store it securely
Basic Usage¶
Creating a Client¶
Environment Variables¶
The config package reads from these environment variables:
| Variable | Description | Default |
|---|---|---|
GITHUB_TOKEN |
Personal access token | (required) |
GITHUB_OWNER |
Default repository owner | - |
GITHUB_REPO |
Default repository name | - |
GITHUB_BRANCH |
Default branch | main |
GITHUB_BASE_URL |
API base URL | https://api.github.com/ |
GITHUB_UPLOAD_URL |
Upload URL | https://uploads.github.com/ |
Common Operations¶
Search for Issues/PRs¶
import "github.com/grokify/gogithub/search"
client := search.NewClient(gh)
// Using Query map
issues, err := client.SearchIssuesAll(ctx, search.Query{
search.ParamUser: "octocat",
search.ParamIs: search.ParamIsValueIssue,
search.ParamState: search.ParamStateValueOpen,
}, nil)
// Using QueryBuilder (type-safe)
qb := search.NewQueryBuilder().
User("octocat").
Is(search.ParamIsValueIssue).
State(search.ParamStateValueOpen)
issues, err := client.SearchIssuesAll(ctx, qb.Build(), nil)
Create a Branch and Commit¶
import "github.com/grokify/gogithub/repo"
// Get the SHA of the base branch
sha, err := repo.GetBranchSHA(ctx, gh, "owner", "repo", "main")
// Create a new branch
err = repo.CreateBranch(ctx, gh, "owner", "repo", "feature-branch", sha)
// Commit files
files := []repo.FileContent{
{Path: "hello.txt", Content: []byte("Hello, World!")},
}
commit, err := repo.CreateCommit(ctx, gh, "owner", "repo", "feature-branch", "Add hello.txt", files)
Create a Pull Request¶
import "github.com/grokify/gogithub/pr"
pullRequest, err := pr.CreatePR(ctx, gh,
"upstream-owner", "upstream-repo", // base repo
"fork-owner", "feature-branch", // head
"main", // base branch
"My PR Title",
"Description of changes",
)
fmt.Printf("PR URL: %s\n", pullRequest.GetHTMLURL())
Get User Contribution Stats (GraphQL)¶
import "github.com/grokify/gogithub/graphql"
client := graphql.NewClient(ctx, "your-github-token")
// Quick stats (like profile page)
from := time.Now().AddDate(-1, 0, 0)
to := time.Now()
stats, err := graphql.GetContributionStats(ctx, client, "octocat", from, to)
fmt.Printf("Total commits: %d\n", stats.TotalCommitContributions)
// Detailed stats with additions/deletions
commitStats, err := graphql.GetCommitStats(ctx, client, "octocat", from, to, graphql.VisibilityPublic)
fmt.Printf("Additions: %d, Deletions: %d\n", commitStats.Additions, commitStats.Deletions)
Next Steps¶
- Authentication Guide - Detailed authentication options
- Search API Guide - Query syntax and examples
- GraphQL Guide - Contribution statistics