diff --git a/job-dsl-core/src/main/docs/examples/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext/gitlab.groovy b/job-dsl-core/src/main/docs/examples/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext/gitlab.groovy new file mode 100644 index 000000000..83b427593 --- /dev/null +++ b/job-dsl-core/src/main/docs/examples/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext/gitlab.groovy @@ -0,0 +1,11 @@ +multibranchPipelineJob('example') { + branchSources { + gitlab { + id('23232323') // IMPORTANT: use a constant and unique identifier + serverName('GitLab') + credentialsId('gitlab-ci') + projectOwner('ownerName') + projectPath('ownerName/projectName') + } + } +} diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext.groovy index 29a3939f4..d7bf36a04 100644 --- a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext.groovy +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContext.groovy @@ -79,6 +79,30 @@ class BranchSourcesContext extends AbstractExtensibleContext { } } + /** + * Adds a GitLab branch source. Can be called multiple times to add more branch sources. + */ + @RequiresPlugin(id = 'gitlab-branch-source', minimumVersion = '670.vf7df45517001') + void gitlab(@DslContext(GitLabBranchSourceContext) Closure branchSourceClosure) { + GitLabBranchSourceContext context = new GitLabBranchSourceContext(jobManagement) + ContextHelper.executeInContext(branchSourceClosure, context) + + Preconditions.checkNotNullOrEmpty(context.id, 'id must be specified') + + branchSourceNodes << new NodeBuilder().'jenkins.branch.BranchSource' { + source(class: 'io.jenkins.plugins.gitlabbranchsource.GitLabSCMSource') { + id(context.id) + serverName(context.serverName ?: '') + credentialsId(context.credentialsId ?: '') + projectOwner(context.projectOwner ?: '') + projectPath(context.projectPath ?: '') + } + strategy(class: 'jenkins.branch.DefaultBranchPropertyStrategy') { + properties(class: 'empty-list') + } + } + } + @Override protected void addExtensionNode(Node node) { branchSourceNodes << node diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitLabBranchSourceContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitLabBranchSourceContext.groovy new file mode 100644 index 000000000..eacda818a --- /dev/null +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitLabBranchSourceContext.groovy @@ -0,0 +1,51 @@ +package javaposse.jobdsl.dsl.helpers.workflow + +import javaposse.jobdsl.dsl.AbstractContext +import javaposse.jobdsl.dsl.JobManagement + +class GitLabBranchSourceContext extends AbstractContext { + String id + String serverName + String credentialsId + String projectOwner + String projectPath + + GitLabBranchSourceContext(JobManagement jobManagement) { + super(jobManagement) + } + + /** + * Specifies a unique ID for this branch source. + */ + void id(String id) { + this.id = id + } + + /** + * Sets the defined connection to GitLab server. + */ + void serverName(String serverName) { + this.serverName = serverName + } + + /** + * Sets checkout credentials for authentication with GitLab. + */ + void credentialsId(String credentialsId) { + this.credentialsId = credentialsId + } + + /** + * Sets the name of the GitLab Group or GitLab User. + */ + void projectOwner(String projectOwner) { + this.projectOwner = projectOwner + } + + /** + * Sets the full path of the GitLab project. + */ + void projectPath(String projectPath) { + this.projectPath = projectPath + } +} diff --git a/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContextsSpec.groovy b/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContextsSpec.groovy index 3215cf135..425fb32af 100644 --- a/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContextsSpec.groovy +++ b/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/workflow/BranchSourcesContextsSpec.groovy @@ -190,4 +190,75 @@ class BranchSourcesContextsSpec extends Specification { } 1 * jobManagement.requireMinimumPluginVersion('github-branch-source', '1.8') } + + def 'gitlab with missing id'() { + when: + context.gitlab {} + + then: + Exception e = thrown DslException + e.message =~ 'id must be specified' + } + + def 'gitlab with minimal options'() { + when: + context.gitlab { + id('test') + } + + then: + context.branchSourceNodes.size() == 1 + with(context.branchSourceNodes[0]) { + name() == 'jenkins.branch.BranchSource' + children().size() == 2 + with(source[0]) { + children().size() == 5 + id[0].value() == 'test' + serverName[0].value().empty + credentialsId[0].value().empty + projectOwner[0].value().empty + projectPath[0].value().empty + } + with(strategy[0]) { + children().size() == 1 + attribute('class') == 'jenkins.branch.DefaultBranchPropertyStrategy' + properties[0].value() == [] + properties[0].attribute('class') == 'empty-list' + } + } + 1 * jobManagement.requireMinimumPluginVersion('gitlab-branch-source', '670.vf7df45517001') + } + + def 'gitlab with all options'() { + when: + context.gitlab { + id('test') + serverName('GitLab') + credentialsId('checkoutCreds') + projectOwner('ownerName') + projectPath('ownerName/projectName') + } + + then: + context.branchSourceNodes.size() == 1 + with(context.branchSourceNodes[0]) { + name() == 'jenkins.branch.BranchSource' + children().size() == 2 + with(source[0]) { + children().size() == 5 + id[0].value() == 'test' + serverName[0].value() == 'GitLab' + credentialsId[0].value() == 'checkoutCreds' + projectOwner[0].value() == 'ownerName' + projectPath[0].value() == 'ownerName/projectName' + } + with(strategy[0]) { + children().size() == 1 + attribute('class') == 'jenkins.branch.DefaultBranchPropertyStrategy' + properties[0].value() == [] + properties[0].attribute('class') == 'empty-list' + } + } + 1 * jobManagement.requireMinimumPluginVersion('gitlab-branch-source', '670.vf7df45517001') + } }