Files
Willie 792d236485 feat: optimize tracked subdir installs and devcontainer isolation
Implement remaining Issue 46 optimizations and developer experience fixes.

Install and update optimization:
- optimize install --track for subdirectory sources by attempting sparse checkout first while preserving git metadata
- fallback to existing tracked clone strategy on sparse capability or sparse path errors
- clean sparse created destination before fallback clone to avoid destination already exists failures
- improve tracked install spinner wording for subdir sources
- add tracked subdir unit tests for sparse success and sparse to full clone fallback

Progress user experience:
- normalize git progress lines by stripping volatile transfer rate suffix
- dedupe and throttle spinner and tree spinner update frequency to reduce tty flicker
- add unit tests for spinner progress normalization and throttling behavior

GitHub API messaging:
- improve rate limit guidance to suggest setting token env vars or using gh auth login

Devcontainer isolation:
- make ssenv enter command mode run from isolated environment cwd
- fix setup global init detection to use skillshare status global mode
- add auto mode hint in devcontainer wrapper when workspace project config takes precedence

E2E and tests:
- expand Issue 46 runbook with install, install track, update, no token fallback, sparse paths, and isolation checks
- update compact warning output test expectations to match current grouped warning rendering

Verification:
- go test internal install package
- go test internal install sparse and tracked subdir tests
- go test internal ui package
- go test cmd skillshare package
- go build skillshare binary and run integration TestInstall_Subdir
- run tracked subdir tests inside devcontainer
2026-02-25 07:02:54 +08:00

90 lines
2.0 KiB
Go

package github
import (
"fmt"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
)
// RateLimitError indicates GitHub API rate limit was exceeded.
type RateLimitError struct {
Limit string
Remaining string
Reset string
}
func (e *RateLimitError) Error() string {
msg := "GitHub API rate limit exceeded"
if e.Remaining == "0" {
msg += fmt.Sprintf(" (0/%s remaining)", e.Limit)
}
msg += "; set GITHUB_TOKEN/GH_TOKEN or run 'gh auth login'"
return msg
}
// NewClient creates an HTTP client for GitHub/GHE APIs.
func NewClient() *http.Client {
return &http.Client{Timeout: 15 * time.Second}
}
// NewRequest creates a GET request with default GitHub headers and optional
// token auth (GITHUB_TOKEN / GH_TOKEN / gh auth token).
func NewRequest(url string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github.v3+json")
if token := GetToken(); token != "" {
req.Header.Set("Authorization", "token "+token)
}
return req, nil
}
var (
tokenOnce sync.Once
cachedToken string
)
// GetToken returns an auth token from environment variables first, then from
// `gh auth token` (cached with sync.Once).
func GetToken() string {
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
return token
}
if token := os.Getenv("GH_TOKEN"); token != "" {
return token
}
tokenOnce.Do(func() {
cmd := exec.Command("gh", "auth", "token")
out, err := cmd.Output()
if err != nil {
return
}
cachedToken = strings.TrimSpace(string(out))
})
return cachedToken
}
// CheckRateLimit checks response status and returns a rate-limit typed error
// when the API has throttled the caller.
func CheckRateLimit(resp *http.Response) error {
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests {
return &RateLimitError{
Limit: resp.Header.Get("X-RateLimit-Limit"),
Remaining: resp.Header.Get("X-RateLimit-Remaining"),
Reset: resp.Header.Get("X-RateLimit-Reset"),
}
}
return nil
}