Class: HustleAndFlow::IssueTrackers::Github::Issue

Inherits:
Object
  • Object
show all
Defined in:
lib/hustle_and_flow/issue_trackers/github/issue.rb

Constant Summary collapse

DEFAULT_CATEGORY_LABELS =
%w{feature bug refactor test style chore docs spike}
ISSUE_ACTIONS =
%w{closes references fixes}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracker:, io: nil, data:) ⇒ Issue

Returns a new instance of Issue.



17
18
19
20
21
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 17

def initialize(tracker:, io: nil, data:)
  self.tracker = tracker
  self.io      = io || HustleAndFlow::Io::Shell.new
  self.data    = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 13

def data
  @data
end

#ioObject

Returns the value of attribute io.



13
14
15
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 13

def io
  @io
end

#trackerObject

Returns the value of attribute tracker.



13
14
15
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 13

def tracker
  @tracker
end

Class Method Details

.create(tracker: nil, io: nil, title:, body: '', **args) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 23

def self.create(tracker: nil, io: nil, title:, body: '', **args)
  new(io:      io,
      tracker: tracker,
      data:    tracker.client.create_issue(tracker.repo_name,
                                           title,
                                           body,
                                           args))
end

.from_branch_name(branch_name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 153

def self.from_branch_name(branch_name)
  branch_name = branch_name.dup
  branch_data = {}

  version_pattern = /\-v(\d+)\z/
  number_pattern  = /\-(\d+)\z/
  action_pattern  = /\-(closes|references|fixes)\z/
  type_pattern    = %r{\A(\w+)/}
  title_pattern   = /([\w\-]+)\z/

  branch_data[:version] = branch_name[version_pattern, 1] and branch_name.sub!(version_pattern, '')
  branch_data[:number]  = branch_name[number_pattern,  1] and branch_name.sub!(number_pattern,  '')
  branch_data[:action]  = branch_name[action_pattern,  1] and branch_name.sub!(action_pattern,  '')
  branch_data[:type]    = branch_name[type_pattern,    1] and branch_name.sub!(type_pattern,    '')
  branch_data[:title]   = branch_name[title_pattern,   1] and branch_name.sub!(title_pattern,   '')

  branch_data
end

Instance Method Details

#assign(to:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 37

def assign(to:)
  reassigned_issue_data = client.update_issue(tracker.repo_name,
                                              number,
                                              title,
                                              body,
                                              assignee: to)

  Issue.new(tracker: tracker,
            data:    reassigned_issue_data)
end

#assign_issue?(to:) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 70

def assign_issue?(to:)
  return self unless needs_reassignment?(assignee_to_assign_to: to)

  assign to: to
end

#assigned_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 84

def assigned_to?(other)
  assignee == other
end

#bodyObject



119
120
121
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 119

def body
  data[:body]
end

#categoryObject



48
49
50
51
52
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 48

def category
  @category ||= labels.find do |label|
    DEFAULT_CATEGORY_LABELS.include? label
  end
end

#closed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 76

def closed?
  status == 'closed'
end

#contactObject



107
108
109
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 107

def contact
  assignee_data[:login] || user_data[:login]
end

#convert_to_pull_request(branch: branch) ⇒ Object



147
148
149
150
151
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 147

def convert_to_pull_request(branch: branch)
  pr_data = client.create_pull_request_for_issue(tracker.repo_name, 'master', branch, number)

  PullRequest.new(tracker: tracker, data: pr_data)
end

#created_atObject



123
124
125
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 123

def created_at
  data[:created_at]
end

#has_body?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 66

def has_body?
  !(body.nil? || body.empty?)
end

#has_label?(other = []) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 60

def has_label?(other = [])
  other = !other.is_a?(Array) ? [other] : other

  (labels & other).any?
end

#match?(labels: nil, **args) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 88

def match?(labels: nil, **args)
  result     = true
  conditions = args

  args.delete(:version)

  if args.key?(:title)
    title = args.delete(:title)

    result &&= sanitize(title) == sanitize(self.title)
  end

  conditions.each do |key, value|
    result &&= public_send(key) == value
  end

  result &&= labels ? has_label?(labels) : true
end

#new?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 54

def new?
  five_minutes_ago = Time.now.utc - (5 * 60)

  five_minutes_ago <= created_at
end

#numberObject



115
116
117
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 115

def number
  data[:number]
end

#start(me: nil) ⇒ Object



32
33
34
35
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 32

def start(me: nil)
  reopen.
  assign_issue? to: me
end

#statusObject



131
132
133
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 131

def status
  data[:state]
end

#titleObject



111
112
113
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 111

def title
  data[:title]
end

#to_branch_name(action: 'closes', version: 1) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 135

def to_branch_name(action:  'closes',
                   version: 1)
  branch_name = []
  branch_name << category
  branch_name << base_branch_name(action:  action,
                                  version: version)

  branch_name.compact!

  branch_name.join('/')
end

#unassigned?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 80

def unassigned?
  assigned_to? 'unassigned'
end

#urlObject



127
128
129
# File 'lib/hustle_and_flow/issue_trackers/github/issue.rb', line 127

def url
  data[:html_url]
end