jansepar / node-jenkins-api

Jenkins API for NodeJS
MIT License
359 stars 173 forks source link

create_folder fails to create folder #77

Open avishnyakov opened 5 years ago

avishnyakov commented 5 years ago

Hi team,

Using the following sniper to create new folders. It fails with 400 response.

jenkins.create_folder(folderName, function(err, data) {
    if (err) {
        console.log(err)
        throw err;
    } else {
        console.log(" [+] created folder: " + folderName);
    }
});

Response:

No Content-Type header set

Page generated: Feb 2, 2019 7:06:01 AM UTCREST APIJenkins ver. 2.162
1
You have not configured the CSRF issuer. This could be a security issue. For more information, please refer to this page.
You can change the current configuration using the Security section CSRF Protection.

Not sure if this is my jenkins setup but curl execution from https://github.com/jansepar/node-jenkins-api/pull/55 works well

curl -XPOST 'http://jenkins/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22FolderName%22%2C%22mode%22%3A%22com.cloudbees.hudson.plugins.folder.Folder%22%2C%22from%22%3A%22%22%2C%22Submit%22%3A%22OK%22%7D&Submit=OK' --user user.name:YourAPIToken -H "Content-Type:application/x-www-form-urlencoded"
           https://gist.github.com/stuart-warren/7786892

It feels that we need to add headers while making doRequest calls. The following fix works well, creates folders.

create_folder: function (folderName, customParams, callback) {
      [folderName, customParams, callback] = doArgs(arguments, ['string', ['object', {}], 'function']);

      const mode = 'com.cloudbees.hudson.plugins.folder.Folder';

      customParams.name = folderName;
      customParams.mode = mode;
      customParams.Submit = 'OK';

      doRequest({
        method: 'POST',
        urlPattern: [NEWFOLDER],
        // added missing headers
        // also noparse, response is XML
        request: {
          headers: { 'Content-Type':  'application/x-www-form-urlencoded' }
        },
         noparse: true
      }, customParams, callback);
    }

That said, a few quetions regarding create_folder methods: