Getting whole repository structure from GitHub API using one request - github-api

Is it possible to somehow get information about whole repository using one request to github api or we have to recursively asking API for specific level?
According to this request: https://api.github.com/repos/microsoft/terminal/contents?ref=master
we are only getting one level.

You can get the tree recursively using :
/repos/:owner/:repo/git/trees/:tree_sha?recursive=1
In your case it would be :
https://api.github.com/repos/microsoft/terminal/git/trees/master?recursive=true

You can make a request to here to see details about your branch:
https://api.github.com/repos/{owner}/{repo}/branches/{default_branch}
In the response you will see "commit" field and inside of it there is "sha". Take that one put it in this request:
https://api.github.com/repos/{owner}/{repo}/git/trees/YOUR_SHA_GOES_HERE?recursive=1
You will receive every file under your repo like this.
Cheers!

Related

GitLab API: Get branch associated with issue

In a GitLab issue, you can associate a branch with an issue, and in the issue there will be the line
#whoever created the branch branchname to address this issue.
Is there a way of getting that branch using the issues API? I'm trying to set up an automation script that will merge all branches associated with issues that have a certain label into the prod branch, then push the result as a development brnach so I can deploy that to a dev environment. I don't want to use merge requests as they will be used when the dev work is complete and ready to be merged for deployment to production.
Unfortunately, there currently is no official API to fetch an issue's related branches.
Some possible ways you can work around this:
Use the notes API
When a user uses the issue interface to create the branch, you will see a system message, like you mention. This message will be present in the notes API for the issue.
Example using the python-gitlab library:
import re
...
branch_note_pattern = '^created branch \[\`(.*)\`\].*to address this issue'
issue = project.issues.get(ISSUE_NUMBER)
all_notes = list(issue.notes.list(as_list=False))
system_notes = [note for note in all_notes if note.system]
related_branches = []
for note in system_notes:
match = re.match(branch_note_pattern, note.body):
if match:
branch = match.groups()[0]
related_branches.append(branch)
print('BRANCHES RELATED TO ISSUE', ISSUE_NUMBER)
for branch_name in related_branches:
print(branch_name)
However, it is possible to have a related branch without that note appearing because the related branches is just based on naming convention. So, if someone just creates a branch with named like <issue_number>-some-branch-name then it will show up as a related branch, but there will not be a system message in the API.
So, if you rely on the notes API, you may miss related branches created manually.
Use the unofficial frontend API
The issues controller only returns related branches for the purposes of the frontend to render as HTML.
If you request /<:project_url>/-/issues/<:issue_number>/related_branches?format=json you will get a JSON response containing the HTML for the frontend to insert in the issue view. You can parse this HTML to get the related branches.
This will reliably fetch the same related branches you'll see in the UI, but is more work to implement, and is fragile because the API is not guaranteed to be stable between versions of GitLab.

How to get all my github pull requests of my github account using github api

i want get get all pull requests of all my repos using github api v3.
I have tried the following issue endpoint:
https://api.github.com/user/issues?filter=all&state=all&sort=create&direction=desc
This will return all issues including the pull requests. but is there any way of getting just the pull requests (not all issues) using page parameters. I.e. I want to get the first 10 pull requests and then next 10 pull requests using the "page" and "per_page" parameters.
How can i do this?
You can use the type:pr to search only the pull requests. i.e. https://api.github.com/search/issues?q=type=pr&state=all&sort=create&direction=desc&page=1&per_page=10
If I understand the question correctly, what you want are the pull requests initiated by you (I guess that's what you mean by pull request of repos of my github account
Please add your username instead of mine (swarajpure) and you'll get your PRs:
https://api.github.com/search/issues?q=user:swarajpure+type:pr&per_page=10&page=1
This by default will show first 10 PRs. To see next 10 PRs, please enter page=2 and so on.
For more details: https://swarajpure.medium.com/github-apis-fetching-pull-requests-issues-by-a-user-organisation-repository-84ae934a106b

Get all results from a repo on the GitHub API that does not give a value for 'Link'?

I'm trying to get all of the pull requests for a given repo. The GitHub API paginates results such that you cannot get all the results at once. In the documentation, they say that getting all of the results will require knowing how many pages there are. They say you can learn how many pages there are by getting the Link response header, which you should be able to get with curl -I https://api.github.com/repos/rails/rails, for instance. But, while that works for the rails repository, it does not work for the repo that I need: /lodash/lodash. When I run the same command with lodash, I get:
curl -I https://api.github.com/repos/lodash/lodash/pulls
HTTP/1.1 200 OK
...
Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-
Limit,...
...
In other words, Link is an Access-Control-Expose-Header for the lodash repository. I haven't been able to find any information on how to get it, given that.
So I believe the crux of my question is "How do I get an Access-Control-Expose-Header?" but I wanted to provide context in case there is another way of getting all pull requests.
As for today, there is no opened pull request for repository lodash, so you will have no result.
From Github API, default state is open when you retrieve pull requests :
Either open, closed, or all to filter by state. Default: open
Applying a filter that gives more pages will give you the Link header :
curl -I https://api.github.com/repos/lodash/lodash/pulls?state=all

Is there a command or a "git log" option to retrieve comments/discussion logged for a merge request?

We have a GITlab(8.14) running for collaboration within the company.
I am working on a python script to collect information about merge requests being raised by developers across projects. I can very easily isolate the merge requests using 'git log'
git log --merges
However, I haven't been able to locate the correct command or option to retrieve all the discussion/comments taking place in the Merge Request.
Solution 1: use Gitlab Log System
Have you thought to use the Gitlab Log System instead of using a Git command?
It contains information about all performed requests.... Also you can see all
SQL request that have been performed and how much time it took.
Please take a look here https://docs.gitlab.com/ee/administration/logs.html
So in your Python script of collecting information, you can use queries like that:
SELECT <things> FROM "merge_requests" WHERE <condition>
Solution 2: use Gitlab API
Another way is to directly request Gitlab API to get a list of all notes for a single merge request.
Notes are comments on snippets, issues or merge requests.
like this:
GET /projects/:id/merge_requests/:merge_request_id/notes
The complete API reference for merge request notes is available here.
Does this help you?

How to update a fork from it's original via the Github API

I've created a fork of a github repository via the github API. Now, later on, I want to pull any updates from the origin repository into the fork. This should always be a fast-forward in my use case. I have read access to the origin repository and read-write to the fork.
I thought of maybe creating a Pull Request then accepting (both of which you can do via the API) but this creates noise (Pull Requests being created and destroyed) and just doesn't seem right.
Is there any way to do this via the API?
I don't have the inside scoop on this, so this might be a miss-feature that will be removed at some point. Until then:
Github makes available all commits across (I assume) the entire fork network; So APIs that accept commit hashes will be happy to work on hashes from the upstream, or across other forks (This is explicitly documented for repos/commits/compare and creating a pull requst).
So there are a couple of ways to update via APIs only:
Using Git data api: This will usually be the best option, if you don't change your fork's master.
Get upstream ref /repos/upstream/repo/git/refs/heads/master, and get the hash from it
Update your fork PATCH /repos/my/repo/git/refs/heads/master with the same hash.
Using a higher-level merge api: This will create a merge commit, which some people like.
Get the upstream ref like before
Create a merge to branch master in your repo.
Pull-request to yourself and merge it via api: This will end up creating not only a merge commit, but a PR as well.
Create PR: POST to /repos/your/repo/pulls with head = "upstream:master"
Get the PR url from the response,
Merge it: PUT to /repos/your/repo/pulls/number/merge
It's possible that the "upstream:master" notation would also work for options 1 & 2, saving an API call.
Not possible currently, but I've gone ahead and added that to our API wishlist. :)
This that work for me, because I needed update from upstream but without a merge request commit. My ref is master.
Create a pull request POST /repos/:myUsername/:myRepo/pulls
INPUT: {title, head: 'ownerFromUpStream:master', base: 'master', ...}
Get sha from pull request (ex. response.data.head.sha)
PATCH /repos/:myUsername/:myRepo/git/refs/master
PARAMS: {sha: shaFromPullRequest}
DOC.
Update ref
Create pull request
This is now possible in the GitHub API; documentation here, and announcement here.
In summary, make a POST request to /repos/{owner}/{repo}/merge-upstream with the proper authentication and the payload of { "branch": "branch-name" }.

Resources