~cosmicrose/sourcehut_cli

55ea6405e2f6f425209b2913f5f15846baec5af4 — Rosa Richter 8 months ago 71c9683 main
Add some filtering flags
1 files changed, 46 insertions(+), 7 deletions(-)

M lib/sourcehut/cli/todo.ex
M lib/sourcehut/cli/todo.ex => lib/sourcehut/cli/todo.ex +46 -7
@@ 10,6 10,34 @@ defmodule Sourcehut.CLI.Todo do
          required: false
        ]
      ],
      options: [
        status: [
          value_name: "STATUS",
          short: "-s",
          long: "--status",
          help: "Filter by status: {reported|confimed|in_progress|pending|resolved} (default: no filter)"
        ],
        resolution: [
          value_name: "RESOLUTION",
          short: "-r",
          long: "--resolution",
          help: "Filter by resolution: {unresolved|fixed|implemented|wont_fix|by_design|invalid|duplicate|not_our_bug} (default: \"unresolved\"",
          parser: &{:ok, String.upcase(&1)},
          default: "UNRESOLVED"
        ],
        assignee: [
          value_name: "ASSIGNEE",
          short: "-a",
          long: "--assignee",
          help: "Filter by assignee"
        ],
        author: [
          value_name: "AUTHOR",
          short: "-A",
          long: "--author",
          help: "Filter by author"
        ]
      ],
      subcommands: [
        list_trackers: [
          name: "trackers",


@@ 21,13 49,13 @@ defmodule Sourcehut.CLI.Todo do

  def do_command({[:todo], opts}, config) do
    if opts.args[:tracker] do
      print_tracker_tickets(opts.args.tracker, config)
      print_tracker_tickets(opts.args.tracker, opts, config)
    else
      print_all_tickets(config)
    end
  end

  defp print_tracker_tickets(name, config) do
  defp print_tracker_tickets(name, opts, config) do
    {:ok, resp} =
      Neuron.query(
        """


@@ 42,9 70,12 @@ defmodule Sourcehut.CLI.Todo do
            },
            tickets {
              results {
                id, subject,
                id, subject, status, resolution
                assignees {
                  canonicalName
                },
                submitter {
                  canonicalName
                }
              }
            }


@@ 58,10 89,18 @@ defmodule Sourcehut.CLI.Todo do

    tracker = resp.body["data"]["trackerByName"]

    IO.puts(tracker["owner"]["canonicalName"] <> "/" <> tracker["name"])
    IO.puts("")

    Enum.each(tracker["tickets"]["results"], fn ticket ->
    tracker["tickets"]["results"]
    |> Enum.filter(fn ticket ->
      ticket["resolution"] == opts.options[:resolution]
    end)
    |> Enum.filter(fn ticket ->
      assignees = Enum.map(ticket["assignees"], &Access.get(&1, "canonicalName"))
      is_nil(opts.options[:assignee]) or ("~" <> opts.options[:assignee]) in assignees
    end)
    |> Enum.filter(fn ticket ->
      is_nil(opts.options[:author]) or ticket["submitter"]["canonicalName"] == ("~" <> opts.options[:author])
    end)
    |> Enum.each(fn ticket ->
      IO.puts("#{ticket["id"]}\t#{ticket["subject"]}")
    end)
  end