-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
151 lines (141 loc) · 4.26 KB
/
Copy pathgithub.go
File metadata and controls
151 lines (141 loc) · 4.26 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
)
type GitHubCollector struct{}
type GitHubCommitRecord struct {
Month string `json:"month"`
Repo string `json:"repo"`
SHA string `json:"sha"`
CommittedAt string `json:"committed_at"`
AuthorLogin string `json:"author_login,omitempty"`
AuthorName string `json:"author_name,omitempty"`
Message string `json:"message"`
HTMLURL string `json:"html_url"`
IsMergeCommit bool `json:"is_merge_commit"`
Parents []string `json:"parents,omitempty"`
SourceWindow TimeWindow `json:"source_window"`
}
type ghCommit struct {
SHA string `json:"sha"`
HTMLURL string `json:"html_url"`
Author *struct {
Login string `json:"login"`
} `json:"author"`
Commit struct {
Author struct {
Name string `json:"name"`
Date string `json:"date"`
} `json:"author"`
Message string `json:"message"`
} `json:"commit"`
Parents []struct {
SHA string `json:"sha"`
} `json:"parents"`
}
func (g GitHubCollector) SmokeTest(ctx context.Context, cfg Config) (GitHubSmokeResult, error) {
if _, err := exec.LookPath("gh"); err != nil {
return GitHubSmokeResult{}, fmt.Errorf("gh not found in PATH: %w", err)
}
if _, err := runCommand(ctx, "gh", "auth", "status"); err != nil {
return GitHubSmokeResult{}, fmt.Errorf("gh auth status failed: %w", err)
}
for _, repo := range cfg.Repos {
if _, err := runCommand(ctx, "gh", "api", "repos/"+repo); err != nil {
return GitHubSmokeResult{}, fmt.Errorf("gh cannot access repo %s: %w", repo, err)
}
}
return GitHubSmokeResult{RepoCount: len(cfg.Repos)}, nil
}
func (g GitHubCollector) StreamMonth(ctx context.Context, cfg Config, month MonthRange, progress *ProgressReporter, emit func(GitHubCommitRecord) error) (int, error) {
monthStart, monthEnd := clipMonth(month, cfg)
window := TimeWindow{From: monthStart.Format(time.RFC3339), To: monthEnd.Format(time.RFC3339)}
count := 0
progress.AddPlannedWork(len(cfg.Repos))
for i, repo := range cfg.Repos {
progress.StartGitHubRepo(i+1, len(cfg.Repos), repo)
commits, err := fetchRepoCommits(ctx, repo, cfg.GitHubUser, monthStart, monthEnd)
if err != nil {
return count, err
}
progress.AddPlannedWork(len(commits))
for _, commit := range commits {
record := GitHubCommitRecord{
Month: month.Label,
Repo: repo,
SHA: commit.SHA,
CommittedAt: commit.Commit.Author.Date,
AuthorName: commit.Commit.Author.Name,
Message: commit.Commit.Message,
HTMLURL: commit.HTMLURL,
IsMergeCommit: len(commit.Parents) > 1,
SourceWindow: window,
}
if commit.Author != nil {
record.AuthorLogin = commit.Author.Login
}
for _, parent := range commit.Parents {
record.Parents = append(record.Parents, parent.SHA)
}
progress.GitHubCommit(record)
if err := emit(record); err != nil {
return count, err
}
count++
}
progress.doneWork()
progress.GitHubRepoDone(repo, len(commits))
}
return count, nil
}
func fetchRepoCommits(ctx context.Context, repo, author string, since, until time.Time) ([]ghCommit, error) {
args := []string{
"api",
"repos/" + repo + "/commits",
"--method", "GET",
"--paginate",
"--slurp",
"-f", "author=" + author,
"-f", "since=" + since.Format(time.RFC3339),
"-f", "until=" + until.Format(time.RFC3339),
"-f", "per_page=100",
}
output, err := runCommand(ctx, "gh", args...)
if err != nil {
return nil, fmt.Errorf("fetch commits for %s: %w", repo, err)
}
output = bytes.TrimSpace(output)
if len(output) == 0 {
return nil, nil
}
var pages [][]ghCommit
if err := json.Unmarshal(output, &pages); err != nil {
return nil, fmt.Errorf("decode gh commits for %s: %w", repo, err)
}
var commits []ghCommit
for _, page := range pages {
commits = append(commits, page...)
}
return commits, nil
}
func runCommand(ctx context.Context, name string, args ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, name, args...)
output, err := cmd.CombinedOutput()
if err != nil {
message := strings.TrimSpace(string(output))
if message == "" {
return nil, err
}
return nil, fmt.Errorf("%w: %s", err, message)
}
return output, nil
}
type GitHubSmokeResult struct {
RepoCount int
}