Get all file names from a Github repo through the Github API

后端 未结 4 1304
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 01:02

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

I\'m currently trying to tinker this using PyGithub, but I\'m totally ok with manually

4条回答
  •  悲哀的现实
    2020-12-10 01:38

    Much eaiser now with the graphql api, you can get it all in a single query

    first you get your repo:

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

    then you get its defaultBranchRef to make life easy

        defaultBranchRef{
    
        }
    

    Now all a branch ref really is, is just a pointer to a commit, and since graphql is strongly typed (and refs can be different things) we need to let it know it is a commit,

       target{
          ...on Commit {
    
          }
       }
    

    so target is what our ref is pointing to, and we say "if its a commit, do this"

    and what should it do? it should get the most recent commit (since that will have the latest files in the repo)

    so to do that we query history

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

    now inside of nodes we are inside of our commit and now we can see the files, the files in a commits pointer are really just a pointer to a tree, and a tree just has entries, which can be objects of either type Tree, or type blob

    entries that represent files are known as blobs, but since we dont do anything with them but list their names, you dont even need to know that

    but its important to know that trees are also entries, so if you find a tree you need to dig in deeper, but you can only go a pre defined amount of levels deep.

           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
                                      }                                  
                                    }
                                  }
                                }   
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

提交回复
热议问题