summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorVõ Anh Duy <voanhduy1512@live.com>2014-02-23 06:42:04 +0700
committerVõ Anh Duy <voanhduy1512@live.com>2014-02-25 00:21:41 +0700
commitf5fc67bc2c3f20f80aef701254542d17ea5ccd8a (patch)
treef4184b6cbbdc5eda524cfd261954c831e2c709fe /app/models
parenta1ecd0fbd6742bf827efe8c869a8b9115d2e4c05 (diff)
downloadgitlab-ci-f5fc67bc2c3f20f80aef701254542d17ea5ccd8a.tar.gz
create web hook
Diffstat (limited to 'app/models')
-rw-r--r--app/models/build.rb3
-rw-r--r--app/models/project.rb5
-rw-r--r--app/models/web_hook.rb30
3 files changed, 38 insertions, 0 deletions
diff --git a/app/models/build.rb b/app/models/build.rb
index 793400b..090af88 100644
--- a/app/models/build.rb
+++ b/app/models/build.rb
@@ -78,6 +78,9 @@ class Build < ActiveRecord::Base
after_transition any => [:success, :failed, :canceled] do |build, transition|
build.update_attributes finished_at: Time.now
project = build.project
+ if project.web_hooks?
+ WebHookService.new.execute_hooks(build)
+ end
if project.email_notification?
if build.status.to_sym == :failed || !project.email_only_broken_builds
diff --git a/app/models/project.rb b/app/models/project.rb
index b916c9f..9486d23 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -31,6 +31,7 @@ class Project < ActiveRecord::Base
has_many :builds, dependent: :destroy
has_many :runner_projects, dependent: :destroy
has_many :runners, through: :runner_projects
+ has_many :web_hooks
#
# Validations
@@ -142,6 +143,10 @@ class Project < ActiveRecord::Base
email_add_committer || email_recipients.present?
end
+ def web_hooks?
+ web_hooks.any?
+ end
+
# onlu check for toggling build status within same ref.
def last_build_changed_status?
ref = last_build.ref
diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
new file mode 100644
index 0000000..dc1d1db
--- /dev/null
+++ b/app/models/web_hook.rb
@@ -0,0 +1,30 @@
+class WebHook < ActiveRecord::Base
+ belongs_to :project
+ include HTTParty
+
+ attr_accessible :url
+
+ # HTTParty timeout
+ default_timeout 10
+
+ validates :url, presence: true,
+ format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
+
+ def execute(data)
+ parsed_url = URI.parse(url)
+ if parsed_url.userinfo.blank?
+ WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" }, verify: false)
+ else
+ post_url = url.gsub("#{parsed_url.userinfo}@", "")
+ auth = {
+ username: URI.decode(parsed_url.user),
+ password: URI.decode(parsed_url.password),
+ }
+ WebHook.post(post_url,
+ body: data.to_json,
+ headers: {"Content-Type" => "application/json"},
+ verify: false,
+ basic_auth: auth)
+ end
+ end
+end