Using CURL to list github repository tree (github API) - api

Using the github API, you can get a repository tree using (example done with CURL):
curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/{owner}/{repo}/git/trees/{tree_sha}
Assuming I have a repo called dev by owner NewCo, and I want to list the repo tree called XXXX, I would:
curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/NewCo/dev/git/trees/{tree_sha}
How can I find out the {tree_sha} value for tree XXXX? Any idea where can I find out this value?

You can use a commit sha from the commits endpoint for that: /repos/{owner}/{repo}/commits. For example:
#!/usr/bin/env bash
set -e
owner=zacanger
repo=fetchyeah
sha=$(curl -s -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$owner/$repo/commits?per_page=1 \
| jq -r '.[0].sha')
curl -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$owner/$repo/git/trees/$sha
You could also use the pull requests API, or any other endpoint that returns commit info (meaning, most of them except for user and org APIs).

Related

curl command to tarball ALL organizations repos thru API

Was making a bash script to backup organization's repos (private included) convert them into a tar file and then send off to s3 bucket.
curl -H "Authorization: token {PAT}" -L https://api.github.com/repos/{org}/tarball/main > main.tar.gz
When I do this command with one single repo it works, but my task was to have it grab all 100+ repos in the organization. Any thoughts of what I am missing?
I don't think that there is any API from GitHub to provide your desired HTTP call out of the box.
You can try to create a Bash/PowerShell script to:
List the organization:
curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
https://api.github.com/orgs/ORG/repos
(reference: https://docs.github.com/en/rest/repos/repos#list-organization-repositories)
Take the result and convert it to an array.
For each element in the array, run your command:
curl -H "Authorization: token {PAT}" -L
https://api.github.com/repos/{org}/tarball/main > main.tar.gz

github api: how to get clones?

I'm trying to get clones with (from here):
curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/{myname}/{myrep}/traffic/clones
However, I get this error:
"message": "Must have push access to repository"
I even tried:
curl -H "Authorization: token {mytoken}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/{myname}/{myrep}/traffic/clones
but I get the same error...
I used the old API version and it was easy and quick, and I cannot find a working solution... what am I missing?
(I would like to use curl since I use this command in a sh file)
curl \
-H "Authorization: token bade46eb8b548dcb47c4a3263492faa7fb857d83" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/{user}/{repository name}/traffic/clones
This command works with the new API
(the token is fake, but useful for people to have a general idea of how it looks)

Why I can't delete Link between person and group identities with Platform of Trust's Identity API?

I have created account on Sandbox
I have then created a group with
curl -i -X POST \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-H "Content-Type: application/json" \
-d \
"{
\"context\": \"https://standards.oftrust.net/v2/Context/Identity/Group/\",
\"type\": \"Group\",
\"data\": {
\"name\": \"Company Oy\"
}
}" "https://api-sandbox.oftrust.net/identities/v1"
I have also created a Link between person and group, I used MemberOf
curl -i --request POST \
--url https://api-sandbox.oftrust.net/identities/v1/{fromIdentityId}/link/{toIdentityId} \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--header 'content-type: application/json' \
--data '{
"context": "https://standards.oftrust.net/v2/Context/Link/Role/MemberOf/",
"type": "Member"
}'
I got successful response that link was created between those identities.
Trying to delete this link now, but I get as response 404 and message Link not found.
What I try is according with example from documentation
curl -i -X DELETE \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
"https://api-sandbox.oftrust.net/identities/v1/{fromIdentityId}/link/{toIdentityId}/MemberOf"
[UPDATE]: I discovered also in Identity API documentation that can list all links of identity.
And have made this for group identity:
curl -i -X GET \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
"https://api-sandbox.oftrust.net/identities/v1/<group_id>/links"
The response shows that link between group and person identities.
Firstly, make sure you respected the id values (their order) for From and To. They should be the same you get in response of https://api-sandbox.oftrust.net/identities/v1/<group_id>/links
Secondly, delete Link endpoint needs to be used with a type, as exemplified. In this case MemberOf. But looking at the creation of the link there is a typo: context used is correct, but the type is Member. Type should match the last part of the name in context => MemberOf
In this case, since you are trying to delete it, simply use Member
curl -i -X DELETE \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
"https://api-sandbox.oftrust.net/identities/v1/{fromIdentityId}/link/{toIdentityId}/Member"

GoCD POST requests return "The resource you requested was not found!"

I have GoCD instance and want to automate regular actions like scheduling pipelines and checking pipelines statuses using GoCD API.
When I do GET request it works:
curl 'https://gocd.demo.kagarlickij.com/go/api/pipelines/frankenstein/status' \
-u 'kagarlickij:Pa$$w0rd' | jq
..but when I do POST request it returns "The resource you requested was not found!":
curl 'https://gocd.demo.kagarlickij.com/go/api/pipelines/frankenstein/pause' \
-u 'kagarlickij:Pa$$w0rd' \
-H 'Accept: application/vnd.go.cd.v1+json' -H 'Content-Type: application/json' \
-X POST -d '{"pause_cause": "Investigating build failures"}' | jq
..another POST example:
curl 'https://gocd.demo.kagarlickij.com/go/api/pipelines/frankenstein/schedule' \
-u 'kagarlickij:P#$$w0rd' \
-H 'Accept: application/vnd.go.cd.v1+json' -H 'Content-Type: application/json' \
-X POST -d #gocd.json | jq
json content:
{
"environment_variables": {},
"materials": {},
"update_materials_before_scheduling": false
}
Any ideas how pipelines could be started using API?
Some GoCD API calls require 'Confirm': 'true' header.
In you case, you can try running curl like this:
curl 'https://gocd.demo.kagarlickij.com/go/api/pipelines/frankenstein/pause' \
-u 'kagarlickij:Pa$$w0rd' \
-H 'Accept: application/vnd.go.cd.v1+json' \
-H 'Content-Type: application/json' \
-H 'Confirm: true' \
-X POST -d '{"pause_cause": "Investigating build failures"}' | jq
I can recommend my lib yagocd for GoCD, which takes cares about version incompatibilities and makes working with GoCD API much easier.
The answer turned out to be very simple - that API actions require GoCD v18.2.0 but I had v18.0.0
After upgrade API calls work as expected

Get image list using Orange Cloud API

Using the endpoint /folders of the Orange Cloud API, I can only get the listing of the files in the main directory:
curl -X GET -H "X-Orange-CA-ESID: OFR-2588c...2e64f249ab" -H \
"Authorization: Bearer OFR-2588c...2e64f249ab" \
https://api.orange.com/cloud/v1/folders/Lw
How could I get photos entries only, including the ones in subdirectories?
You can get all photos this way:
curl -X GET \ -H \
-H "Authorization: Bearer OFR-948ef5..." \
"https://api.orange.com/cloud/v1/folders?filter=image&flat=true"
By the way, the session header is no more necessary
https://developer.orange.com/apis/cloud-france/getting-started#filtering-on-photos,-videos,-audio-files