Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering workflows by more than one status value #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Owner

@fchimpan fchimpan Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought fetchWorkflowRuns function could be called directly within the client.FetchWorkflowRuns method rather than being defined as a separate function. Is there a specific reason why it was defined as a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason why it was defined as a function?

Mainly to avoid bloating workflowStats function which is already fairly long. Feel free to adjust formatting to your liking.

// 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
}