Commit b90cbfd21ec9bd366ee931d246d51f8821b2a023
1 parent
9c668133
Exists in
master
and in
4 other branches
Move project services models to own subdirectory
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
13 changed files
with
398 additions
and
391 deletions
Show diff stats
app/models/assembla_service.rb
@@ -1,45 +0,0 @@ | @@ -1,45 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -class AssemblaService < Service | ||
19 | - include HTTParty | ||
20 | - | ||
21 | - validates :token, presence: true, if: :activated? | ||
22 | - | ||
23 | - def title | ||
24 | - 'Assembla' | ||
25 | - end | ||
26 | - | ||
27 | - def description | ||
28 | - 'Project Management Software (Source Commits Endpoint)' | ||
29 | - end | ||
30 | - | ||
31 | - def to_param | ||
32 | - 'assembla' | ||
33 | - end | ||
34 | - | ||
35 | - def fields | ||
36 | - [ | ||
37 | - { type: 'text', name: 'token', placeholder: '' } | ||
38 | - ] | ||
39 | - end | ||
40 | - | ||
41 | - def execute(push) | ||
42 | - url = "https://atlas.assembla.com/spaces/ouposp/github_tool?secret_key=#{token}" | ||
43 | - AssemblaService.post(url, body: { payload: push }.to_json, headers: { 'Content-Type' => 'application/json' }) | ||
44 | - end | ||
45 | -end |
app/models/campfire_service.rb
@@ -1,78 +0,0 @@ | @@ -1,78 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -class CampfireService < Service | ||
19 | - attr_accessible :subdomain, :room | ||
20 | - | ||
21 | - validates :token, presence: true, if: :activated? | ||
22 | - | ||
23 | - def title | ||
24 | - 'Campfire' | ||
25 | - end | ||
26 | - | ||
27 | - def description | ||
28 | - 'Simple web-based real-time group chat' | ||
29 | - end | ||
30 | - | ||
31 | - def to_param | ||
32 | - 'campfire' | ||
33 | - end | ||
34 | - | ||
35 | - def fields | ||
36 | - [ | ||
37 | - { type: 'text', name: 'token', placeholder: '' }, | ||
38 | - { type: 'text', name: 'subdomain', placeholder: '' }, | ||
39 | - { type: 'text', name: 'room', placeholder: '' } | ||
40 | - ] | ||
41 | - end | ||
42 | - | ||
43 | - def execute(push_data) | ||
44 | - room = gate.find_room_by_name(self.room) | ||
45 | - return true unless room | ||
46 | - | ||
47 | - message = build_message(push_data) | ||
48 | - | ||
49 | - room.speak(message) | ||
50 | - end | ||
51 | - | ||
52 | - private | ||
53 | - | ||
54 | - def gate | ||
55 | - @gate ||= Tinder::Campfire.new(subdomain, token: token) | ||
56 | - end | ||
57 | - | ||
58 | - def build_message(push) | ||
59 | - ref = push[:ref].gsub("refs/heads/", "") | ||
60 | - before = push[:before] | ||
61 | - after = push[:after] | ||
62 | - | ||
63 | - message = "" | ||
64 | - message << "[#{project.name_with_namespace}] " | ||
65 | - message << "#{push[:user_name]} " | ||
66 | - | ||
67 | - if before =~ /000000/ | ||
68 | - message << "pushed new branch #{ref} \n" | ||
69 | - elsif after =~ /000000/ | ||
70 | - message << "removed branch #{ref} \n" | ||
71 | - else | ||
72 | - message << "pushed #{push[:total_commits_count]} commits to #{ref}. " | ||
73 | - message << "#{project.web_url}/compare/#{before}...#{after}" | ||
74 | - end | ||
75 | - | ||
76 | - message | ||
77 | - end | ||
78 | -end |
app/models/flowdock_service.rb
@@ -1,54 +0,0 @@ | @@ -1,54 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -require "flowdock-git-hook" | ||
19 | - | ||
20 | -class FlowdockService < Service | ||
21 | - validates :token, presence: true, if: :activated? | ||
22 | - | ||
23 | - def title | ||
24 | - 'Flowdock' | ||
25 | - end | ||
26 | - | ||
27 | - def description | ||
28 | - 'Flowdock is a collaboration web app for technical teams.' | ||
29 | - end | ||
30 | - | ||
31 | - def to_param | ||
32 | - 'flowdock' | ||
33 | - end | ||
34 | - | ||
35 | - def fields | ||
36 | - [ | ||
37 | - { type: 'text', name: 'token', placeholder: '' } | ||
38 | - ] | ||
39 | - end | ||
40 | - | ||
41 | - def execute(push_data) | ||
42 | - repo_path = File.join(Gitlab.config.gitlab_shell.repos_path, "#{project.path_with_namespace}.git") | ||
43 | - Flowdock::Git.post( | ||
44 | - push_data[:ref], | ||
45 | - push_data[:before], | ||
46 | - push_data[:after], | ||
47 | - token: token, | ||
48 | - repo: repo_path, | ||
49 | - repo_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}", | ||
50 | - commit_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/%s", | ||
51 | - diff_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/compare/%s...%s", | ||
52 | - ) | ||
53 | - end | ||
54 | -end |
app/models/gitlab_ci_service.rb
@@ -1,78 +0,0 @@ | @@ -1,78 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -class GitlabCiService < Service | ||
19 | - attr_accessible :project_url | ||
20 | - | ||
21 | - validates :project_url, presence: true, if: :activated? | ||
22 | - validates :token, presence: true, if: :activated? | ||
23 | - | ||
24 | - delegate :execute, to: :service_hook, prefix: nil | ||
25 | - | ||
26 | - after_save :compose_service_hook, if: :activated? | ||
27 | - | ||
28 | - def compose_service_hook | ||
29 | - hook = service_hook || build_service_hook | ||
30 | - hook.url = [project_url, "/build", "?token=#{token}"].join("") | ||
31 | - hook.save | ||
32 | - end | ||
33 | - | ||
34 | - def commit_status_path sha | ||
35 | - project_url + "/builds/#{sha}/status.json?token=#{token}" | ||
36 | - end | ||
37 | - | ||
38 | - def commit_status sha | ||
39 | - response = HTTParty.get(commit_status_path(sha)) | ||
40 | - | ||
41 | - if response.code == 200 and response["status"] | ||
42 | - response["status"] | ||
43 | - else | ||
44 | - :error | ||
45 | - end | ||
46 | - end | ||
47 | - | ||
48 | - def build_page sha | ||
49 | - project_url + "/builds/#{sha}" | ||
50 | - end | ||
51 | - | ||
52 | - def builds_path | ||
53 | - project_url + "?ref=" + project.default_branch | ||
54 | - end | ||
55 | - | ||
56 | - def status_img_path | ||
57 | - project_url + "/status.png?ref=" + project.default_branch | ||
58 | - end | ||
59 | - | ||
60 | - def title | ||
61 | - 'GitLab CI' | ||
62 | - end | ||
63 | - | ||
64 | - def description | ||
65 | - 'Continuous integration server from GitLab' | ||
66 | - end | ||
67 | - | ||
68 | - def to_param | ||
69 | - 'gitlab_ci' | ||
70 | - end | ||
71 | - | ||
72 | - def fields | ||
73 | - [ | ||
74 | - { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' }, | ||
75 | - { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3'} | ||
76 | - ] | ||
77 | - end | ||
78 | -end |
app/models/hipchat_service.rb
@@ -1,74 +0,0 @@ | @@ -1,74 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -class HipchatService < Service | ||
19 | - attr_accessible :room | ||
20 | - | ||
21 | - validates :token, presence: true, if: :activated? | ||
22 | - | ||
23 | - def title | ||
24 | - 'Hipchat' | ||
25 | - end | ||
26 | - | ||
27 | - def description | ||
28 | - 'Private group chat and IM' | ||
29 | - end | ||
30 | - | ||
31 | - def to_param | ||
32 | - 'hipchat' | ||
33 | - end | ||
34 | - | ||
35 | - def fields | ||
36 | - [ | ||
37 | - { type: 'text', name: 'token', placeholder: '' }, | ||
38 | - { type: 'text', name: 'room', placeholder: '' } | ||
39 | - ] | ||
40 | - end | ||
41 | - | ||
42 | - def execute(push_data) | ||
43 | - gate[room].send('Gitlab', create_message(push_data)) | ||
44 | - end | ||
45 | - | ||
46 | - private | ||
47 | - | ||
48 | - def gate | ||
49 | - @gate ||= HipChat::Client.new(token) | ||
50 | - end | ||
51 | - | ||
52 | - def create_message(push) | ||
53 | - ref = push[:ref].gsub("refs/heads/", "") | ||
54 | - before = push[:before] | ||
55 | - after = push[:after] | ||
56 | - | ||
57 | - message = "" | ||
58 | - message << "#{push[:user_name]} " | ||
59 | - if before =~ /000000/ | ||
60 | - message << "pushed new branch <a href=\"#{project.web_url}/commits/#{ref}\">#{ref}</a> to <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a>\n" | ||
61 | - elsif after =~ /000000/ | ||
62 | - message << "removed branch #{ref} from <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> \n" | ||
63 | - else | ||
64 | - message << "#pushed to branch <a href=\"#{project.web_url}/commits/#{ref}\">#{ref}</a> " | ||
65 | - message << "of <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> " | ||
66 | - message << "(<a href=\"#{project.web_url}/compare/#{before}...#{after}\">Compare changes</a>)" | ||
67 | - for commit in push[:commits] do | ||
68 | - message << "<br /> - #{commit[:message]} (<a href=\"#{commit[:url]}\">#{commit[:id][0..5]}</a>)" | ||
69 | - end | ||
70 | - end | ||
71 | - | ||
72 | - message | ||
73 | - end | ||
74 | -end |
app/models/pivotaltracker_service.rb
@@ -1,62 +0,0 @@ | @@ -1,62 +0,0 @@ | ||
1 | -# == Schema Information | ||
2 | -# | ||
3 | -# Table name: services | ||
4 | -# | ||
5 | -# id :integer not null, primary key | ||
6 | -# type :string(255) | ||
7 | -# title :string(255) | ||
8 | -# token :string(255) | ||
9 | -# project_id :integer not null | ||
10 | -# created_at :datetime not null | ||
11 | -# updated_at :datetime not null | ||
12 | -# active :boolean default(FALSE), not null | ||
13 | -# project_url :string(255) | ||
14 | -# subdomain :string(255) | ||
15 | -# room :string(255) | ||
16 | -# | ||
17 | - | ||
18 | -class PivotaltrackerService < Service | ||
19 | - include HTTParty | ||
20 | - | ||
21 | - validates :token, presence: true, if: :activated? | ||
22 | - | ||
23 | - def title | ||
24 | - 'PivotalTracker' | ||
25 | - end | ||
26 | - | ||
27 | - def description | ||
28 | - 'Project Management Software (Source Commits Endpoint)' | ||
29 | - end | ||
30 | - | ||
31 | - def to_param | ||
32 | - 'pivotaltracker' | ||
33 | - end | ||
34 | - | ||
35 | - def fields | ||
36 | - [ | ||
37 | - { type: 'text', name: 'token', placeholder: '' } | ||
38 | - ] | ||
39 | - end | ||
40 | - | ||
41 | - def execute(push) | ||
42 | - url = 'https://www.pivotaltracker.com/services/v5/source_commits' | ||
43 | - push[:commits].each do |commit| | ||
44 | - message = { | ||
45 | - 'source_commit' => { | ||
46 | - 'commit_id' => commit[:id], | ||
47 | - 'author' => commit[:author][:name], | ||
48 | - 'url' => commit[:url], | ||
49 | - 'message' => commit[:message] | ||
50 | - } | ||
51 | - } | ||
52 | - PivotaltrackerService.post( | ||
53 | - url, | ||
54 | - body: message.to_json, | ||
55 | - headers: { | ||
56 | - 'Content-Type' => 'application/json', | ||
57 | - 'X-TrackerToken' => token | ||
58 | - } | ||
59 | - ) | ||
60 | - end | ||
61 | - end | ||
62 | -end |
app/models/project.rb
@@ -23,6 +23,13 @@ | @@ -23,6 +23,13 @@ | ||
23 | # visibility_level :integer default(0), not null | 23 | # visibility_level :integer default(0), not null |
24 | # | 24 | # |
25 | 25 | ||
26 | +require_relative "project_services/assembla_service" | ||
27 | +require_relative "project_services/campfire_service" | ||
28 | +require_relative "project_services/flowdock_service" | ||
29 | +require_relative "project_services/gitlab_ci_service" | ||
30 | +require_relative "project_services/hipchat_service" | ||
31 | +require_relative "project_services/pivotaltracker_service" | ||
32 | + | ||
26 | class Project < ActiveRecord::Base | 33 | class Project < ActiveRecord::Base |
27 | include Gitlab::ShellAdapter | 34 | include Gitlab::ShellAdapter |
28 | include Gitlab::VisibilityLevel | 35 | include Gitlab::VisibilityLevel |
@@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +class AssemblaService < Service | ||
19 | + include HTTParty | ||
20 | + | ||
21 | + validates :token, presence: true, if: :activated? | ||
22 | + | ||
23 | + def title | ||
24 | + 'Assembla' | ||
25 | + end | ||
26 | + | ||
27 | + def description | ||
28 | + 'Project Management Software (Source Commits Endpoint)' | ||
29 | + end | ||
30 | + | ||
31 | + def to_param | ||
32 | + 'assembla' | ||
33 | + end | ||
34 | + | ||
35 | + def fields | ||
36 | + [ | ||
37 | + { type: 'text', name: 'token', placeholder: '' } | ||
38 | + ] | ||
39 | + end | ||
40 | + | ||
41 | + def execute(push) | ||
42 | + url = "https://atlas.assembla.com/spaces/ouposp/github_tool?secret_key=#{token}" | ||
43 | + AssemblaService.post(url, body: { payload: push }.to_json, headers: { 'Content-Type' => 'application/json' }) | ||
44 | + end | ||
45 | +end |
@@ -0,0 +1,78 @@ | @@ -0,0 +1,78 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +class CampfireService < Service | ||
19 | + attr_accessible :subdomain, :room | ||
20 | + | ||
21 | + validates :token, presence: true, if: :activated? | ||
22 | + | ||
23 | + def title | ||
24 | + 'Campfire' | ||
25 | + end | ||
26 | + | ||
27 | + def description | ||
28 | + 'Simple web-based real-time group chat' | ||
29 | + end | ||
30 | + | ||
31 | + def to_param | ||
32 | + 'campfire' | ||
33 | + end | ||
34 | + | ||
35 | + def fields | ||
36 | + [ | ||
37 | + { type: 'text', name: 'token', placeholder: '' }, | ||
38 | + { type: 'text', name: 'subdomain', placeholder: '' }, | ||
39 | + { type: 'text', name: 'room', placeholder: '' } | ||
40 | + ] | ||
41 | + end | ||
42 | + | ||
43 | + def execute(push_data) | ||
44 | + room = gate.find_room_by_name(self.room) | ||
45 | + return true unless room | ||
46 | + | ||
47 | + message = build_message(push_data) | ||
48 | + | ||
49 | + room.speak(message) | ||
50 | + end | ||
51 | + | ||
52 | + private | ||
53 | + | ||
54 | + def gate | ||
55 | + @gate ||= Tinder::Campfire.new(subdomain, token: token) | ||
56 | + end | ||
57 | + | ||
58 | + def build_message(push) | ||
59 | + ref = push[:ref].gsub("refs/heads/", "") | ||
60 | + before = push[:before] | ||
61 | + after = push[:after] | ||
62 | + | ||
63 | + message = "" | ||
64 | + message << "[#{project.name_with_namespace}] " | ||
65 | + message << "#{push[:user_name]} " | ||
66 | + | ||
67 | + if before =~ /000000/ | ||
68 | + message << "pushed new branch #{ref} \n" | ||
69 | + elsif after =~ /000000/ | ||
70 | + message << "removed branch #{ref} \n" | ||
71 | + else | ||
72 | + message << "pushed #{push[:total_commits_count]} commits to #{ref}. " | ||
73 | + message << "#{project.web_url}/compare/#{before}...#{after}" | ||
74 | + end | ||
75 | + | ||
76 | + message | ||
77 | + end | ||
78 | +end |
@@ -0,0 +1,54 @@ | @@ -0,0 +1,54 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +require "flowdock-git-hook" | ||
19 | + | ||
20 | +class FlowdockService < Service | ||
21 | + validates :token, presence: true, if: :activated? | ||
22 | + | ||
23 | + def title | ||
24 | + 'Flowdock' | ||
25 | + end | ||
26 | + | ||
27 | + def description | ||
28 | + 'Flowdock is a collaboration web app for technical teams.' | ||
29 | + end | ||
30 | + | ||
31 | + def to_param | ||
32 | + 'flowdock' | ||
33 | + end | ||
34 | + | ||
35 | + def fields | ||
36 | + [ | ||
37 | + { type: 'text', name: 'token', placeholder: '' } | ||
38 | + ] | ||
39 | + end | ||
40 | + | ||
41 | + def execute(push_data) | ||
42 | + repo_path = File.join(Gitlab.config.gitlab_shell.repos_path, "#{project.path_with_namespace}.git") | ||
43 | + Flowdock::Git.post( | ||
44 | + push_data[:ref], | ||
45 | + push_data[:before], | ||
46 | + push_data[:after], | ||
47 | + token: token, | ||
48 | + repo: repo_path, | ||
49 | + repo_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}", | ||
50 | + commit_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/%s", | ||
51 | + diff_url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/compare/%s...%s", | ||
52 | + ) | ||
53 | + end | ||
54 | +end |
@@ -0,0 +1,78 @@ | @@ -0,0 +1,78 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +class GitlabCiService < Service | ||
19 | + attr_accessible :project_url | ||
20 | + | ||
21 | + validates :project_url, presence: true, if: :activated? | ||
22 | + validates :token, presence: true, if: :activated? | ||
23 | + | ||
24 | + delegate :execute, to: :service_hook, prefix: nil | ||
25 | + | ||
26 | + after_save :compose_service_hook, if: :activated? | ||
27 | + | ||
28 | + def compose_service_hook | ||
29 | + hook = service_hook || build_service_hook | ||
30 | + hook.url = [project_url, "/build", "?token=#{token}"].join("") | ||
31 | + hook.save | ||
32 | + end | ||
33 | + | ||
34 | + def commit_status_path sha | ||
35 | + project_url + "/builds/#{sha}/status.json?token=#{token}" | ||
36 | + end | ||
37 | + | ||
38 | + def commit_status sha | ||
39 | + response = HTTParty.get(commit_status_path(sha)) | ||
40 | + | ||
41 | + if response.code == 200 and response["status"] | ||
42 | + response["status"] | ||
43 | + else | ||
44 | + :error | ||
45 | + end | ||
46 | + end | ||
47 | + | ||
48 | + def build_page sha | ||
49 | + project_url + "/builds/#{sha}" | ||
50 | + end | ||
51 | + | ||
52 | + def builds_path | ||
53 | + project_url + "?ref=" + project.default_branch | ||
54 | + end | ||
55 | + | ||
56 | + def status_img_path | ||
57 | + project_url + "/status.png?ref=" + project.default_branch | ||
58 | + end | ||
59 | + | ||
60 | + def title | ||
61 | + 'GitLab CI' | ||
62 | + end | ||
63 | + | ||
64 | + def description | ||
65 | + 'Continuous integration server from GitLab' | ||
66 | + end | ||
67 | + | ||
68 | + def to_param | ||
69 | + 'gitlab_ci' | ||
70 | + end | ||
71 | + | ||
72 | + def fields | ||
73 | + [ | ||
74 | + { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' }, | ||
75 | + { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3'} | ||
76 | + ] | ||
77 | + end | ||
78 | +end |
@@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +class HipchatService < Service | ||
19 | + attr_accessible :room | ||
20 | + | ||
21 | + validates :token, presence: true, if: :activated? | ||
22 | + | ||
23 | + def title | ||
24 | + 'Hipchat' | ||
25 | + end | ||
26 | + | ||
27 | + def description | ||
28 | + 'Private group chat and IM' | ||
29 | + end | ||
30 | + | ||
31 | + def to_param | ||
32 | + 'hipchat' | ||
33 | + end | ||
34 | + | ||
35 | + def fields | ||
36 | + [ | ||
37 | + { type: 'text', name: 'token', placeholder: '' }, | ||
38 | + { type: 'text', name: 'room', placeholder: '' } | ||
39 | + ] | ||
40 | + end | ||
41 | + | ||
42 | + def execute(push_data) | ||
43 | + gate[room].send('Gitlab', create_message(push_data)) | ||
44 | + end | ||
45 | + | ||
46 | + private | ||
47 | + | ||
48 | + def gate | ||
49 | + @gate ||= HipChat::Client.new(token) | ||
50 | + end | ||
51 | + | ||
52 | + def create_message(push) | ||
53 | + ref = push[:ref].gsub("refs/heads/", "") | ||
54 | + before = push[:before] | ||
55 | + after = push[:after] | ||
56 | + | ||
57 | + message = "" | ||
58 | + message << "#{push[:user_name]} " | ||
59 | + if before =~ /000000/ | ||
60 | + message << "pushed new branch <a href=\"#{project.web_url}/commits/#{ref}\">#{ref}</a> to <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a>\n" | ||
61 | + elsif after =~ /000000/ | ||
62 | + message << "removed branch #{ref} from <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> \n" | ||
63 | + else | ||
64 | + message << "#pushed to branch <a href=\"#{project.web_url}/commits/#{ref}\">#{ref}</a> " | ||
65 | + message << "of <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> " | ||
66 | + message << "(<a href=\"#{project.web_url}/compare/#{before}...#{after}\">Compare changes</a>)" | ||
67 | + for commit in push[:commits] do | ||
68 | + message << "<br /> - #{commit[:message]} (<a href=\"#{commit[:url]}\">#{commit[:id][0..5]}</a>)" | ||
69 | + end | ||
70 | + end | ||
71 | + | ||
72 | + message | ||
73 | + end | ||
74 | +end |
@@ -0,0 +1,62 @@ | @@ -0,0 +1,62 @@ | ||
1 | +# == Schema Information | ||
2 | +# | ||
3 | +# Table name: services | ||
4 | +# | ||
5 | +# id :integer not null, primary key | ||
6 | +# type :string(255) | ||
7 | +# title :string(255) | ||
8 | +# token :string(255) | ||
9 | +# project_id :integer not null | ||
10 | +# created_at :datetime not null | ||
11 | +# updated_at :datetime not null | ||
12 | +# active :boolean default(FALSE), not null | ||
13 | +# project_url :string(255) | ||
14 | +# subdomain :string(255) | ||
15 | +# room :string(255) | ||
16 | +# | ||
17 | + | ||
18 | +class PivotaltrackerService < Service | ||
19 | + include HTTParty | ||
20 | + | ||
21 | + validates :token, presence: true, if: :activated? | ||
22 | + | ||
23 | + def title | ||
24 | + 'PivotalTracker' | ||
25 | + end | ||
26 | + | ||
27 | + def description | ||
28 | + 'Project Management Software (Source Commits Endpoint)' | ||
29 | + end | ||
30 | + | ||
31 | + def to_param | ||
32 | + 'pivotaltracker' | ||
33 | + end | ||
34 | + | ||
35 | + def fields | ||
36 | + [ | ||
37 | + { type: 'text', name: 'token', placeholder: '' } | ||
38 | + ] | ||
39 | + end | ||
40 | + | ||
41 | + def execute(push) | ||
42 | + url = 'https://www.pivotaltracker.com/services/v5/source_commits' | ||
43 | + push[:commits].each do |commit| | ||
44 | + message = { | ||
45 | + 'source_commit' => { | ||
46 | + 'commit_id' => commit[:id], | ||
47 | + 'author' => commit[:author][:name], | ||
48 | + 'url' => commit[:url], | ||
49 | + 'message' => commit[:message] | ||
50 | + } | ||
51 | + } | ||
52 | + PivotaltrackerService.post( | ||
53 | + url, | ||
54 | + body: message.to_json, | ||
55 | + headers: { | ||
56 | + 'Content-Type' => 'application/json', | ||
57 | + 'X-TrackerToken' => token | ||
58 | + } | ||
59 | + ) | ||
60 | + end | ||
61 | + end | ||
62 | +end |