diff --git a/lib/ext/community.rb b/lib/ext/community.rb index 14fbb20..33280d3 100644 --- a/lib/ext/community.rb +++ b/lib/ext/community.rb @@ -6,32 +6,38 @@ class Community settings_items :allow_gitlab_integration, :type => :boolean, :default => true settings_items :allow_jenkins_integration, :type => :boolean, :default => true - #FIXME make test for default option - settings_items :serpro_integration_plugin, :type => Hash, :default => {} + settings_items :serpro_integration_plugin_gitlab, :type => Hash, :default => {} + settings_items :serpro_integration_plugin_jenkins, :type => Hash, :default => {} + settings_items :serpro_integration_plugin_sonar, :type => Hash, :default => {} - attr_accessible :allow_unauthenticated_comments, :allow_gitlab_integration, :gitlab, :allow_sonar_integration, :sonar, :allow_jenkins_integration, :jenkins + 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 after_update :create_integration_projects def create_integration_projects - gitlab_project = SerproIntegrationPlugin::GitlabIntegration.create_gitlab_project(self) - SerproIntegrationPlugin::JenkinsIntegration.create_jenkis_project(self, jenkins_project_name, gitlab_project.path_with_namespace, gitlab_project.web_url, gitlab_project.http_url_to_repo) + return unless setting_changed?(:serpro_integration_plugin_gitlab) + + if allow_gitlab_integration + gitlab_integration = SerproIntegrationPlugin::GitlabIntegration.new(gitlab_host, gitlab_private_token) + gitlab_project = gitlab_integration.create_gitlab_project(self) + + if allow_jenkins_integration + jenkins_integration = SerproIntegrationPlugin::JenkinsIntegration.new(jenkins_host, jenkins_private_token, jenkins_user) + jenkins_integration.create_jenkis_project(self, gitlab_project.path_with_namespace, gitlab_project.web_url, gitlab_project.http_url_to_repo) + end + end end def serpro_integration_plugin_settings @settings ||= Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin) end - def gitlab= params - self.serpro_integration_plugin[:gitlab] = params - end - - def gitlab - self.serpro_integration_plugin[:gitlab] ||= {} + def gitlab_group + serpro_integration_plugin_gitlab[:group] || self.identifier end def gitlab_project_name - gitlab[:project_name] || self.identifier + serpro_integration_plugin_gitlab[:project_name] || self.identifier end def gitlab_host @@ -42,24 +48,20 @@ class Community serpro_integration_plugin_settings.gitlab[:private_token] end - def sonar= params - self.serpro_integration_plugin[:sonar] = params - end - - def sonar - self.serpro_integration_plugin[:sonar] ||= {} + def jenkins_host + serpro_integration_plugin_settings.jenkins[:host] end - def jenkins= params - self.serpro_integration_plugin[:jenkins] = params + def jenkins_private_token + serpro_integration_plugin_settings.jenkins[:private_token] end - def jenkins - self.serpro_integration_plugin[:jenkins] ||= {} + def jenkins_user + serpro_integration_plugin_settings.jenkins[:user] end def jenkins_project_name - jenkins[:project_name] || self.identifier + serpro_integration_plugin_jenkins[:project_name] || self.identifier end end diff --git a/lib/serpro_integration_plugin.rb b/lib/serpro_integration_plugin.rb index c24c05e..455deff 100644 --- a/lib/serpro_integration_plugin.rb +++ b/lib/serpro_integration_plugin.rb @@ -19,7 +19,7 @@ class SerproIntegrationPlugin < Noosfero::Plugin #FIXME make this test def profile_editor_extras lambda do - render :file => 'profile-editor-extras' + render :file => 'profile-editor-extras' if profile.kind_of?(Community) end end diff --git a/lib/serpro_integration_plugin/gitlab_integration.rb b/lib/serpro_integration_plugin/gitlab_integration.rb index 2a99156..2fb0d14 100644 --- a/lib/serpro_integration_plugin/gitlab_integration.rb +++ b/lib/serpro_integration_plugin/gitlab_integration.rb @@ -2,53 +2,61 @@ 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 + def initialize(host, private_token) + @client = Gitlab.client(:endpoint => host, :private_token => private_token) + end - if gitlab_user.nil? - profile.gitlab[:errors] = _('Gitlab user could not be created') - return nil - end + def create_group(group_name) + #FIXME find group by name + group = @client.groups.select {|group| group.name == group_name}.first + group ||= @client.create_group(group_name, group_name) + end - #Create project for user - begin - #FIXME Why this? - if gitlab_user.is_a?(Array) - gitlab_user = gitlab_user[0] - end + def create_project(project_name, group) + path_with_namespace = "#{group.name}/#{project_name}" + #FIXME find project by namespace + project = @client.projects(:scope => :all).select do |project| + project.path_with_namespace == path_with_namespace + end.first + if project.nil? project_options = {} - project_options[:user_id] = gitlab_user.id + project_options[:namespace_id] = group.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) + project = @client.create_project(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 + #Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now") + end project end + def create_user(email, group) + user = @client.users(:search => email).first + username = name = email[/[^@]+/] + user ||= @client.create_user(email, '123456', {:username => username, :name => name, :provider => 'ldap'}) + + begin + @client.add_group_member(group.id, user.id, 40) + rescue Gitlab::Error::Conflict => e + #already member + end + user + end + + #http://rubydoc.info/gems/gitlab/frames + def create_gitlab_project(profile) + group = create_group(profile.gitlab_group) + + #create admins and add to group + profile.admins.each do |person| + create_user(person.user.email, group) + end + + project = create_project(profile.gitlab_project_name, group) + end end diff --git a/lib/serpro_integration_plugin/jenkins_integration.rb b/lib/serpro_integration_plugin/jenkins_integration.rb index 0a23305..d5db130 100644 --- a/lib/serpro_integration_plugin/jenkins_integration.rb +++ b/lib/serpro_integration_plugin/jenkins_integration.rb @@ -2,27 +2,25 @@ require 'jenkins_api_client' class SerproIntegrationPlugin::JenkinsIntegration - #FIXME make jenkins integration works - def self.create_jenkis_project(profile, projectName, repositoryPath, webUrl, gitUrl) - @client = JenkinsApi::Client.new(:server_url => profile.serpro_integration_plugin_settings.jenkins[:host], - :password => profile.serpro_integration_plugin_settings.jenkins[:private_token], - :username => profile.serpro_integration_plugin_settings.jenkins[:user]) + def initialize(host, private_token, user) + @client = JenkinsApi::Client.new(:server_url => host, :password => private_token, :username => user) + end + #FIXME make jenkins integration works + def create_jenkis_project(profile, repository_path, web_url, git_url) #begin - @client.job.create(projectName, xml_jenkins(repositoryPath, webUrl, gitUrl)) + @client.job.create(profile.jenkins_project_name, xml_jenkins(repository_path, web_url, git_url)) #rescue JenkinsApi::Exceptions::ApiException - #end - end #FIXME - def self.xml_jenkins(repositoryPath, webUrl, gitUrl) + def xml_jenkins(repository_path, web_url, git_url) " - Projeto criado para o repositório #{repositoryPath} do Gitlab - #{webUrl} + Projeto criado para o repositório #{repository_path} do Gitlab - #{web_url} -1 2 @@ -35,7 +33,7 @@ class SerproIntegrationPlugin::JenkinsIntegration 2 - #{gitUrl} + #{git_url} diff --git a/views/profile-editor-extras.html.erb b/views/profile-editor-extras.html.erb index 8aae89c..ec1a18b 100644 --- a/views/profile-editor-extras.html.erb +++ b/views/profile-editor-extras.html.erb @@ -29,10 +29,10 @@ jQuery( document ).ready(function( $ ) {
  • - <%= labelled_text_field(_('Server Host'), 'profile_data[sonar]host]', profile.sonar[:host]) %> + <%= labelled_text_field(_('Server Host'), 'profile_data[serpro_integration_plugin_sonar][host]', profile.serpro_integration_plugin_sonar[:host]) %>
  • - <%= labelled_text_field(_('Project: '), 'profile_data[sonar][project]', profile.sonar[:project]) %> + <%= labelled_text_field(_('Project: '), 'profile_data[serpro_integration_plugin_sonar][project]', profile.serpro_integration_plugin_sonar[:project]) %>
diff --git a/views/profile_editor/_gitlab.html.erb b/views/profile_editor/_gitlab.html.erb index 444331d..31d3e68 100644 --- a/views/profile_editor/_gitlab.html.erb +++ b/views/profile_editor/_gitlab.html.erb @@ -11,14 +11,10 @@ <%= link_to _('Force'), {:controller => 'serpro_integration_plugin_myprofile', :action => 'create_gitlab'}, {:remote => true} %>
  • - <%= labelled_text_field(_('Project Name:'), 'profile_data[gitlab][project_name]', profile.gitlab_project_name ) %> + <%= labelled_text_field(_('Group Name:'), 'profile_data[serpro_integration_plugin_gitlab][group]', profile.gitlab_group) %>
  • - <%= labelled_text_field(_('Owner Email:'), 'profile_data[gitlab][email]', profile.gitlab[:email] || current_user.person.email ) %> + <%= labelled_text_field(_('Project Name:'), 'profile_data[serpro_integration_plugin_gitlab][project_name]', profile.gitlab_project_name ) %>
  • -
  • - <%= _('Erros: %s') % (profile.gitlab[:errors] || _('None')) %> -
  • - diff --git a/views/profile_editor/_jenkins.html.erb b/views/profile_editor/_jenkins.html.erb index 5624a31..c918863 100644 --- a/views/profile_editor/_jenkins.html.erb +++ b/views/profile_editor/_jenkins.html.erb @@ -5,7 +5,7 @@
    • - <%= labelled_text_field(_('Project Name:'), 'profile_data[jenkins][project_name]', profile.jenkins_project_name ) %> + <%= labelled_text_field(_('Project Name:'), 'profile_data[serpro_integration_plugin_jenkins][project_name]', profile.jenkins_project_name ) %>
    -- libgit2 0.21.2