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 2bc395351..d7ea76fbb 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 @@ -44,7 +44,7 @@ class BranchSourcesContext extends AbstractExtensibleContext { * * @since 1.46 */ - @RequiresPlugin(id = 'github-branch-source', minimumVersion = '1.8') + @RequiresPlugin(id = 'github-branch-source', minimumVersion = '2.2.0') void github(@DslContext(GitHubBranchSourceContext) Closure branchSourceClosure) { GitHubBranchSourceContext context = new GitHubBranchSourceContext(jobManagement) ContextHelper.executeInContext(branchSourceClosure, context) @@ -55,18 +55,54 @@ class BranchSourcesContext extends AbstractExtensibleContext { if (context.apiUri) { apiUri(context.apiUri) } - scanCredentialsId(context.scanCredentialsId ?: '') - checkoutCredentialsId(context.checkoutCredentialsId ?: '') + credentialsId(context.scanCredentialsId ?: '') repoOwner(context.repoOwner ?: '') repository(context.repository ?: '') - includes(context.includes ?: '') - excludes(context.excludes ?: '') - buildOriginBranch(context.buildOriginBranch) - buildOriginBranchWithPR(context.buildOriginBranchWithPR) - buildOriginPRMerge(context.buildOriginPRMerge) - buildOriginPRHead(context.buildOriginPRHead) - buildForkPRMerge(context.buildForkPRMerge) - buildForkPRHead(context.buildForkPRHead) + traits { + if (context.buildOriginBranch || context.buildOriginBranchWithPR) { + 'org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait' { + strategyId((context.buildOriginBranch ? 1 : 0) + (context.buildOriginBranchWithPR ? 2 : 0)) + } + } + if (context.buildOriginPRMerge || context.buildOriginPRHead) { + 'org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait' { + strategyId((context.buildOriginPRMerge ? 1 : 0) + (context.buildOriginPRHead ? 2 : 0)) + } + } + if (context.buildForkPRMerge || context.buildForkPRHead) { + 'org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait' { + strategyId((context.buildForkPRMerge ? 1 : 0) + (context.buildForkPRHead ? 2 : 0)) + trust(class: + 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustPermission') + } + } + if (context.checkoutCredentialsId != null && + 'SAME' != context.scanCredentialsId && + context.checkoutCredentialsId != context.scanCredentialsId) { + 'org.jenkinsci.plugins.github__branch__source.SSHCheckoutTrait' { + credentialsId(context.checkoutCredentialsId) + } + } + if ((context.includes != null && '*' != context.includes) || + (context.excludes != null && '' != context.excludes)) { + 'jenkins.scm.impl.trait.WildcardSCMHeadFilterTrait' { + includes(context.includes ?: '*') + excludes(context.excludes ?: '') + } + } + if (context.pattern != null && '.*' != context.pattern) { + 'jenkins.scm.impl.trait.RegexSCMSourceFilterTrait' { + regex(context.pattern) + } + } + if (!context.noTags) { + 'jenkins.plugins.git.traits.CloneOptionTrait' { + extension(class: 'hudson.plugins.git.extensions.impl.CloneOption') { + noTags(context.noTags) + } + } + } + } } strategy(class: 'jenkins.branch.DefaultBranchPropertyStrategy') { properties(class: 'empty-list') diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitHubBranchSourceContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitHubBranchSourceContext.groovy index 2704b3f85..d8feb3eef 100644 --- a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitHubBranchSourceContext.groovy +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/workflow/GitHubBranchSourceContext.groovy @@ -12,12 +12,14 @@ class GitHubBranchSourceContext extends AbstractContext { String repository String includes = '*' String excludes + String pattern = '.*' boolean buildOriginBranch = true boolean buildOriginBranchWithPR = true boolean buildOriginPRMerge = false boolean buildOriginPRHead = false boolean buildForkPRMerge = true boolean buildForkPRHead = false + boolean noTags = true GitHubBranchSourceContext(JobManagement jobManagement) { super(jobManagement) @@ -81,6 +83,15 @@ class GitHubBranchSourceContext extends AbstractContext { this.excludes = excludes } + /** + * Regex for project names to include. Defaults to {@code .*}. + * + * @since 1.71 + */ + void pattern(String pattern) { + this.pattern = pattern + } + /** * Build origin branches. Defaults to {@code true}. * @@ -134,4 +145,13 @@ class GitHubBranchSourceContext extends AbstractContext { void buildForkPRHead(boolean buildForkPRHead = true) { this.buildForkPRHead = buildForkPRHead } + + /** + * Do not clone tags. Defaults to {@code true}. + * + * @since 1.71 + */ + void noTags(boolean noTags = true) { + this.noTags = noTags + } } 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 9fcd33c6e..a2cfc8b4f 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 @@ -92,20 +92,14 @@ class BranchSourcesContextsSpec extends Specification { name() == 'jenkins.branch.BranchSource' children().size() == 2 with(source[0]) { - children().size() == 13 + children().size() == 5 id[0].value() instanceof String - scanCredentialsId[0].value().empty - checkoutCredentialsId[0].value() == 'SAME' + credentialsId[0].value().empty repoOwner[0].value().empty repository[0].value().empty - includes[0].value() == '*' - excludes[0].value().empty - buildOriginBranch[0].value() == true - buildOriginBranchWithPR[0].value() == true - buildOriginPRMerge[0].value() == false - buildOriginPRHead[0].value() == false - buildForkPRMerge[0].value() == true - buildForkPRHead[0].value() == false + with(traits[0]) { + children().size() == 3 + } } with(strategy[0]) { children().size() == 1 @@ -114,7 +108,7 @@ class BranchSourcesContextsSpec extends Specification { properties[0].attribute('class') == 'empty-list' } } - 1 * jobManagement.requireMinimumPluginVersion('github-branch-source', '1.8') + 1 * jobManagement.requireMinimumPluginVersion('github-branch-source', '2.2.0') } def 'github with all options'() { @@ -142,21 +136,15 @@ class BranchSourcesContextsSpec extends Specification { name() == 'jenkins.branch.BranchSource' children().size() == 2 with(source[0]) { - children().size() == 14 + children().size() == 6 id[0].value() == 'test' apiUri[0].value() == 'https://custom.url' - scanCredentialsId[0].value() == 'scanCreds' - checkoutCredentialsId[0].value() == 'checkoutCreds' + credentialsId[0].value() == 'scanCreds' repoOwner[0].value() == 'ownerName' repository[0].value() == 'repoName' - includes[0].value() == 'lorem' - excludes[0].value() == 'ipsum' - buildOriginBranch[0].value() == false - buildOriginBranchWithPR[0].value() == false - buildOriginPRMerge[0].value() == true - buildOriginPRHead[0].value() == true - buildForkPRMerge[0].value() == false - buildForkPRHead[0].value() == true + with(traits[0]) { + children().size() == 4 + } } with(strategy[0]) { children().size() == 1 @@ -165,6 +153,6 @@ class BranchSourcesContextsSpec extends Specification { properties[0].attribute('class') == 'empty-list' } } - 1 * jobManagement.requireMinimumPluginVersion('github-branch-source', '1.8') + 1 * jobManagement.requireMinimumPluginVersion('github-branch-source', '2.2.0') } }