gitlab_ci_service.rb 2.2 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
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
31

32 33 34 35
  def ensure_gitlab_ci_project
    project.ensure_gitlab_ci_project
  end

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

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

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

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

56
  def get_ci_commit(sha, ref)
57
    Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha!(sha)
58 59
  end

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

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

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

  def title
    'GitLab CI'
  end

  def description
    'Continuous integration server from GitLab'
  end

  def to_param
    'gitlab_ci'
  end

  def fields
91
    []
92
  end
93
end