Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github REST API - how to retrieve specific lines of codes (code snippet)

I would like to retrieve specific lines of codes via the REST API.

After a user has authorized access by connecting to his github account (via Web Application flow), I'd like to be able to programtically retrieve with the REST API a block of lines from a repo's file.

On the github.com UI, it's quite easy to get only certain lines: you can select multiples lines and get a "permalink" such as if it's form line 3 to 7 for example:

https://github.com/{username}/{repo_name}/blob/{specific file ex: ce3f225c2025556705353f8369097e760d063c6bbce3}/{file_path_in_the_repo}#L3-L7

On the API however I don't manage to do it. I manage to get the code but only for the WHOLE file, not restricted to certain lines with: https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}

For example the following code works:

https://api.github.com/repos/getsentry/sentry-ruby/contents/sentry-rails/app/jobs/sentry/send_event_job.rb

The result is

{
  "name": "send_event_job.rb",
  "path": "sentry-rails/app/jobs/sentry/send_event_job.rb",
  "sha": "55314dd99703fc121516513a59e20377b2534f48",
  "size": 980,
  "url": "https://api.github.com/repos/getsentry/sentry-ruby/contents/sentry-rails/app/jobs/sentry/send_event_job.rb?ref=master",
  "html_url": "https://github.com/getsentry/sentry-ruby/blob/master/sentry-rails/app/jobs/sentry/send_event_job.rb",
  "git_url": "https://api.github.com/repos/getsentry/sentry-ruby/git/blobs/55314dd99703fc121516513a59e20377b2534f48",
  "download_url": "https://raw.githubusercontent.com/getsentry/sentry-ruby/master/sentry-rails/app/jobs/sentry/send_event_job.rb",
  "type": "file",
  "content": "aWYgZGVmaW5lZD8oQWN0aXZlSm9iKQogIG1vZHVsZSBTZW50cnkKICAgIHBh\ncmVudF9qb2IgPQogICAgICBpZiBkZWZpbmVkPyg6OkFwcGxpY2F0aW9uSm9i\nKSAmJiA6OkFwcGxpY2F0aW9uSm9iLmFuY2VzdG9ycy5pbmNsdWRlPyg6OkFj\ndGl2ZUpvYjo6QmFzZSkKICAgICAgICA6OkFwcGxpY2F0aW9uSm9iCiAgICAg\nIGVsc2UKICAgICAgICA6OkFjdGl2ZUpvYjo6QmFzZQogICAgICBlbmQKCiAg\nICBjbGFzcyBTZW5kRXZlbnRKb2IgPCBwYXJlbnRfam9iCiAgICAgICMgdGhl\nIGV2ZW50IGFyZ3VtZW50IGlzIHVzdWFsbHkgbGFyZ2UgYW5kIGNyZWF0ZXMg\nbm9pc2UKICAgICAgc2VsZi5sb2dfYXJndW1lbnRzID0gZmFsc2UgaWYgcmVz\ncG9uZF90bz8oOmxvZ19hcmd1bWVudHM9KQoKICAgICAgIyB0aGlzIHdpbGwg\ncHJldmVudCBpbmZpbml0ZSBsb29wIHdoZW4gdGhlcmUncyBhbiBpc3N1ZSBk\nZXNlcmlhbGl6aW5nIFNlbnRyeUpvYgogICAgICBpZiByZXNwb25kX3RvPyg6\nZGlzY2FyZF9vbikKICAgICAgICBkaXNjYXJkX29uIEFjdGl2ZUpvYjo6RGVz\nZXJpYWxpemF0aW9uRXJyb3IKICAgICAgZWxzZQogICAgICAgICMgbWltaWMg\nd2hhdCBkaXNjYXJkX29uIGRvZXMgZm9yIFJhaWxzIDUuMAogICAgICAgIHJl\nc2N1ZV9mcm9tIEFjdGl2ZUpvYjo6RGVzZXJpYWxpemF0aW9uRXJyb3IgZG8K\nICAgICAgICAgIGxvZ2dlci5lcnJvciAiRGlzY2FyZGVkICN7c2VsZi5jbGFz\nc30gZHVlIHRvIGEgI3tleGNlcHRpb259LiBUaGUgb3JpZ2luYWwgZXhjZXB0\naW9uIHdhcyAje2Vycm9yLmNhdXNlLmluc3BlY3R9LiIKICAgICAgICBlbmQK\nICAgICAgZW5kCgogICAgICBkZWYgcGVyZm9ybShldmVudCwgaGludCA9IHt9\nKQogICAgICAgIFNlbnRyeS5zZW5kX2V2ZW50KGV2ZW50LCBoaW50KQogICAg\nICBlbmQKICAgIGVuZAogIGVuZAplbHNlCiAgbW9kdWxlIFNlbnRyeQogICAg\nY2xhc3MgU2VuZEV2ZW50Sm9iOyBlbmQKICBlbmQKZW5kCgo=\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/getsentry/sentry-ruby/contents/sentry-rails/app/jobs/sentry/send_event_job.rb?ref=master",
    "git": "https://api.github.com/repos/getsentry/sentry-ruby/git/blobs/55314dd99703fc121516513a59e20377b2534f48",
    "html": "https://github.com/getsentry/sentry-ruby/blob/master/sentry-rails/app/jobs/sentry/send_event_job.rb"
  }
}

But if I add L3-L7, like below it does not change anything. I would have lked it to change for exmaple the download_url so that it only includes line 3 to 7:

https://api.github.com/repos/getsentry/sentry-ruby/contents/sentry-rails/app/jobs/sentry/send_event_job.rb#L3-L7

I don't find on the Github Docs which url to call to retrieve PROGRAMATICALLY with the REST API this type of multi-line code snippet?

Note: I know how to get the whole "download_url": https://raw.githubusercontent.com/getsentry/sentry-ruby/master/sentry-rails/app/jobs/sentry/send_event_job.rb file and then parse it to only keep line X to line Y but i would like to know if there's a direct API command to do what you can easily do with the UI.

Thanks

like image 296
Mathieu Avatar asked Jun 12 '26 23:06

Mathieu


1 Answers

GitHub's REST API does not provide a way to extract just a few lines of a file. In the web interface, you get the entire rendered file with just a few lines highlighted, not just a snippet.

The reason this is the case is because extracting a limited number of lines from a file is actually much more work than extracting the entire file. All files are stored as Git blobs, and there isn't a way to extract only certain lines from a blob without reading the entire file up to that point, since blobs are stored compressed. Therefore, GitHub would actually expend much more effort to read the entire file into memory and then restrict it to just the lines you wanted, and as a result, such an API would be much more restricted and not be able to handle files that were nearly as large.

Also, in some cases, there is no sane answer to what constitutes a line. While Git normally wants files to be stored with LF endings, if a file has been checked in with CRLF endings, should those be handled? (If so, that's additional work to properly handle them.) If you have a binary file, like a JPEG, there are no lines. Similarly, while files in UTF-16 probably have lines, Git considers them binary files, so they probably wouldn't be able to be handled.

Note that the reason that your #L3-L7 doesn't work as part of the API, besides that the API doesn't support it, is that this is a fragment and is generally not sent to the server. It's supposed to identify a specific portion of a document, and that's typically done client-side, in the web browser. Since with your API request there is no client to do this, the server doesn't even see your request.

like image 111
bk2204 Avatar answered Jun 15 '26 14:06

bk2204