go-github: DownloadContents can't download files from root of repo
GitHub Apps have the ability to request access to individual files which is great for fine-grained access.
However, as of v47.1.0
, this will prevent someone from being able to download a file from the root repository because of the way DownloadContents
implements directory scanning first:
The first thing that the function does is get just the directory name with path.Dir
but if you try to download a file like just CODEOWNERS
which is a valid location for a CODEOWNERS
file, then the first request to the API will create a URL like so:
https://api.github.com/repos/$owner/$repo/contents/
At the moment, there’s no way to add the root directory to the list of single file paths. You cannot add a blank path and adding just /
doesn’t work either.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 19
@gmlewis I’m almost done with a solution to the point brought by @abatilo, that is, downloading the file without having to list the base directory. Just have to adjust the tests. I’ll try to send a PR till the end of the week if you think it’s still valid.
I did NOT have the time when I had hoped to. Please feel free to assign to @petersondmg! I appreciate it though @gmlewis
It’s possible. I discovered this because I am building an app to do things based on CODEOWNERS, which can be in 3 locations.
CODEOWNERS
,docs/CODEOWNERS
, and.github/CODEOWNERS
.There’s no actual need for the app to request permissions or have any ability to read files in the root of the repo and my guess is that security minded organizations would prefer that.
I do still think that the “correct” solution here is to change the
DownloadContents
code from it’s original implementation from 8 years ago to not need to do aGetContents
call on thepath.Dir
of the path before attempting to do a for loop over each file in the folder to get a string match.I may have time this week to open a PR with that change and see if the proposed solution is alright!
Thank you, @abatilo .
This would be a great PR for any new contributor to this repo or a new Go developer. All contributions are greatly appreciated!
Feel free to volunteer for any issue and the issue can be assigned to you so that others don’t attempt to duplicate the work.
Please check out our CONTRIBUTING.md guide to get started. (In particular, please remember to
go generate ./...
and don’t use force-push to your PRs.)Thank you!
Hi @gmlewis, I think I actually poorly chose my words. I shouldn’t have said “as of v47.1.0” but instead it’s more that I found this behavior while using
v47.1.0
. It appears that this has been the behavior for the history of the function? I don’t think I have the availability to work on this so I’d be happy to have this open up to anyone who wants to contribute!Hi @petersondmg. If you configure the GitHub App with the
Contents
permissions, then there aren’t any problems with downloading from the root. However, if you configure single files only like I have in the screenshot in the main post, and you provide a single file in the root as the only permission, then you won’t be able to download it because you’ve only granted permission to the single path and not the directory.Hello Maybe I misunderstood the point of the issue, but I was able to ‘download files from root of repo with DownloadContents’, using a GitHub App token, and a personal token as well.
To generate a Github App Token I did:
1 - generated a private key for the app 2 - authenticated as a GitHub App (generated the jwt) 3 - retrieved an access token by requesting the endpoint using the jwt https://api.github.com/app/installations/<app_installation_id>/access_tokens 4 - used the access token to execute the script posted on comments above
It was able to list files on the root and download the file.
Should I try a different approach? @gmlewis @abatilo
Thanks.
No problem, @abatilo - thank you for the note, and best wishes to you.
@petersondmg - it is yours.
Sorry, @petersondmg , I should have assigned this to @abatilo from the previous comment. Let’s wait to hear back what @abatilo discovers.
Hi @zaataylor, your solution demonstrates accessing things at the root of the repo for a GitHub Personal Access Token which does not have the same fine grained per file permissions that a GitHub App might request.
Maybe this issue needs to be re-worded to be specific about the file path permissions that you can set on a GitHub App.
Thank you, @zaataylor , for the detailed analysis and excellent write-up… It is greatly appreciated! I believe that you have demonstrated that this is working as intended.
I will leave this issue open for a short while in case there are any other comments, but otherwise I believe this issue can be closed.
I took a look into this tonight because I’d like to get involved in the project. Based on the
path.Dir
docs I found here, it seems like it should be possible for the existing code to download files from the root of a repo:I was able to construct a minimal working example of this as follows:
go-gh-testcase
.main.go
of this module looks like:go-github/github/repos_content.go
very slightly to add somefmt.Println
s in the appropriate places:Because of the behavior of
path.Dir
, as outlined here, afilepath
without any/
s (such as the path"LICENSE"
I chose in the first code block) will result indir
being set to"."
. So I think this would actually result in a URL that looks like:https://api.github.com/repos/$owner/$repo/contents/.
Running the code above, I got the result:
which is the first 100 bytes of
go-github
’sLICENSE
file.Doing the same test on the
go.sum
file (last file in the root of the repo as it appears in GitHub UI) yields:So, it seems like specifying a filename in the repo root will still behave properly; the API call will just consider
dir
to be"."
, but the file will be found in thefor
loop over the contents of"."
if it’s in the root.I’ve never participated in this project before, and I tried to make sure I understood the context before commenting, but if I’ve totally misunderstood something here, please let me know! I’m just trying to help and learn a little Golang! 😄