feat: replace last update with last commit

main
sudo pacman -Syu 2023-08-16 00:39:23 +07:00
parent a1eba99c90
commit 43cbe0f6af
1 changed files with 34 additions and 17 deletions

View File

@ -10,6 +10,7 @@ import (
"text/tabwriter" "text/tabwriter"
"time" "time"
"github.com/google/go-github/v53/github"
"github.com/sourcegraph/conc/pool" "github.com/sourcegraph/conc/pool"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -19,9 +20,9 @@ const maxPoolGoroutine = 8
var reGitHub = regexp.MustCompile(`github\.com/([^/]*)/([^/]*)`) var reGitHub = regexp.MustCompile(`github\.com/([^/]*)/([^/]*)`)
type GitHubRepoData struct { type GitHubRepoData struct {
UpdatedAt time.Time LastCommitAt time.Time
Name string Name string
Star int StarCount int
} }
func (a *action) Overlook(c *cli.Context) error { func (a *action) Overlook(c *cli.Context) error {
@ -51,6 +52,8 @@ func (a *action) Overlook(c *cli.Context) error {
for module := range mapImportedModules { for module := range mapImportedModules {
module := module module := module
p.Go(func() { p.Go(func() {
ctx := context.Background()
if !reGitHub.MatchString(module) { if !reGitHub.MatchString(module) {
return return
} }
@ -60,41 +63,55 @@ func (a *action) Overlook(c *cli.Context) error {
return return
} }
ghRepoName := parts[0] name := parts[0]
mMutex.RLock() mMutex.RLock()
if _, ok := mGHRepoData[ghRepoName]; ok { if _, ok := mGHRepoData[name]; ok {
mMutex.RUnlock() mMutex.RUnlock()
return return
} }
mMutex.RUnlock() mMutex.RUnlock()
mMutex.Lock() mMutex.Lock()
mGHRepoData[ghRepoName] = struct{}{} mGHRepoData[name] = struct{}{}
mMutex.Unlock() mMutex.Unlock()
owner := parts[1] owner := parts[1]
repo := parts[2] repo := parts[2]
ghRepo, _, err := a.ghClient.Repositories.Get(context.Background(), owner, repo) ghRepo, _, err := a.ghClient.Repositories.Get(ctx, owner, repo)
if err != nil { if err != nil {
a.log("Failed to get GitHub %s/%s: %s\n", owner, repo, err) a.log("GitHub failed to get repo %s/%s: %s\n", owner, repo, err)
} }
var ghStar int var starCount int
if ghRepo.StargazersCount != nil { if ghRepo.StargazersCount != nil {
ghStar = *ghRepo.StargazersCount starCount = *ghRepo.StargazersCount
} }
var ghUpdatedAt time.Time ghCommits, _, err := a.ghClient.Repositories.ListCommits(ctx, owner, repo, &github.CommitsListOptions{
if ghRepo.UpdatedAt != nil { ListOptions: github.ListOptions{
ghUpdatedAt = ghRepo.UpdatedAt.Time Page: 1,
PerPage: 1,
},
})
if err != nil {
a.log("GitHub failed to get commits %s/%s: %s\n", owner, repo, err)
}
var lastCommitAt time.Time
if len(ghCommits) != 0 {
if ghCommits[0].Commit != nil &&
ghCommits[0].Commit.Author != nil &&
ghCommits[0].Commit.Author.Date != nil {
lastCommitAt = ghCommits[0].Commit.Author.Date.Time
}
} }
listMutex.Lock() listMutex.Lock()
listGHRepoData = append(listGHRepoData, GitHubRepoData{ listGHRepoData = append(listGHRepoData, GitHubRepoData{
UpdatedAt: ghUpdatedAt, LastCommitAt: lastCommitAt,
Name: ghRepoName, Name: name,
Star: ghStar, StarCount: starCount,
}) })
listMutex.Unlock() listMutex.Unlock()
}) })
@ -109,7 +126,7 @@ func (a *action) Overlook(c *cli.Context) error {
// Print // Print
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
for _, r := range listGHRepoData { for _, r := range listGHRepoData {
fmt.Fprintf(w, "Module %s\t%d\t⭐\tLast updated %s\n", r.Name, r.Star, r.UpdatedAt.Format(time.DateOnly)) fmt.Fprintf(w, "Module %s\t%d\t⭐\tLast commit %s\n", r.Name, r.StarCount, r.LastCommitAt.Format(time.DateOnly))
} }
w.Flush() w.Flush()