From 2a60008b6f38a9f9397014e90b37ec6ae6955f66 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 4 Jun 2014 18:16:08 -0300 Subject: [PATCH] Added serpro integration plugin global configuration and fix gitlab project creation --- plugins/serpro_integration/Gemfile | 1 + plugins/serpro_integration/controllers/serpro_integration_plugin_admin_controller.rb | 15 +++++++++++++++ plugins/serpro_integration/lib/ext/community.rb | 43 ++++++++++++++++++++++++------------------- plugins/serpro_integration/lib/serpro_integration_plugin.rb | 25 ------------------------- plugins/serpro_integration/views/profile-editor-extras.html.erb | 49 ++++++++++++++++++++++++++++++++----------------- plugins/serpro_integration/views/profile_editor/_gitlab.html.erb | 26 ++++++++++++++------------ plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb | 18 ++++++++++++++++++ 7 files changed, 104 insertions(+), 73 deletions(-) create mode 100644 plugins/serpro_integration/Gemfile create mode 100644 plugins/serpro_integration/controllers/serpro_integration_plugin_admin_controller.rb create mode 100644 plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb diff --git a/plugins/serpro_integration/Gemfile b/plugins/serpro_integration/Gemfile new file mode 100644 index 0000000..5b55ef1 --- /dev/null +++ b/plugins/serpro_integration/Gemfile @@ -0,0 +1 @@ +gem 'gitlab', '~> 3.1.0' diff --git a/plugins/serpro_integration/controllers/serpro_integration_plugin_admin_controller.rb b/plugins/serpro_integration/controllers/serpro_integration_plugin_admin_controller.rb new file mode 100644 index 0000000..516de72 --- /dev/null +++ b/plugins/serpro_integration/controllers/serpro_integration_plugin_admin_controller.rb @@ -0,0 +1,15 @@ +class SerproIntegrationPluginAdminController < AdminController + + def index + settings = params[:settings] + settings ||= {} + + @settings = Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin, settings) + if request.post? + @settings.save! + session[:notice] = 'Settings succefully saved.' + redirect_to :action => 'index' + end + end + +end diff --git a/plugins/serpro_integration/lib/ext/community.rb b/plugins/serpro_integration/lib/ext/community.rb index 977f3dd..caadf98 100644 --- a/plugins/serpro_integration/lib/ext/community.rb +++ b/plugins/serpro_integration/lib/ext/community.rb @@ -1,5 +1,5 @@ require_dependency 'community' -#require 'gitlab' +require 'gitlab' #require 'jenkins_api_client' class Community @@ -9,53 +9,54 @@ class Community settings_items :allow_jenkins_integration, :type => :boolean, :default => true #FIXME make test for default option - settings_items :serpro_integration_plugin, :type => Hash + settings_items :serpro_integration_plugin, :type => Hash, :default => {} + + attr_accessible :allow_unauthenticated_comments, :allow_gitlab_integration, :gitlab, :allow_sonar_integration, :sonar, :allow_jenkins_integration, :jenkins ########################################## # Gitlab stuff # ########################################## - after_create :create_gitlab_project + #after_create :create_gitlab_project def gitlab= params self.serpro_integration_plugin[:gitlab] = params end def gitlab - self.serpro_integration_plugin ||= {} self.serpro_integration_plugin[:gitlab] ||= {} - self.serpro_integration_plugin[:gitlab] end - def create_gitlab_project - Gitlab.endpoint = self.gitlab_host - Gitlab.private_token = self.gitlab_private_token + def serpro_integration_plugin_settings + @settings ||= Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin) + end - user = nil + def create_gitlab_project + Gitlab.endpoint = gitlab_host + Gitlab.private_token = serpro_integration_plugin_settings.gitlab[:private_token] #Find user by email begin - user = Gitlab.users(:search => email) + gitlab_user = Gitlab.users(:search => gitlab[:email]) rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing - user = nil + gitlab_user = nil end #User not found, create user - if user == nil || user.count == 0 - user = self.admins.first + 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? self.gitlab[:errors] = _('Gitlab user could not be created') return nil - end - + end + #Create project for user begin #FIXME Why this? if gitlab_user.is_a?(Array) - gitlab_user = user[0] + gitlab_user = gitlab_user[0] end project_options = {} @@ -64,7 +65,7 @@ class Community project_options[:wall_enabled] = true project_options[:wiki_enabled] = true project_options[:public] = true - project = Gitlab.create_project(self.identifier, project_options) + project = Gitlab.create_project(gitlab_project_name, project_options) #Create Web Hook for Jenkins' integration # Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now") @@ -77,14 +78,18 @@ class Community self.gitlab[:errors] = nil end + def gitlab_project_name + gitlab[:project_name] || self.identifier + end + # set an API endpoint def gitlab_host - self.serpro_integration_plugin[:gitlab_host] + serpro_integration_plugin_settings.gitlab[:host] end # set a user private token def gitlab_private_token - self.serpro_integration_plugin[:gitlab_private_token] + serpro_integration_plugin_settings.gitlab[:private_token] end ########################################## diff --git a/plugins/serpro_integration/lib/serpro_integration_plugin.rb b/plugins/serpro_integration/lib/serpro_integration_plugin.rb index a49ba27..c24c05e 100644 --- a/plugins/serpro_integration/lib/serpro_integration_plugin.rb +++ b/plugins/serpro_integration/lib/serpro_integration_plugin.rb @@ -1,14 +1,5 @@ class SerproIntegrationPlugin < Noosfero::Plugin -#include ActionController::UrlWriter -# include ActionView::Helpers::TagHelper -# include ActionView::Helpers::FormTagHelper -# include FormsHelper - - -# include ActionView::Helpers -# include FormsHelper - def self.plugin_name "Serpro Integration Plugin" end @@ -17,10 +8,6 @@ class SerproIntegrationPlugin < Noosfero::Plugin _("Make integration with serpro servers.") end -# def filter_comment(c) -# c.reject! unless logged_in? || allowed_by_profile -# end - #FIXME make this test # User could not have this block def self.extra_blocks @@ -49,16 +36,4 @@ class SerproIntegrationPlugin < Noosfero::Plugin ['smile_face.js'] end -# def body_beginning -# "" if allowed_by_profile -# end -# -# protected -# -# delegate :logged_in?, :to => :context -# -# def allowed_by_profile -# context.profile && context.profile.allow_unauthenticated_comments -# end - end diff --git a/plugins/serpro_integration/views/profile-editor-extras.html.erb b/plugins/serpro_integration/views/profile-editor-extras.html.erb index 336ccfd..e628e9d 100644 --- a/plugins/serpro_integration/views/profile-editor-extras.html.erb +++ b/plugins/serpro_integration/views/profile-editor-extras.html.erb @@ -1,16 +1,32 @@ - + +
-
- <%= render :partial => 'gitlab' %> -
+

<%= _('Serpro Integration') %>

+ <%= render :partial => 'gitlab' %> -
- <%= labelled_check_box(_('Uses sonar integration'), 'profile_data[allow_sonar_integration]', true, profile.allow_sonar_integration) %> -
- +
+

+ <%= labelled_check_box('', 'profile_data[allow_sonar_integration]', true, profile.allow_sonar_integration, {:class => "toggle_checkbox", 'data-selector' => 'ul.sonar'}) %> + <%= _('Sonar Integration') %> +

    -

    <%= _('Sonar Integration') %>

  • <%= labelled_text_field(_('Server Host'), 'profile_data[sonar]host]', profile.sonar[:host]) %>
  • @@ -18,14 +34,14 @@ <%= labelled_text_field(_('Project: '), 'profile_data[sonar][project]', profile.sonar[:project]) %>
- - -
- <%= labelled_check_box(_('Uses Jenkins integration'), 'profile_data[allow_jenkins_integration]', true, profile.allow_jenkins_integration) %>
- + +
+

+ <%= labelled_check_box('', 'profile_data[allow_jenkins_integration]', true, profile.allow_jenkins_integration, {:class => "toggle_checkbox", 'data-selector' => 'ul.jenkins'}) %> + <%= _('Jenkins Integration') %> +

    -

    <%= _('Jenkins Integration') %>

  • <%= labelled_text_field(_('Server Host'), 'profile_data[jenkins][host]', profile.jenkins[:host]) %>
  • @@ -44,7 +60,6 @@
  • <%= labelled_text_field(_('Server Url'), 'profile_data[jenkins][url]', profile.jenkins[:url], {:disabled => true}) %>
  • -
- +
diff --git a/plugins/serpro_integration/views/profile_editor/_gitlab.html.erb b/plugins/serpro_integration/views/profile_editor/_gitlab.html.erb index 86799ba..444331d 100644 --- a/plugins/serpro_integration/views/profile_editor/_gitlab.html.erb +++ b/plugins/serpro_integration/views/profile_editor/_gitlab.html.erb @@ -1,22 +1,24 @@ -
- <%= labelled_check_box(_('Uses Gitlab integration'), 'profile_data[allow_gitlab_integration]', true, profile.allow_gitlab_integration) %> -
- -
diff --git a/plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb b/plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb new file mode 100644 index 0000000..8cfa641 --- /dev/null +++ b/plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb @@ -0,0 +1,18 @@ +

<%= _('Serpro Integration Settings')%>

+ +<%= form_for(:settings) do |f| %> + +
+

<%= _('Gitlab Settings')%>

+ <%= f.fields_for :gitlab, OpenStruct.new(@settings.gitlab) do |g| %> + <%= labelled_form_field _('Server Host'), g.text_field("host") %> + <%= labelled_form_field _('Private Token'), g.text_field(:private_token) %> + <% end %> +
+ + <% button_bar do %> + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> + <% end %> + +<% end %> + -- libgit2 0.21.2