Bitbucket API 2 - create repository in a team project
Asked Answered
N

1

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.

Natascha answered 13/7, 2016 at 9:17 Comment(0)
N
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"}}'
Natascha answered 13/7, 2016 at 9:45 Comment(4)
Is there any way to script this without writing/prompt a password? Using SSH for example?Expropriate
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.Helman
I tried that I got * Connection #0 to host api.bitbucket.org left intact {"type": "error", "error": {"message": "Forbidden"}}Mckeown
Docs are at developer.atlassian.com/bitbucket/api/2/reference/resource/…Underfoot

© 2022 - 2024 — McMap. All rights reserved.