gitlab_ci_service.rb 3.08 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)
18
19
#

20
class GitlabCiService < CiService
Valery Sizov's avatar
Valery Sizov committed
21
22
  API_PREFIX = "api/v1"

Drew Blessing's avatar
Drew Blessing committed
23
  prop_accessor :project_url, :token
24
25
26
27
28
29
30
31
32
33
34
  validates :project_url, presence: true, if: :activated?
  validates :token, presence: true, if: :activated?

  after_save :compose_service_hook, if: :activated?

  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.url = [project_url, "/build", "?token=#{token}"].join("")
    hook.save
  end

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

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

    service_hook.execute(data)
  end

Valery Sizov's avatar
Valery Sizov committed
45
46
  def commit_status_path(sha, ref)
    project_url + "/refs/#{ref}/commits/#{sha}/status.json?token=#{token}"
47
48
  end

Valery Sizov's avatar
Valery Sizov committed
49
  def get_ci_build(sha, ref)
50
    @ci_builds ||= {}
Valery Sizov's avatar
Valery Sizov committed
51
    @ci_builds[sha] ||= HTTParty.get(commit_status_path(sha, ref), verify: false)
52
53
  end

Valery Sizov's avatar
Valery Sizov committed
54
55
  def commit_status(sha, ref)
    response = get_ci_build(sha, ref)
56
57
58
59
60
61
62
63

    if response.code == 200 and response["status"]
      response["status"]
    else
      :error
    end
  end

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

    HTTParty.post(
Valery Sizov's avatar
Valery Sizov committed
74
      fork_registration_path,
Valery Sizov's avatar
Valery Sizov committed
75
76
77
      body: {
        project_id: project.id,
        project_token: token,
Valery Sizov's avatar
Valery Sizov committed
78
79
        private_token: private_token,
        data: params },
Valery Sizov's avatar
Valery Sizov committed
80
81
82
83
      verify: false
    )
  end

Valery Sizov's avatar
Valery Sizov committed
84
85
  def commit_coverage(sha, ref)
    response = get_ci_build(sha, ref)
86
87
88
89
90
91

    if response.code == 200 and response["coverage"]
      response["coverage"]
    end
  end

Valery Sizov's avatar
Valery Sizov committed
92
93
  def build_page(sha, ref)
    project_url + "/refs/#{ref}/commits/#{sha}"
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  end

  def builds_path
    project_url + "?ref=" + project.default_branch
  end

  def status_img_path
    project_url + "/status.png?ref=" + project.default_branch
  end

  def title
    'GitLab CI'
  end

  def description
    'Continuous integration server from GitLab'
  end

  def to_param
    'gitlab_ci'
  end

  def fields
    [
      { type: 'text', name: 'token', placeholder: 'GitLab CI project specific token' },
119
      { type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3' }
120
121
    ]
  end
Valery Sizov's avatar
Valery Sizov committed
122
123
124
125
126
127

  private

  def fork_registration_path
    project_url.sub(/projects\/\d*/, "#{API_PREFIX}/forks")
  end
128
end