OSDN Git Service

4eb39c7ef4d5a7aebdc43a6071d34440199a88b7
[wvm/gitlab.git] / app / models / gitlab_ci_service.rb
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 #
15
16 class GitlabCiService < Service
17   attr_accessible :project_url
18
19   validates :project_url, presence: true, if: :activated?
20   validates :token, presence: true, if: :activated?
21
22   delegate :execute, to: :service_hook, prefix: nil
23
24   after_save :compose_service_hook, if: :activated?
25
26   def compose_service_hook
27     hook = service_hook || build_service_hook
28     hook.url = [project_url, "/build", "?token=#{token}"].join("")
29     hook.save
30   end
31
32   def commit_status_path sha
33     project_url + "/builds/#{sha}/status.json?token=#{token}"
34   end
35
36   def commit_status sha
37     response = HTTParty.get(commit_status_path(sha))
38
39     if response.code == 200 and response["status"]
40       response["status"]
41     else
42       :error
43     end
44   end
45
46   def build_page sha
47     project_url + "/builds/#{sha}"
48   end
49 end