refactor(times): Move Gitea time log API code into getTimeLogs()

This commit is contained in:
Noah 2024-03-17 22:06:31 -05:00
parent f8d6d8f5b2
commit 6b5ad657b6

View File

@ -14,6 +14,53 @@ import (
"code.gitea.io/sdk/gitea"
)
// getTimeLogs gets every single time log possible.
func getTimeLogs(since time.Time, on_process_repo func(repo gitea.Repository, took time.Duration)) []gitea.TrackedTime {
var times []gitea.TrackedTime
for _, client := range Clients {
page := 1
user, _, err := client.GetMyUserInfo()
if err != nil {
panic(err)
}
for {
repos, _, err := client.ListMyRepos(
gitea.ListReposOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 10,
},
},
)
if err != nil {
panic(err)
} else if len(repos) == 0 {
break
}
for _, repo := range repos {
if repo.Fork {
continue
}
duration_start := time.Now()
repo_times, _, _ := client.ListRepoTrackedTimes(
repo.Owner.UserName,
repo.Name,
gitea.ListTrackedTimesOptions{
User: user.UserName,
Since: since,
},
)
for _, t := range repo_times {
times = append(times, *t)
}
on_process_repo(*repo, time.Now().Sub(duration_start))
}
page++
}
}
return times
}
var textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#5b6078"))
type timesviewer struct {
@ -116,45 +163,13 @@ func times() {
}
}()
var times []gitea.TrackedTime
since := time.Now().AddDate(0, 0, -7)
for _, client := range Clients {
page := 1
user, _, err := client.GetMyUserInfo()
if err != nil {
panic(err)
}
for {
repos, _, err := client.ListMyRepos(gitea.ListReposOptions{ListOptions: gitea.ListOptions{Page: page, PageSize: 10}})
if err != nil {
panic(err)
} else if len(repos) == 0 {
break
}
for _, repo := range repos {
if repo.Fork {
continue
}
duration_start := time.Now()
repo_times, _, _ := client.ListRepoTrackedTimes(
repo.Owner.UserName,
repo.Name,
gitea.ListTrackedTimesOptions{
User: user.UserName,
Since: since,
},
)
for _, t := range repo_times {
times = append(times, *t)
}
times := getTimeLogs(since, func(repo gitea.Repository, took time.Duration) {
p.Send(IndicatorInfo{
info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name),
duration: time.Now().Sub(duration_start),
duration: took,
})
})
}
page++
}
}
p.Send(IndicatorInfo{
info: "Done!",
quitting: true,