Skip to content

Commit

Permalink
Merge pull request #41 from IvanRibakov/feature/filter_multiple_statuses
Browse files Browse the repository at this point in the history
Allow filtering workflows by more than one status value
  • Loading branch information
fchimpan authored Oct 28, 2024
2 parents 4ec798f + cba9515 commit 78b1872
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
actor string
branch string
event string
status string
status []string
created string
headSHA string
excludePullRequests bool
Expand Down Expand Up @@ -81,7 +81,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&actor, "actor", "a", "", "Workflow run actor")
rootCmd.PersistentFlags().StringVarP(&branch, "branch", "b", "", "Workflow run branch. Returns workflow runs associated with a branch. Use the name of the branch of the push.")
rootCmd.PersistentFlags().StringVarP(&event, "event", "e", "", "Workflow run event. e.g. push, pull_request, pull_request_target, etc.\n See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows")
rootCmd.PersistentFlags().StringVarP(&status, "status", "s", "", "Workflow run status. e.g. completed, in_progress, queued, etc.\n See https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository")
rootCmd.PersistentFlags().StringSliceVarP(&status, "status", "s", []string{""}, "Workflow run status. e.g. completed, in_progress, queued, etc.\n Multiple values can be provided separated by a comma. For a full list of supported values see https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository")
rootCmd.PersistentFlags().StringVarP(&created, "created", "c", "", "Workflow run createdAt. Returns workflow runs created within the given date-time range.\n For more information on the syntax, see https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates")
rootCmd.PersistentFlags().StringVarP(&headSHA, "head-sha", "S", "", "Workflow run head SHA")
rootCmd.PersistentFlags().BoolVarP(&excludePullRequests, "exclude-pull-requests", "x", false, "Workflow run exclude pull requests")
Expand Down
63 changes: 45 additions & 18 deletions cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"fmt"
"io"
"os"
"slices"

"github.com/fchimpan/gh-workflow-stats/internal/github"
"github.com/fchimpan/gh-workflow-stats/internal/parser"
"github.com/fchimpan/gh-workflow-stats/internal/printer"

go_github "github.com/google/go-github/v60/github"
)

const (
Expand All @@ -31,7 +34,7 @@ type options struct {
actor string
branch string
event string
status string
status []string
created string
headSHA string
excludePullRequests bool
Expand Down Expand Up @@ -62,23 +65,7 @@ func workflowStats(cfg config, opt options, isJobs bool) error {
defer s.Stop()

isRateLimit := false
runs, err := client.FetchWorkflowRuns(ctx, &github.WorkflowRunsConfig{
Org: cfg.org,
Repo: cfg.repo,
WorkflowFileName: cfg.workflowFileName,
WorkflowID: cfg.workflowID,
}, &github.WorkflowRunsOptions{
All: opt.all,
Actor: opt.actor,
Branch: opt.branch,
Event: opt.event,
Status: opt.status,
Created: opt.created,
HeadSHA: opt.headSHA,
ExcludePullRequests: opt.excludePullRequests,
CheckSuiteID: opt.checkSuiteID,
},
)
runs, err := fetchWorkflowRuns(ctx, client, cfg, opt)
if err != nil {
if errors.As(err, &github.RateLimitError{}) {
isRateLimit = true
Expand Down Expand Up @@ -138,3 +125,43 @@ func workflowStats(cfg config, opt options, isJobs bool) error {

return nil
}

func fetchWorkflowRuns(ctx context.Context, client *github.WorkflowStatsClient, cfg config, opt options) ([]*go_github.WorkflowRun, error) {
// Intentionally not using Github API status filter as it applies only to the last run attempt.
// Instead retrieving all qualifying workflow runs and their run attempts and filtering by status manually (if needed)
runs, err := client.FetchWorkflowRuns(ctx, &github.WorkflowRunsConfig{
Org: cfg.org,
Repo: cfg.repo,
WorkflowFileName: cfg.workflowFileName,
WorkflowID: cfg.workflowID,
}, &github.WorkflowRunsOptions{
All: opt.all,
Actor: opt.actor,
Branch: opt.branch,
Event: opt.event,
Status: "",
Created: opt.created,
HeadSHA: opt.headSHA,
ExcludePullRequests: opt.excludePullRequests,
CheckSuiteID: opt.checkSuiteID,
},
)
if err == nil {
return filterRunAttemptsByStatus(runs, opt.status), nil
} else {
return nil, err
}
}

func filterRunAttemptsByStatus(runs []*go_github.WorkflowRun, status []string) []*go_github.WorkflowRun {
if len(status) == 0 || (len(status) == 1 && status[0] == "") {
return runs
}
filteredRuns := []*go_github.WorkflowRun{}
for _, r := range runs {
if (r.Status != nil && slices.Contains(status, *r.Status)) || (r.Conclusion != nil && slices.Contains(status, *r.Conclusion)) {
filteredRuns = append(filteredRuns, r)
}
}
return filteredRuns
}

0 comments on commit 78b1872

Please sign in to comment.