push_event.rb
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
module PushEvent
  def valid_push?
    data[:ref]
  rescue => ex
    false
  end
  def tag?
    data[:ref]["refs/tags"]
  end
  def branch?
    data[:ref]["refs/heads"]
  end
  def new_branch?
    commit_from =~ /^00000/
  end
  def new_ref?
    commit_from =~ /^00000/
  end
  def rm_ref?
    commit_to =~ /^00000/
  end
  def md_ref?
    !(rm_ref? || new_ref?)
  end
  def commit_from
    data[:before]
  end
  def commit_to
    data[:after]
  end
  def ref_name
    if tag?
      tag_name
    else
      branch_name
    end
  end
  def branch_name
    @branch_name ||= data[:ref].gsub("refs/heads/", "")
  end
  def tag_name
    @tag_name ||= data[:ref].gsub("refs/tags/", "")
  end
  # Max 20 commits from push DESC
  def commits
    @commits ||= data[:commits].map { |commit| project.commit(commit[:id]) }.reverse
  end
  def commits_count 
    data[:total_commits_count] || commits.count || 0
  end
  def ref_type
    tag? ? "tag" : "branch"
  end
  def push_action_name
    if new_ref?
      "pushed new"
    elsif rm_ref?
      "deleted"
    else
      "pushed to"
    end
  end
  def parent_commit
    project.commit(commit_from)
  rescue => ex
    nil
  end
  def last_commit
    project.commit(commit_to)
  rescue => ex
    nil
  end
  def push_with_commits? 
    md_ref? && commits.any? && parent_commit && last_commit
  rescue Grit::NoSuchPathError
    false
  end
  def last_push_to_non_root?
    branch? && project.default_branch != branch_name
  end
end