gitlab_ci_service.rb 2.84 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
25
26
27
28
29
30
  after_save :compose_service_hook, if: :activated?

  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.save
  end

31
32
33
34
  def supported_events
    %w(push tag_push)
  end

35
  def execute(data)
36
    return unless supported_events.include?(data[:object_kind])
37

38
    sha = data[:checkout_sha]
39

40
41
42
43
44
45
    if sha.present?
      file = ci_yaml_file(sha)

      if file && file.data
        data.merge!(ci_yaml_file: file.data)
      end
46
47
    end

Valery Sizov's avatar
Valery Sizov committed
48
    ci_project = Ci::Project.find_by(gitlab_id: project.id)
49
50
51
    if ci_project
      Ci::CreateCommitService.new.execute(ci_project, data)
    end
52
53
  end

54
55
56
57
58
59
  def token
    if project.gitlab_ci_project.present?
      project.gitlab_ci_project.token
    end
  end

Valery Sizov's avatar
Valery Sizov committed
60
61
  def get_ci_commit(sha, ref)
    Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha_and_ref!(sha, ref)
62
63
  end

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

Valery Sizov's avatar
Valery Sizov committed
70
71
  def fork_registration(new_project, current_user)
    params = OpenStruct.new({
Valery Sizov's avatar
Valery Sizov committed
72
73
      id:                  new_project.id,
      name_with_namespace: new_project.name_with_namespace,
Valery Sizov's avatar
Valery Sizov committed
74
      path_with_namespace: new_project.path_with_namespace,
Valery Sizov's avatar
Valery Sizov committed
75
      web_url:             new_project.web_url,
76
      default_branch:      new_project.default_branch
Valery Sizov's avatar
Valery Sizov committed
77
    })
Valery Sizov's avatar
Valery Sizov committed
78

Valery Sizov's avatar
Valery Sizov committed
79
    ci_project = Ci::Project.find_by!(gitlab_id: project.id)
80

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

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

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

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

  private

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

122
123
124
  def repository
    project.repository
  end
125
end