diff --git a/go.mod b/go.mod index f22cc4d..cb6d995 100644 --- a/go.mod +++ b/go.mod @@ -1,26 +1,29 @@ module github.com/DMcP89/tinycare-tui -go 1.20 +go 1.23.0 + +toolchain go1.24.6 require ( - github.com/PuerkitoBio/goquery v1.8.1 github.com/gdamore/tcell/v2 v2.6.0 github.com/go-git/go-git/v5 v5.7.0 + github.com/google/go-github/v57 v57.0.0 + github.com/h2non/gock v1.2.0 github.com/rivo/tview v0.0.0-20230530133550-8bd761dda819 + golang.org/x/oauth2 v0.30.0 ) require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230528122434-6f98819771a1 // indirect github.com/acomagu/bufpipe v1.0.4 // indirect - github.com/andybalholm/cascadia v1.3.1 // indirect github.com/cloudflare/circl v1.3.3 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/gdamore/encoding v1.0.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.4.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/h2non/gock v1.2.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect diff --git a/go.sum b/go.sum index 92dc3c0..a528d9d 100644 --- a/go.sum +++ b/go.sum @@ -3,14 +3,12 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v0.0.0-20230528122434-6f98819771a1 h1:JMDGhoQvXNTqH6Y3MC0IUw6tcZvaUdujNqzK2HYWZc8= github.com/ProtonMail/go-crypto v0.0.0-20230528122434-6f98819771a1/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= -github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= -github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= -github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= -github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= @@ -19,6 +17,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 h1:RIB4cRk+lBqKK3Oy0r2gRX4ui7tuhiZq2SuTtTCi0/0= +github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= @@ -26,16 +25,24 @@ github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg= github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y= github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= +github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.4.1 h1:Uwp5tDRkPr+l/TnbHOQzp+tmJfLceOlbVucgpTz8ix4= github.com/go-git/go-billy/v5 v5.4.1/go.mod h1:vjbugF6Fz7JIflbVpl1hJsGjSHNltrSw45YK/ukIvQg= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f h1:Pz0DHeFij3XFhoBRGUDPzSJ+w2UcK5/0JvF8DRI58r8= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo= github.com/go-git/go-git/v5 v5.7.0 h1:t9AudWVLmqzlo+4bqdf7GY+46SUuRsx59SboFxkq2aE= github.com/go-git/go-git/v5 v5.7.0/go.mod h1:coJHKEOk5kUClpsNlXrUvPrDxY3w3gjHvhcZd8Fodw8= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs= +github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE= github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= @@ -59,6 +66,7 @@ github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= @@ -98,19 +106,20 @@ golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= +golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -149,6 +158,7 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/internal/apis/gitstatus.go b/internal/apis/gitstatus.go index 3b938b7..cb136c8 100644 --- a/internal/apis/gitstatus.go +++ b/internal/apis/gitstatus.go @@ -24,114 +24,58 @@ The GetWeeklyCommits should be similar however instead of only returning commits package apis import ( - "encoding/json" + "context" "fmt" - "net/http" "time" "github.com/DMcP89/tinycare-tui/internal/utils" + "github.com/google/go-github/v57/github" + "golang.org/x/oauth2" ) -// Actor represents the actor in the JSON structure. -type Actor struct { - ID int `json:"id"` - Login string `json:"login"` - DisplayLogin string `json:"display_login"` - GravatarID string `json:"gravatar_id"` - URL string `json:"url"` - AvatarURL string `json:"avatar_url"` -} - -// Repo represents the repo in the JSON structure. -type Repo struct { - ID int `json:"id"` - Name string `json:"name"` - URL string `json:"url"` -} - -// Author represents the author in the Commit structure. -type Author struct { - Email string `json:"email"` - Name string `json:"name"` -} - -// Commit represents a commit in the JSON structure. -type Commit struct { - SHA string `json:"sha"` - Author Author `json:"author"` - Message string `json:"message"` - Distinct bool `json:"distinct"` - URL string `json:"url"` -} - -// Payload represents the payload in the JSON structure. -type Payload struct { - RepositoryID int `json:"repository_id"` - PushID int64 `json:"push_id"` - Size int `json:"size"` - DistinctSize int `json:"distinct_size"` - Ref string `json:"ref"` - Head string `json:"head"` - Before string `json:"before"` - Commits []Commit `json:"commits"` -} +const missingTokenMessage = "GITHUB_TOKEN environment variable not set correctly" -// Event represents the main structure of each event in the JSON array. -type Event struct { - ID string `json:"id"` - Type string `json:"type"` - Actor Actor `json:"actor"` - Repo Repo `json:"repo"` - Payload Payload `json:"payload"` - Public bool `json:"public"` - CreatedAt time.Time `json:"created_at"` +// newGitHubClient creates a new GitHub client with authentication +func newGitHubClient(token string) *github.Client { + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + return github.NewClient(tc) } -const reqUrl = "https://api.github.com" -const missingTokenMessage = "GITHUB_TOKEN environment variable not set correctly" - func GetGitHubUser(token string) (string, error) { - userEndpoint := "/user" - // get the username of the authenticated user - req, err := http.NewRequest("GET", reqUrl+userEndpoint, nil) + client := newGitHubClient(token) + ctx := context.Background() + user, _, err := client.Users.Get(ctx, "") if err != nil { return "", err } - // Set the API token as a header - req.Header.Set("Authorization", "Bearer "+token) - body, err := utils.SendRequest(req) - if err != nil { - return "", err + if user.Login == nil { + return "", fmt.Errorf("user login not found") } - var user map[string]interface{} - err = json.Unmarshal(body, &user) - return user["login"].(string), err + return *user.Login, nil } -func GetGitHubEvents(token string, login string, page int) ([]Event, error) { - eventsEndpoint := fmt.Sprintf("/users/%s/events?per_page=100&page=%d", login, page) - - req, err := http.NewRequest("GET", reqUrl+eventsEndpoint, nil) +func GetGitHubEvents(token string, login string, page int) ([]*github.Event, error) { + client := newGitHubClient(token) + ctx := context.Background() - if err != nil { - return nil, err + opts := &github.ListOptions{ + Page: page, + PerPage: 100, } - // Set the API token as a header - req.Header.Set("Authorization", "Bearer "+token) - body, err := utils.SendRequest(req) + events, _, err := client.Activity.ListEventsPerformedByUser(ctx, login, false, opts) if err != nil { return nil, err } - // Unmarshal the JSON data - var events []Event - err = json.Unmarshal(body, &events) - - return events, err + return events, nil } func GetGitHubCommits(token string) (string, string, error) { @@ -141,7 +85,7 @@ func GetGitHubCommits(token string) (string, string, error) { if userErr != nil { return "", "", fmt.Errorf("unable to get Github User: %w", userErr) } - var totalEvents []Event + var totalEvents []*github.Event dayLookBackTime := time.Now().AddDate(0, 0, -1) weekLookBackTime := time.Now().AddDate(0, 0, -7) @@ -154,7 +98,7 @@ func GetGitHubCommits(token string) (string, string, error) { } totalEvents = append(totalEvents, events...) page++ - if len(events) > 0 && events[len(events)-1].CreatedAt.Before(weekLookBackTime) { + if len(events) > 0 && events[len(events)-1].CreatedAt != nil && events[len(events)-1].CreatedAt.Before(weekLookBackTime) { break } } @@ -162,28 +106,44 @@ func GetGitHubCommits(token string) (string, string, error) { var weekOutput string var dayOutput string - pullCommits := func(event Event) string { + pullCommits := func(event *github.Event) string { var result string - for _, commit := range event.Payload.Commits { - timeSinceCommit := time.Since(event.CreatedAt.In(time.Local)) - formattedTimeSinceCommit := utils.HumanizeDuration(timeSinceCommit) - result += fmt.Sprintf("[yello]%s[white] (%s)\n", commit.Message, formattedTimeSinceCommit) + // Type assertion to get push event payload + if event.Type != nil && *event.Type == "PushEvent" { + // Parse the raw payload as PushEvent + if pushEvent, ok := event.Payload().(*github.PushEvent); ok && pushEvent.Commits != nil { + for _, commit := range pushEvent.Commits { + if event.CreatedAt != nil && commit.Message != nil { + timeSinceCommit := time.Since(event.CreatedAt.In(time.Local)) + formattedTimeSinceCommit := utils.HumanizeDuration(timeSinceCommit) + result += fmt.Sprintf("[yellow]%s[white]: %s (%s)\n", (*commit.SHA)[:7], *commit.Message, formattedTimeSinceCommit) + } + } + } } return result } for _, event := range totalEvents { - if len(event.Payload.Commits) > 0 && event.CreatedAt.In(time.Local).After(dayLookBackTime) { - commitText := pullCommits(event) - dayOutput += fmt.Sprintf("[red]%s[white]\n", event.Repo.Name) - dayOutput += commitText - weekOutput += fmt.Sprintf("[red]%s[white]\n", event.Repo.Name) - weekOutput += commitText - } else if len(event.Payload.Commits) > 0 && event.CreatedAt.In(time.Local).After(weekLookBackTime) { - weekOutput += fmt.Sprintf("[red]%s[white]\n", event.Repo.Name) - weekOutput += pullCommits(event) + if event.Type != nil && *event.Type == "PushEvent" && event.CreatedAt != nil { + // Check if this is a push event with commits and within our time range + if pushEvent, ok := event.Payload().(*github.PushEvent); ok && pushEvent.Commits != nil && len(pushEvent.Commits) > 0 { + if event.CreatedAt.In(time.Local).After(dayLookBackTime) { + commitText := pullCommits(event) + if event.Repo != nil && event.Repo.Name != nil { + dayOutput += fmt.Sprintf("[red]%s[white]\n", *event.Repo.Name) + dayOutput += commitText + weekOutput += fmt.Sprintf("[red]%s[white]\n", *event.Repo.Name) + weekOutput += commitText + } + } else if event.CreatedAt.In(time.Local).After(weekLookBackTime) { + if event.Repo != nil && event.Repo.Name != nil { + weekOutput += fmt.Sprintf("[red]%s[white]\n", *event.Repo.Name) + weekOutput += pullCommits(event) + } + } + } } - } return dayOutput, weekOutput, nil diff --git a/internal/apis/gitstatus_test.go b/internal/apis/gitstatus_test.go index 5733330..0efd46c 100644 --- a/internal/apis/gitstatus_test.go +++ b/internal/apis/gitstatus_test.go @@ -10,6 +10,7 @@ import ( const token = "TESTTOKEN" const user = "DMcP89" const page = 1 +const apiUrl = "https://api.github.com" func Test_GetGitHubUser(t *testing.T) { defer gock.Off() @@ -23,8 +24,8 @@ func Test_GetGitHubUser(t *testing.T) { { name: "Valid User", mockReply: func() { - gock.New(reqUrl + "/user"). - Get("/"). + gock.New(apiUrl). + Get("/user"). Reply(200). File("testdata/github_user.json") }, @@ -58,9 +59,11 @@ func Test_GetGitHubEvents(t *testing.T) { { name: "Valid Events", mockReply: func() { - eventsEndpoint := fmt.Sprintf("/users/%s/events?per_page=100&page=%d", user, page) - gock.New(reqUrl + eventsEndpoint). - Get("/"). + eventsEndpoint := fmt.Sprintf("/users/%s/events", user) + gock.New(apiUrl). + Get(eventsEndpoint). + MatchParam("per_page", "100"). + MatchParam("page", fmt.Sprintf("%d", page)). Reply(200). File("testdata/github_events.json") }, @@ -91,15 +94,17 @@ func Test_GetGitHubCommits(t *testing.T) { name: "Valid Commits", mockReplies: []func(){ func() { - gock.New(reqUrl + "/user"). - Get("/"). + gock.New(apiUrl). + Get("/user"). Reply(200). File("testdata/github_user.json") }, func() { - eventsEndpoint := fmt.Sprintf("/users/%s/events?per_page=100&page=%d", user, page) - gock.New(reqUrl + eventsEndpoint). - Get("/"). + eventsEndpoint := fmt.Sprintf("/users/%s/events", user) + gock.New(apiUrl). + Get(eventsEndpoint). + MatchParam("per_page", "100"). + MatchParam("page", fmt.Sprintf("%d", page)). Reply(200). File("testdata/github_events.json") }, @@ -120,3 +125,21 @@ func Test_GetGitHubCommits(t *testing.T) { }) } } + +func Test_GetGitHubCommits_EmptyToken(t *testing.T) { + day, week, err := GetGitHubCommits("") + + expectedMessage := "GITHUB_TOKEN environment variable not set correctly" + + if day != expectedMessage { + t.Errorf("Expected day output: %s, got: %s", expectedMessage, day) + } + + if week != expectedMessage { + t.Errorf("Expected week output: %s, got: %s", expectedMessage, week) + } + + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } +}