Create Fork on bitbucket using REST API in a given project - bitbucket

I found Using BitBucket's API to fork a repository and this is helpful in so far as letting me know that creating a fork is possible.
However I want to create the fork in a certain project on the bitbucket server.
We have a use case where we want to create a fork of some repositories in another project and then merge back only the parts of the project that are re-usable not the project specific stuff.

Assuming you want to fork PRJ1/repo_AA as PRJ2/repo_BB, you can use:
curl -X POST -H 'Content-Type: application/json' \
-d '{ "slug": "repo_BB", "name": "repo_BB", \
"project": { "key": "PRJ2" } }' \
https://your.serv.er:7990/rest/api/1.1/projects/PRJ1/repo_AA
Be sure project PRJ2 already exists (you can create it via REST API as well) and have the correct credentials.
HTH
Regards

Related

Create repo on Bitbucket programmatically

I used to do
curl -k -X POST --user john#outlook.com:doe13 "https://api.bitbucket.org/1.0/repositories" -d "name=logoApp"
and success.
now I got : error
{"type": "error", "error": {"message": "Resource removed", "detail": "This API is no longer supported.\n\nFor information about its removal, please refer to the deprecation notice at: https://developer.atlassian.com/cloud/bitbucket/deprecation-notice-v1-apis/"}}
Does anyone know a know way to do this ?
There's a difference between a success from curl (OK:200) and an error from the service you're trying to use. The error, however, mentions that you're trying to use the Cloud Rest API version 1, which is deprecated effective 30 June 2018.
Read this for more information.
I don't use Bitbucket Server (a local option), and I think that has more features for this sort of thing.
For the public Bitbucket, you can still do it but it isn't documented.
The v1.0 API has been removed, and the new v2.0 API doesn't document a POST to a /repositories. Instead, you have to hit an endpoint that includes the repo that doesn't yet exist: /repositories/workspace/repo_slug
The JSON payload needs to know the project for the repo: look in the slug for a project that already exists. Fill in the user/team and repo name in the URL. And, you can make an application password so you aren't using your account password. This app password can limit the scope of what that access can do.
% curl -X POST --user 'user:app_pass' \
-H "Content-type: application/json" \
-d '{"project":{"key":"PROJ"}}' \
"https://api.bitbucket.org/2.0/repositories/USER/REPO"

Bitbucket API for updating the build status for a pull request

I see the docs for updating the build status for a commit:
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build
For a pull request, isn't there a unique commit associated with it? Is there no API for updating a PR's build status? Or do we just need to discover which commit is associated with the PR, and then just update the build status for that commit?
For example this view:
You can see on the right that the commit to the temp branch has a passing build status - but the way it works is it should merge temp into master into some new commit/branch and I need to be able to test that.
The view that I want to update is at url:
https://bitbucket.org/<user>/<repo>/pull-requests/1/<commit-message>/diff
Bitbucket does not have Builds on PRs (Check here), What you need is one Successful Build on the LAST Commit:
Then, you can use the API to push the build to that one after you have done what you need to test it.
This guide is really simple to use:
https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/
👆The only recommendation I have on the guide is that it didn't work for me with curl, then, what I did was to do it with Postman and then exported the command to curl and that's it...
curl --location --request POST 'https://bitbucket.org/api/2.0/repositories/{your space}/{Your Repo}/commit/{The long hash of your commit}/statuses/build?Content-Type=application/json' \
--header 'Authorization: Basic {your token}=' \
--header 'Content-Type: application/json' \
--data-raw '{
"state": "SUCCESSFUL",
"key": "MANUAL-BUILD",
"name": "Manual Build",
"url": "http://this.really.doesnt.matter/",
"description": "Successful Build done manually"
}'
Just go to this this url and type "build" and you will find the relevant routes:

How to create queue in Rabbitmq

I am creating a new image taking base as rabbitmq and trying to create queue,exchange which will be reflected on localhost url once the server is up. I am able to manually create queue within rabbitmq container. But I want to achieve this either through dockerfile or entrypoint.sh. I want the exchange, queue to be available as soon as rabbitmq server is up. Please suggest any way to achieve it. Any sample example will be helpful.
Rabbitmq has a Management HTTP API. You can use this api to interact with rabbitmq.
You can create an exchange by doing a PUT request to http://localhost:15672/api/exchanges/${vhost}/${name}. Similarly, you can create a queue by
doing a PUT to http://localhost:15672/api/queues/${vhost}/${name}.
You can call these using curl in the entrypoint script.
You can use HareDu 2 like so:
var result = _container.Resolve<IBrokerObjectFactory>()
.Object<Queue>()
.Create(x =>
{
x.Queue("fake_queue");
x.Configure(c =>
{
c.IsDurable();
c.AutoDeleteWhenNotInUse();
c.HasArguments(arg =>
{
arg.SetQueueExpiration(1000);
arg.SetPerQueuedMessageExpiration(2000);
});
});
x.Targeting(t =>
{
t.VirtualHost("HareDu");
t.Node("Node1");
});
});
Here is a practical example with curl and the REST HTTP API which was already mentioned.
First of all, the HTTP REST API is a separate plugin and if not installed, you have to install it with the command:
rabbitmq-plugins enable rabbitmq_management
if you want to install it in your Docker image, you can do:
RUN rabbitmq-plugins enable rabbitmq_management
Once you plugin is installed, you can just call the API with a curl command:
curl --location --request PUT 'http://localhost:15671/api/queues/%2F/TEST_QUEUE' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=' \
--data-raw '{
"auto_delete": false,
"durable": true,
"arguments": {}
}'
The Basic auth provided is guest/guest as the default user in the docker image.
The trick is for the default vhost, you have to put %2F in your url (equivalent to /) this can make you lose a lot of time if you try other variations.
Here are some references:
Management plugin documentation
HTTP API documentation
If you need a practical docker example, you can have a look to my solace integration project where I set up a rabbitMQ for testing purpose here.

Bitbucket api set repository access roles

I've created a repository in BitBucket using the api:
curl -v -X POST -d '{"scm": "git", "is_private": "true", "fork_policy": "no_forks", "project": {"key": "MARS"}}' -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/myteam/test -u <user-name>
How can I set user-access roles for the new repository?
Specifically I want to set a group TeamAdmins to have admin privileges
Answered by Bitbucket team, but maybe someone will look for this:
There is currently no support for this in api-2, and no current due date on the feature.
You can do this using API-1: using privileges-endpoint

Is there a way to rename a repository on Bitbucket using their API

I couldn't find anything even remotely related in the documentation.
Using the Bitbucket website you can rename a repo as follows:
Go to the repo's overview page, usually https://bitbucket.org/username/oldname/overview
Click the settings cog on the far right end of the menu row !
Instead of 1. and 2. you can type 'r' then 'a' for administration.
Change the name in the Name field.
Click Save repository details.
Be advised that changing the name of the repo will change its URL access too. Previously the access was https://username#bitbucket.org/username/oldname.git Now, however, the repo's URL/Path will be https://username#bitbucket.org/username/newname.git
You can check this by going back to the Overview page, and hovering over the big blue HTTPS button. The bottom of your browser will show that it now points to https://username#bitbucket.org/username/newname.git
If you are using SourceTree you can update the remote's URL by highlighting the local repo in SourceTree and then
Click Repository
Click Repository Settings...
Highlight the row containing the remote branch. Usually origin https://username#bitbucket.org/username/oldname.git
Click Edit
Update the URL/Path field. Change 'oldname.git' to 'newname.git', leave the rest unchanged. So the full path should be https://username#bitbucket.org/username/newname.git
Click OK
For version 2.0 of the API:
According to https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-put
PUT https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug} --data "{\"name\": \"${new_name}\"}"
Using the PUT method allows renaming of a repository.
For version 1.0 of the API:
According to https://confluence.atlassian.com/display/BITBUCKET/repository+Resource+1.0:
PUT https://api.bitbucket.org/1.0/repositories/{accountname}/{repo_slug} --data "name=new name"
This allows to update the visible name of a repository.
In a unix shell you can use cURL;
curl https://api.bitbucket.org/1.0/repositories/{accountname}/{old_repo_name} --data "name=new_repo_name" -X PUT
Is it possible for a user to authenticate in private repositories, but still have only administrators able to execute:
curl https://USER:PASS#api.bitbucket.org/1.0/repositories/{accountname}/{old_repo_name} --data "name=new_repo_name" -X PUT
Just in case anyone hits this with looking for a solution to an old version of the bitbucket API (in my case 5.14.0) to say the documentation on this version is lacking is being quite polite.
curl --location --request PUT 'https://git.local.install/rest/api/1.0/projects/aa/repos/my-repo' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic .....' \
--data-raw '{"name":"my-new-name"}'
According to the lastest API here is the correct curl command:
curl -X PUT --user username:password https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug} --data "name=newRepoName"
Note that the repo_slug is the repository name IN LOWER CASE. If you don't put it all in lower case you would get the not so expressive answer "Not Found".
If you are not sure what is the repository slug execute the following command, which shows you the user's information including current repositories, and look for the field "slug"
curl --user username:password https://bitbucket.org/api/1.0/user

Resources