Bitbucket API 2 - create repository in a team proj

2020-06-18 03:49发布

问题:

I have a team on my bitbucket account, myteam, which contains a project named mainproject. Whenever I want to create a repository inside I only need to execute this command line:

$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo1" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks"}'

This works. However the issue arises when I create a second project, named secondproject. How am I supposed to tell the API to which project the repository should belong?

I tried specifying the project information in the data (-d):

$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo2" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "project": {"name": "secondproject"} }'

or with the key parameter:

$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo2" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks", "project": {"key": "SEC"} }'

This will create the repository repo2 in the project mainproject.

I tried using the uuid but the same occurs, repo is created in the mainproject.

I tried putting the project name in the link:

$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/secondproject/repo1" -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks"

...
{"error": {"message": "Resource not found", "detail": "There is no API hosted at this URL.\n\nFor information about our API's, please refer to the documentation at: https://confluence.atlassian.com/x/IYBGDQ"}}

How can I specify which project the repository I want to create belongs to? I am not looking for a GUI solution, I want to stick to the command line as I need to automatize the creation of repositories this way.

回答1:

The content type has to be specified to curl with this argument: -H "Content-Type: application/json". Then the json data would be accessed normally. So the end commend would look like this:

$ curl -X POST -v -u myaccount:passwd "https://api.bitbucket.org/2.0/repositories/myteam/repo2" -H "Content-Type: application/json"  -d '{"has_wiki": true, "is_private": true, "project": {"key": "PRJ_KEY"}}'