Permalink
Browse files

Added auth basic support to handle private jenkins projects

  • Loading branch information...
1 parent 61a92c2 commit fa976323e5b6f84d73b455ee26943e9b28b2c932 @willdurand willdurand committed Jun 18, 2012
View
2 build.gradle
@@ -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', 'jenkinsUser', 'jenkinsPassword', 'gitUrl', 'templateJobPrefix', 'templateBranchName', 'nestedView', 'printConfig', 'dryRun'].each {
if (System.getProperty(it)) systemProperty it, System.getProperty(it)
}
View
20 src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
@@ -7,15 +7,31 @@ import static groovyx.net.http.ContentType.*
import org.apache.http.conn.HttpHostConnectException
import org.apache.http.client.HttpResponseException
import org.apache.http.HttpStatus
+import org.apache.http.HttpRequestInterceptor
+import org.apache.http.protocol.HttpContext
+import org.apache.http.HttpRequest
class JenkinsApi {
String jenkinsServerUrl
RESTClient restClient
- public void setJenkinsServerUrl(String jenkinsServerUrl) {
- if (!jenkinsServerUrl.endsWith("/")) jenkinsServerUrl += "/"
+ public JenkinsApi(String jenkinsServerUrl, String jenkinsServerUser, String jenkinsServerPassword) {
+ if (!jenkinsServerUrl.endsWith("/")) {
+ jenkinsServerUrl += "/"
+ }
this.jenkinsServerUrl = jenkinsServerUrl
this.restClient = new RESTClient(jenkinsServerUrl)
+
+ if (null != jenkinsServerUser && null != jenkinsServerPassword) {
+ println "use basic authentication"
+
+ this.restClient.client.addRequestInterceptor(new HttpRequestInterceptor() {
+ void process(HttpRequest httpRequest, HttpContext httpContext) {
+ def auth = jenkinsServerUser + ':' + jenkinsServerPassword
+ httpRequest.addHeader('Authorization', 'Basic ' + auth.bytes.encodeBase64().toString())
+ }
+ })
+ }
}
List<String> getJobNames(String prefix = null) {
View
4 src/main/groovy/com/entagen/jenkins/JenkinsApiReadOnly.groovy
@@ -11,6 +11,10 @@ import org.apache.http.HttpStatus
class JenkinsApiReadOnly extends JenkinsApi {
+ public JenkinsApiReadOnly(String jenkinsServerUrl, String jenkinsServerUser, String jenkinsServerPassword) {
+ super(jenkinsServerUrl, jenkinsServerUser, jenkinsServerPassword)
+ }
+
@Override
protected Integer post(String path, postBody = [:], params = [:], ContentType contentType = ContentType.URLENC) {
println "READ ONLY! skipping POST to $path with params: ${params}, postBody:\n$postBody"
View
6 src/main/groovy/com/entagen/jenkins/JenkinsJobManager.groovy
@@ -6,6 +6,8 @@ class JenkinsJobManager {
String gitUrl
String nestedView
String jenkinsUrl
+ String jenkinsUser
+ String jenkinsPassword
Boolean dryRun = false
JenkinsApi jenkinsApi
@@ -128,9 +130,9 @@ class JenkinsJobManager {
assert jenkinsUrl != null
if (dryRun) {
println "DRY RUN! Not executing any POST commands to Jenkins, only GET commands"
- this.jenkinsApi = new JenkinsApiReadOnly(jenkinsServerUrl: jenkinsUrl)
+ this.jenkinsApi = new JenkinsApiReadOnly(jenkinsUrl, jenkinsUser, jenkinsPassword)
} else {
- this.jenkinsApi = new JenkinsApi(jenkinsServerUrl: jenkinsUrl)
+ this.jenkinsApi = new JenkinsApi(jenkinsUrl, jenkinsUser, jenkinsPassword)
}
}
View
4 src/main/groovy/com/entagen/jenkins/Main.groovy
@@ -13,7 +13,9 @@ 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"],
+ 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>"]
]
public static void main(String[] args) {

1 comment on commit fa97632

@beckje01

Great add on.

Please sign in to comment.