How to get a list of submodules through github API? - github

I'm trying to get a list of submodules of a repository through GitHub API. After reading Github API docs, I did the following things: In Order to access the submodules of the Jquery, I use the following link to get a list of submodules, however, I cannot see any submodules from it. Could anyone please tell me what field should I use to get a list of submodules of a repository from GitHub API?

You can get the list of git submodules using Github GraphQL API v4 with the submodules connection property :
{
repository(owner: "bertrandmartel", name: "javacard-tutorial") {
submodules(first: 100) {
nodes {
name
path
}
}
}
}
Try this in the explorer
Output :
{
"data": {
"repository": {
"submodules": {
"nodes": [
{
"name": "oracle_javacard_sdks",
"path": "oracle_javacard_sdks"
}
]
}
}
}
}
Note that this will give only submodules on default branch's HEAD, from the documentation :
Returns a list of all submodules in this repository parsed from the .gitmodules file as of the default branch's HEAD commit.
Note that the jquery repository doesn't have any git submodules in its default branch's HEAD (and I don't see any in its other branches)

If you use v3 REST API you can
Get the HEAD commit SHA of the branch.
Use the returned commit SHA as the tree_sha to get tree. GET /repos/{owner}/{repo}/git/trees/{tree_sha}
Iterate the returned tree object for entry that has mode 160000 (submodule)

Related

How to get GitHub repository Default Branch name with GraphQL?

Is there a way to use GitHub's GraphQL API to retrieve the default branch name of a repository? I can build a query to get all the branch names, but none of the properties I see reference the default branch name. Some repositories I deal with have 'master' for their default, and others have 'main'.
I am aware I can use the HEAD in some commands to workaround this. Such as "HEAD:README.md" for fetching the README.md file, like in this example query below. This repository's default branch is 'main', and I only know that from manual trial and error.
query {
repository(owner: "newrelic", name: "newrelic-ruby-agent") {
object(expression: "HEAD:README.md") {
... on Blob {
text
}
}
}
}
But when I check the local cloned copy of that repository, the .git/HEAD file points to ref: refs/heads/dev. So it seems I am actually pulling the README.md file from the dev branch, and not the default branch?
So is there a nice way to get the name of the default branch for a GitHub repository using their GraphQL API?
There's a field defaultBranchRef on the repository object:
query {
repository(owner: "newrelic", name: "newrelic-ruby-agent") {
defaultBranchRef {
name
}
}
}

Field 'dependencyGraphManifests' doesn't exist on type 'Repository'

I am trying to make a react project which can fetch data from a github repo and list all the project dependencies, their current version in the project, latest version available, outdated or not and whether vulnerable. I am trying to use the github graphQL to fetch data but keeps on throwing this error.
Field 'dependencyGraphManifests' doesn't exist on type 'Repository'
. Can anyone help me with this? How can I get these data from github? Is it even possible?
Thanks in advance
Read https://docs.github.com/en/graphql/overview/schema-previews#access-to-a-repositories-dependency-graph-preview.
If you set the following header it will be there:
Accept: application/vnd.github.hawkgirl-preview+json
gh api -H 'Accept: application/vnd.github.hawkgirl-preview+json' graphql
--paginate -f query='query {
repository(owner:"octocat",name:"Hello-World") {
dependencyGraphManifests {
totalCount
nodes {
filename
}
edges {
node {
blobPath
dependencies {
totalCount
nodes {
packageName
requirements
hasDependencies
packageManager
}
}
}
}
}
}
}

cvs2svn cvs2git: map cvs users to github users

I used cvs2git to convert 1 cvs repo to git and push it to github. Everything works except that I don't see any contributors for my repo on github. I am wondering if there is a way to bring all the users from cvs and map them to github users.
If you are ok with converting the repository again, you should use the author_transforms field in the options file.
Here is the example from the example options file I linked to:
author_transforms={
'jrandom' : ('J. Random', 'jrandom#example.com'),
'mhagger' : 'Michael Haggerty <mhagger#alum.mit.edu>',
'brane' : (u'Branko Čibej', 'brane#xbc.nu'),
'ringstrom' : 'Tobias Ringström <tobias#ringstrom.mine.nu>',
'dionisos' : (u'Erik Hülsmann', 'e.huelsmann#gmx.net'),
# This one will be used for commits for which CVS doesn't record
# the original author, as explained above.
'cvs2git' : 'cvs2git <admin#example.com>',
}
The example file has more details.
If you need to keep the existing repo in github, you may be able to do some form of rewriting of the commit history, but I don't know anything about that.

How to search content on specific branch using Github API

I'm using the Github Kohsuke API to search for content on a remote repo on Github. Currently, I'm only able to pull down content from my default master branch. Is there a way to pull down content on a branch besides master? Here's the code snippet I have, which currently only pulls from my master branch. Thanks for any help!
public class GitCompareTest {
public static void main(String[] args) throws IOException {
GitHub github = GitHub.connect();
GHRepository repo = github.getRepository(repo_name);
List<GHContent> articleContents = repo.getDirectoryContent(path);
for (GHContent content : articleContents) {
if (content.isDirectory()) {
PagedIterable<GHContent> childArticleContents = content.listDirectoryContent();
for (GHContent childArticleContent : childArticleContents) {
String childText = childArticleContent.getContent();
String.out.println(childText);
}
}
}
}
}
Try getDirectoryContent(String path, String ref) using your branch's name for ref.
A branch name is essentially a name for a specific commit in git, and "ref" is git terminology for "name of a specific commit".
(Disclaimer: I haven't used Kohsuke's GitHub library yet, I'm drawing from general git knowledge.)

GitHub Api: How to get Root :tree_sha of a repository?

How do I get the Root :tree_sha of a GitHub repository via the GitHub API?
The GitHib API help pages don't seem to explain this critical piece of information:
http://develop.github.com/p/object.html
Can get the contents of a tree by tree
SHA
tree/show/:user/:repo/:tree_sha
To get a listing of the root tree for
the facebox project from our commit
listing, we can call this:
$ curl
http://github.com/api/v2/yaml/tree/show/defunkt/facebox/a47803c9ba26213ff194f042ab686a7749b17476
Each commit contains the sha of the entire tree as of that commit.
Use the API to get a JSON object representing the master branch.
https://api.github.com/repos/:owner/:repo/branches/master
That branch's last commit includes the tree's sha that I think you're asking for.
This bit of code demonstrates how to get the head_tree_sha in Python.
import requests
token = '0...f'
key = {'Authorization':'token '+token}
master = requests.get('https://api.github.com/repos/'+owner+'/' + repo '/branches/master', headers=key)
master = master.json()
head_tree_sha = master['commit']['commit']['tree']['sha']
https://developer.github.com/v3/git/commits/
http://develop.github.com/p/commits.html
The commit tells you its tree sha.
[EDIT]
If you want the tree sha of a subfolder cd into the parent folder of the one you're interested in and run:
git ls-tree HEAD
If you want Root tree sha:
git show HEAD --format=raw
1st line has commit sha
2nd line has tree sha
I'm not sure about the GitHub API — however if you want just the hash you can use this command in your clone:
git show HEAD --format=%T | head -1
Or use %t for the abbreviated hash.