Skip to content

Commit

Permalink
Merge pull request #13 from rsese/rsese-status-flag
Browse files Browse the repository at this point in the history
add status flag
  • Loading branch information
rsese authored May 15, 2023
2 parents 0944d83 + 2c637d0 commit e65ad25
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type options struct {
Repositories []string
Last time.Duration
Selector string
Status string
}

func workflowHealth(w workflow) string {
Expand Down Expand Up @@ -267,7 +268,7 @@ func _main(opts *options) error {
totalBillableMs := 0

for _, r := range repos {
workflows, err := getWorkflows(*r, last)
workflows, err := getWorkflows(*r, last, opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -352,7 +353,7 @@ func getAllRepos(path string) ([]*repositoryData, error) {
return repoData, nil
}

func getWorkflows(repoData repositoryData, last time.Duration) ([]*workflow, error) {
func getWorkflows(repoData repositoryData, last time.Duration, opts *options) ([]*workflow, error) {
workflowsPath := fmt.Sprintf("repos/%s/actions/workflows", repoData.Name)

stdout, _, err := gh.Exec("api", "--cache", defaultApiCacheTime, workflowsPath, "--jq", ".workflows")
Expand Down Expand Up @@ -403,7 +404,13 @@ func getWorkflows(repoData repositoryData, last time.Duration) ([]*workflow, err
continue
}

runsPath := fmt.Sprintf("%s/runs", w.URL)
var runsPath string
if opts.Status != "" {
runsPath = fmt.Sprintf("%s/runs?status=%s", w.URL, opts.Status)
} else {
runsPath = fmt.Sprintf("%s/runs", w.URL)
}

stdout, _, err = gh.Exec("api", "--cache", defaultApiCacheTime, runsPath, "--jq", ".workflow_runs")
if err != nil {
return nil, fmt.Errorf("could not call gh: %w", err)
Expand Down Expand Up @@ -462,6 +469,7 @@ func parseArgs() (*options, error) {

repositories := flag.StringSliceP("repos", "r", []string{}, "One or more repository names from the provided org or user")
last := flag.StringP("last", "l", "30d", "What period of time to cover in hours (eg 1h) or days (eg 30d). Default: 30d")
runStatus := flag.StringP("status", "s", "", "What workflow run status (eg completed, cancelled, failure, success) to query for")

flag.Parse()

Expand Down Expand Up @@ -522,6 +530,7 @@ func parseArgs() (*options, error) {
Repositories: *repositories,
Last: duration,
Selector: selector,
Status: *runStatus,
}, nil
}

Expand Down

0 comments on commit e65ad25

Please sign in to comment.