Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Last active April 28, 2016 20:46
Show Gist options
  • Select an option

  • Save dphiffer/0ab9b7a69510b04f1a189e5df9b43ae3 to your computer and use it in GitHub Desktop.

Select an option

Save dphiffer/0ab9b7a69510b04f1a189e5df9b43ae3 to your computer and use it in GitHub Desktop.
The repos/contents GitHub API method only works on "small" repos.

This is a minimal demonstration of a problem that's cropped up, where committing changes via this API method has stopped working: https://developer.github.com/v3/repos/contents/#update-a-file

But only sometimes! It does work on "small" repos, but the same process fails inexplicably when attempted on a "big" repo.

Below I compare results committing against the README.md of two repos:

First, get a copy of the current README (base64 content + sha hash).

$ curl https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md
{
  "name": "README.md",
  "path": "README.md",
  "sha": "f4a869184376afcca7498d472955c14a0f0f5ca7",
  "size": 112,
  "url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md?ref=master",
  "html_url": "https://github.com/whosonfirst/whosonfirst-sources/blob/master/README.md",
  "git_url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/blobs/f4a869184376afcca7498d472955c14a0f0f5ca7",
  "download_url": "https://raw.githubusercontent.com/whosonfirst/whosonfirst-sources/master/README.md",
  "type": "file",
  "content": "IyB3aG9zb25maXJzdC1zb3VyY2VzCgpXaGVyZSB0aGluZ3MgY29tZSBmcm9t\nIGluIFdobydzIE9uIEZpcnN0LgoKIyMgU2VlIGFsc28KCiogaHR0cHM6Ly9n\naXRodWIuY29tL3dob3NvbmZpcnN0Cg==\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md?ref=master",
    "git": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/blobs/f4a869184376afcca7498d472955c14a0f0f5ca7",
    "html": "https://github.com/whosonfirst/whosonfirst-sources/blob/master/README.md"
  }
}

Now we'll PUT that same content back as a commit.

$ curl -XPUT https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md \
-H "Authorization: token [snip]" \
-H "Content-Type: application/json" \
-d '
{
"message": "Updating README (via API)",
"content": "IyB3aG9zb25maXJzdC1zb3VyY2VzCgpXaGVyZSB0aGluZ3MgY29tZSBmcm9t\nIGluIFdobydzIE9uIEZpcnN0LgoKIyMgU2VlIGFsc28KCiogaHR0cHM6Ly9n\naXRodWIuY29tL3dob3NvbmZpcnN0Cg==\n",
"sha": "f4a869184376afcca7498d472955c14a0f0f5ca7"
}
'
{
  "content": {
    "name": "README.md",
    "path": "README.md",
    "sha": "f4a869184376afcca7498d472955c14a0f0f5ca7",
    "size": 112,
    "url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md?ref=master",
    "html_url": "https://github.com/whosonfirst/whosonfirst-sources/blob/master/README.md",
    "git_url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/blobs/f4a869184376afcca7498d472955c14a0f0f5ca7",
    "download_url": "https://raw.githubusercontent.com/whosonfirst/whosonfirst-sources/master/README.md",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/contents/README.md?ref=master",
      "git": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/blobs/f4a869184376afcca7498d472955c14a0f0f5ca7",
      "html": "https://github.com/whosonfirst/whosonfirst-sources/blob/master/README.md"
    }
  },
  "commit": {
    "sha": "ab592e1bc13d93e70b891f9664d816d6ee8c712b",
    "url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/commits/ab592e1bc13d93e70b891f9664d816d6ee8c712b",
    "html_url": "https://github.com/whosonfirst/whosonfirst-sources/commit/ab592e1bc13d93e70b891f9664d816d6ee8c712b",
    "author": {
      "name": "Dan Phiffer",
      "email": "[email protected]",
      "date": "2016-04-28T20:25:54Z"
    },
    "committer": {
      "name": "Dan Phiffer",
      "email": "[email protected]",
      "date": "2016-04-28T20:25:54Z"
    },
    "tree": {
      "sha": "d43a2f4102a8d5489d96b7c0f5bc4f563fd17ca8",
      "url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/trees/d43a2f4102a8d5489d96b7c0f5bc4f563fd17ca8"
    },
    "message": "Updating README (via API)",
    "parents": [
      {
        "sha": "12d5a45dca89f2a4dadddd25b785910041454910",
        "url": "https://api.github.com/repos/whosonfirst/whosonfirst-sources/git/commits/12d5a45dca89f2a4dadddd25b785910041454910",
        "html_url": "https://github.com/whosonfirst/whosonfirst-sources/commit/12d5a45dca89f2a4dadddd25b785910041454910"
      }
    ]
  }
}

Yay, it works!

Now let's try the same thing on a "big" repo (i.e., one with many files in it). First we get some info about the current README.md.

$ curl https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/contents/README.md
{
  "name": "README.md",
  "path": "README.md",
  "sha": "e82531da02037f3e0e2dfbb276e4ca968a701f25",
  "size": 386,
  "url": "https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/contents/README.md?ref=master",
  "html_url": "https://github.com/whosonfirst-data/whosonfirst-data-venue-us-new-york/blob/master/README.md",
  "git_url": "https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/git/blobs/e82531da02037f3e0e2dfbb276e4ca968a701f25",
  "download_url": "https://raw.githubusercontent.com/whosonfirst-data/whosonfirst-data-venue-us-new-york/master/README.md",
  "type": "file",
  "content": "IyB3aG9zb25maXJzdC1kYXRhLXZlbnVlLXVzLW5ldy15b3JrCgpXaG8ncyBP\nbiBGaXJzdCB2ZW51ZSBkYXRhIGZvciBOZXcgWW9yayAoVVMpCgojIyBJbXBv\ncnRhbnQKClRoaXMgd29yayBpcyBzdGlsbCBpbiB0aGUgZWFybHkgc3RhZ2Vz\nIGFuZCBzb21lIGltcG9ydGFudCBhbmNpbGxhcnkgYXJ0aWZhY3RzIG5vcm1h\nbGx5IGZvdW5kIGluIFdobydzIE9uIEZpcnN0IGRhdGEgcmVwb3NpdG9yaWVz\nIChsaWtlIGBtZXRhYCBmaWxlcykgaGF2ZSBub3QgYmVlbiBjcmVhdGVkIHll\ndC4KCiMjIFNlZSBhbHNvCgoqIGh0dHBzOi8vd2hvc29uZmlyc3QubWFwemVu\nLmNvbS8KKiBodHRwczovL2dpdGh1Yi5jb20vd2hvc29uZmlyc3QtZGF0YS93\naG9zb25maXJzdC1kYXRhLXZlbnVlLXVzLwo=\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/contents/README.md?ref=master",
    "git": "https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/git/blobs/e82531da02037f3e0e2dfbb276e4ca968a701f25",
    "html": "https://github.com/whosonfirst-data/whosonfirst-data-venue-us-new-york/blob/master/README.md"
  }
}

And now we PUT the same content + sha hash.

$ curl -XPUT https://api.github.com/repos/whosonfirst-data/whosonfirst-data-venue-us-new-york/contents/README.md \
-H "Authorization: token [snip]" \
-H "Content-Type: application/json" \
-d '
{                                      
"message": "Updating README (via API)",
"content": "IyB3aG9zb25maXJzdC1kYXRhLXZlbnVlLXVzLW5ldy15b3JrCgpXaG8ncyBP\nbiBGaXJzdCB2ZW51ZSBkYXRhIGZvciBOZXcgWW9yayAoVVMpCgojIyBJbXBv\ncnRhbnQKClRoaXMgd29yayBpcyBzdGlsbCBpbiB0aGUgZWFybHkgc3RhZ2Vz\nIGFuZCBzb21lIGltcG9ydGFudCBhbmNpbGxhcnkgYXJ0aWZhY3RzIG5vcm1h\nbGx5IGZvdW5kIGluIFdobydzIE9uIEZpcnN0IGRhdGEgcmVwb3NpdG9yaWVz\nIChsaWtlIGBtZXRhYCBmaWxlcykgaGF2ZSBub3QgYmVlbiBjcmVhdGVkIHll\ndC4KCiMjIFNlZSBhbHNvCgoqIGh0dHBzOi8vd2hvc29uZmlyc3QubWFwemVu\nLmNvbS8KKiBodHRwczovL2dpdGh1Yi5jb20vd2hvc29uZmlyc3QtZGF0YS93\naG9zb25maXJzdC1kYXRhLXZlbnVlLXVzLwo=\n",
"sha": "e82531da02037f3e0e2dfbb276e4ca968a701f25"
}
'
{
  "message": "Server Error",
  "documentation_url": "https://developer.github.com/v3/repos/contents/"
}

Womp womp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment