Commit db037ae6b4334e7c66ae23ffac68fb1a56b4ec12

Authored by Victor Costa
1 parent 2a60008b

Refactoring serpro_integration plugin

plugins/serpro_integration/Gemfile
1 1 gem 'gitlab', '~> 3.1.0'
  2 +gem 'jenkins_api_client', '~> 0.14.1'
... ...
plugins/serpro_integration/lib/ext/community.rb
1 1 require_dependency 'community'
2   -require 'gitlab'
3   -#require 'jenkins_api_client'
4 2  
5 3 class Community
6 4  
... ... @@ -13,11 +11,16 @@ class Community
13 11  
14 12 attr_accessible :allow_unauthenticated_comments, :allow_gitlab_integration, :gitlab, :allow_sonar_integration, :sonar, :allow_jenkins_integration, :jenkins
15 13  
16   - ##########################################
17   - # Gitlab stuff #
18   - ##########################################
  14 + after_update :create_integration_projects
19 15  
20   - #after_create :create_gitlab_project
  16 + 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)
  19 + end
  20 +
  21 + def serpro_integration_plugin_settings
  22 + @settings ||= Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin)
  23 + end
21 24  
22 25 def gitlab= params
23 26 self.serpro_integration_plugin[:gitlab] = params
... ... @@ -27,202 +30,36 @@ class Community
27 30 self.serpro_integration_plugin[:gitlab] ||= {}
28 31 end
29 32  
30   - def serpro_integration_plugin_settings
31   - @settings ||= Noosfero::Plugin::Settings.new(environment, SerproIntegrationPlugin)
32   - end
33   -
34   - def create_gitlab_project
35   - Gitlab.endpoint = gitlab_host
36   - Gitlab.private_token = serpro_integration_plugin_settings.gitlab[:private_token]
37   -
38   - #Find user by email
39   - begin
40   - gitlab_user = Gitlab.users(:search => gitlab[:email])
41   - rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing
42   - gitlab_user = nil
43   - end
44   -
45   - #User not found, create user
46   - if gitlab_user == nil || gitlab_user.count == 0
47   - gitlab_user = Gitlab.create_user(user.email, '123456', {:username => user.identifier, :name => user.name, :provider => 'ldap'})
48   - end
49   -
50   - if gitlab_user.nil?
51   - self.gitlab[:errors] = _('Gitlab user could not be created')
52   - return nil
53   - end
54   -
55   - #Create project for user
56   - begin
57   - #FIXME Why this?
58   - if gitlab_user.is_a?(Array)
59   - gitlab_user = gitlab_user[0]
60   - end
61   -
62   - project_options = {}
63   - project_options[:user_id] = gitlab_user.id
64   - project_options[:issues_enabled ] = true
65   - project_options[:wall_enabled] = true
66   - project_options[:wiki_enabled] = true
67   - project_options[:public] = true
68   - project = Gitlab.create_project(gitlab_project_name, project_options)
69   -
70   - #Create Web Hook for Jenkins' integration
71   -# Gitlab.add_project_hook(project.id, "#{self.jenkins[:url]}/gitlab/build_now")
72   -# createJenkinsJob(project.name, project.path_with_namespace, project.web_url, project.http_url_to_repo)
73   -
74   - rescue Gitlab::Error::NotFound, Gitlab::Error::Parsing
75   - #Project already exists
76   - end
77   -
78   - self.gitlab[:errors] = nil
79   - end
80   -
81 33 def gitlab_project_name
82 34 gitlab[:project_name] || self.identifier
83 35 end
84 36  
85   - # set an API endpoint
86 37 def gitlab_host
87 38 serpro_integration_plugin_settings.gitlab[:host]
88 39 end
89 40  
90   - # set a user private token
91 41 def gitlab_private_token
92 42 serpro_integration_plugin_settings.gitlab[:private_token]
93 43 end
94 44  
95   - ##########################################
96   - # Sonar stuff #
97   - ##########################################
98   -
99   -# after_create :create_sonar_project
100   -
101 45 def sonar= params
102 46 self.serpro_integration_plugin[:sonar] = params
103 47 end
104 48  
105 49 def sonar
106 50 self.serpro_integration_plugin[:sonar] ||= {}
107   - self.serpro_integration_plugin[:sonar]
108 51 end
109 52  
110   - ##########################################
111   - # Jenkins stuff #
112   - ##########################################
113   -
114   -# after_create :create_jenkis_project
115   -
116 53 def jenkins= params
117 54 self.serpro_integration_plugin[:jenkins] = params
118 55 end
119 56  
120 57 def jenkins
121 58 self.serpro_integration_plugin[:jenkins] ||= {}
122   - url = "#{self.serpro_integration_plugin[:jenkins][:host]}:"
123   - url += "#{self.serpro_integration_plugin[:jenkins][:port]}/"
124   - url += "#{self.serpro_integration_plugin[:jenkins][:context_name]}"
125   - self.serpro_integration_plugin[:jenkins][:url] = url
126   - self.serpro_integration_plugin[:jenkins]
127 59 end
128 60  
129   -
130   - #FIXME make jenkins integration works
131   - def create_jenkis_project
132   -#(projectName, repositoryPath, webUrl, gitUrl)
133   -
134   - @client = JenkinsApi::Client.new(:server_url => "#{$jenkins_url}/",
135   - :password => $jenkins_private_token,
136   - :username => $jenkins_user)
137   -
138   - xmlJenkins = ""
139   -
140   -# xmlJenkins = "
141   -# <maven2-moduleset plugin='maven-plugin@1.509'>
142   -# <actions/>
143   -# <description>Projeto criado para o repositório #{repositoryPath} do Gitlab - #{webUrl}</description>
144   -# <logRotator class='hudson.tasks.LogRotator'>
145   -# <daysToKeep>-1</daysToKeep>
146   -# <numToKeep>2</numToKeep>
147   -# <artifactDaysToKeep>-1</artifactDaysToKeep>
148   -# <artifactNumToKeep>-1</artifactNumToKeep>
149   -# </logRotator>
150   -# <keepDependencies>false</keepDependencies>
151   -# <properties/>
152   -# <scm class='hudson.plugins.git.GitSCM' plugin='git@2.2.1'>
153   -# <configVersion>2</configVersion>
154   -# <userRemoteConfigs>
155   -# <hudson.plugins.git.UserRemoteConfig>
156   -# <url>#{gitUrl}</url>
157   -# </hudson.plugins.git.UserRemoteConfig>
158   -# </userRemoteConfigs>
159   -# <branches>
160   -# <hudson.plugins.git.BranchSpec>
161   -# <name>*/master</name>
162   -# </hudson.plugins.git.BranchSpec>
163   -# </branches>
164   -# <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
165   -# <submoduleCfg class='list'/>
166   -# <extensions/>
167   -# </scm>
168   -# <canRoam>true</canRoam>
169   -# <disabled>false</disabled>
170   -# <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
171   -# <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
172   -# <jdk>(Inherit From Job)</jdk>
173   -# <triggers class='vector'/>
174   -# <concurrentBuild>false</concurrentBuild>
175   -# <goals>clean package install deploy</goals>
176   -# <aggregatorStyleBuild>true</aggregatorStyleBuild>
177   -# <incrementalBuild>false</incrementalBuild>
178   -# <perModuleEmail>true</perModuleEmail>
179   -# <ignoreUpstremChanges>false</ignoreUpstremChanges>
180   -# <archivingDisabled>false</archivingDisabled>
181   -# <resolveDependencies>false</resolveDependencies>
182   -# <processPlugins>false</processPlugins>
183   -# <mavenValidationLevel>-1</mavenValidationLevel>
184   -# <runHeadless>false</runHeadless>
185   -# <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>
186   -# <settings class='jenkins.mvn.DefaultSettingsProvider'/>
187   -# <globalSettings class='jenkins.mvn.DefaultGlobalSettingsProvider'/>
188   -# <reporters>
189   -# <hudson.maven.reporters.MavenMailer>
190   -# <recipients/>
191   -# <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
192   -# <sendToIndividuals>true</sendToIndividuals>
193   -# <perModuleEmail>true</perModuleEmail>
194   -# </hudson.maven.reporters.MavenMailer>
195   -# </reporters>
196   -# <publishers>
197   -# <hudson.plugins.sonar.SonarPublisher plugin='sonar@2.1'>
198   -# <jdk>(Inherit From Job)</jdk>
199   -# <branch/>
200   -# <language/>
201   -# <mavenOpts/>
202   -# <jobAdditionalProperties/>
203   -# <settings class='jenkins.mvn.DefaultSettingsProvider'/>
204   -# <globalSettings class='jenkins.mvn.DefaultGlobalSettingsProvider'/>
205   -# <usePrivateRepository>false</usePrivateRepository>
206   -# </hudson.plugins.sonar.SonarPublisher>
207   -# </publishers>
208   -# <buildWrappers/>
209   -# <prebuilders/>
210   -# <postbuilders/>
211   -# <runPostStepsIfResult>
212   -# <name>FAILURE</name>
213   -# <ordinal>2</ordinal>
214   -# <color>RED</color>
215   -# </runPostStepsIfResult>
216   -# </maven2-moduleset>
217   -# "
218   -
219   - begin
220   - @client.job.create(projectName, xmlJenkins)
221   - rescue JenkinsApi::Exceptions::ApiException
222   -
223   - end
224   -
  61 + def jenkins_project_name
  62 + jenkins[:project_name] || self.identifier
225 63 end
226 64  
227   -
228 65 end
... ...
plugins/serpro_integration/lib/serpro_integration_plugin/gitlab_integration.rb 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +require 'gitlab'
  2 +
  3 +class SerproIntegrationPlugin::GitlabIntegration
  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
  21 +
  22 + if gitlab_user.nil?
  23 + profile.gitlab[:errors] = _('Gitlab user could not be created')
  24 + return nil
  25 + end
  26 +
  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
  33 +
  34 + project_options = {}
  35 + project_options[:user_id] = gitlab_user.id
  36 + project_options[:issues_enabled ] = true
  37 + project_options[:wall_enabled] = true
  38 + project_options[:wiki_enabled] = true
  39 + project_options[:public] = true
  40 + project = Gitlab.create_project(profile.gitlab_project_name, project_options)
  41 +
  42 + #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
  50 + project
  51 + end
  52 +
  53 +
  54 +end
... ...
plugins/serpro_integration/lib/serpro_integration_plugin/jenkins_integration.rb 0 → 100644
... ... @@ -0,0 +1,101 @@
  1 +# encoding: UTF-8
  2 +require 'jenkins_api_client'
  3 +
  4 +class SerproIntegrationPlugin::JenkinsIntegration
  5 + #FIXME make jenkins integration works
  6 + def self.create_jenkis_project(profile, projectName, repositoryPath, webUrl, gitUrl)
  7 +
  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])
  11 +
  12 + #begin
  13 + @client.job.create(projectName, xml_jenkins(repositoryPath, webUrl, gitUrl))
  14 + #rescue JenkinsApi::Exceptions::ApiException
  15 +
  16 + #end
  17 +
  18 + end
  19 +
  20 + #FIXME
  21 + def self.xml_jenkins(repositoryPath, webUrl, gitUrl)
  22 + "
  23 + <maven2-moduleset plugin='maven-plugin@1.509'>
  24 + <actions/>
  25 + <description>Projeto criado para o repositório #{repositoryPath} do Gitlab - #{webUrl}</description>
  26 + <logRotator class='hudson.tasks.LogRotator'>
  27 + <daysToKeep>-1</daysToKeep>
  28 + <numToKeep>2</numToKeep>
  29 + <artifactDaysToKeep>-1</artifactDaysToKeep>
  30 + <artifactNumToKeep>-1</artifactNumToKeep>
  31 + </logRotator>
  32 + <keepDependencies>false</keepDependencies>
  33 + <properties/>
  34 + <scm class='hudson.plugins.git.GitSCM' plugin='git@2.2.1'>
  35 + <configVersion>2</configVersion>
  36 + <userRemoteConfigs>
  37 + <hudson.plugins.git.UserRemoteConfig>
  38 + <url>#{gitUrl}</url>
  39 + </hudson.plugins.git.UserRemoteConfig>
  40 + </userRemoteConfigs>
  41 + <branches>
  42 + <hudson.plugins.git.BranchSpec>
  43 + <name>*/master</name>
  44 + </hudson.plugins.git.BranchSpec>
  45 + </branches>
  46 + <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
  47 + <submoduleCfg class='list'/>
  48 + <extensions/>
  49 + </scm>
  50 + <canRoam>true</canRoam>
  51 + <disabled>false</disabled>
  52 + <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  53 + <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  54 + <jdk>(Inherit From Job)</jdk>
  55 + <triggers class='vector'/>
  56 + <concurrentBuild>false</concurrentBuild>
  57 + <goals>clean package install deploy</goals>
  58 + <aggregatorStyleBuild>true</aggregatorStyleBuild>
  59 + <incrementalBuild>false</incrementalBuild>
  60 + <perModuleEmail>true</perModuleEmail>
  61 + <ignoreUpstremChanges>false</ignoreUpstremChanges>
  62 + <archivingDisabled>false</archivingDisabled>
  63 + <resolveDependencies>false</resolveDependencies>
  64 + <processPlugins>false</processPlugins>
  65 + <mavenValidationLevel>-1</mavenValidationLevel>
  66 + <runHeadless>false</runHeadless>
  67 + <disableTriggerDownstreamProjects>false</disableTriggerDownstreamProjects>
  68 + <settings class='jenkins.mvn.DefaultSettingsProvider'/>
  69 + <globalSettings class='jenkins.mvn.DefaultGlobalSettingsProvider'/>
  70 + <reporters>
  71 + <hudson.maven.reporters.MavenMailer>
  72 + <recipients/>
  73 + <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
  74 + <sendToIndividuals>true</sendToIndividuals>
  75 + <perModuleEmail>true</perModuleEmail>
  76 + </hudson.maven.reporters.MavenMailer>
  77 + </reporters>
  78 + <publishers>
  79 + <hudson.plugins.sonar.SonarPublisher plugin='sonar@2.1'>
  80 + <jdk>(Inherit From Job)</jdk>
  81 + <branch/>
  82 + <language/>
  83 + <mavenOpts/>
  84 + <jobAdditionalProperties/>
  85 + <settings class='jenkins.mvn.DefaultSettingsProvider'/>
  86 + <globalSettings class='jenkins.mvn.DefaultGlobalSettingsProvider'/>
  87 + <usePrivateRepository>false</usePrivateRepository>
  88 + </hudson.plugins.sonar.SonarPublisher>
  89 + </publishers>
  90 + <buildWrappers/>
  91 + <prebuilders/>
  92 + <postbuilders/>
  93 + <runPostStepsIfResult>
  94 + <name>FAILURE</name>
  95 + <ordinal>2</ordinal>
  96 + <color>RED</color>
  97 + </runPostStepsIfResult>
  98 + </maven2-moduleset>
  99 + "
  100 + end
  101 +end
... ...
plugins/serpro_integration/views/profile-editor-extras.html.erb
... ... @@ -20,6 +20,7 @@ jQuery( document ).ready(function( $ ) {
20 20 <h2><%= _('Serpro Integration') %></h2>
21 21  
22 22 <%= render :partial => 'gitlab' %>
  23 + <%= render :partial => 'jenkins' %>
23 24  
24 25 <div id="sonar">
25 26 <h3>
... ... @@ -36,30 +37,4 @@ jQuery( document ).ready(function( $ ) {
36 37 </ul>
37 38 </div>
38 39  
39   - <div id="jenkins">
40   - <h3>
41   - <%= labelled_check_box('', 'profile_data[allow_jenkins_integration]', true, profile.allow_jenkins_integration, {:class => "toggle_checkbox", 'data-selector' => 'ul.jenkins'}) %>
42   - <%= _('Jenkins Integration') %>
43   - </h3>
44   - <ul class='jenkins'>
45   - <li>
46   - <%= labelled_text_field(_('Server Host'), 'profile_data[jenkins][host]', profile.jenkins[:host]) %>
47   - </li>
48   - <li>
49   - <%= labelled_text_field(_('Server Port'), 'profile_data[jenkins][port]', profile.jenkins[:port]) %>
50   - </li>
51   - <li>
52   - <%= labelled_text_field(_('Server Context Name'), 'profile_data[jenkins][context_name]', profile.jenkins[:context_name]) %>
53   - </li>
54   - <li>
55   - <%= labelled_text_field(_('Server User'), 'profile_data[jenkins][user]', profile.jenkins[:user]) %>
56   - </li>
57   - <li>
58   - <%= labelled_text_field(_('Server Private Token'), 'profile_data[jenkins][private_token]', profile.jenkins[:private_token]) %>
59   - </li>
60   - <li>
61   - <%= labelled_text_field(_('Server Url'), 'profile_data[jenkins][url]', profile.jenkins[:url], {:disabled => true}) %>
62   - </li>
63   - </ul>
64   - </div>
65 40 </div>
... ...
plugins/serpro_integration/views/profile_editor/_jenkins.html.erb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<div id="jenkins">
  2 +<h3>
  3 + <%= labelled_check_box('', 'profile_data[allow_jenkins_integration]', true, profile.allow_jenkins_integration, {:class => "toggle_checkbox", 'data-selector' => 'ul.jenkins'}) %>
  4 + <%= _('Jenkins Integration') %>
  5 +</h3>
  6 +<ul class='jenkins'>
  7 + <li>
  8 + <%= labelled_text_field(_('Project Name:'), 'profile_data[jenkins][project_name]', profile.jenkins_project_name ) %>
  9 + </li>
  10 +</ul>
  11 +</div>
... ...
plugins/serpro_integration/views/serpro_integration_plugin_admin/index.html.erb
... ... @@ -10,6 +10,15 @@
10 10 <% end %>
11 11 </div>
12 12  
  13 + <div class="jenkins_settings">
  14 + <h2><%= _('Jenkins Settings')%></h2>
  15 + <%= f.fields_for :jenkins, OpenStruct.new(@settings.jenkins) do |g| %>
  16 + <%= labelled_form_field _('Server Host'), g.text_field("host") %>
  17 + <%= labelled_form_field _('Server User'), g.text_field(:user) %>
  18 + <%= labelled_form_field _('Private Token'), g.text_field(:private_token) %>
  19 + <% end %>
  20 + </div>
  21 +
13 22 <% button_bar do %>
14 23 <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
15 24 <% end %>
... ...