Skip to content

Commit

Permalink
Merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bougyman committed Feb 6, 2024
2 parents ada3936 + 44ef689 commit 9daeb8d
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 145 deletions.
6 changes: 5 additions & 1 deletion exe/lc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

exec File.join(__dir__, 'lc.sh'), *ARGV
require 'pathname'
script_dir = Pathname(__dir__).join('scripts')
basename = File.basename(__FILE__)
script = script_dir.join(basename).exist? ? script_dir.join(basename) : script_dir.join("#{basename}.sh")
exec script.to_s, *ARGV
4 changes: 0 additions & 4 deletions exe/lclose

This file was deleted.

1 change: 1 addition & 0 deletions exe/lclose
4 changes: 0 additions & 4 deletions exe/lcls

This file was deleted.

1 change: 1 addition & 0 deletions exe/lcls
2 changes: 0 additions & 2 deletions exe/lcls.sh

This file was deleted.

4 changes: 0 additions & 4 deletions exe/lcreate

This file was deleted.

1 change: 1 addition & 0 deletions exe/lcreate
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions exe/scripts/lcls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exec lc issue list "$@"
2 changes: 2 additions & 0 deletions exe/scripts/lcomment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exec linear-cli issue update --comment - "$@"
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/linear/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call(body)

def query(query)
call format('{ "query": %s }', query.to_s.to_json)
rescue StandardError => e
rescue SmellsBad => e
logger.error('Error in query', query:, error: e)
raise e unless Rubyists::Linear.verbosity > 2

Expand Down
72 changes: 5 additions & 67 deletions lib/linear/cli/sub_commands.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# frozen_string_literal: true

# This is where all the _for methods live
require_relative 'what_for'

module Rubyists
module Linear
module CLI
# The SubCommands module should be included in all commands with subcommands
module SubCommands
include CLI::WhatFor

def self.included(mod)
mod.instance_eval do
def const_added(const)
Expand Down Expand Up @@ -41,73 +46,6 @@ def prompt
@prompt ||= CLI.prompt
end

def team_for(key = nil)
return Rubyists::Linear::Team.find(key) if key

ask_for_team
end

def reason_for(reason = nil, four: nil)
return reason if reason

question = four ? "Reason for #{four}:" : 'Reason:'
prompt.ask(question)
end

def cancelled_state_for(thingy)
states = thingy.cancelled_states
return states.first if states.size == 1

selection = prompt.select('Choose a cancelled state', states.to_h { |s| [s.name, s.id] })
Rubyists::Linear::WorkflowState.find selection
end

def completed_state_for(thingy)
states = thingy.completed_states
return states.first if states.size == 1

selection = prompt.select('Choose a completed state', states.to_h { |s| [s.name, s.id] })
Rubyists::Linear::WorkflowState.find selection
end

def description_for(description = nil)
return description if description

prompt.multiline('Description:').map(&:chomp).join('\\n')
end

def title_for(title = nil)
return title if title

prompt.ask('Title:')
end

def labels_for(team, labels = nil)
return Rubyists::Linear::Label.find_all_by_name(labels.map(&:strip)) if labels

prompt.on(:keypress) do |event|
prompt.trigger(:keydown) if event.value == 'j'
prompt.trigger(:keyup) if event.value == 'k'
end
prompt.multi_select('Labels:', team.labels.to_h { |t| [t.name, t] })
end

def cut_branch!(branch_name)
if current_branch != default_branch
prompt.yes?("You are not on the default branch (#{default_branch}). Do you want to checkout #{default_branch} and create a new branch?") && git.checkout(default_branch) # rubocop:disable Layout/LineLength
end
git.branch(branch_name)
end

def branch_for(branch_name)
logger.trace('Looking for branch', branch_name:)
existing = git.branches[branch_name]
return cut_branch!(branch_name) unless existing

logger.trace('Branch found', branch: existing&.name)
existing
end

def current_branch
git.current_branch
end
Expand Down
79 changes: 79 additions & 0 deletions lib/linear/cli/what_for.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module Rubyists
module Linear
module CLI
# Module for the _for methods
module WhatFor
def editor_for(prefix)
file = Tempfile.open(prefix, Rubyists::Linear.tmpdir)
TTY::Editor.open(file.path)
file.close
File.readlines(file.path).map(&:chomp).join('\\n')
ensure
file&.close
end

def comment_for(issue, comment)
return comment unless comment.nil? || comment == '-'

comment = prompt.ask("Comment for #{issue.identifier} - #{issue.title} (- to open an editor)", default: '-')
return comment unless comment == '-'

editor_for %w[comment .md]
end

def team_for(key = nil)
return Rubyists::Linear::Team.find(key) if key

ask_for_team
end

def reason_for(reason = nil, four: nil)
return reason if reason

question = four ? "Reason for #{four}:" : 'Reason:'
prompt.ask(question)
end

def cancelled_state_for(thingy)
states = thingy.cancelled_states
return states.first if states.size == 1

selection = prompt.select('Choose a cancelled state', states.to_h { |s| [s.name, s.id] })
Rubyists::Linear::WorkflowState.find selection
end

def completed_state_for(thingy)
states = thingy.completed_states
return states.first if states.size == 1

selection = prompt.select('Choose a completed state', states.to_h { |s| [s.name, s.id] })
Rubyists::Linear::WorkflowState.find selection
end

def description_for(description = nil)
return description if description

prompt.multiline('Description:').map(&:chomp).join('\\n')
end

def title_for(title = nil)
return title if title

prompt.ask('Title:')
end

def labels_for(team, labels = nil)
return Rubyists::Linear::Label.find_all_by_name(labels.map(&:strip)) if labels

prompt.on(:keypress) do |event|
prompt.trigger(:keydown) if event.value == 'j'
prompt.trigger(:keyup) if event.value == 'k'
end
prompt.multi_select('Labels:', team.labels.to_h { |t| [t.name, t] })
end
end
end
end
end
5 changes: 3 additions & 2 deletions lib/linear/commands/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Issue
}.freeze

def issue_comment(issue, comment)
issue.add_comment comment
issue.add_comment comment_for(issue, comment)
prompt.ok "Comment added to #{issue.identifier}"
end

Expand Down Expand Up @@ -121,7 +121,8 @@ def make_da_issue!(**options)
description = description_for(options[:description])
team = team_for(options[:team])
labels = labels_for(team, options[:labels])
Rubyists::Linear::Issue.create(title:, description:, team:, labels:)
project = project_for(team, options[:project])
Rubyists::Linear::Issue.create(title:, description:, team:, labels:, project:)
end

def gimme_da_issue!(issue_id, me: Rubyists::Linear::User.me) # rubocop:disable Naming/MethodParameterName
Expand Down
1 change: 1 addition & 0 deletions lib/linear/commands/issue/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Create
option :description, type: :string, aliases: ['-d'], desc: 'Issue Description'
option :team, type: :string, aliases: ['-T'], desc: 'Team Identifier'
option :labels, type: :array, aliases: ['-l'], desc: 'Labels for the issue (Comma separated list)'
option :project, type: :string, aliases: ['-p'], desc: 'Project Identifier'
option :develop, type: :boolean, aliases: ['-D', '--dev'], desc: 'Start development after creating the issue'

def call(**options)
Expand Down
9 changes: 5 additions & 4 deletions lib/linear/commands/issue/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ class Update
include Rubyists::Linear::CLI::Issue # for #gimme_da_issue! and other Issue methods
desc 'Update an issue'
argument :issue_ids, type: :array, required: true, desc: 'Issue IDs (i.e. CRY-1)'
option :comment, type: :string, aliases: ['-m'], desc: 'Comment to add to the issue'
option :pr, type: :boolean, aliases: ['--pull-request'], default: false, desc: 'Create a pull request'
option :comment, type: :string, aliases: ['-m'], desc: 'Comment to add to the issue. - openan editor'
option :project, type: :string, aliases: ['-p'], desc: 'Project to move the issue to. - select from a list'
option :cancel, type: :boolean, default: false, desc: 'Cancel the issue'
option :close, type: :boolean, default: false, desc: 'Close the issue'
option :reason, type: :string, aliases: ['--butwhy'], desc: 'Reason for closing the issue'
option :reason, type: :string, aliases: ['--butwhy'], desc: 'Reason for closing the issue. - open an editor'
option :trash,
type: :boolean,
default: false,
desc: 'Also trash the issue (--close and --cancel support this option)'

example [
'--comment "This is a comment" CRY-1 CRY2 # Add a comment to multiple issues',
'--pr CRY-10 # Create a pull request for the issue',
'--comment - CRY-1 CRY2 # Add a comment to multiple issues, open an editor',
'--project "Manhattan" CRY-3 CRY-4 # Move tickets to a different project',
'--close CRY-2 # Close an issue. Will be prompted for a reason',
'--close --reason "Done" CRY-1 CRY-2 # Close multiple issues with a reason',
'--cancel --trash --reason "Garbage" CRY-2 # Cancel an issue, and throw it in the trash'
Expand Down
50 changes: 41 additions & 9 deletions lib/linear/models/base_model/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,55 @@ module Linear
class BaseModel
# Class methods for Linear models.
module ClassMethods
def many_to_one(relation, klass)
def setter!(relation, klass)
define_method "#{relation}=" do |val|
hash = val.is_a?(Hash) ? val : val.updated_data
updated_data[relation] = hash
instance_variable_set("@#{relation}", Rubyists::Linear.const_get(klass).new(hash))
end
end

def getter!(relation)
define_method relation do
return instance_variable_get("@#{relation}") if instance_variable_defined?("@#{relation}")
return unless (val = data[relation])

instance_variable_set("@#{relation}", Rubyists::Linear.const_get(klass).new(val))
return unless (val = updated_data[relation])

send("#{relation}=", val)
end
end

def many_to_one(relation, klass = nil)
klass ||= relation.to_s.camelize.to_sym
getter! relation
setter! relation, klass
end

alias one_to_one many_to_one

def many_setter!(relation, klass)
define_method "#{relation}=" do |val|
hash = val.is_a?(Hash) ? val : val.data
updated_data[relation] = hash
instance_variable_set("@#{relation}", Rubyists::Linear.const_get(klass).new(hash))
vals = if val&.key?(:nodes)
val[:nodes]
else
Array(val)
end
updated_data[relation] = vals.map { |v| v.is_a?(Hash) ? v : v.updated_data }
new_relations = vals.map { |v| v.is_a?(Hash) ? Rubyists::Linear.const_get(klass).new(v) : v }
instance_variable_set("@#{relation}", new_relations)
end
end

alias one_to_one many_to_one
def one_to_many(relation, klass = nil)
klass ||= relation.to_s.singularize.camelize.to_sym
getter! relation
many_setter! relation, klass
end

def find(id_val)
camel_name = just_name.camelize :lower
bf = base_fragment
query_data = Api.query(query { __node(camel_name, id: id_val) { ___ bf } })
ff = full_fragment
query_data = Api.query(query { __node(camel_name, id: id_val) { ___ ff } })
new query_data[camel_name.to_sym]
end

Expand Down Expand Up @@ -63,6 +91,10 @@ def base_fragment
const_get(:Base)
end

def full_fragment
base_fragment
end

def basic_filter
return const_get(:BASIC_FILTER) if const_defined?(:BASIC_FILTER)

Expand Down
Loading

0 comments on commit 9daeb8d

Please sign in to comment.