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

Fix issue where jobs not matching branch regex are being deleted #78

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1ac0bc6
add eclispe ide items to .gitignore
jpaolini Dec 19, 2014
5349f17
Fixed issue where jobs that match the templateJobPrefix but do not ma…
jpaolini Dec 19, 2014
8a0b81a
added enable job switch. it allows new jobs to be enabled regardless of
jpaolini Jan 30, 2015
8c8c273
Merge pull request #1 from jpaolini/feature/jp-addOptionToEnableSCMPo…
jpaolini Jan 30, 2015
c4a04cf
added the enableJob to the build.gradle file. modified gitignore to …
jpaolini Jan 30, 2015
665dd5c
in progress. adding ability to create builds based on last commit da…
Sep 8, 2016
7b061ac
modified so the number of days since the last commit could be is an a…
Sep 9, 2016
f5f3b4d
uncommented line that shows the configuration
Sep 9, 2016
1587ead
set the days since commit to not be required, it defaults to 5
Sep 9, 2016
4479408
fixed spacing
Sep 9, 2016
71c1013
Merge pull request #2 from jpaolini/feature/jp_JobsByCommitDate
jpaolini Sep 9, 2016
afbda16
modified so it only clones the repo if it hasn't been cloned before
Sep 9, 2016
d12531b
reset the gradle build back to the original
Sep 9, 2016
b80e702
modified name of days since last commit parameter
Sep 9, 2016
742c5b3
fixed incorrect parameter
Sep 9, 2016
2c262e1
added days to the gradle file
Sep 9, 2016
c6656a1
added more logging
Sep 9, 2016
4dcca5c
disabled the check for date
Sep 9, 2016
255d7df
added more logging
Sep 9, 2016
f4376e0
added more logging
Sep 9, 2016
92378ca
updated gradle wrapper to v2.13. updated gitignore to ignore the .gr…
joepaolini Sep 10, 2016
298ca60
cleaned up code. updated gradle wrapper
joepaolini Sep 10, 2016
a719737
added ability to disable job creation using the last commit date for …
joepaolini Sep 10, 2016
5769005
Merge pull request #3 from jpaolini/feature/jp_LastCommitDate
jpaolini Sep 10, 2016
79f0bd1
Revert "Feature/jp last commit date"
jpaolini Sep 10, 2016
0224ded
Merge pull request #4 from jpaolini/revert-3-feature/jp_LastCommitDate
jpaolini Sep 10, 2016
c9ff1e1
testing disableLastCommit
joepaolini Sep 10, 2016
070ebb7
testing pring statements
joepaolini Sep 10, 2016
ea001cd
fixed logging
joepaolini Sep 10, 2016
981b95d
reverted compile to groovy
joepaolini Sep 10, 2016
0c2bfb2
updated gradle/groovy versions. added check for the git clone so it …
joepaolini Sep 10, 2016
5ca36c9
added debug logging
joepaolini Sep 10, 2016
3b620c0
more debug logging
joepaolini Sep 10, 2016
851f300
testing
joepaolini Sep 10, 2016
b8f7faa
more testing
joepaolini Sep 10, 2016
eb49488
more testing
joepaolini Sep 10, 2016
9bad4c7
testing again
joepaolini Sep 10, 2016
3bac818
debugging regex issue
joepaolini Sep 11, 2016
d9244b8
more testing
joepaolini Sep 11, 2016
87a3584
more testing
joepaolini Sep 11, 2016
c6aefc7
testing replacing literal with \t
joepaolini Sep 11, 2016
04dfdde
code clean up
joepaolini Sep 11, 2016
42a9d9a
merged in master
joepaolini Sep 11, 2016
f39bbc8
Merge pull request #5 from jpaolini/feature/jp_LastCommitDate
jpaolini Sep 11, 2016
5b4a6e9
fixed issues missed during merge with master
joepaolini Sep 11, 2016
f78209c
fixed spacing
joepaolini Sep 11, 2016
d5ebddd
Merge pull request #6 from jpaolini/feature/jp_LastCommitDate
jpaolini Sep 11, 2016
3ebd355
removed print statement that wasn't needed
joepaolini Sep 11, 2016
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
*.iws
*.diff
build
.gradle
#.gradle
.settings
.classpath
.project

out
atlassian-ide-plugin.xml
/bin
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repositories {
}

dependencies {
groovy 'org.codehaus.groovy:groovy-all:1.8.8'
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile 'org.apache.ivy:ivy:2.2.0'
compile 'commons-cli:commons-cli:1.2' // should be part of groovy, but not available when running for some reason
testCompile 'junit:junit:4.10'
Expand All @@ -24,7 +24,7 @@ task createSourceDirs(description : 'Create empty source directories for all def

idea {
project {
jdkName = '1.6'
jdkName = '1.8'
}
}

Expand All @@ -33,14 +33,14 @@ 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', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'branchNameRegex', 'nestedView', 'viewRegex', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete'].each {
['help', 'jenkinsUrl', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'branchNameRegex', 'nestedView', 'viewRegex', 'printConfig', 'dryRun', 'startOnCreate', 'noViews', 'noDelete', 'enableJob', 'days', 'disableLastCommit'].each {
if (System.getProperty(it)) systemProperty it, System.getProperty(it)
}

}

task wrapper(type: Wrapper) {
gradleVersion = '1.0'
gradleVersion = '3.0'
}


Expand Down
8 changes: 8 additions & 0 deletions get-branches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

if [ ! -d $2 ]; then
git clone $1 || { echo >&2 "failed with $?"; exit 1; }
fi
cd $2
git fetch -p
for branch in `git branch -r | grep -v HEAD`;do echo `git show --format="%ct" $branch | head -n 1` \\t$branch; done | sort -r
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jun 20 11:41:01 CDT 2012
#Sat Sep 10 12:51:28 EDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-all.zip
42 changes: 27 additions & 15 deletions src/main/groovy/com/entagen/jenkins/GitApi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,40 @@ import java.util.regex.Pattern
class GitApi {
String gitUrl
Pattern branchNameFilter = null
Integer daysSinceLastCommit = 5;
Boolean disableLastCommit = false;

public List<String> getBranchNames() {
String command = "git ls-remote --heads ${gitUrl}"
String repo = gitUrl.substring(gitUrl.lastIndexOf('/') + 1, gitUrl.lastIndexOf('.git'))
String command = "sh get-branches.sh ${gitUrl} ${repo}"
List<String> branchNames = []

eachResultLine(command) { String line ->
String branchNameRegex = "^.*\trefs/heads/(.*)\$"
line = line.replace("\\t", "\t")
String branchNameRegex = "^.*\torigin/(.*)\$"
String branchName = line.find(branchNameRegex) { full, branchName -> branchName }
Boolean selected = passesFilter(branchName)
println "\t" + (selected ? "* " : " ") + "$line"
// lines are in the format of: <SHA>\trefs/heads/BRANCH_NAME
// ex: b9c209a2bf1c159168bf6bc2dfa9540da7e8c4a26\trefs/heads/master
Boolean selected = passesFilter(branchName) && (disableLastCommit || passesLastCommitDateFilter(line))

println "${(selected ? '* ' : '')} ${line}"
// lines are in the format of: lastCommitDate\torigin/BRANCH_NAME
// ex: 1471048873\torigin/master
if (selected) branchNames << branchName
}

return branchNames
}

public Boolean passesLastCommitDateFilter(String branch) {
Date lastCommitForBranch = new Date(branch.tokenize()[0].toLong() * 1000)
Date commitCutoff = new Date() - daysSinceLastCommit
return lastCommitForBranch.after(commitCutoff)
}

public Boolean passesFilter(String branchName) {
if (!branchName) return false
if (!branchNameFilter) return true
return branchName ==~ branchNameFilter
Boolean passed = branchName ==~ branchNameFilter
return passed
}

// assumes all commands are "safe", if we implement any destructive git commands, we'd want to separate those out for a dry-run
Expand All @@ -37,18 +49,18 @@ class GitApi {
def gitOutput = ""

while(true) {
int readByte = inputStream.read()
if (readByte == -1) break // EOF
byte[] bytes = new byte[1]
bytes[0] = readByte
gitOutput = gitOutput.concat(new String(bytes))
int readByte = inputStream.read()
if (readByte == -1) break // EOF
byte[] bytes = new byte[1]
bytes[0] = readByte
gitOutput = gitOutput.concat(new String(bytes))
}
process.waitFor()

if (process.exitValue() == 0) {
gitOutput.eachLine { String line ->
closure(line)
}
closure(line)
}
} else {
String errorText = process.errorStream.text?.trim()
println "error executing command: $command"
Expand All @@ -57,4 +69,4 @@ class GitApi {
}
}

}
}
4 changes: 4 additions & 0 deletions src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class JenkinsApi {
post('job/' + missingJob.jobName + '/enable')
}
}

void enableJob(ConcreteJob job) {
post('job/' + job.jobName + '/enable')
}

void startJob(ConcreteJob job) {
println "Starting job ${job.jobName}."
Expand Down
12 changes: 9 additions & 3 deletions src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class JenkinsJobManager {
Boolean noViews = false
Boolean noDelete = false
Boolean startOnCreate = false
Boolean enableJob = false
String days
Boolean disableLastCommit

JenkinsApi jenkinsApi
GitApi gitApi
Expand Down Expand Up @@ -63,7 +66,10 @@ class JenkinsJobManager {
for(ConcreteJob missingJob in missingJobs) {
println "Creating missing job: ${missingJob.jobName} from ${missingJob.templateJob.jobName}"
jenkinsApi.cloneJobForBranch(missingJob, templateJobs)
if (startOnCreate) {
if(enableJob) {
jenkinsApi.enableJob(missingJob)
}
if (startOnCreate) {
jenkinsApi.startJob(missingJob)
}
}
Expand All @@ -90,7 +96,7 @@ class JenkinsJobManager {

// don't want actual template jobs, just the jobs that were created from the templates
return (allJobNames - templateJobNames).findAll { String jobName ->
templateBaseJobNames.find { String baseJobName -> jobName.startsWith(baseJobName)}
templateBaseJobNames.find { String baseJobName -> jobName.startsWith(baseJobName) && jobName.tokenize("-")[2] ==~ branchNameRegex.replace("/", "_")}
}
}

Expand Down Expand Up @@ -161,7 +167,7 @@ class JenkinsJobManager {
GitApi initGitApi() {
if (!gitApi) {
assert gitUrl != null
this.gitApi = new GitApi(gitUrl: gitUrl)
this.gitApi = new GitApi(gitUrl: gitUrl, daysSinceLastCommit: days.toInteger(), disableLastCommit: disableLastCommit)
if (this.branchNameRegex){
this.gitApi.branchNameFilter = ~this.branchNameRegex
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/groovy/com/entagen/jenkins/Main.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class Main {
r: [longOpt: 'view-regex', required: false, args: 1, argName: 'viewRegex', description: "Supply a custom regex to be applied to any generated views, overriding the default template regex - gradle flag: -DviewRegex=<regex>"],
k: [longOpt: 'no-delete', required: false, args: 0, argName: 'noDelete', description: "Do not delete (keep) branches and views - gradle flag -DnoDelete=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>"],
usr: [longOpt: 'jenkins-user', required: false, args: 1, argName: 'jenkinsUser', description: "Jenkins username - gradle flag -DjenkinsUser=<jenkinsUser>"],
pwd: [longOpt: 'jenkins-password', required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword=<jenkinsPassword>"]
e: [longOpt: 'enable-job', required: false, args: 1, argName: 'enableJob', description: "When creating a new job, enable it even if template job was disabled - gradle flag: -DenableJob=true"],
usr: [longOpt: 'jenkins-user', required: false, args: 1, argName: 'jenkinsUser', description: "Jenkins username - gradle flag -DjenkinsUser=<jenkinsUser>"],
pwd: [longOpt: 'jenkins-password', required: false, args: 1, argName: 'jenkinsPassword', description: "Jenkins password - gradle flag -DjenkinsPassword=<jenkinsPassword>"],
days: [longOpt: 'days', required: false, args: 1, argName: 'days', description: "Creates jobs with commits within the specified number of days - gradle flag -Ddays=5"],
dlc: [longOpt: 'disable-last-commit', required: false, args: 1, argName: 'disableLastCommit', description: "Disables the creation of jobs based on last commit date - gradle flag = -DdisableLastCommit=true"]
]

public static void main(String[] args) {
Expand Down