Class: Gitcycle

Inherits:
Object
  • Object
show all
Defined in:
lib/gitcycle.rb

Constant Summary collapse

API =
if ENV['ENV'] == 'development'
  "http://127.0.0.1:8080/api"
else
  "http://gitcycle.bleacherreport.com/api"
end

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Gitcycle

Returns a new instance of Gitcycle.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitcycle.rb', line 25

def initialize(args=nil)
  if ENV['CONFIG']
    @config_path = File.expand_path(ENV['CONFIG'])
  else
    @config_path = File.expand_path("~/.gitcycle.yml")
  end

  load_config
  load_git

  start(args) if args
end

Instance Method Details

#create_branch(url_or_title) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gitcycle.rb', line 38

def create_branch(url_or_title)
  require_git && require_config

  params = {}

  if url_or_title.strip[0..3] == 'http'
    if url_or_title.include?('lighthouseapp.com/')
      params = { 'branch[lighthouse_url]' => url_or_title }
    elsif url_or_title.include?('github.com/')
      params = { 'branch[issue_url]' => url_or_title }
    end
  else
    params = {
      'branch[name]' => url_or_title,
      'branch[title]' => url_or_title
    }
  end

  puts "\nRetrieving branch information from gitcycle.\n".green
  branch = get('branch', params)

  name = branch['name']
  owner, repo = branch['repo'].split(':')

  unless branch['exists']
    branch['home'] = 
    branch['source'] = branches(:current => true)

    unless yes?("\nYour work will eventually merge into '#{branch['source']}'. Is this correct?")
      branch['source'] = q("What branch would you like to eventually merge into?")
    end

    unless yes?("Would you like to name your branch '#{name}'?")
      name = q("\nWhat would you like to name your branch?")
      name = name.gsub(/[\s\W]/, '-')
    end

    checkout_remote_branch(
      :owner => owner,
      :repo => repo,
      :branch => branch['source'],
      :target => name
    )
  end

  if branch['exists']
    if branches(:match => name)
      puts "Checking out branch '#{name}'.\n".green
      run("git checkout #{name}")
    else
      puts "Tracking branch '#{name}'.\n".green
      run("git fetch && git checkout -b #{name} origin/#{name}")
    end
  else
    puts "Sending branch information to gitcycle.".green
    get('branch',
      'branch[home]' => branch['home'],
      'branch[name]' => branch['name'],
      'branch[rename]' => name != branch['name'] ? name : nil,
      'branch[source]' => branch['source']
    )
  end

  puts "\n"
end

#discuss(*issues) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gitcycle.rb', line 104

def discuss(*issues)
  require_git && require_config

  if issues.empty?
    branch = create_pull_request

    if branch == false
      puts "Branch not found.\n".red
    elsif branch['issue_url']
      puts "Opening issue: #{branch['issue_url']}\n".green
      Launchy.open(branch['issue_url'])
    else
      puts "You must push code before opening a pull request.\n".red
    end
  else
    puts "\nRetrieving branch information from gitcycle.\n".green

    get('branch', 'issues' => issues, 'scope' => 'repo').each do |branch|
      if branch['issue_url']
        puts "Opening issue: #{branch['issue_url']}\n".green
        Launchy.open(branch['issue_url'])
      end
    end
  end
end

#pullObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gitcycle.rb', line 130

def pull
  require_git && require_config

  puts "\nRetrieving branch information from gitcycle.\n".green
  branch = get('branch',
    'branch[name]' => branches(:current => true),
    'include' => [ 'repo' ],
    'create' => 0
  )

  if branch
    merge_remote_branch(
      :owner => branch['repo']['owner'],
      :repo => branch['repo']['name'],
      :branch => branch['source']
    )
  else
    puts "Branch not found.\n".red
  end
end

#qa(*issues) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/gitcycle.rb', line 151

def qa(*issues)
  require_git && require_config

  if issues.empty?
    puts "\n"
    get('qa_branch').each do |branches|
      puts "qa_#{branches['source']}".green
      branches['branches'].each do |branch|
        puts "  #{"issue ##{branch['issue']}".yellow}\t#{branch['user']}/#{branch['branch']}"
      end
      puts "\n"
    end
  elsif issues.first == 'fail' || issues.first == 'pass'
    branch = branches(:current => true)
    label = issues.first.capitalize

    if branch =~ /^qa_/
      puts "\nRetrieving branch information from gitcycle.\n".green
      qa_branch = get('qa_branch', :source => branch.gsub(/^qa_/, ''))

      pass_fail = issues.first
      issues = issues[1..-1]

      if pass_fail == 'pass'
        puts "Checking out #{qa_branch['source']}.".green
        run("git checkout #{qa_branch['source']}")
        run("git pull origin #{qa_branch['source']}")
        # TODO: track if source branch doesn't exist
      end

      if issues.empty? 
        branches = qa_branch['branches']
      else
        branches = qa_branch['branches'].select do |b|
          issues.include?(b['issue'])
        end
      end

      branches.each do |branch|
        if pass_fail == 'pass'
          merge_remote_branch(
            :owner => branch['home'],
            :repo => branch['repo'].split(':')[1],
            :branch => branch['branch'],
            :issue => branch['issue'],
            :issues => qa_branch['branches'].collect { |b| b['issue'] },
            :type => :from_qa
          )
        end

        if !issues.empty? || pass_fail == 'pass'
          puts "\nLabeling issue #{branch['issue']} as '#{label}'.\n".green
          get('label',
            'qa_branch[source]' => qa_branch['source'],
            'issue' => branch['issue'],
            'labels' => [ label ]
          )
        end
      end

      if issues.empty? && pass_fail == 'fail'
        puts "\nLabeling all issues as '#{label}'.\n".green
        get('label',
          'qa_branch[source]' => qa_branch['source'],
          'labels' => [ label ]
        )
      end

      if pass_fail == 'pass'
        puts "Marking Lighthouse tickets as 'pending-approval'.\n".green
        branches = branches.collect do |b|
          { :name => b['branch'], :repo => b['repo'], :user => b['user'] }
        end
        get('ticket_resolve', 'branches' => Yajl::Encoder.encode(branches))
      end
    else
      puts "\nYou are not in a QA branch.\n".red
    end
  elsif issues.first == 'resolved'
    branch = branches(:current => true)

    if branch =~ /^qa_/
      puts "\nRetrieving branch information from gitcycle.\n".green
      qa_branch = get('qa_branch', :source => branch.gsub(/^qa_/, ''))
      
      branches = qa_branch['branches']
      conflict = branches.detect { |branch| branch['conflict'] }

      if qa_branch && conflict
        puts "Committing merge resolution of #{conflict['branch']} (issue ##{conflict['issue']}).\n".green
        run("git add . && git add . -u && git commit -a -F .git/MERGE_MSG")

        puts "Pushing merge resolution of #{conflict['branch']} (issue ##{conflict['issue']}).\n".green
        run("git push origin qa_#{qa_branch['source']}")

        puts "\nDe-conflicting on gitcycle.\n".green
        get('qa_branch',
          'issues' => branches.collect { |branch| branch['issue'] }
        )

        create_qa_branch(
          :preserve => true,
          :range => (branches.index(conflict)+1..-1),
          :qa_branch => qa_branch
        )
      else
        puts "Couldn't find record of a conflicted merge.\n".red
      end
    else
      puts "\nYou aren't on a QA branch.\n".red
    end
  else
    create_qa_branch(:issues => issues)
  end
end

#ready(*issues) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/gitcycle.rb', line 267

def ready(*issues)
  require_git && require_config

  if issues.empty?
    branch = create_pull_request

    if branch == false
      puts "Branch not found.\n".red
    elsif branch['issue_url']
      puts "\nLabeling issue as 'Pending Review'.\n".green
      get('label',
        'branch[name]' => branches(:current => true),
        'labels' => [ 'Pending Review' ]
      )

      puts "Opening issue: #{branch['issue_url']}\n".green
      Launchy.open(branch['issue_url'])
    end
  else
    puts "\nLabeling issues as 'Pending Review'.\n".green
    get('label',
      'issues' => issues,
      'labels' => [ 'Pending Review' ],
      'scope' => 'repo'
    )
  end
end

#reviewed(*issues) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/gitcycle.rb', line 295

def reviewed(*issues)
  require_git && require_config

  if issues.include?("fail")
    issues = issues.reject{|x| x=='fail'}
    label = 'Fail'
  else
    issues = issues.reject{|x| x=='pass'}
    label = 'Pending QA'
  end

  if issues.empty?
    puts "\nLabeling issue as '#{label}'.\n".green
    get('label',
      'branch[name]' => branches(:current => true),
      'labels' => [ label ]
    )
  else
    puts "\nLabeling issues as '#{label}'.\n".green
    get('label',
      'issues' => issues,
      'labels' => [ label ],
      'scope' => 'repo'
    )
  end
end

#setup(login, repo, token) ⇒ Object



322
323
324
325
326
327
# File 'lib/gitcycle.rb', line 322

def setup(, repo, token)
  repo = "#{login}/#{repo}" unless repo.include?('/')
  @config[repo] = [ , token ]
  save_config
  puts "\nConfiguration saved.\n".green
end

#start(args = []) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/gitcycle.rb', line 329

def start(args=[])
  command = args.shift
  if command.nil?
    puts "\nNo command specified\n".red
  elsif self.respond_to?(command)
    send(command, *args)
  elsif args.empty?
    create_branch(command)
  else
    puts "\nCommand '#{command}' not found.\n".red
  end
end