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

Commit

Permalink
commit of patch created from jbbrwcky/jenkins-build-per-branch jabbrw…
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaleid committed Jun 18, 2012
1 parent 61a92c2 commit 48b099b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -2,7 +2,9 @@
*.iml
*.ipr
*.iws
*.diff
build
.gradle

out/
out
atlassian-ide-plugin.xml
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -33,7 +33,7 @@ task syncWithRepo(dependsOn: 'classes', type: JavaExec) {
main = 'com.entagen.jenkins.Main'
classpath = sourceSets.main.runtimeClasspath
// pass through specified system properties to the call to main
['help', 'jenkinsUrl', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'nestedView', 'printConfig', 'dryRun'].each {
['help', 'jenkinsUrl', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'nestedView', 'printConfig', 'dryRun', 'branchNameRegex'].each {
if (System.getProperty(it)) systemProperty it, System.getProperty(it)
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/groovy/com/entagen/jenkins/GitApi.groovy
@@ -1,7 +1,10 @@
package com.entagen.jenkins

import java.util.regex.Pattern

class GitApi {
String gitUrl
Pattern branchNameFilter = ~/.+/

public List<String> getBranchNames() {
String command = "git ls-remote --heads ${gitUrl}"
Expand All @@ -13,7 +16,7 @@ class GitApi {
// ex: b9c209a2bf1c159168bf6bc2dfa9540da7e8c4a26\trefs/heads/master
String branchNameRegex = "^.*\trefs/heads/(.*)\$"
String branchName = line.find(branchNameRegex) { full, branchName -> branchName }
if (branchName) branchNames << branchName
if (branchName && branchNameFilter.matcher(branchName).matches()) branchNames << branchName
}

return branchNames
Expand Down
5 changes: 5 additions & 0 deletions src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
@@ -1,11 +1,15 @@
package com.entagen.jenkins

import java.util.regex.Pattern

class JenkinsJobManager {
String templateJobPrefix
String templateBranchName
String gitUrl
String nestedView
String jenkinsUrl
String branchNameRegex

Boolean dryRun = false

JenkinsApi jenkinsApi
Expand Down Expand Up @@ -141,6 +145,7 @@ class JenkinsJobManager {
if (!gitApi) {
assert gitUrl != null
this.gitApi = new GitApi(gitUrl: gitUrl)
gitApi.branchNameFilter = Pattern.compile(this.branchNameRegex)
}

return this.gitApi
Expand Down
3 changes: 2 additions & 1 deletion src/main/groovy/com/entagen/jenkins/Main.groovy
Expand Up @@ -13,7 +13,8 @@ class Main {
t: [longOpt: 'template-branch', required: true, args: 1, argName: 'templateBranchName', description: "Template Branch Name - gradle flag -DtemplateBranchName=<branchName>"],
n: [longOpt: 'nested-view', required: false, args: 1, argName: 'nestedView', description: "Nested Parent View Name - gradle flag -DnestedView=<nestedView> - optional - must have Jenkins Nested View Plugin installed"],
c: [longOpt: 'print-config', required: false, args: 0, argName: 'printConfig', description: "Check configuration - print out settings then exit - gradle flag -DprintConfig=true"],
d: [longOpt: 'dry-run', required: false, args: 0, argName: 'dryRun', description: "Dry run, don't actually modify, create, or delete any jobs, just print out what would happen - gradle flag: -DdryRun=true"]
d: [longOpt: 'dry-run', required: false, args: 0, argName: 'dryRun', description: "Dry run, don't actually modify, create, or delete any jobs, just print out what would happen - gradle flag: -DdryRun=true"],
f: [longOpt: 'filter-branch-names', required: false, args: 1, argName: 'branchNameRegex', description: "Only branches matching the regex will be accepted - gradle flag: -DbranchNameRegex=<regex>"],
]

public static void main(String[] args) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/groovy/com/entagen/jenkins/GitApiTests.groovy
Expand Up @@ -37,6 +37,22 @@ garbage line that should be ignored
assert ["master", "release_1.0rc1", "ted/feature_branch"] == branchNames.sort()
}

@Test public void testGetFilteredBranchNames() {
String mockResult = """
10b42258f451ebf2640d3c18850e0c22eecdad4\trefs/heads/feature/myfeature
b9c209a2bf1c159168bf6bc2dfa9540da7e8c4a26\trefs/heads/master
abd856d2ae658ee5f14889b465f3adcaf65fb52b\trefs/heads/release/1.0rc1
abd856d2ae658ee5f14889b465f3adcaf65fb52b\trefs/heads/other_branch
""".trim()

GitApi gitApi = new GitApiMockedResult(mockResult: mockResult)

gitApi.branchNameFilter = ~/feature\/.+|release\/.+|master/
List<String> branchNames = gitApi.branchNames

assert ["feature/myfeature", "master", "release/1.0rc1"] == branchNames.sort()
}

}


Expand Down

0 comments on commit 48b099b

Please sign in to comment.