How to list files of the default branch for a repository using the Github API via one query? - github-api

Related problem: How do I find the default branch for a repository using the Github v3 API
Different from the related problem, I want to list the files of the default branch.
The API I know to list files of the master branch is:
https://api.github.com/repos/owner/repo_name/git/trees/master
However, the default branch name could be main.
Is there a way to list files of the default branch through only one query?

You could use HEAD simply (which refers to the default remote branch).
I tried it using gh api after a (gh auth login)
gh api repos/owner/repo_name/git/trees/HEAD
Example:
gh api repos/VonC/gitcred/git/trees/HEAD --jq ".tree[].path"

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 list files from enterprise GitHub repository without cloning

I am trying to list the files present on an enterprise GitHub repository. I tried using GitHub API but base URL for it is
api.github.com where as base URL would be different for me and when I put that URL it says couldn't resolve. How can I achieve it for a URL in below format
https://company.net/org/reponame
Below example works for me but the URL that I have it doesn't work
curl -k https://api.github.com/user/repo?refs=master
For an on-premise GitHub Enterprise, the documentation mentions
REST API endpoints — except Management Console API endpoints — are prefixed with the following URL:
http(s)://[hostname]/api/v3
In your case: https://company.net/api/v3
From there, using the Git Database API -- list tree, you can list all files, as illustrated here.

Github API get repo with exact repo name

The get repository api (link) works like "search". For example, when I have "foo-bar" repository and I use /repos/MY_NAME/foo it returns foo-bar.
There seems to be no option for disabling this lenient repo name.
Search API just returns more than one item that worsk for the :repo

Azure REST API Get Branches returning 0 branches

I am trying to get all branches for a project with
https://dev.azure.com/$organization/$project/_apis/tfvc/branches?&api-
version=5.0
I use the credentials, organization, and project variables in other queries that are working. The request format is based off the example here
https://learn.microsoft.com/en-us/rest/api/azure/devops/tfvc/branches/get?view=azure-devops-rest-5.0
The response I am getting is
count value
----- -----
0 {}
when there are many branches in the repository.
Should I be specifying the repository somewhere? Or what else? Thanks!
First you need to confirm which type of repository you need to use rest API to get branch from, git or tfvc.
If you want to get branches from a tfvc repo, then use the rest api as you refer to in your case.
But, you need to be aware that if the count you want to return is not 0, you should ensure that there are branches in your tfvc repo, not just folders and files.
To convert the folder in tfvc repo to branch, you can convert it in vs source control explorer. For details, please refer to this docs.
If you want to get branches from a git repo, you need to use the rest API as follows.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.0

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