Class: Sppuppet

Inherits:
Object
  • Object
show all
Defined in:
lib/tutter/action/sppuppet.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings, client, project, data) ⇒ Sppuppet

Returns a new instance of Sppuppet.



3
4
5
6
7
8
9
# File 'lib/tutter/action/sppuppet.rb', line 3

def initialize(settings, client, project, data)
  @settings = settings
  @client = client
  @project = project
  @data = data
  @debug = settings['debug'] unless settings['debug'].nil?
end

Instance Method Details

#debug(message) ⇒ Object



11
12
13
# File 'lib/tutter/action/sppuppet.rb', line 11

def debug(message)
  puts message if @debug
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/tutter/action/sppuppet.rb', line 15

def run
  pull_request_id = @data['issue']['number']
  debug "pull request id: #{pull_request_id}"
  pr = @client.pull_request @project, pull_request_id
  plus_one = {}
  merge = false

  if pr.mergeable_state != 'clean'
    debug "merge state for #{@project} #{pull_request_id} is not clean. Current state: #{pr.mergeable_state}"
    return false
  end

  # No comments, no need to go further.
  if pr.comments == 0
    debug 'no comments, skipping'
    return false
  end

  # Don't care about code we can't merge
  return false unless pr.mergeable

  # We fetch the latest commit and it's date.
  last_commit = @client.pull_request_commits(@project, pull_request_id).last
  last_commit_date = last_commit.commit.committer.date

  comments = @client.issue_comments(@project, pull_request_id)

  # Check each comment for +1 and merge comments
  comments.each do |i|

    # Comment is older than last commit. We only want to check for +1 in newer comments
    next if last_commit_date > i.created_at

    if /^(\+1|:\+1)/.match i.body
      # pull request submitter cant +1
      unless pr.user. == i.attrs[:user].attrs[:login]
        plus_one[i.attrs[:user].attrs[:login]] = 1
      end
    end

    # TODO it should calculate the +1's - the -1's
    # Never merge if someone says -1
    if /^(\-1|:\-1:)/.match i.body
      debug "#{@project} #{pull_request_id} has a -1. I will not take the blame"
      return false
    end
  end

  merge = true if comments.last.body == '!merge'

  if plus_one.count >= @settings['plus_ones_required'] and merge
    debug "merging #{pull_request_id} #{@project}"
    @client.merge_pull_request(@project, pull_request_id, 'SHIPPING!!')
  end
end