gitlab_ci_service.rb 2.23 KB
Newer Older
1
2
3
4
# == Schema Information
#
# Table name: services
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5
6
7
8
9
10
11
12
13
14
15
16
17
#  id                    :integer          not null, primary key
#  type                  :string(255)
#  title                 :string(255)
#  project_id            :integer
#  created_at            :datetime
#  updated_at            :datetime
#  active                :boolean          default(FALSE), not null
#  properties            :text
#  template              :boolean          default(FALSE)
#  push_events           :boolean          default(TRUE)
#  issues_events         :boolean          default(TRUE)
#  merge_requests_events :boolean          default(TRUE)
#  tag_push_events       :boolean          default(TRUE)
Stan Hu's avatar
Stan Hu committed
18
#  note_events           :boolean          default(TRUE), not null
19
20
#

21
class GitlabCiService < CiService
Kamil Trzcinski's avatar
Fix CI    
Kamil Trzcinski committed
22
23
  include Gitlab::Application.routes.url_helpers

24
  after_save :compose_service_hook, if: :activated?
25
  after_save :ensure_gitlab_ci_project, if: :activated?
26

27
28
29
30
  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.save
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
31

32
  def ensure_gitlab_ci_project
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
    return unless project
34
35
36
    project.ensure_gitlab_ci_project
  end

37
38
39
40
  def supported_events
    %w(push tag_push)
  end

41
  def execute(data)
42
    return unless supported_events.include?(data[:object_kind])
43

44
    ci_project = project.gitlab_ci_project
45
    if ci_project
46
      current_user = User.find_by(id: data[:user_id])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
      Ci::CreateCommitService.new.execute(ci_project, current_user, data)
48
    end
49
50
  end

51
52
53
54
55
56
  def token
    if project.gitlab_ci_project.present?
      project.gitlab_ci_project.token
    end
  end

Valery Sizov's avatar
Valery Sizov committed
57
  def get_ci_commit(sha, ref)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
58
    Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha!(sha)
59
60
  end

Valery Sizov's avatar
Valery Sizov committed
61
  def commit_status(sha, ref)
Valery Sizov's avatar
Valery Sizov committed
62
63
    get_ci_commit(sha, ref).status
  rescue ActiveRecord::RecordNotFound
64
    :error
65
66
  end

Valery Sizov's avatar
Valery Sizov committed
67
  def commit_coverage(sha, ref)
Valery Sizov's avatar
Valery Sizov committed
68
69
70
    get_ci_commit(sha, ref).coverage
  rescue ActiveRecord::RecordNotFound
    :error
71
72
  end

Valery Sizov's avatar
Valery Sizov committed
73
  def build_page(sha, ref)
74
    if project.gitlab_ci_project.present?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
75
      builds_namespace_project_commit_url(project.namespace, project, sha)
76
    end
77
  end
78
79
80
81
82
83
84
85
86
87
88
89
90
91

  def title
    'GitLab CI'
  end

  def description
    'Continuous integration server from GitLab'
  end

  def to_param
    'gitlab_ci'
  end

  def fields
Valery Sizov's avatar
Valery Sizov committed
92
    []
93
  end
94
end