Skip to content

Commit

Permalink
Added support for pipeline projects jenkinsci#31
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Mähr committed Aug 16, 2017
1 parent cd8c1b2 commit b69f15f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
4 changes: 2 additions & 2 deletions models/services/get_build_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def with(project, details)
return [] unless project.parametrized? # no need to process if not parameterized

if details.kind == 'merge_request'
ParametersAction.new( java.util.ArrayList.new(GetParametersValues.new.with_mr(project, details)) )
[ ParametersAction.new( java.util.ArrayList.new(GetParametersValues.new.with_mr(project, details)) ) ]
else
ParametersAction.new( java.util.ArrayList.new(GetParametersValues.new.with(project, details)) )
[ ParametersAction.new( java.util.ArrayList.new(GetParametersValues.new.with(project, details)) ) ]
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion models/services/get_build_cause.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
include Java

java_import Java.hudson.model.Cause
java_import Java.hudson.model.CauseAction

module GitlabWebHook
class GetBuildCause
def with(details)
raise ArgumentError.new('details are required') unless details

notes = details.payload ? from_payload(details) : 'no payload available'
Cause::RemoteCause.new(details.repository_uri.host, notes)
CauseAction.new(Cause::RemoteCause.new(details.repository_uri.host, notes))
end

def from_payload(details)
Expand Down
4 changes: 2 additions & 2 deletions models/services/get_jenkins_projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

include Java

java_import Java.hudson.model.AbstractProject
java_import Java.hudson.model.Job
java_import Java.hudson.matrix.MatrixConfiguration

java_import Java.hudson.plugins.git.GitSCM
Expand Down Expand Up @@ -51,7 +51,7 @@ def master(details)
def all
projects = nil
Security.impersonate(ACL::SYSTEM) do
projects = Java.jenkins.model.Jenkins.instance.getAllItems(AbstractProject.java_class).map do |jenkins_project|
projects = Java.jenkins.model.Jenkins.instance.getAllItems(Job.java_class).map do |jenkins_project|
Project.new(jenkins_project) unless jenkins_project.java_kind_of?(MatrixConfiguration)
end - [nil]
end
Expand Down
5 changes: 4 additions & 1 deletion models/use_cases/build_now.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BuildNow

java_import Java.java.util.logging.Logger
java_import Java.java.util.logging.Level
java_import Java.hudson.model.Action

def initialize(project, logger = Logger.getLogger(self.class.name))
raise ArgumentError.new('project is required') unless project
Expand All @@ -21,7 +22,9 @@ def with(details, cause_builder = GetBuildCause.new, actions_builder = GetBuildA
raise ArgumentError.new('details are required') unless details

begin
return "#{project} scheduled for build" if project.scheduleBuild2(project.getQuietPeriod(), cause_builder.with(details), actions_builder.with(project, details))
causeAction = cause_builder.with(details)
actions = [ causeAction ] + actions_builder.with(project, details)
return "#{project} scheduled for build" if project.scheduleBuild2(project.getQuietPeriod(), actions.to_java(Action) )
rescue java.lang.Exception => e
# avoid method signature warnings
severe = logger.java_method(:log, [Level, java.lang.String, java.lang.Throwable])
Expand Down
22 changes: 16 additions & 6 deletions models/use_cases/create_project_for_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative '../util/settings'
require_relative '../services/security'

java_import Java.hudson.model.AbstractProject
java_import Java.hudson.plugins.git.GitSCM
java_import Java.hudson.plugins.git.BranchSpec
java_import Java.hudson.plugins.git.UserRemoteConfig
Expand All @@ -32,7 +33,7 @@ def with(details)

Security.impersonate(ACL::SYSTEM) do
branch_project = Java.jenkins.model.Jenkins.instance.copy(copy_from.jenkins_project, new_project_name)
branch_project.scm = new_project_scm
set_scm(branch_project, new_project_scm)
branch_project.makeDisabled(false)
branch_project.description = settings.description
branch_project.save
Expand All @@ -44,15 +45,15 @@ def with(details)
def from_template(template, details)
return if details.branch.empty?
copy_from = get_template_project(template)
raise ConfigurationException.new("Templates with multiples-scms plugin not supported") if copy_from.multiscm?
new_project_name = details.repository_name
raise ConfigurationException.new("project #{new_project_name} already created from #{template}") unless @get_jenkins_projects.named(new_project_name).empty?
modified_scm = @build_scm.with(copy_from.jenkins_project.scm, details, true)
@logger.warning( "Multiple-SCMs project #{copy_from} incompletelly copied onto #{new_project_name}" ) if copy_from.multiscm?
modified_scm = @build_scm.with(copy_from.jenkins_project.getSCMs().find.first, details, true)
branch_project = nil

Security.impersonate(ACL::SYSTEM) do
branch_project = Java.jenkins.model.Jenkins.instance.copy(copy_from.jenkins_project, new_project_name)
branch_project.scm = modified_scm
set_scm(branch_project, modified_scm)
branch_project.makeDisabled(false)
branch_project.save
end
Expand All @@ -72,7 +73,7 @@ def for_merge(details)

Security.impersonate(ACL::SYSTEM) do
new_project = Java.jenkins.model.Jenkins.instance.copy(copy_from.jenkins_project, new_project_name)
new_project.scm = cloned_scm
set_scm(new_project, cloned_scm)
new_project.makeDisabled(false)
new_project.description = settings.description
new_project.save
Expand Down Expand Up @@ -107,6 +108,15 @@ def get_new_project_name(copy_from, details)
raise ConfigurationException.new("project #{new_project_name} already exists") unless @get_jenkins_projects.named(new_project_name).empty?
new_project_name
end


def set_scm(project, scm)
if project.java_kind_of?(AbstractProject)
project.scm = scm
else
definition = Java.org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.new(scm, project.definition.getScriptPath())
project.definition = definition
end
end

end
end
19 changes: 13 additions & 6 deletions models/values/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

include Java

java_import Java.hudson.model.AbstractProject
java_import Java.hudson.model.ParametersDefinitionProperty
java_import Java.hudson.model.StringParameterDefinition
java_import Java.hudson.model.ChoiceParameterDefinition
Expand Down Expand Up @@ -214,12 +215,18 @@ def matches_branch?(details, branch = false, exactly = false)

def setup_scms
@scms = []
if jenkins_project.scm
if jenkins_project.scm.java_kind_of?(GitSCM)
@scms << jenkins_project.scm
elsif MultipleScmsPluginAvailable && jenkins_project.scm.java_kind_of?(MultiSCM)
@multiscm = true
@scms.concat(jenkins_project.scm.getConfiguredSCMs().select { |scm| scm.java_kind_of?(GitSCM) })
if jenkins_project.java_kind_of?(AbstractProject)
if jenkins_project.scm
if jenkins_project.scm.java_kind_of?(GitSCM)
@scms << jenkins_project.scm
elsif MultipleScmsPluginAvailable && jenkins_project.scm.java_kind_of?(MultiSCM)
@multiscm = true
@scms.concat(jenkins_project.scm.getConfiguredSCMs().select { |scm| scm.java_kind_of?(GitSCM) })
end
end
else
if jenkins_project.getSCMs()
@scms.concat(jenkins_project.getSCMs().select { |scm| scm.java_kind_of?(GitSCM) })
end
end
end
Expand Down

0 comments on commit b69f15f

Please sign in to comment.