node-jenkins-api icon indicating copy to clipboard operation
node-jenkins-api copied to clipboard

Crumb support

Open RameshThangamuthu opened this issue 8 years ago • 3 comments

I don't see any documentation around support for CRUMB. How to make it work for Jenkins that has CSRF protection configured

RameshThangamuthu avatar Jan 10 '17 11:01 RameshThangamuthu

@RameshThangamuthu you can using the following code

quick & dirty

`

    build: function(jobname, params, callback) {
        var buildurl;
        /*
        Trigger Jenkins to build.
        */
        if (typeof params === 'function') { 
            buildurl = build_url(BUILD, jobname);
            callback = params;
        } else {
            buildurl = build_url(BUILDWITHPARAMS+"?"+qs.stringify(params), jobname)
        }
        /*
         http://******/jenkins//job/springbootdemo/build/api/json
         */
        var url = buildurl.replace('/job/'+jobname+'/build/api/json','')+'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)';
        request({method: 'GET', url:url}, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    var crumb = body.split(':', 2)[1];
                    request({method: 'POST', url: buildurl ,headers:{'Jenkins-Crumb':crumb}}, function(error, response) {
                        if ( error || (response.statusCode !== 201 && response.statusCode !== 302) ) {
                            callback(error, response);
                            return;
                        }
                        /*
                         Return queue location of newly-created job as per
                         https://issues.jenkins-ci.org/browse/JENKINS-12827?focusedCommentId=201381#comment-201381
                         */
                        var data = {
                            message: "job is executed",
                            location: response.headers["Location"] || response.headers["location"]
                        };
                        callback(null, data);
                    });
                }else{
                    callback(error, response);
                }
        });

    },

`

galenzhao avatar Jul 16 '17 13:07 galenzhao

@galenzhao where should the code that you posted go? is a patch to the api library ?

Mistobaan avatar Feb 09 '18 00:02 Mistobaan

Opened Pull Request #75, for this issue

romanbalayan avatar Oct 09 '18 16:10 romanbalayan