How to create multiple branch restrictions using Bitbucket api? - bitbucket

I'm trying to automate the branch permissions setup using the bitbucket api but when I try to add multiple rules it doesn't overwrite the old rule (in case it exists). I'm creating 2 rules for a repository for one branch but if I re-run the api again with a little change in the rule, it will add the rule I added, instead of editing it the current rule.
I run this call:
curl -X POST -v -u "username:secret" -H "Content-Type: application/vnd.atl.bitbucket.bulk+json" https://bitbucket.example.com/rest/branch-permissions/2.0/projects/myproj/repos/myrepo/restrictions -d '[{ "type": "read-only","matcher": {"id": "master","displayId": "master","type": {"id":"PATTERN","name": "Pattern"}},"users": ["my.user"],"groups": ["StashAdmins"]},{ "type": "no-deletes","matcher": {"id": "master","displayId": "master","type": { "id":"PATTERN","name": "Pattern"}},"users": ["user.my"],"groups": []}]'
Then I wanted to overwrite the current branch permissions so I changed the first rule from read-only to pull-request-only, so I run :
curl -X POST -v -u "username:secret" -H "Content-Type: application/vnd.atl.bitbucket.bulk+json" https://bitbucket.example.com/rest/branch-permissions/2.0/projects/myproj/repos/myrepo/restrictions -d '[{ "type": "pull-request-only","matcher": {"id": "master","displayId": "master","type": {"id":"PATTERN","name": "Pattern"}},"users": ["my.user"],"groups": ["StashAdmins"]},{ "type": "no-deletes","matcher": {"id": "master","displayId": "master","type": { "id":"PATTERN","name": "Pattern"}},"users": ["user.my"],"groups": []}]'
but it added the new rule (pull-request-only) instead of editing the whole rule.
Does anyone know how to forces overwrite the branch restriction rule?

With this Rest Api endpoint you can just create new restrictions as you can have several ones per repository and/or project.
See here for more: https://docs.atlassian.com/bitbucket-server/rest/6.4.0/bitbucket-ref-restriction-rest.html#idp1
You first need to delete all restrictions which were created before and then post a new one. To get all restrictions per repository you will need to use this endpoint:
GET /rest/branch-permissions/2.0/projects/{projectKey}/repos/{repositorySlug}/restrictions
https://docs.atlassian.com/bitbucket-server/rest/6.4.0/bitbucket-ref-restriction-rest.html#idp3
And then you can delete them one by one with this one:
DELETE /rest/branch-permissions/2.0/projects/{projectKey}/repos/{repositorySlug}/restrictions/{id}
https://docs.atlassian.com/bitbucket-server/rest/6.4.0/bitbucket-ref-restriction-rest.html#idp6

Related

Bitbucket repository creations by api

after reading the doc https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories .
Seems this API is for reading the info of existing repos.
Is there any API for repo creation?
Check out the POST of repo_slug
/2.0/repositories/{workspace}/{repo_slug}
With:
repo_slug string : This can either be the repository slug or the UUID of the repository, surrounded by curly-braces, for example: {repository UUID}.
workspace string : This can either be the workspace ID (slug) or the workspace UUID surrounded by curly-braces, for example: {workspace UUID}.
Creates a new repository.
Note: In order to set the project for the newly created repository, pass in either the project key or the project UUID as part of the request body as shown in the examples below:
$ curl -X POST -H "Content-Type: application/json" -d '{
"scm": "git",
"project": {
"key": "MARS"
}
}' https://api.bitbucket.org/2.0/repositories/teamsinspace/hablanding
or
$ curl -X POST -H "Content-Type: application/json" -d '{
"scm": "git",
"project": {
"key": "{ba516952-992a-4c2d-acbd-17d502922f96}"
}
}' https://api.bitbucket.org/2.0/repositories/teamsinspace/hablanding
The project must be assigned for all repositories. If the project is not provided, the repository is automatically assigned to the oldest project in the workspace.
Note: In the examples above, the workspace ID teamsinspace, and/or the repository name hablanding can be replaced by UUIDs.
For BitBucket server, that would be here, as illustrated in this gist:
/rest/api/1.0/projects/{projectKey}/repos
But V1 API was deprecated in 2018.

What is the NGSI v2 endpoint for mimicking IoT Agent commands?

When testing commands Southbound, I am currently using the NGSI v1 endpoint as shown:
curl -X POST \
'http://{{iot-agent}}/v1/updateContext' \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"contextElements": [
{
"type": "Bell",
"isPattern": "false",
"id": "urn:ngsi-ld:Bell:001",
"attributes": [
{
"name": "ring",
"type": "command",
"value": ""
}
]
}
],
"updateAction": "UPDATE"
}'
As you can see this is an NGSI v1 request. According to this presentation on Slideshare (slide 16) use of NGSI v1 is discouraged - I would like to replace this with an NGSI v2 request. I believe that all IoT Agents are now NGSI v2 capable, however I have been unable to find the details of the replacement NGSI v2 request within the documentation.
So the question is what is the equivalent cUrl command to mimic a command from Orion using NGSI v2?
In this document you can see a good reference on how to send commands using the NGSIv2 API:
If you take a look to the previous device example, you can find that a "ping" command was defined. Any update on this attribute “Ping” at the NGSI entity in the ContextBroker will send a command to your device. For instance, to send the "Ping" command with value "Ping request" you could use the following operation in the ContextBroker API:
PUT /v2/entities/[ENTITY_ID]/attrs/ping
{
"value": "Ping request",
"type": "command"
}
ContextBroker API is quite flexible and allows to update an attribute in several ways. Please have a look to the NGSIv2 specification for details.
Important note: don't use operations in the NGSI API with creation semantics. Otherwise, the entity/attribute will be created locally to ContextBroker and the command will not progress to the device (and you will need to delete the created entity/attribute if you want to make it to work again). Thus, the following operations must not be used:
POST /v2/entities
PUT /v2/entities
POST /v2/op/entites with actionType append, appendStrict or replace
POST /v1/updateContext with actionType APPEND, APPEND_STRICT or REPLACE
EDIT: all the above refers to the Orion endpoint used by final client to send commands. #jason-fox has clarified that question refers to the IOTA endpoint that receives commands request from Orion (it should have been evident by the {{iot-agent}}, but I missed that part sorry :)
The Orion-to-IOTA communication for commands is based on the registration-forwarding mechanism. Currently, Orion always uses NGSIv1 to forward updates (even in the case the client uses NGSIv2 updates). In the future, we envision the usage of NGSIv2 but in order to achieve this, first we need:
To complete the Context Source Forwarding Specification, based on NGSIv2. It is currently under discussion in this PR. Feedback is welcome as comments to that PR!
To implement forwarding based in Context Source Forwarding Specification in Orion
To implement NGSIv2 endpoint compliant with Context Source Forwarding Specification in the IOTAs.
While the above gets completed, the only mechanism is the current one based in NGSIv1. However, note the Orion-IOTA interaction is internal to platform component and final client could base all their interactions to the platform (in particular, to the Orion endpoint) on NGSIv2, so this is not a big issue.
The Context Source Forwarding Specification, based on NGSIv2 is now completed and the old /v1 endpoint has been deprecated. According to the discussions of the associated Support for NGSIv2 issue, the correct request to send is as follows:
curl -iX POST \
http://localhost:4041/v2/op/update \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"actionType": "update",
"entities": [
{
"type": "Bell",
"id": "urn:ngsi-ld:Bell:001",
"ring" : {
"type": "command",
"value": ""
}
}
]
}'

Get cURL response body in Rails console

I'm working on a db migration where I need to upload images in the old db to Filepicker using cURL within the Rails console. I've got a command that works, and all I need from here is to grab the Filepicker URL from the response. However, setting the response as below just returns true.
So this will upload a file to Filepicker as desired:
res = system("curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY")
Giving this output:
{"url": "https://www.filepicker.io/api/file/THEURL", "size": 207610, "type": "image/jpeg", "filename": "oldimage1.jpg"} => true
The problem is I can't assign the URL - the res variable only stores true.
So, in a nutshell, how to I get access to the rest of this data to assign it to an instance in the new db?
If you need run curl you can do this
body=`curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY`
or
body = %x(curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY)
or
IO.popen("curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY").read
But If I were you, I would try to use net:http or another ruby http client as typhoeus or restclient

Update a page in Confluence using REST API

This is what I've currently got and it creates a new Confluence page. It doesn't update it. Also it posts it in the root space, TST, but I want it to be in TST/space1/subsection2/updateThisPage.
curl -v -u admin:admin -X POST -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
This is the error message I get
{"statusCode":400,"message":"A page with this title already exists: A page already exists with the title new page in the space with key TST"}
Would it be a permissions error? I know I do not have access to delete.
Use request /rest/api/content/{id}.
This worked for me.
curl -u admin:admin -X PUT -H "Content-Type: application/json" -d "{\"id\":\"26738701\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"RO\"},\"body\":{\"storage\":{\"value\":\"<p>UPDATE This is a new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":2}}" http://localost:10080/rest/api/content/26738701
JSON Payload:
{
"id":"26738701",
"type":"page",
"title":"new page",
"space":{
"key":"RO"
},
"body":{
"storage":{
"value":"<p>UPDATE This is a new page</p>",
"representation":"storage"
}
},
"version":{
"number":2
}
}
Don't forget to use:
content ID in data part
version number in data part
PUT request
content ID in request
Try to use PUT instead of POST.
curl -v -u admin:admin -X PUT -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
If anyone is looking for javascript solution, here is my answer to another question like that
Unexpected grunt-http error when posting to Atlassian Confluence api
And here you can find working code i've developed on confluence hackathon
https://github.com/devex-web-frontend/dxWebPlugins/blob/master/src/confluence/helpers/buffer.js

BigCommerce webhooks listener in rails

I'm following the instructions here http://developer.bigcommerce.com/api/webhooks/quickstart to set up webhooks to initiate some third-party order processing. We've been doing this on an hourly batch, real-time webhook triggers will save us a lot of lag time.
I think I've set up the webhook broadcaster, but can't see any evidence that it's being fired- I've created a bunch of new orders and nothing reaches the rails server.
How can I tell if BigCommerce is firing events when / where I expect?
Generated the access token for the given app/user/domain:
curl -XPOST -d '{
"client_id":"[BigCommerceAppClientId]",
"client_secret":"[BigCommerceAppSecret]",
"user":"admin",
"token":"[adminAPIToken]",
"store_domain":"https://[myStore].mybigcommerce.com"
}' https://hooks-beta.bigcommerce.com/token
yields ===>
{
"access_token":"[webHooksAccessToken]",
"producer":"store/[myStoreKey]"
}
Subscribed to webhooks for store/events/listener:
curl -XPOST -d '{
"producer":"store/[myStoreKey]",
"scope":"store/order/created",
"deliverymethod":"HTTP_POST",
"destination":{"url":"http://[myPublicRailsServer]/hooks"}
}' -H 'X-Auth-Client: [BigCommerceAppClientId]' -H 'X-Auth-Token:[X-Auth-Token]' https://hooks-beta.bigcommerce.com
yields ===>
{
"client_id":"[webHooksAccessToken]",
"created_at":"2013-06-27T19:57:38+00:00",
"deliverymethod":"HTTP_POST","destination":{"url":"http://[myPublicRailsServer]/hooks"},
"id":651,
"producer":"store/[myStoreKey]",
"scope":"store/order/created",
"updated_at":"2013-06-27T19:57:38+00:00"
}
I lied. The problem was apparently trying to use https instead of http. Everything works as expected.
Furthermore- BigCommerce provides a hook to check the active clients for a given application:
curl -XGET -H
'X-Auth-Client: [BigCommerceAppClientId]' -H
'X-Auth-Token: [BigCommerceAppSecret]'
https://hooks-beta.bigcommerce.com/producer/store/[myStoreKey]

Resources