Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
implement issue creation by trello card
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Apr 2, 2014
1 parent 263c2bf commit 9a19c60
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/trellohub/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ def issue_labels
@lists.map(&:issue_label).compact
end

def repository_by(full_name: nil, milestone: nil)
def repository_by(full_name: nil, name: nil, milestone: nil)
case
when full_name
@repositories.find { |repo| repo.full_name == full_name }
when name
@repositories.find { |repo| repo.name == name }
when milestone
@repositories.find { |repo| repo.milestone == milestone }
end
Expand Down
4 changes: 4 additions & 0 deletions lib/trellohub/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def closed?
@state == 'closed'
end

def key?
!@key.nil?
end

def own_key
if @imported_from == :issue
@key
Expand Down
45 changes: 39 additions & 6 deletions lib/trellohub/form/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ def key_matcher
/synced_issue:\shttps?:\/\/.*?\/([\w\-\/]+)\/(?:issues|pulls)\/(\d+)/
end

def issue_creation_matcher
/^((?:[\w\-]+\/)?([\w\-]+))#\s/
end

def card_name_prefix_matcher
/^[\w\-]+#\d+\s/
/^[\w\-]+#(\d+)?\s/
end

def scrum_point_matcher
/\(\d+\)/
end

def build_card_attributes_by_card
Expand All @@ -69,8 +77,16 @@ def build_card_attributes_by_card
}.compact unless @origin_card.idMembers.empty?
end

def sanitized_card_name
@origin_card.name.gsub(scrum_point_matcher, '').strip
end

def card_name_without_prefix
sanitized_card_name.gsub(card_name_prefix_matcher, '').strip
end

def build_issue_attributes_by_card
@issue_title = @origin_card.name.gsub(card_name_prefix_matcher, '')
@issue_title = card_name_without_prefix
@issue_state = @state = @origin_card.closed ? 'closed' : 'open'

if @origin_card.desc =~ key_matcher
Expand All @@ -80,6 +96,11 @@ def build_issue_attributes_by_card

repo = Trellohub.repository_by(full_name: @issue_repository)
@issue_milestone = repo.milestone.title if repo.milestone?

elsif sanitized_card_name =~ issue_creation_matcher
search_key = $1 == $2 ? :name : :full_name
repo = Trellohub.repository_by(:"#{search_key}" => $1)
@issue_repository = repo.full_name if repo
end

unless @origin_card.idMembers.empty?
Expand Down Expand Up @@ -125,10 +146,22 @@ def card_id
@card_id
end

def card_update?
def card_id?
!card_id.nil?
end

def create_card?
open?
end

def update_card?
card_id? && open?
end

def update_card?
card_id? && close?
end

def create_card
Trell.create_card(to_valid_card)
end
Expand All @@ -143,9 +176,9 @@ def close_card

def save_as_card
case
when card_update? && open? then update_card
when card_update? && closed? then close_card
when open? then create_card
when update_card? then update_card
when close_card? then close_card
when create_card? then create_card
end
end

Expand Down
43 changes: 36 additions & 7 deletions lib/trellohub/form/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def import_issue(repository, issue)
def build_card_attributes_by_issue
@card_idBoard = Trellohub::Board.id
@card_name = "#{issue_repo_name}##{@origin_issue.number} #{@origin_issue.title}"
@card_desc = "synced_issue: #{Octokit.web_endpoint}#{@issue_repository}/issues/#{@origin_issue.number}"
@card_closed = @origin_issue.state == 'closed'
assign_card_desc_by_issue
assign_card_members_by_issue
assign_card_list_by_issue
end
Expand Down Expand Up @@ -127,12 +127,24 @@ def assign_card_list_by_issue
end
end

def issue_update?
!@key.nil?
def issue_repository?
!@issue_repository.nil?
end

def create_issue?
!key? && issue_repository? && open?
end

def update_issue?
key? && open?
end

def close_issue?
key? && close?
end

def issue_there?
!!Trellohub::Form.with_issues.find_by_key(@key) if @key
!!Trellohub::Form.with_issues.find_by_key(@key) if key?
end

def issue_body
Expand All @@ -144,13 +156,17 @@ def issue_body
@issue_body
end

def default_issue_body
"created by @#{card_create_user || card_update_user} in trello: #{@card_shortUrl}"
end

def create_issue
return if @issue_repository.nil? || @issue_title.nil?

Octokit.create_issue(
@issue_repository,
@issue_title,
nil,
default_issue_body,
to_valid_issue
)
end
Expand All @@ -177,10 +193,23 @@ def close_issue
)
end

def inject_issue_number_to_card_name(number = nil)
@card_name.gsub!(/#\s/, "##{@issue_number ||= number} ")
end

def assign_card_desc_by_issue
@card_desc = "synced_issue: #{Octokit.web_endpoint}#{@issue_repository}/issues/#{@issue_number}"
end

def save_as_issue
case
when issue_update? && open? then update_issue
when issue_update? && closed? then close_issue
when create_issue?
if issue = create_issue
inject_issue_number_to_card_name(issue.number)
assign_card_desc_by_issue
end
when update_issue? then update_issue
when close_issue? then close_issue
when open? then create_issue
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/trellohub/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Trellohub
class Repository < OpenStruct
def name
full_name.split('/').last
end

def issues
issues_with_state_all
rescue Octokit::UnprocessableEntity
Expand Down
11 changes: 10 additions & 1 deletion lib/trellohub/synchronal.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
module Trellohub
module Synchronal
def synchronize
fetch

synchronize_to_cards_from_issues
synchronize_to_issues_from_cards

Trellohub::Mocking.print_request_summary if Trellohub.dry_run

true
end
alias_method :sync, :synchronize

def fetch
Form.with_issues!
Form.with_cards!
end

def synchronize_to_cards_from_issues
Form.with_issues.each do |issue_form|
card_form = Form.with_cards.find_by_key(issue_form.key)
Expand All @@ -26,7 +35,7 @@ def synchronize_to_issues_from_cards
issue_form = Form.with_issues.find_by_key(card_form.key)

case
when issue_form.nil? && card_form.issue_update?
when issue_form.nil? && card_form.update_issue?
card_form.delete
when issue_form.nil?
card_form.save_as_issue
Expand Down

0 comments on commit 9a19c60

Please sign in to comment.