A robust, lightweight Go package designed to fetch issues from the GitHub API using go-github with correct, Link-header-based pagination.
This library ensures filters like since are properly propagated and preserved across all pages, handling edge cases such as empty pages with a Next link correctly.
- Link-Header Pagination: Relies on GitHub's API response metadata (
resp.NextPage) rather than relying on slice lengths, preventing early loop termination. - Filter Preservation: Ensures parameter states (like the
Sincetimestamp) are preserved consistently across all paginated requests. - Edge Case Protection: Correctly handles intermediate empty pages and API rate-limiting feedback.
- Robust Mock Testing: Covered by a comprehensive unit-test suite utilizing Go's
net/http/httptestto mock paginated API responses.
To install the package, run:
go get github.com/alisteuber4ee1/API-Integration-PaginationHere is a quick example of how to fetch all issues updated within the last 7 days:
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/alisteuber4ee1/API-Integration-Pagination/pkg/github"
google_github "github.com/google/go-github/v57/github"
)
func main() {
// 1. Initialize go-github client (optional: with authentication token)
client := google_github.NewClient(nil)
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
client = google_github.NewClient(nil).WithAuthToken(token)
}
// 2. Create IssueFetcher
fetcher := github.NewIssueFetcher(client)
// 3. Fetch issues updated in the last 7 days
since := time.Now().Add(-7 * 24 * time.Hour)
issues, err := fetcher.FetchIssues(context.Background(), "google", "go-github", since, 100)
if err != nil {
log.Fatalf("Error fetching issues: %v", err)
}
fmt.Printf("Successfully fetched %d issues.\n", len(issues))
for _, issue := range issues {
fmt.Printf("- [#%d] %s\n", issue.GetNumber(), issue.GetTitle())
}
}Run the unit test suite to verify the pagination logic and mock integration:
go test -v ./...