Get all file names from a Github repository via the Github API

Is it possible to get all file names from the repository using the GitHub API?

I'm currently trying to fix this with PyGithub , but I completely agree with manually executing the request while it works.

My algorithm so far:

  1. Get repo usernames
  2. Get a user repository that matches a specific description
  3. ??? get repo file names?
+12
source share
2 answers

This should be relative to a specific commit, as some files may be present in some commits and not in others, so before you can watch the files, you need to use something like the List of commits in the repository :

GET /repos/:owner/:repo/commits 

If you are interested in the last commit on a branch, you can set the branch name for the sha parameter:

sha string SHA or branch to start enumerating commits from.

Once you have a commit hash, you can check what to commit

 GET /repos/:owner/:repo/git/commits/:sha 

which should return something like this (truncated from the GitHub documentation):

 { "sha": "...", "...", "tree": { "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb", "sha": "691272480426f78a0138979dd3ce63b77f706feb" }, "...": "..." } 

Look at the hash of your tree, which is essentially its contents in the directory. In this case, 691272480426f78a0138979dd3ce63b77f706feb . Now we can request the contents of this tree :

 GET /repos/:owner/:repo/git/trees/:sha 

The result from the GitHub example is

 { "sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312", "tree": [ { "path": "file.rb", "mode": "100644", "type": "blob", "size": 30, "sha": "44b4fc6d56897b048c772eb4087f854f46256132", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132" }, { "path": "subdir", "mode": "040000", "type": "tree", "sha": "f484d249c660418515fb01c2b9662073663c242e", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e" }, { "path": "exec_file", "mode": "100755", "type": "blob", "size": 75, "sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057" } ] } 

As you can see, we have some blob that correspond to files and some additional trees that correspond to subdirectories. You can do this recursively .

+20
source

Now you can use the graphql API, and all this can be obtained with a single request.

first you get a repo:

 query { repository(name: "MyRepo" owner: "mylogin"){ } } 

then you will get defaultBranchRef to simplify life

  defaultBranchRef{ } 

Now all the links to the branches are really just a pointer to a commit, and since graphql is strongly typed (and the links may be different), we must make it clear that this is a commit,

  target{ ...on Commit { } } 

therefore, the goal is what our ref points to, and we say, “if it's a commit, do it”

and what should he do? it should get the most recent commit (since it will contain the most recent files in the repo)

so for this we request a story

  history(first: 1 until: "2019-10-08T00:00:00"){ nodes{ } } 

now inside the nodes we are inside our commit and now we can see the files, the files in the commit pointer are actually just a pointer to a tree, and in the tree there are only records that can be objects of type Tree or blob of type

The records representing the files are called blobs, but since we are not doing anything with them, but listing their names, you don’t even need to know that

but it’s important to know that trees are also records, so if you find a tree you need to dig deeper, but you can only go through a predetermined number of levels.

  tree{ entries { name object { ...on Tree{ entries{ name object { ...on Tree{ entries{ name } } } } } } } } 

now to put it all together:

 query{ repository(owner: "MyLogin", name: "MyRepo") { defaultBranchRef { target { ... on Commit { history(first: 1 until: "2019-10-08T00:00:00") { nodes { tree { entries { name object { ... on Tree { entries { name object{ ...on Tree{ entries{ name object{ ...on Tree{ entries{ name } } } } } } } } } } } } } } } } } } 
0
source

Source: https://habr.com/ru/post/973057/


All Articles