0

I am trying to create a repository from a command line app that I am writing. I am using node.js to send a post request to the Bitbucket 2.0 api but even though I am successful in creating the repo, it doesn't seem to honour the settings I create the repo with (e.g. is_private = true, has_wiki = true, language = PHP). I am not sure what I am doing wrong. My guess is it's the way I am formatting the body? Below is the code that I am using:

        var dataString = '{"has_wiki": true, "is_private": true}';

        request({
            url: 'https://api.bitbucket.org/2.0/repositories/' + username + '/' + _.kebabCase(answers.reponame),
            method: 'POST',
            headers: {'Authorization': 'Bearer ' + prefs.ginit.token},
            body: dataString
        }, function (err, res) {
            status.stop();

            if (err) {
                console.log(err);
                reject(new Error('Couldn\'t create remote repo.'));
            }

            let json = JSON.parse(res.body);

            if(res.statusCode == 400) {
                reject(new Error(json.error.message));
            }

            if (res.statusCode == 200) {
                console.log(chalk.green('\n' + json.name + ' created sucessfully.'));
                console.log(chalk.green('You can view it here: ' + json.links.html.href + '\n'));

                resolve(json);
            }

        });

The api docs are here. Anyone able to help? Thanks in advance!

5k313t0r
  • 51
  • 1
  • 1
  • 8
  • Have you tried `var dataString = {has_wiki: true, is_private: true};` ? – BassMHL Sep 17 '17 at 03:38
  • Thanks for getting back to me. If I try passing in your suggestion, the repository is successfully created but again it doesn't honour the values passed in :( – 5k313t0r Sep 17 '17 at 04:01

1 Answers1

0

I was able to find the answer based on this question. I needed to pass the repository settings through a bit differently. Through the form key instead of the body.

request({
    url: 'https://api.bitbucket.org/2.0/repositories/' + username + '/' + _.kebabCase(answers.reponame),
    method: 'POST',
    headers: {'Authorization': 'Bearer ' + prefs.ginit.token},
    form: {
        "scm": "git",
        "name": answers.reponame,
        "is_private": answers.visibility === 'private' ? true : false,
        "description": answers.description,
        "language": answers.language,
    }
}, function (err, res) {
    status.stop();

    if (err) {
        console.log(err);
        reject(new Error('Couldn\'t create remote repo.'));
    }

    let json = JSON.parse(res.body);

    if(res.statusCode == 400) {
        reject(new Error(json.error.message));
    }

    if (res.statusCode == 200) {
        console.log(chalk.green('\n' + json.name + ' created sucessfully.'));
        console.log(chalk.green('You can view it here: ' + json.links.html.href + '\n'));

        resolve(json);
    }

});
5k313t0r
  • 51
  • 1
  • 1
  • 8