Skip to content

Commit

Permalink
Filter issue list by project (#7)
Browse files Browse the repository at this point in the history
* add --project filter

* WIP support methods for filtering issue list by Project

* Allow projects to be found with ID or URL as well

* move project finder methods to separate module

* Use the new common project finder methods in CLI::Projects

* Appease the cyclic complexity gods

* tests around Project#match_score?

* Need to include CLI::SubCommands here to get #prompt support

* improve comment

* Abstract #project_for so it can be used in both Listing Issues and Creating Issues

* removed by rubocop

* prefer #fetch so we know if we try to query on a nonexistant key

* remove the safe-nav operator that could end up doing the exact opposite of what I want

* chore: Remove Gemfile.lock

* fix(linting): Corrects rubocop offenses

---------

Co-authored-by: Tj (bougyman) Vanderpoel <[email protected]>
  • Loading branch information
xunker and bougyman authored Sep 18, 2024
1 parent c4c4349 commit 72eeb05
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 288 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
# dotenv
.envrc

# built gems
# Gem stuff/Gemfile.lock
*.gem
Gemfile.lock
252 changes: 0 additions & 252 deletions Gemfile.lock

This file was deleted.

1 change: 0 additions & 1 deletion lib/linear.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def self.verbosity=(debug)
@verbosity = debug
level = @verbosity > (DEBUG_LEVELS.size - 1) ? :trace : DEBUG_LEVELS[@verbosity]
SemanticLogger.default_level = level
@verbosity
end
end
end
35 changes: 35 additions & 0 deletions lib/linear/cli/projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Rubyists
module Linear
module CLI
# The Project module contains support for finding and selecting projects
# as part of a filter or query
module Projects
def ask_for_projects(projects, search: true)
prompt.warn("No project found matching #{search}.") if search
return projects.first if projects.size == 1

prompt.select('Project:', projects.to_h { |p| [p.name, p] })
end

def project_scores(projects, search_term)
projects.select { |p| p.match_score?(search_term).positive? }.sort_by { |p| p.match_score?(search_term) }
end

def project_for(project = nil, projects: Project.all)
return nil if projects.empty?

possibles = project ? project_scores(projects, project) : []
return ask_for_projects(projects, search: project) if possibles.empty?

first = possibles.first
return first if first.match_score?(project) == 100

selections = possibles + (projects - possibles)
prompt.select('Project:', selections.to_h { |p| [p.name, p] }) if possibles.size.positive?
end
end
end
end
end
29 changes: 3 additions & 26 deletions lib/linear/cli/what_for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Rubyists
module Linear
module CLI
# Module for the _for methods
module WhatFor # rubocop:disable Metrics/ModuleLength
module WhatFor
include CLI::Projects # for methods called within #project_for

# TODO: Make this configurable
PR_TYPES = {
fix: 'Bug fixes',
Expand Down Expand Up @@ -87,31 +89,6 @@ def title_for(title = nil)
prompt.ask('Title:')
end

def ask_for_projects(projects, search: true)
prompt.warn("No project found matching #{search}.") if search
return projects.first if projects.size == 1

prompt.select('Project:', projects.to_h { |p| [p.name, p] })
end

def project_scores(projects, search_term)
projects.select { |p| p.match_score?(search_term).positive? }.sort_by { |p| p.match_score?(search_term) }
end

def project_for(team, project = nil) # rubocop:disable Metrics/AbcSize
projects = team.projects
return nil if projects.empty?

possibles = project ? project_scores(projects, project) : []
return ask_for_projects(projects, search: project) if possibles.empty?

first = possibles.first
return first if first.match_score?(project) == 100

selections = possibles + (projects - possibles)
prompt.select('Project:', selections.to_h { |p| [p.name, p] }) if possibles.size.positive?
end

def pr_title_for(issue)
proposed = [pr_type_for(issue)]
proposed_scope = pr_scope_for(issue.title)
Expand Down
4 changes: 2 additions & 2 deletions lib/linear/commands/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def issue_pr(issue, **options)
end

def attach_project(issue, project_search)
project = project_for(issue.team, project_search)
project = project_for(project_search, projects: issue.team.projects)
issue.attach_to_project project
prompt.ok "#{issue.identifier} was attached to #{project.name}"
end
Expand All @@ -88,7 +88,7 @@ def make_da_issue!(**options)
description = description_for(options[:description])
team = team_for(options[:team])
labels = labels_for(team, options[:labels])
project = project_for(team, options[:project])
project = project_for(options[:project], projects: team.projects)
Rubyists::Linear::Issue.create(title:, description:, team:, labels:, project:)
end

Expand Down
Loading

0 comments on commit 72eeb05

Please sign in to comment.