diff --git a/app/models/assembla_service.rb b/app/models/assembla_service.rb deleted file mode 100644 index 66ecf39..0000000 --- a/app/models/assembla_service.rb +++ /dev/null @@ -1,45 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -class AssemblaService < Service - include HTTParty - - validates :token, presence: true, if: :activated? - - def title - 'Assembla' - end - - def description - 'Project Management Software (Source Commits Endpoint)' - end - - def to_param - 'assembla' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: '' } - ] - end - - def execute(push) - url = "https://atlas.assembla.com/spaces/ouposp/github_tool?secret_key=#{token}" - AssemblaService.post(url, body: { payload: push }.to_json, headers: { 'Content-Type' => 'application/json' }) - end -end diff --git a/app/models/campfire_service.rb b/app/models/campfire_service.rb deleted file mode 100644 index fb2a49f..0000000 --- a/app/models/campfire_service.rb +++ /dev/null @@ -1,78 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -class CampfireService < Service - attr_accessible :subdomain, :room - - validates :token, presence: true, if: :activated? - - def title - 'Campfire' - end - - def description - 'Simple web-based real-time group chat' - end - - def to_param - 'campfire' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: '' }, - { type: 'text', name: 'subdomain', placeholder: '' }, - { type: 'text', name: 'room', placeholder: '' } - ] - end - - def execute(push_data) - room = gate.find_room_by_name(self.room) - return true unless room - - message = build_message(push_data) - - room.speak(message) - end - - private - - def gate - @gate ||= Tinder::Campfire.new(subdomain, token: token) - end - - def build_message(push) - ref = push[:ref].gsub("refs/heads/", "") - before = push[:before] - after = push[:after] - - message = "" - message << "[#{project.name_with_namespace}] " - message << "#{push[:user_name]} " - - if before =~ /000000/ - message << "pushed new branch #{ref} \n" - elsif after =~ /000000/ - message << "removed branch #{ref} \n" - else - message << "pushed #{push[:total_commits_count]} commits to #{ref}. " - message << "#{project.web_url}/compare/#{before}...#{after}" - end - - message - end -end diff --git a/app/models/flowdock_service.rb b/app/models/flowdock_service.rb deleted file mode 100644 index f72d9fa..0000000 --- a/app/models/flowdock_service.rb +++ /dev/null @@ -1,54 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -require "flowdock-git-hook" - -class FlowdockService < Service - validates :token, presence: true, if: :activated? - - def title - 'Flowdock' - end - - def description - 'Flowdock is a collaboration web app for technical teams.' - end - - def to_param - 'flowdock' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: '' } - ] - end - - def execute(push_data) - repo_path = File.join(Gitlab.config.gitlab_shell.repos_path, "#{project.path_with_namespace}.git") - Flowdock::Git.post( - push_data[:ref], - push_data[:before], - push_data[:after], - token: token, - repo: repo_path, - repo_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}", - commit_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/%s", - diff_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/compare/%s...%s", - ) - end -end diff --git a/app/models/gitlab_ci_service.rb b/app/models/gitlab_ci_service.rb deleted file mode 100644 index 7f5380a..0000000 --- a/app/models/gitlab_ci_service.rb +++ /dev/null @@ -1,78 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -class GitlabCiService < Service - attr_accessible :project_url - - validates :project_url, presence: true, if: :activated? - validates :token, presence: true, if: :activated? - - delegate :execute, to: :service_hook, prefix: nil - - after_save :compose_service_hook, if: :activated? - - def compose_service_hook - hook = service_hook || build_service_hook - hook.url = [project_url, "/build", "?token=#{token}"].join("") - hook.save - end - - def commit_status_path sha - project_url + "/builds/#{sha}/status.json?token=#{token}" - end - - def commit_status sha - response = HTTParty.get(commit_status_path(sha)) - - if response.code == 200 and response["status"] - response["status"] - else - :error - end - end - - def build_page sha - project_url + "/builds/#{sha}" - end - - def builds_path - project_url + "?ref=" + project.default_branch - end - - def status_img_path - project_url + "/status.png?ref=" + project.default_branch - end - - def title - 'GitLab CI' - end - - def description - 'Continuous integration server from GitLab' - end - - def to_param - 'gitlab_ci' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' }, - { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3'} - ] - end -end diff --git a/app/models/hipchat_service.rb b/app/models/hipchat_service.rb deleted file mode 100644 index ea2169f..0000000 --- a/app/models/hipchat_service.rb +++ /dev/null @@ -1,74 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -class HipchatService < Service - attr_accessible :room - - validates :token, presence: true, if: :activated? - - def title - 'Hipchat' - end - - def description - 'Private group chat and IM' - end - - def to_param - 'hipchat' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: '' }, - { type: 'text', name: 'room', placeholder: '' } - ] - end - - def execute(push_data) - gate[room].send('Gitlab', create_message(push_data)) - end - - private - - def gate - @gate ||= HipChat::Client.new(token) - end - - def create_message(push) - ref = push[:ref].gsub("refs/heads/", "") - before = push[:before] - after = push[:after] - - message = "" - message << "#{push[:user_name]} " - if before =~ /000000/ - message << "pushed new branch #{ref} to #{project.name_with_namespace.gsub!(/\s/,'')}\n" - elsif after =~ /000000/ - message << "removed branch #{ref} from #{project.name_with_namespace.gsub!(/\s/,'')} \n" - else - message << "#pushed to branch #{ref} " - message << "of #{project.name_with_namespace.gsub!(/\s/,'')} " - message << "(Compare changes)" - for commit in push[:commits] do - message << "
- #{commit[:message]} (#{commit[:id][0..5]})" - end - end - - message - end -end diff --git a/app/models/pivotaltracker_service.rb b/app/models/pivotaltracker_service.rb deleted file mode 100644 index c5b1b9a..0000000 --- a/app/models/pivotaltracker_service.rb +++ /dev/null @@ -1,62 +0,0 @@ -# == Schema Information -# -# Table name: services -# -# id :integer not null, primary key -# type :string(255) -# title :string(255) -# token :string(255) -# project_id :integer not null -# created_at :datetime not null -# updated_at :datetime not null -# active :boolean default(FALSE), not null -# project_url :string(255) -# subdomain :string(255) -# room :string(255) -# - -class PivotaltrackerService < Service - include HTTParty - - validates :token, presence: true, if: :activated? - - def title - 'PivotalTracker' - end - - def description - 'Project Management Software (Source Commits Endpoint)' - end - - def to_param - 'pivotaltracker' - end - - def fields - [ - { type: 'text', name: 'token', placeholder: '' } - ] - end - - def execute(push) - url = 'https://www.pivotaltracker.com/services/v5/source_commits' - push[:commits].each do |commit| - message = { - 'source_commit' => { - 'commit_id' => commit[:id], - 'author' => commit[:author][:name], - 'url' => commit[:url], - 'message' => commit[:message] - } - } - PivotaltrackerService.post( - url, - body: message.to_json, - headers: { - 'Content-Type' => 'application/json', - 'X-TrackerToken' => token - } - ) - end - end -end diff --git a/app/models/project.rb b/app/models/project.rb index 1bfc27d..2843d56 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -23,6 +23,13 @@ # visibility_level :integer default(0), not null # +require_relative "project_services/assembla_service" +require_relative "project_services/campfire_service" +require_relative "project_services/flowdock_service" +require_relative "project_services/gitlab_ci_service" +require_relative "project_services/hipchat_service" +require_relative "project_services/pivotaltracker_service" + class Project < ActiveRecord::Base include Gitlab::ShellAdapter include Gitlab::VisibilityLevel diff --git a/app/models/project_services/assembla_service.rb b/app/models/project_services/assembla_service.rb new file mode 100644 index 0000000..66ecf39 --- /dev/null +++ b/app/models/project_services/assembla_service.rb @@ -0,0 +1,45 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +class AssemblaService < Service + include HTTParty + + validates :token, presence: true, if: :activated? + + def title + 'Assembla' + end + + def description + 'Project Management Software (Source Commits Endpoint)' + end + + def to_param + 'assembla' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: '' } + ] + end + + def execute(push) + url = "https://atlas.assembla.com/spaces/ouposp/github_tool?secret_key=#{token}" + AssemblaService.post(url, body: { payload: push }.to_json, headers: { 'Content-Type' => 'application/json' }) + end +end diff --git a/app/models/project_services/campfire_service.rb b/app/models/project_services/campfire_service.rb new file mode 100644 index 0000000..fb2a49f --- /dev/null +++ b/app/models/project_services/campfire_service.rb @@ -0,0 +1,78 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +class CampfireService < Service + attr_accessible :subdomain, :room + + validates :token, presence: true, if: :activated? + + def title + 'Campfire' + end + + def description + 'Simple web-based real-time group chat' + end + + def to_param + 'campfire' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: '' }, + { type: 'text', name: 'subdomain', placeholder: '' }, + { type: 'text', name: 'room', placeholder: '' } + ] + end + + def execute(push_data) + room = gate.find_room_by_name(self.room) + return true unless room + + message = build_message(push_data) + + room.speak(message) + end + + private + + def gate + @gate ||= Tinder::Campfire.new(subdomain, token: token) + end + + def build_message(push) + ref = push[:ref].gsub("refs/heads/", "") + before = push[:before] + after = push[:after] + + message = "" + message << "[#{project.name_with_namespace}] " + message << "#{push[:user_name]} " + + if before =~ /000000/ + message << "pushed new branch #{ref} \n" + elsif after =~ /000000/ + message << "removed branch #{ref} \n" + else + message << "pushed #{push[:total_commits_count]} commits to #{ref}. " + message << "#{project.web_url}/compare/#{before}...#{after}" + end + + message + end +end diff --git a/app/models/project_services/flowdock_service.rb b/app/models/project_services/flowdock_service.rb new file mode 100644 index 0000000..f72d9fa --- /dev/null +++ b/app/models/project_services/flowdock_service.rb @@ -0,0 +1,54 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +require "flowdock-git-hook" + +class FlowdockService < Service + validates :token, presence: true, if: :activated? + + def title + 'Flowdock' + end + + def description + 'Flowdock is a collaboration web app for technical teams.' + end + + def to_param + 'flowdock' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: '' } + ] + end + + def execute(push_data) + repo_path = File.join(Gitlab.config.gitlab_shell.repos_path, "#{project.path_with_namespace}.git") + Flowdock::Git.post( + push_data[:ref], + push_data[:before], + push_data[:after], + token: token, + repo: repo_path, + repo_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}", + commit_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/%s", + diff_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/compare/%s...%s", + ) + end +end diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb new file mode 100644 index 0000000..7f5380a --- /dev/null +++ b/app/models/project_services/gitlab_ci_service.rb @@ -0,0 +1,78 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +class GitlabCiService < Service + attr_accessible :project_url + + validates :project_url, presence: true, if: :activated? + validates :token, presence: true, if: :activated? + + delegate :execute, to: :service_hook, prefix: nil + + after_save :compose_service_hook, if: :activated? + + def compose_service_hook + hook = service_hook || build_service_hook + hook.url = [project_url, "/build", "?token=#{token}"].join("") + hook.save + end + + def commit_status_path sha + project_url + "/builds/#{sha}/status.json?token=#{token}" + end + + def commit_status sha + response = HTTParty.get(commit_status_path(sha)) + + if response.code == 200 and response["status"] + response["status"] + else + :error + end + end + + def build_page sha + project_url + "/builds/#{sha}" + end + + def builds_path + project_url + "?ref=" + project.default_branch + end + + def status_img_path + project_url + "/status.png?ref=" + project.default_branch + end + + def title + 'GitLab CI' + end + + def description + 'Continuous integration server from GitLab' + end + + def to_param + 'gitlab_ci' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' }, + { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3'} + ] + end +end diff --git a/app/models/project_services/hipchat_service.rb b/app/models/project_services/hipchat_service.rb new file mode 100644 index 0000000..ea2169f --- /dev/null +++ b/app/models/project_services/hipchat_service.rb @@ -0,0 +1,74 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +class HipchatService < Service + attr_accessible :room + + validates :token, presence: true, if: :activated? + + def title + 'Hipchat' + end + + def description + 'Private group chat and IM' + end + + def to_param + 'hipchat' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: '' }, + { type: 'text', name: 'room', placeholder: '' } + ] + end + + def execute(push_data) + gate[room].send('Gitlab', create_message(push_data)) + end + + private + + def gate + @gate ||= HipChat::Client.new(token) + end + + def create_message(push) + ref = push[:ref].gsub("refs/heads/", "") + before = push[:before] + after = push[:after] + + message = "" + message << "#{push[:user_name]} " + if before =~ /000000/ + message << "pushed new branch #{ref} to #{project.name_with_namespace.gsub!(/\s/,'')}\n" + elsif after =~ /000000/ + message << "removed branch #{ref} from #{project.name_with_namespace.gsub!(/\s/,'')} \n" + else + message << "#pushed to branch #{ref} " + message << "of #{project.name_with_namespace.gsub!(/\s/,'')} " + message << "(Compare changes)" + for commit in push[:commits] do + message << "
- #{commit[:message]} (#{commit[:id][0..5]})" + end + end + + message + end +end diff --git a/app/models/project_services/pivotaltracker_service.rb b/app/models/project_services/pivotaltracker_service.rb new file mode 100644 index 0000000..c5b1b9a --- /dev/null +++ b/app/models/project_services/pivotaltracker_service.rb @@ -0,0 +1,62 @@ +# == Schema Information +# +# Table name: services +# +# id :integer not null, primary key +# type :string(255) +# title :string(255) +# token :string(255) +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# active :boolean default(FALSE), not null +# project_url :string(255) +# subdomain :string(255) +# room :string(255) +# + +class PivotaltrackerService < Service + include HTTParty + + validates :token, presence: true, if: :activated? + + def title + 'PivotalTracker' + end + + def description + 'Project Management Software (Source Commits Endpoint)' + end + + def to_param + 'pivotaltracker' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: '' } + ] + end + + def execute(push) + url = 'https://www.pivotaltracker.com/services/v5/source_commits' + push[:commits].each do |commit| + message = { + 'source_commit' => { + 'commit_id' => commit[:id], + 'author' => commit[:author][:name], + 'url' => commit[:url], + 'message' => commit[:message] + } + } + PivotaltrackerService.post( + url, + body: message.to_json, + headers: { + 'Content-Type' => 'application/json', + 'X-TrackerToken' => token + } + ) + end + end +end -- libgit2 0.21.2