【问题标题】:Create a repository with Bitbucket 2.0 api and node.js使用 Bitbucket 2.0 api 和 node.js 创建一个存储库
【发布时间】:2017-09-17 03:36:08
【问题描述】:

我正在尝试从我正在编写的命令行应用程序创建存储库。我正在使用 node.js 向 Bitbucket 2.0 api 发送一个发布请求,但即使我成功创建了 repo,它似乎也不尊重我创建 repo 的设置(例如 is_private = true,has_wiki = true , 语言 = PHP)。我不确定我做错了什么。我猜这是我格式化正文的方式吗?以下是我正在使用的代码:

        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);
            }

        });

API 文档是here。有谁能帮忙吗?提前致谢!

【问题讨论】:

  • 你试过var dataString = {has_wiki: true, is_private: true};吗?
  • 感谢您回复我。如果我尝试传递您的建议,则存储库已成功创建,但它再次不遵守传入的值:(

标签: node.js bitbucket-api


【解决方案1】:

我能够根据this question 找到答案。我需要以不同的方式传递存储库设置。通过表单键而不是正文。

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);
    }

});

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2016-07-19
    • 2017-08-28
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多