/
checker.go
51 lines (42 loc) · 1.16 KB
/
checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package checker
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func CheckRepos(repos []string, token string) {
// Create a context
ctx := context.Background()
// Create a client with your token
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// Iterate over the list of repos
for _, repo := range repos {
splitted := splitRepoName(repo)
commits, _, err := client.Repositories.ListCommits(ctx, splitted[0], splitted[1], &github.CommitsListOptions{})
if err != nil {
fmt.Printf("Error retrieving commits for repo: %v\n", err)
continue
}
// Get the latest commit
latestCommit := commits[0]
if latestCommit.Commit.Committer.Date.After(time.Now().AddDate(-1, 0, 0)) {
fmt.Printf("Repo %s has a commit in the last year\n", repo)
} else {
fmt.Printf("Repo %s hasn't had a commit in the last year\n", repo)
}
}
}
func splitRepoName(repo string) []string {
splitted := strings.Split(repo, "/")
if len(splitted) != 2 {
panic("Invalid repo format, expected 'owner/repo'")
}
return splitted
}