Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Properly handle template branch names that contain a forward slash #45

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.entagen.jenkins

import java.util.regex.Pattern

class JenkinsJobManager {
String templateJobPrefix
String templateBranchName
Expand Down Expand Up @@ -94,12 +92,13 @@ class JenkinsJobManager {
}

List<TemplateJob> findRequiredTemplateJobs(List<String> allJobNames) {
String regex = /^($templateJobPrefix-[^-]*)-($templateBranchName)$/
def templateBranchJobName = templateBranchName.replaceAll('/', '_')
String regex = /^($templateJobPrefix-[^-]*)-($templateBranchJobName)$/

List<TemplateJob> templateJobs = allJobNames.findResults { String jobName ->
TemplateJob templateJob = null
jobName.find(regex) { full, baseJobName, branchName ->
templateJob = new TemplateJob(jobName: full, baseJobName: baseJobName, templateBranchName: branchName)
templateJob = new TemplateJob(jobName: full, baseJobName: baseJobName, templateBranchName: templateBranchName)
}
return templateJob
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/groovy/com/entagen/jenkins/JenkinsJobManagerTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ class JenkinsJobManagerTests extends GroovyTestCase {
}


@Test public void testFindTemplateJobs_withSlash() {
JenkinsJobManager jenkinsJobManager = new JenkinsJobManager(templateJobPrefix: "myproj", templateBranchName: "my/slashed/branch", jenkinsUrl: "http://dummy.com", gitUrl: "[email protected]:company/myproj.git")
List<String> allJobNames = [
"myproj-foo-my_slashed_branch",
"otherproj-foo-master",
"myproj-foo-featurebranch"
]
List<TemplateJob> templateJobs = jenkinsJobManager.findRequiredTemplateJobs(allJobNames)
assert templateJobs.size() == 1
TemplateJob templateJob = templateJobs.first()
assert templateJob.jobName == "myproj-foo-my_slashed_branch"
assert templateJob.baseJobName == "myproj-foo"
assert templateJob.templateBranchName == "my/slashed/branch"
}


@Test public void testFindTemplateJobs_noMatchingJobsThrowsException() {
JenkinsJobManager jenkinsJobManager = new JenkinsJobManager(templateJobPrefix: "myproj", templateBranchName: "master", jenkinsUrl: "http://dummy.com", gitUrl: "[email protected]:company/myproj.git")
List<String> allJobNames = [
Expand Down