Skip to content

Commit b2fdc9d

Browse files
Jasstknalexellis
authored andcommitted
Feat: allow to run for user
Signed-off-by: Mariia Kotliarevskaia <mariia.kotliarevskaia@gmail.com>
1 parent 14f657c commit b2fdc9d

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

main.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
func main() {
1818

1919
var (
20-
orgName, token string
21-
since int
20+
orgName, userName, token string
21+
since int
2222
)
2323

2424
flag.StringVar(&orgName, "org", "", "Organization name")
25+
flag.StringVar(&userName, "user", "", "User name")
2526
flag.StringVar(&token, "token", "", "GitHub token")
2627
flag.IntVar(&since, "since", 30, "Since when to fetch the data (in days)")
2728

@@ -31,11 +32,11 @@ func main() {
3132
&oauth2.Token{AccessToken: token},
3233
))
3334

34-
if orgName == "" {
35-
log.Fatal("Organization name is required")
36-
}
37-
if token == "" {
38-
log.Fatal("GitHub token is required")
35+
switch {
36+
case orgName == "" && userName == "":
37+
log.Fatal("Organization name or username is required")
38+
case orgName != "" && userName != "":
39+
log.Fatal("Only org or username must be specified at the same time")
3940
}
4041

4142
client := github.NewClient(auth)
@@ -52,13 +53,24 @@ func main() {
5253

5354
fmt.Printf("Fetching last %d days of data (created>=%s)\n", since, created.Format("2006-01-02"))
5455

55-
var allRepos []*github.Repository
56+
var repos, allRepos []*github.Repository
57+
var res *github.Response
58+
var err error
5659
ctx := context.Background()
5760
page := 0
5861
for {
59-
opts := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{Page: page, PerPage: 100}, Type: "all"}
6062
log.Printf("Fetching repos %s page %d", orgName, page)
61-
repos, res, err := client.Repositories.ListByOrg(ctx, orgName, opts)
63+
if orgName != "" {
64+
opts := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{Page: page, PerPage: 100}, Type: "all"}
65+
log.Printf("Fetching repos %s page %d", orgName, page)
66+
repos, res, err = client.Repositories.ListByOrg(ctx, orgName, opts)
67+
}
68+
69+
if userName != "" {
70+
opts := &github.RepositoryListOptions{ListOptions: github.ListOptions{Page: page, PerPage: 100}, Type: "all"}
71+
repos, res, err = client.Repositories.List(ctx, userName, opts)
72+
}
73+
6274
if err != nil {
6375
log.Fatal(err)
6476
}
@@ -98,8 +110,19 @@ func main() {
98110

99111
opts := &github.ListWorkflowRunsOptions{Created: createdQuery, ListOptions: github.ListOptions{Page: page, PerPage: 100}}
100112

113+
var runs *github.WorkflowRuns
101114
log.Printf("Listing workflow runs for: %s", repo.GetFullName())
102-
runs, res, err := client.Actions.ListRepositoryWorkflowRuns(ctx, orgName, repo.GetName(), opts)
115+
if orgName != "" {
116+
runs, res, err = client.Actions.ListRepositoryWorkflowRuns(ctx, orgName, repo.GetName(), opts)
117+
}
118+
if userName != "" {
119+
realOwner := userName
120+
// if user is a member of repository
121+
if userName != *repo.Owner.Login {
122+
realOwner = *repo.Owner.Login
123+
}
124+
runs, res, err = client.Actions.ListRepositoryWorkflowRuns(ctx, realOwner, repo.GetName(), opts)
125+
}
103126
if err != nil {
104127
log.Fatal(err)
105128
}
@@ -118,7 +141,14 @@ func main() {
118141
}
119142
totalRuns += len(workflowRuns)
120143

121-
log.Printf("Found %d workflow runs for %s/%s", len(workflowRuns), orgName, repo.GetName())
144+
var entity string
145+
if orgName != "" {
146+
entity = orgName
147+
}
148+
if userName != "" {
149+
entity = userName
150+
}
151+
log.Printf("Found %d workflow runs for %s/%s", len(workflowRuns), entity, repo.GetName())
122152

123153
for _, run := range workflowRuns {
124154
log.Printf("Fetching jobs for: run ID: %d, startedAt: %s, conclusion: %s", run.GetID(), run.GetRunStartedAt().Format("2006-01-02 15:04:05"), run.GetConclusion())
@@ -127,7 +157,7 @@ func main() {
127157
page := 0
128158
for {
129159
log.Printf("Fetching jobs for: %d, page %d", run.GetID(), page)
130-
jobs, res, err := client.Actions.ListWorkflowJobs(ctx, orgName,
160+
jobs, res, err := client.Actions.ListWorkflowJobs(ctx, entity,
131161
repo.GetName(),
132162
run.GetID(),
133163
&github.ListWorkflowJobsOptions{Filter: "all", ListOptions: github.ListOptions{Page: page, PerPage: 100}})

0 commit comments

Comments
 (0)