gitlab_ci_service.rb 2.79 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
Valery Sizov's avatar
Valery Sizov committed
22
  prop_accessor :token
23
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

Valery Sizov's avatar
Valery Sizov committed
54
55
  def get_ci_commit(sha, ref)
    Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha_and_ref!(sha, ref)
56
57
  end

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

Valery Sizov's avatar
Valery Sizov committed
64
65
  def fork_registration(new_project, current_user)
    params = OpenStruct.new({
Valery Sizov's avatar
Valery Sizov committed
66
67
      id:                  new_project.id,
      name_with_namespace: new_project.name_with_namespace,
Valery Sizov's avatar
Valery Sizov committed
68
      path_with_namespace: new_project.path_with_namespace,
Valery Sizov's avatar
Valery Sizov committed
69
70
71
      web_url:             new_project.web_url,
      default_branch:      new_project.default_branch,
      ssh_url_to_repo:     new_project.ssh_url_to_repo
Valery Sizov's avatar
Valery Sizov committed
72
    })
Valery Sizov's avatar
Valery Sizov committed
73

Valery Sizov's avatar
Valery Sizov committed
74
75
76
77
78
79
    ci_project = Ci::Project.find_by!(gitlab_id: project.id)
    
    Ci::CreateProjectService.new.execute(
      current_user,
      params,
      ci_project
Valery Sizov's avatar
Valery Sizov committed
80
81
82
    )
  end

Valery Sizov's avatar
Valery Sizov committed
83
  def commit_coverage(sha, ref)
Valery Sizov's avatar
Valery Sizov committed
84
85
86
    get_ci_commit(sha, ref).coverage
  rescue ActiveRecord::RecordNotFound
    :error
87
88
  end

Valery Sizov's avatar
Valery Sizov committed
89
  def build_page(sha, ref)
90
91
92
    if project.gitlab_ci_project.present?
      Ci::RoutesHelper.ci_project_ref_commits_path(project.gitlab_ci_project, ref, sha)
    end
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  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
108
    []
109
  end
Valery Sizov's avatar
Valery Sizov committed
110
111
112

  private

113
114
  def ci_yaml_file(sha)
    repository.blob_at(sha, '.gitlab-ci.yml')
115
116
  end

117
118
119
  def repository
    project.repository
  end
120
end