Merge branch 'web_hooks' of https://github.com/ariejan/gitlabhq into ariejan-web_hooks
Conflicts: app/models/project.rb config/routes.rb db/schema.rb
This commit is contained in:
commit
66fb3909a5
18 changed files with 377 additions and 3 deletions
|
@ -14,6 +14,7 @@ class Project < ActiveRecord::Base
|
|||
has_many :users, :through => :users_projects
|
||||
has_many :notes, :dependent => :destroy
|
||||
has_many :snippets, :dependent => :destroy
|
||||
has_many :web_hooks, :dependent => :destroy
|
||||
|
||||
acts_as_taggable
|
||||
|
||||
|
@ -82,12 +83,58 @@ class Project < ActiveRecord::Base
|
|||
:heads,
|
||||
:commits_since,
|
||||
:fresh_commits,
|
||||
:commits_between,
|
||||
:to => :repository, :prefix => nil
|
||||
|
||||
def to_param
|
||||
code
|
||||
end
|
||||
|
||||
def web_url
|
||||
[GIT_HOST['host'], code].join("/")
|
||||
end
|
||||
|
||||
def execute_web_hooks(oldrev, newrev, ref)
|
||||
ref_parts = ref.split('/')
|
||||
|
||||
# Return if this is not a push to a branch (e.g. new commits)
|
||||
return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
|
||||
|
||||
data = web_hook_data(oldrev, newrev, ref)
|
||||
web_hooks.each { |web_hook| web_hook.execute(data) }
|
||||
end
|
||||
|
||||
def web_hook_data(oldrev, newrev, ref)
|
||||
data = {
|
||||
before: oldrev,
|
||||
after: newrev,
|
||||
ref: ref,
|
||||
repository: {
|
||||
name: name,
|
||||
url: web_url,
|
||||
description: description,
|
||||
homepage: web_url,
|
||||
private: private?
|
||||
},
|
||||
commits: []
|
||||
}
|
||||
|
||||
commits_between(oldrev, newrev).each do |commit|
|
||||
data[:commits] << {
|
||||
id: commit.id,
|
||||
message: commit.safe_message,
|
||||
timestamp: commit.date.xmlschema,
|
||||
url: "http://#{GIT_HOST['host']}/#{code}/commits/#{commit.id}",
|
||||
author: {
|
||||
name: commit.author_name,
|
||||
email: commit.author_email
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def team_member_by_name_or_email(email = nil, name = nil)
|
||||
user = users.where("email like ? or name like ?", email, name).first
|
||||
users_projects.find_by_user_id(user.id) if user
|
||||
|
|
|
@ -31,6 +31,22 @@ class Repository
|
|||
project.id
|
||||
end
|
||||
|
||||
def write_hooks
|
||||
%w(post-receive).each do |hook|
|
||||
write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook")))
|
||||
end
|
||||
end
|
||||
|
||||
def write_hook(name, content)
|
||||
hook_file = File.join(project.path_to_repo, 'hooks', name)
|
||||
|
||||
File.open(hook_file, 'w') do |f|
|
||||
f.write(content)
|
||||
end
|
||||
|
||||
File.chmod(0775, hook_file)
|
||||
end
|
||||
|
||||
def repo
|
||||
@repo ||= Grit::Repo.new(project.path_to_repo)
|
||||
end
|
||||
|
@ -47,6 +63,8 @@ class Repository
|
|||
Gitlabhq::GitHost.system.new.configure do |c|
|
||||
c.update_project(path, project)
|
||||
end
|
||||
|
||||
write_hooks
|
||||
end
|
||||
|
||||
def destroy_repository
|
||||
|
@ -115,4 +133,8 @@ class Repository
|
|||
repo.commits(ref)
|
||||
end.map{ |c| Commit.new(c) }
|
||||
end
|
||||
|
||||
def commits_between(from, to)
|
||||
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
||||
end
|
||||
end
|
||||
|
|
20
app/models/web_hook.rb
Normal file
20
app/models/web_hook.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
class WebHook < ActiveRecord::Base
|
||||
include HTTParty
|
||||
|
||||
# HTTParty timeout
|
||||
default_timeout 10
|
||||
|
||||
belongs_to :project
|
||||
|
||||
validates :url,
|
||||
presence: true,
|
||||
format: {
|
||||
with: URI::regexp(%w(http https)),
|
||||
message: "should be a valid url" }
|
||||
|
||||
def execute(data)
|
||||
WebHook.post(url, body: data.to_json)
|
||||
rescue
|
||||
# There was a problem calling this web hook, let's forget about it.
|
||||
end
|
||||
end
|
8
app/workers/post_receive.rb
Normal file
8
app/workers/post_receive.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
class PostReceive
|
||||
def self.perform(reponame, oldrev, newrev, ref)
|
||||
project = Project.find_by_path(reponame)
|
||||
return false if project.nil?
|
||||
|
||||
project.execute_web_hooks(oldrev, newrev, ref)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue