-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
228 lines (199 loc) · 5.24 KB
/
cli.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package ghaprofiler
import (
"context"
"log"
"os"
"regexp"
"sort"
"strings"
"github.com/go-git/go-git/v5"
"github.com/google/go-github/v32/github"
"github.com/jessevdk/go-flags"
"golang.org/x/sync/errgroup"
)
type CLI struct {
verbose bool
}
func NewCLI() *CLI {
return &CLI{}
}
func (cli *CLI) SetVerbosity(verbose bool) {
cli.verbose = verbose
}
func (cli *CLI) logVerbose(s interface{}) {
if !cli.verbose {
return
}
log.Print(s)
}
func (cli *CLI) loglnVerbose(s interface{}) {
if !cli.verbose {
return
}
log.Println(s)
}
func (cli *CLI) logfVerbose(format string, args ...interface{}) {
if !cli.verbose {
return
}
log.Printf(format, args...)
}
func (cli *CLI) overrideRepositoryFromCWD(config *ProfileConfig) {
if config.Owner != "" && config.Repository != "" {
return
}
cwd, err := os.Getwd()
if err != nil {
return
}
repo, err := git.PlainOpen(cwd)
if err != nil {
cli.loglnVerbose(err)
return
}
remote, err := repo.Remote("origin")
if err != nil {
cli.loglnVerbose(err)
return
}
urls := remote.Config().URLs
if len(urls) == 0 {
return
}
url := urls[0]
splitted := strings.Split(url, "/")
if len(splitted) <= 1 {
return
}
owner, repoName := splitted[len(splitted)-2], splitted[len(splitted)-1]
repoName = strings.Replace(repoName, ".git", "", 1)
if lastColonPos := strings.LastIndex(owner, ":"); lastColonPos != -1 {
// [email protected]:owner/repo.git
owner = owner[lastColonPos:]
}
config.Owner = owner
config.Repository = repoName
}
func (cli *CLI) Start(ctx context.Context, args []string) {
var config *ProfileConfig = DefaultProfileConfig()
var configFromArgs ProfileConfigCLIArgs
args, err := flags.ParseArgs(&configFromArgs, args)
if err != nil {
// flags.ParseArgs() outputs error message, so discarding it here...
return
}
var configTomlPath string
if configFromArgs.ConfigPath != nil {
configTomlPath = *configFromArgs.ConfigPath
configFromTOML, err := LoadConfigFromTOML(configTomlPath)
if err != nil {
log.Fatalf("Failed to load %s: %v", configTomlPath, err)
}
config = OverrideCLIArgs(configFromTOML, &configFromArgs)
} else {
config = OverrideCLIArgs(DefaultProfileConfig(), &configFromArgs)
}
cli.overrideRepositoryFromCWD(config)
cli.SetVerbosity(config.Verbose)
cli.logfVerbose("config=%v", configTomlPath)
cli.logVerbose(config.Dump())
if err := config.Validate(); err != nil {
log.Fatal(err)
}
jobNameRegex, err := regexp.Compile(config.JobNameRegexp)
if err != nil {
log.Fatal(err)
}
client := NewClientWithConfig(ctx, &ClientConfig{
AccessToken: config.AccessToken,
Cache: config.Cache,
CacheDirectory: config.CacheDirectory,
})
listWorkflowRunsOpts := &github.ListWorkflowRunsOptions{
ListOptions: github.ListOptions{
PerPage: config.NumberOfJob,
},
}
cli.loglnVerbose("ListWorkflowRunsByFileName start")
workflowRuns, _, err := client.ListWorkflowRunsByFileName(ctx, config.Owner, config.Repository, config.WorkflowFileName, listWorkflowRunsOpts)
if err != nil {
log.Fatal(err)
}
cli.loglnVerbose("ListWorkflowRunsByFileName finish")
jobsByJobName := NewJobsByJobNameMap()
eg := new(errgroup.Group)
sem := make(chan struct{}, config.Concurrency)
for _, run := range workflowRuns.WorkflowRuns {
sem <- struct{}{}
eg.Go(func() error {
defer func() {
<-sem
}()
cli.logfVerbose("ListWorkflowJobs start: run_id=%d", *run.ID)
jobs, _, err := client.ListWorkflowJobs(ctx, config.Owner, config.Repository, *run.ID, nil)
if err != nil {
return err
}
cli.logfVerbose("ListWorkflowJobs finish: run_id=%d", *run.ID)
for _, job := range jobs.Jobs {
jobName := *job.Name
if !jobNameRegex.MatchString(jobName) {
continue
}
cli.logfVerbose("Job name (before replacement): %#v", jobName)
for _, rule := range config.Replace {
jobName = rule.Apply(jobName)
}
cli.logfVerbose("Job name (after replacement): %#v", jobName)
jobsByJobName.Append(jobName, job)
}
return nil
})
}
if err = eg.Wait(); err != nil {
log.Fatal(err)
}
profileResult := make(map[string][]*TaskStepProfile)
for jobName, jobs := range jobsByJobName.Iterate() {
if len(jobs) == 0 {
continue
}
var steps []*github.TaskStep
for _, job := range jobs {
steps = append(steps, job.Steps...)
}
stepProfile, err := ProfileTaskStep(steps)
if err != nil {
log.Fatal(err)
}
err = SortProfileBy(stepProfile, config.SortBy)
if err != nil {
log.Fatal(err)
}
// reverse slice
if config.Reverse {
reversedStepProfile := make(TaskStepProfileResult, len(stepProfile))
for i := 0; i < len(stepProfile); i++ {
j := len(stepProfile) - i - 1
reversedStepProfile[i] = stepProfile[j]
}
profileResult[jobName] = reversedStepProfile
} else {
profileResult[jobName] = stepProfile
}
}
var formatterInputJobNames []string
for k := range profileResult {
formatterInputJobNames = append(formatterInputJobNames, k)
}
sort.Strings(formatterInputJobNames)
var profileFormatterInput ProfileInput
for _, jobName := range formatterInputJobNames {
result := profileResult[jobName]
profileFormatterInput = append(profileFormatterInput, &ProfileForFormatter{
Name: jobName,
Profile: result,
})
}
WriteWithFormat(os.Stdout, profileFormatterInput, config.Format)
}