Skip to content

Commit

Permalink
Add support for GitLab Branch Source plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ljackiewicz committed Nov 23, 2023
1 parent fb7d49a commit 536cd1c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
@@ -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')
}
}

0 comments on commit 536cd1c

Please sign in to comment.