gitlab_integration.rb
1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'gitlab'
class SerproIntegrationPlugin::GitlabIntegration
def self.create_gitlab_project(profile)
Gitlab.endpoint = profile.gitlab_host
Gitlab.private_token = profile.serpro_integration_plugin_settings.gitlab[:private_token]
#Find user by email
begin
gitlab_user = Gitlab.users(:search => profile.gitlab[:email])
rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing
gitlab_user = nil
end
#User not found, create user
#FIXME
if gitlab_user == nil || gitlab_user.count == 0
gitlab_user = Gitlab.create_user(user.email, '123456', {:username => user.identifier, :name => user.name, :provider => 'ldap'})
end
if gitlab_user.nil?
profile.gitlab[:errors] = _('Gitlab user could not be created')
return nil
end
#Create project for user
begin
#FIXME Why this?
if gitlab_user.is_a?(Array)
gitlab_user = gitlab_user[0]
end
project_options = {}
project_options[:user_id] = gitlab_user.id
project_options[:issues_enabled ] = true
project_options[:wall_enabled] = true
project_options[:wiki_enabled] = true
project_options[:public] = true
project = Gitlab.create_project(profile.gitlab_project_name, project_options)
#Create Web Hook for Jenkins' integration
# Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now")
# createJenkinsJob(project.name, project.path_with_namespace, project.web_url, project.http_url_to_repo)
rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing
#Project already exists
end
profile.gitlab[:errors] = nil
project
end
end