Commit 439fe345de21edcdc35be19d8ce3a5b53f835b20
1 parent
db037ae6
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Refactoring serpro_integration
Showing
7 changed files
with
83 additions
and
79 deletions
Show diff stats
plugins/serpro_integration/lib/ext/community.rb
... | ... | @@ -6,32 +6,38 @@ class Community |
6 | 6 | settings_items :allow_gitlab_integration, :type => :boolean, :default => true |
7 | 7 | settings_items :allow_jenkins_integration, :type => :boolean, :default => true |
8 | 8 | |
9 | - #FIXME make test for default option | |
10 | - settings_items :serpro_integration_plugin, :type => Hash, :default => {} | |
9 | + settings_items :serpro_integration_plugin_gitlab, :type => Hash, :default => {} | |
10 | + settings_items :serpro_integration_plugin_jenkins, :type => Hash, :default => {} | |
11 | + settings_items :serpro_integration_plugin_sonar, :type => Hash, :default => {} | |
11 | 12 | |
12 | - attr_accessible :allow_unauthenticated_comments, :allow_gitlab_integration, :gitlab, :allow_sonar_integration, :sonar, :allow_jenkins_integration, :jenkins | |
13 | + attr_accessible :allow_unauthenticated_comments, :allow_gitlab_integration, :allow_sonar_integration, :allow_jenkins_integration, :serpro_integration_plugin_gitlab, :serpro_integration_plugin_jenkins, :serpro_integration_plugin_sonar | |
13 | 14 | |
14 | 15 | after_update :create_integration_projects |
15 | 16 | |
16 | 17 | def create_integration_projects |
17 | - gitlab_project = SerproIntegrationPlugin::GitlabIntegration.create_gitlab_project(self) | |
18 | - SerproIntegrationPlugin::JenkinsIntegration.create_jenkis_project(self, jenkins_project_name, gitlab_project.path_with_namespace, gitlab_project.web_url, gitlab_project.http_url_to_repo) | |
18 | + return unless setting_changed?(:serpro_integration_plugin_gitlab) | |
19 | + | |
20 | + if allow_gitlab_integration | |
21 | + gitlab_integration = SerproIntegrationPlugin::GitlabIntegration.new(gitlab_host, gitlab_private_token) | |
22 | + gitlab_project = gitlab_integration.create_gitlab_project(self) | |
23 | + | |
24 | + if allow_jenkins_integration | |
25 | + jenkins_integration = SerproIntegrationPlugin::JenkinsIntegration.new(jenkins_host, jenkins_private_token, jenkins_user) | |
26 | + jenkins_integration.create_jenkis_project(self, gitlab_project.path_with_namespace, gitlab_project.web_url, gitlab_project.http_url_to_repo) | |
27 | + end | |
28 | + end | |
19 | 29 | end |
20 | 30 | |
21 | 31 | def serpro_integration_plugin_settings |
22 | 32 | @settings ||= Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin) |
23 | 33 | end |
24 | 34 | |
25 | - def gitlab= params | |
26 | - self.serpro_integration_plugin[:gitlab] = params | |
27 | - end | |
28 | - | |
29 | - def gitlab | |
30 | - self.serpro_integration_plugin[:gitlab] ||= {} | |
35 | + def gitlab_group | |
36 | + serpro_integration_plugin_gitlab[:group] || self.identifier | |
31 | 37 | end |
32 | 38 | |
33 | 39 | def gitlab_project_name |
34 | - gitlab[:project_name] || self.identifier | |
40 | + serpro_integration_plugin_gitlab[:project_name] || self.identifier | |
35 | 41 | end |
36 | 42 | |
37 | 43 | def gitlab_host |
... | ... | @@ -42,24 +48,20 @@ class Community |
42 | 48 | serpro_integration_plugin_settings.gitlab[:private_token] |
43 | 49 | end |
44 | 50 | |
45 | - def sonar= params | |
46 | - self.serpro_integration_plugin[:sonar] = params | |
47 | - end | |
48 | - | |
49 | - def sonar | |
50 | - self.serpro_integration_plugin[:sonar] ||= {} | |
51 | + def jenkins_host | |
52 | + serpro_integration_plugin_settings.jenkins[:host] | |
51 | 53 | end |
52 | 54 | |
53 | - def jenkins= params | |
54 | - self.serpro_integration_plugin[:jenkins] = params | |
55 | + def jenkins_private_token | |
56 | + serpro_integration_plugin_settings.jenkins[:private_token] | |
55 | 57 | end |
56 | 58 | |
57 | - def jenkins | |
58 | - self.serpro_integration_plugin[:jenkins] ||= {} | |
59 | + def jenkins_user | |
60 | + serpro_integration_plugin_settings.jenkins[:user] | |
59 | 61 | end |
60 | 62 | |
61 | 63 | def jenkins_project_name |
62 | - jenkins[:project_name] || self.identifier | |
64 | + serpro_integration_plugin_jenkins[:project_name] || self.identifier | |
63 | 65 | end |
64 | 66 | |
65 | 67 | end | ... | ... |
plugins/serpro_integration/lib/serpro_integration_plugin.rb
... | ... | @@ -19,7 +19,7 @@ class SerproIntegrationPlugin < Noosfero::Plugin |
19 | 19 | #FIXME make this test |
20 | 20 | def profile_editor_extras |
21 | 21 | lambda do |
22 | - render :file => 'profile-editor-extras' | |
22 | + render :file => 'profile-editor-extras' if profile.kind_of?(Community) | |
23 | 23 | end |
24 | 24 | end |
25 | 25 | ... | ... |
plugins/serpro_integration/lib/serpro_integration_plugin/gitlab_integration.rb
... | ... | @@ -2,53 +2,61 @@ require 'gitlab' |
2 | 2 | |
3 | 3 | class SerproIntegrationPlugin::GitlabIntegration |
4 | 4 | |
5 | - def self.create_gitlab_project(profile) | |
6 | - Gitlab.endpoint = profile.gitlab_host | |
7 | - Gitlab.private_token = profile.serpro_integration_plugin_settings.gitlab[:private_token] | |
8 | - | |
9 | - #Find user by email | |
10 | - begin | |
11 | - gitlab_user = Gitlab.users(:search => profile.gitlab[:email]) | |
12 | - rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing | |
13 | - gitlab_user = nil | |
14 | - end | |
15 | - | |
16 | - #User not found, create user | |
17 | - #FIXME | |
18 | - if gitlab_user == nil || gitlab_user.count == 0 | |
19 | - gitlab_user = Gitlab.create_user(user.email, '123456', {:username => user.identifier, :name => user.name, :provider => 'ldap'}) | |
20 | - end | |
5 | + def initialize(host, private_token) | |
6 | + @client = Gitlab.client(:endpoint => host, :private_token => private_token) | |
7 | + end | |
21 | 8 | |
22 | - if gitlab_user.nil? | |
23 | - profile.gitlab[:errors] = _('Gitlab user could not be created') | |
24 | - return nil | |
25 | - end | |
9 | + def create_group(group_name) | |
10 | + #FIXME find group by name | |
11 | + group = @client.groups.select {|group| group.name == group_name}.first | |
12 | + group ||= @client.create_group(group_name, group_name) | |
13 | + end | |
26 | 14 | |
27 | - #Create project for user | |
28 | - begin | |
29 | - #FIXME Why this? | |
30 | - if gitlab_user.is_a?(Array) | |
31 | - gitlab_user = gitlab_user[0] | |
32 | - end | |
15 | + def create_project(project_name, group) | |
16 | + path_with_namespace = "#{group.name}/#{project_name}" | |
17 | + #FIXME find project by namespace | |
18 | + project = @client.projects(:scope => :all).select do |project| | |
19 | + project.path_with_namespace == path_with_namespace | |
20 | + end.first | |
33 | 21 | |
22 | + if project.nil? | |
34 | 23 | project_options = {} |
35 | - project_options[:user_id] = gitlab_user.id | |
24 | + project_options[:namespace_id] = group.id | |
36 | 25 | project_options[:issues_enabled ] = true |
37 | 26 | project_options[:wall_enabled] = true |
38 | 27 | project_options[:wiki_enabled] = true |
39 | 28 | project_options[:public] = true |
40 | - project = Gitlab.create_project(profile.gitlab_project_name, project_options) | |
41 | 29 | |
30 | + project = @client.create_project(project_name, project_options) | |
42 | 31 | #Create Web Hook for Jenkins' integration |
43 | -# Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now") | |
44 | -# createJenkinsJob(project.name, project.path_with_namespace, project.web_url, project.http_url_to_repo) | |
45 | - | |
46 | - rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing | |
47 | - #Project already exists | |
48 | - end | |
49 | - profile.gitlab[:errors] = nil | |
32 | + #Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now") | |
33 | + end | |
50 | 34 | project |
51 | 35 | end |
52 | 36 | |
37 | + def create_user(email, group) | |
38 | + user = @client.users(:search => email).first | |
39 | + username = name = email[/[^@]+/] | |
40 | + user ||= @client.create_user(email, '123456', {:username => username, :name => name, :provider => 'ldap'}) | |
41 | + | |
42 | + begin | |
43 | + @client.add_group_member(group.id, user.id, 40) | |
44 | + rescue Gitlab::Error::Conflict => e | |
45 | + #already member | |
46 | + end | |
47 | + user | |
48 | + end | |
49 | + | |
50 | + #http://rubydoc.info/gems/gitlab/frames | |
51 | + def create_gitlab_project(profile) | |
52 | + group = create_group(profile.gitlab_group) | |
53 | + | |
54 | + #create admins and add to group | |
55 | + profile.admins.each do |person| | |
56 | + create_user(person.user.email, group) | |
57 | + end | |
58 | + | |
59 | + project = create_project(profile.gitlab_project_name, group) | |
60 | + end | |
53 | 61 | |
54 | 62 | end | ... | ... |
plugins/serpro_integration/lib/serpro_integration_plugin/jenkins_integration.rb
... | ... | @@ -2,27 +2,25 @@ |
2 | 2 | require 'jenkins_api_client' |
3 | 3 | |
4 | 4 | class SerproIntegrationPlugin::JenkinsIntegration |
5 | - #FIXME make jenkins integration works | |
6 | - def self.create_jenkis_project(profile, projectName, repositoryPath, webUrl, gitUrl) | |
7 | 5 | |
8 | - @client = JenkinsApi::Client.new(:server_url => profile.serpro_integration_plugin_settings.jenkins[:host], | |
9 | - :password => profile.serpro_integration_plugin_settings.jenkins[:private_token], | |
10 | - :username => profile.serpro_integration_plugin_settings.jenkins[:user]) | |
6 | + def initialize(host, private_token, user) | |
7 | + @client = JenkinsApi::Client.new(:server_url => host, :password => private_token, :username => user) | |
8 | + end | |
11 | 9 | |
10 | + #FIXME make jenkins integration works | |
11 | + def create_jenkis_project(profile, repository_path, web_url, git_url) | |
12 | 12 | #begin |
13 | - @client.job.create(projectName, xml_jenkins(repositoryPath, webUrl, gitUrl)) | |
13 | + @client.job.create(profile.jenkins_project_name, xml_jenkins(repository_path, web_url, git_url)) | |
14 | 14 | #rescue JenkinsApi::Exceptions::ApiException |
15 | - | |
16 | 15 | #end |
17 | - | |
18 | 16 | end |
19 | 17 | |
20 | 18 | #FIXME |
21 | - def self.xml_jenkins(repositoryPath, webUrl, gitUrl) | |
19 | + def xml_jenkins(repository_path, web_url, git_url) | |
22 | 20 | " |
23 | 21 | <maven2-moduleset plugin='maven-plugin@1.509'> |
24 | 22 | <actions/> |
25 | - <description>Projeto criado para o repositório #{repositoryPath} do Gitlab - #{webUrl}</description> | |
23 | + <description>Projeto criado para o repositório #{repository_path} do Gitlab - #{web_url}</description> | |
26 | 24 | <logRotator class='hudson.tasks.LogRotator'> |
27 | 25 | <daysToKeep>-1</daysToKeep> |
28 | 26 | <numToKeep>2</numToKeep> |
... | ... | @@ -35,7 +33,7 @@ class SerproIntegrationPlugin::JenkinsIntegration |
35 | 33 | <configVersion>2</configVersion> |
36 | 34 | <userRemoteConfigs> |
37 | 35 | <hudson.plugins.git.UserRemoteConfig> |
38 | - <url>#{gitUrl}</url> | |
36 | + <url>#{git_url}</url> | |
39 | 37 | </hudson.plugins.git.UserRemoteConfig> |
40 | 38 | </userRemoteConfigs> |
41 | 39 | <branches> | ... | ... |
plugins/serpro_integration/views/profile-editor-extras.html.erb
... | ... | @@ -29,10 +29,10 @@ jQuery( document ).ready(function( $ ) { |
29 | 29 | </h3> |
30 | 30 | <ul class='sonar'> |
31 | 31 | <li> |
32 | - <%= labelled_text_field(_('Server Host'), 'profile_data[sonar]host]', profile.sonar[:host]) %> | |
32 | + <%= labelled_text_field(_('Server Host'), 'profile_data[serpro_integration_plugin_sonar][host]', profile.serpro_integration_plugin_sonar[:host]) %> | |
33 | 33 | </li> |
34 | 34 | <li> |
35 | - <%= labelled_text_field(_('Project: '), 'profile_data[sonar][project]', profile.sonar[:project]) %> | |
35 | + <%= labelled_text_field(_('Project: '), 'profile_data[serpro_integration_plugin_sonar][project]', profile.serpro_integration_plugin_sonar[:project]) %> | |
36 | 36 | </li> |
37 | 37 | </ul> |
38 | 38 | </div> | ... | ... |
plugins/serpro_integration/views/profile_editor/_gitlab.html.erb
... | ... | @@ -11,14 +11,10 @@ |
11 | 11 | <%= link_to _('Force'), {:controller => 'serpro_integration_plugin_myprofile', :action => 'create_gitlab'}, {:remote => true} %> |
12 | 12 | </li> |
13 | 13 | <li> |
14 | - <%= labelled_text_field(_('Project Name:'), 'profile_data[gitlab][project_name]', profile.gitlab_project_name ) %> | |
14 | + <%= labelled_text_field(_('Group Name:'), 'profile_data[serpro_integration_plugin_gitlab][group]', profile.gitlab_group) %> | |
15 | 15 | </li> |
16 | 16 | <li> |
17 | - <%= labelled_text_field(_('Owner Email:'), 'profile_data[gitlab][email]', profile.gitlab[:email] || current_user.person.email ) %> | |
17 | + <%= labelled_text_field(_('Project Name:'), 'profile_data[serpro_integration_plugin_gitlab][project_name]', profile.gitlab_project_name ) %> | |
18 | 18 | </li> |
19 | - <li> | |
20 | - <%= _('Erros: %s') % (profile.gitlab[:errors] || _('None')) %> | |
21 | - </li> | |
22 | - | |
23 | 19 | </ul> |
24 | 20 | </div> | ... | ... |
plugins/serpro_integration/views/profile_editor/_jenkins.html.erb
... | ... | @@ -5,7 +5,7 @@ |
5 | 5 | </h3> |
6 | 6 | <ul class='jenkins'> |
7 | 7 | <li> |
8 | - <%= labelled_text_field(_('Project Name:'), 'profile_data[jenkins][project_name]', profile.jenkins_project_name ) %> | |
8 | + <%= labelled_text_field(_('Project Name:'), 'profile_data[serpro_integration_plugin_jenkins][project_name]', profile.jenkins_project_name ) %> | |
9 | 9 | </li> |
10 | 10 | </ul> |
11 | 11 | </div> | ... | ... |