7

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
kaligne
  • 3,098
  • 9
  • 34
  • 60

1 Answers1

20

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 final command 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"}}'
kaligne
  • 3,098
  • 9
  • 34
  • 60
  • 2
    Is there any way to script this without writing/prompt a password? Using SSH for example? – Wingjam Mar 15 '17 at 19:05
  • It should be noted that the TEAM in the url is case-sensitive...in this case 'myteam'. In my case we had some uppercase letters in that name, and we did NOT get a friendly error when it failed. – Brett Green Mar 21 '17 at 20:05
  • I tried that I got `* Connection #0 to host api.bitbucket.org left intact {"type": "error", "error": {"message": "Forbidden"}}` – code-8 Oct 03 '19 at 13:20
  • Docs are at https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D#post – brian d foy Jan 24 '21 at 15:30