Commit 2cf5a9efbc420649f1b2554378782239835a8357

Authored by randx
1 parent c6034af4

Better name for Project push methods module

app/models/project.rb
... ... @@ -2,7 +2,7 @@ require "grit"
2 2  
3 3 class Project < ActiveRecord::Base
4 4 include Repository
5   - include GitPush
  5 + include ProjectPush
6 6 include Authority
7 7 include Team
8 8  
... ...
app/roles/git_push.rb
... ... @@ -1,105 +0,0 @@
1   -module GitPush
2   - def observe_push(oldrev, newrev, ref, user)
3   - data = post_receive_data(oldrev, newrev, ref, user)
4   -
5   - Event.create(
6   - :project => self,
7   - :action => Event::Pushed,
8   - :data => data,
9   - :author_id => data[:user_id]
10   - )
11   - end
12   -
13   - def update_merge_requests(oldrev, newrev, ref, user)
14   - return true unless ref =~ /heads/
15   - branch_name = ref.gsub("refs/heads/", "")
16   - c_ids = self.commits_between(oldrev, newrev).map(&:id)
17   -
18   - # Update code for merge requests
19   - mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
20   - mrs.each { |merge_request| merge_request.reload_code; merge_request.mark_as_unchecked }
21   -
22   - # Close merge requests
23   - mrs = self.merge_requests.opened.where(:target_branch => branch_name).all
24   - mrs = mrs.select(&:last_commit).select { |mr| c_ids.include?(mr.last_commit.id) }
25   - mrs.each { |merge_request| merge_request.merge!(user.id) }
26   -
27   - true
28   - end
29   -
30   - def execute_hooks(oldrev, newrev, ref, user)
31   - ref_parts = ref.split('/')
32   -
33   - # Return if this is not a push to a branch (e.g. new commits)
34   - return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
35   -
36   - data = post_receive_data(oldrev, newrev, ref, user)
37   -
38   - hooks.each { |hook| hook.execute(data) }
39   - end
40   -
41   - def post_receive_data(oldrev, newrev, ref, user)
42   -
43   - push_commits = commits_between(oldrev, newrev)
44   -
45   - # Total commits count
46   - push_commits_count = push_commits.size
47   -
48   - # Get latest 20 commits ASC
49   - push_commits_limited = push_commits.last(20)
50   -
51   - # Hash to be passed as post_receive_data
52   - data = {
53   - before: oldrev,
54   - after: newrev,
55   - ref: ref,
56   - user_id: user.id,
57   - user_name: user.name,
58   - repository: {
59   - name: name,
60   - url: web_url,
61   - description: description,
62   - homepage: web_url,
63   - },
64   - commits: [],
65   - total_commits_count: push_commits_count
66   - }
67   -
68   - # For perfomance purposes maximum 20 latest commits
69   - # will be passed as post receive hook data.
70   - #
71   - push_commits_limited.each do |commit|
72   - data[:commits] << {
73   - id: commit.id,
74   - message: commit.safe_message,
75   - timestamp: commit.date.xmlschema,
76   - url: "#{Gitlab.config.url}/#{code}/commits/#{commit.id}",
77   - author: {
78   - name: commit.author_name,
79   - email: commit.author_email
80   - }
81   - }
82   - end
83   -
84   - data
85   - end
86   -
87   -
88   - # This method will be called after each post receive
89   - # and only if user present in gitlab.
90   - # All callbacks for post receive should be placed here
91   - #
92   - def trigger_post_receive(oldrev, newrev, ref, user)
93   - # Create push event
94   - self.observe_push(oldrev, newrev, ref, user)
95   -
96   - # Close merged MR
97   - self.update_merge_requests(oldrev, newrev, ref, user)
98   -
99   - # Execute web hooks
100   - self.execute_hooks(oldrev, newrev, ref, user)
101   -
102   - # Create satellite
103   - self.satellite.create unless self.satellite.exists?
104   - end
105   -end
app/roles/project_push.rb 0 → 100644
... ... @@ -0,0 +1,105 @@
  1 +module ProjectPush
  2 + def observe_push(oldrev, newrev, ref, user)
  3 + data = post_receive_data(oldrev, newrev, ref, user)
  4 +
  5 + Event.create(
  6 + :project => self,
  7 + :action => Event::Pushed,
  8 + :data => data,
  9 + :author_id => data[:user_id]
  10 + )
  11 + end
  12 +
  13 + def update_merge_requests(oldrev, newrev, ref, user)
  14 + return true unless ref =~ /heads/
  15 + branch_name = ref.gsub("refs/heads/", "")
  16 + c_ids = self.commits_between(oldrev, newrev).map(&:id)
  17 +
  18 + # Update code for merge requests
  19 + mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
  20 + mrs.each { |merge_request| merge_request.reload_code; merge_request.mark_as_unchecked }
  21 +
  22 + # Close merge requests
  23 + mrs = self.merge_requests.opened.where(:target_branch => branch_name).all
  24 + mrs = mrs.select(&:last_commit).select { |mr| c_ids.include?(mr.last_commit.id) }
  25 + mrs.each { |merge_request| merge_request.merge!(user.id) }
  26 +
  27 + true
  28 + end
  29 +
  30 + def execute_hooks(oldrev, newrev, ref, user)
  31 + ref_parts = ref.split('/')
  32 +
  33 + # Return if this is not a push to a branch (e.g. new commits)
  34 + return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
  35 +
  36 + data = post_receive_data(oldrev, newrev, ref, user)
  37 +
  38 + hooks.each { |hook| hook.execute(data) }
  39 + end
  40 +
  41 + def post_receive_data(oldrev, newrev, ref, user)
  42 +
  43 + push_commits = commits_between(oldrev, newrev)
  44 +
  45 + # Total commits count
  46 + push_commits_count = push_commits.size
  47 +
  48 + # Get latest 20 commits ASC
  49 + push_commits_limited = push_commits.last(20)
  50 +
  51 + # Hash to be passed as post_receive_data
  52 + data = {
  53 + before: oldrev,
  54 + after: newrev,
  55 + ref: ref,
  56 + user_id: user.id,
  57 + user_name: user.name,
  58 + repository: {
  59 + name: name,
  60 + url: web_url,
  61 + description: description,
  62 + homepage: web_url,
  63 + },
  64 + commits: [],
  65 + total_commits_count: push_commits_count
  66 + }
  67 +
  68 + # For perfomance purposes maximum 20 latest commits
  69 + # will be passed as post receive hook data.
  70 + #
  71 + push_commits_limited.each do |commit|
  72 + data[:commits] << {
  73 + id: commit.id,
  74 + message: commit.safe_message,
  75 + timestamp: commit.date.xmlschema,
  76 + url: "#{Gitlab.config.url}/#{code}/commits/#{commit.id}",
  77 + author: {
  78 + name: commit.author_name,
  79 + email: commit.author_email
  80 + }
  81 + }
  82 + end
  83 +
  84 + data
  85 + end
  86 +
  87 +
  88 + # This method will be called after each post receive
  89 + # and only if user present in gitlab.
  90 + # All callbacks for post receive should be placed here
  91 + #
  92 + def trigger_post_receive(oldrev, newrev, ref, user)
  93 + # Create push event
  94 + self.observe_push(oldrev, newrev, ref, user)
  95 +
  96 + # Close merged MR
  97 + self.update_merge_requests(oldrev, newrev, ref, user)
  98 +
  99 + # Execute web hooks
  100 + self.execute_hooks(oldrev, newrev, ref, user)
  101 +
  102 + # Create satellite
  103 + self.satellite.create unless self.satellite.exists?
  104 + end
  105 +end
... ...