gitlab_ci_service.rb 2.81 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
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
    sha = data[:checkout_sha]
44

45
46
47
48
49
50
    if sha.present?
      file = ci_yaml_file(sha)

      if file && file.data
        data.merge!(ci_yaml_file: file.data)
      end
51
52
    end

Valery Sizov's avatar
Valery Sizov committed
53
    ci_project = Ci::Project.find_by(gitlab_id: project.id)
54
55
56
    if ci_project
      Ci::CreateCommitService.new.execute(ci_project, data)
    end
57
58
  end

59
60
61
62
63
64
  def token
    if project.gitlab_ci_project.present?
      project.gitlab_ci_project.token
    end
  end

Valery Sizov's avatar
Valery Sizov committed
65
66
  def get_ci_commit(sha, ref)
    Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha_and_ref!(sha, ref)
67
68
  end

Valery Sizov's avatar
Valery Sizov committed
69
  def commit_status(sha, ref)
Valery Sizov's avatar
Valery Sizov committed
70
71
    get_ci_commit(sha, ref).status
  rescue ActiveRecord::RecordNotFound
72
    :error
73
74
  end

Valery Sizov's avatar
Valery Sizov committed
75
76
  def fork_registration(new_project, current_user)
    params = OpenStruct.new({
Valery Sizov's avatar
Valery Sizov committed
77
      id:                  new_project.id,
78
      default_branch:      new_project.default_branch
Valery Sizov's avatar
Valery Sizov committed
79
80
81
    })

    ci_project = Ci::Project.find_by!(gitlab_id: project.id)
82

Valery Sizov's avatar
Valery Sizov committed
83
84
85
86
    Ci::CreateProjectService.new.execute(
      current_user,
      params,
      ci_project
Valery Sizov's avatar
Valery Sizov committed
87
88
89
    )
  end

Valery Sizov's avatar
Valery Sizov committed
90
  def commit_coverage(sha, ref)
Valery Sizov's avatar
Valery Sizov committed
91
92
93
    get_ci_commit(sha, ref).coverage
  rescue ActiveRecord::RecordNotFound
    :error
94
95
  end

Valery Sizov's avatar
Valery Sizov committed
96
  def build_page(sha, ref)
97
    if project.gitlab_ci_project.present?
98
      ci_project_ref_commits_url(project.gitlab_ci_project, ref, sha)
99
    end
100
  end
101
102
103
104
105
106
107
108
109
110
111
112
113
114

  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
115
    []
116
  end
Valery Sizov's avatar
Valery Sizov committed
117
118
119

  private

120
121
  def ci_yaml_file(sha)
    repository.blob_at(sha, '.gitlab-ci.yml')
122
123
  end

124
125
126
  def repository
    project.repository
  end
127
end