Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GitLab Branch Source plugin #1405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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')
}
}
}
Expand Up @@ -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
Expand Down
@@ -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
}
}
Expand Up @@ -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')
}
}