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 workflow runs by date range in gh run list #9042

Open
hatsu38 opened this issue May 3, 2024 · 2 comments
Open

Allow filtering workflow runs by date range in gh run list #9042

hatsu38 opened this issue May 3, 2024 · 2 comments
Labels
enhancement a request to improve CLI gh-run relating to the gh run command

Comments

@hatsu38
Copy link

hatsu38 commented May 3, 2024

Describe the feature or problem you'd like to solve

The current gh run list command does not allow filtering workflow runs within a specific date range. While gh run list --created 2024-05-03 works to filter by a single date, it is not possible to search across multiple dates or a date range.
I would like to be able to view a list of jobs within a one-month period using this feature.

Proposed solution

We propose adding options like --from and --to to the gh run list command, which would allow displaying only the workflow runs within the specified date range.

Additional context

I attempted to search for a one-month list of jobs and then calculate the usage time per job for that month, but the lack of date range filtering made this task cumbersome.

@hatsu38 hatsu38 added the enhancement a request to improve CLI label May 3, 2024
@cliAutomation cliAutomation added the needs-triage needs to be reviewed label May 3, 2024
@hatsu38
Copy link
Author

hatsu38 commented May 3, 2024

I may have found a solution.
By doing gh run list --limit 10 --created '2024-04-01T00:00:00..2024-04-30T23:59:59', it seems possible to search within a specified range.

@andyfeller
Copy link
Contributor

andyfeller commented May 7, 2024

@hatsu38 : Thank you for opening up this idea as well as sharing what you learned figuring it out! ❤

As you discovered, gh run list --created calls "List workflow runs for a workflow" endpoint specifying the created field which states:

Returns workflow runs created within the given date-time range. For more information on the syntax, see "Understanding the search syntax."

func GetRuns(client *api.Client, repo ghrepo.Interface, opts *FilterOptions, limit int) (*RunsPayload, error) {
path := fmt.Sprintf("repos/%s/actions/runs", ghrepo.FullName(repo))
if opts != nil && opts.WorkflowID > 0 {
path = fmt.Sprintf("repos/%s/actions/workflows/%d/runs", ghrepo.FullName(repo), opts.WorkflowID)
}
perPage := limit
if limit > 100 {
perPage = 100
}
path += fmt.Sprintf("?per_page=%d", perPage)
path += "&exclude_pull_requests=true" // significantly reduces payload size
if opts != nil {
if opts.Branch != "" {
path += fmt.Sprintf("&branch=%s", url.QueryEscape(opts.Branch))
}
if opts.Actor != "" {
path += fmt.Sprintf("&actor=%s", url.QueryEscape(opts.Actor))
}
if opts.Status != "" {
path += fmt.Sprintf("&status=%s", url.QueryEscape(opts.Status))
}
if opts.Event != "" {
path += fmt.Sprintf("&event=%s", url.QueryEscape(opts.Event))
}
if opts.Created != "" {
path += fmt.Sprintf("&created=%s", url.QueryEscape(opts.Created))
}
if opts.Commit != "" {
path += fmt.Sprintf("&head_sha=%s", url.QueryEscape(opts.Commit))
}
}

Suggestion

The gh run list --help documentation could do a better job clarifying --created takes more than a simple date as well as listing some examples:

$ gh run list --help
List recent workflow runs

For more information about output formatting flags, see `gh help formatting`.

USAGE
  gh run list [flags]

ALIASES
  ls

FLAGS
  -b, --branch string     Filter runs by branch
  -c, --commit SHA        Filter runs by the SHA of the commit
      --created date      Filter runs by the date it was created
  -e, --event event       Filter runs by which event triggered the run
  -q, --jq expression     Filter JSON output using a jq expression
      --json fields       Output JSON with the specified fields
  -L, --limit int         Maximum number of runs to fetch (default 20)
  -s, --status string     Filter runs by status: {queued|completed|in_progress|requested|waiting|action_required|cancelled|failure|neutral|skipped|stale|startup_failure|success|timed_out}
  -t, --template string   Format JSON output using a Go template; see "gh help formatting"
  -u, --user string       Filter runs by user who triggered the run
  -w, --workflow string   Filter runs by workflow

INHERITED FLAGS
      --help                     Show help for command
  -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format

LEARN MORE
  Use `gh <command> <subcommand> --help` for more information about a command.
  Read the manual at https://cli.github.com/manual

More specifically:

      --created date      Filter runs by the date it was created

@hatsu38 : is there anything else you'd suggest for improving the experience?

@andyfeller andyfeller added gh-run relating to the gh run command and removed needs-triage needs to be reviewed labels May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a request to improve CLI gh-run relating to the gh run command
Projects
None yet
Development

No branches or pull requests

4 participants
@andyfeller @hatsu38 @cliAutomation and others