octokat.js icon indicating copy to clipboard operation
octokat.js copied to clipboard

Thank you!

Open timaschew opened this issue 9 years ago • 6 comments

Just wanted to say Thank you :pray:

Your library is awesome! It's just fun to use the API with it and async await / Promise.coroutine And the REPL link to also great!

I just finished with a flow of API calls with octocat which automate the whole pull request flow (changing, adding, removing files, forking a repo, create a new branch, apply the commits and send the pull request)

Just one thing where I'm not sure: Does the GitHub API support multiple file changes within one commit?

timaschew avatar Mar 09 '16 15:03 timaschew

Thanks for using octokat!

You can write many files using octokat, just not directly... yet (see below).

To commit multiple files now, you will need to go through these steps:

  1. Find the base commit tree (ie repo.git.refs('heads/master'))
  2. Create new blobs for each file (ie repo.git.blobs.create({content: 'Hi'}))
    • Note: binary files need to be handled with care (base64 encode & send a special header)
  3. Generate a new commit tree which includes the sha's of the created blobs (ie repo.git.trees.create({tree, base_tree}))
  4. Create a new commit using the commit tree (ie repo.git.commits.create({message, tree, parents}))
  5. Update the branch to point to the new commit (ie repo.git.refs('heads/master'))
    • Or, create a new branch instead of updating an existing one
    • This could also be used to Merge a Pull Request

For reference, the relevant code to do it using a now-defunct library can be found here: https://github.com/philschatz/octokit.js/blob/master/octokit.coffee#L1012-L1070

Also, there is a refactor PR (#81) that pulls out most of the code into separate plugins (ie Binary Files, Pagination, Hypermedia, Cache Handling, Camel Casing) so you can include only the plugins you use.

I was planning on adding a writeMany() plugin that would add the functionality described above but have not yet had the time or the motivation from the community to add it in yet :smile:.

Hope that helps! If you get it working with await (probably relatively easier than in ES5) please add a snippet here!

philschatz avatar Mar 09 '16 19:03 philschatz

Wow, that looks good, I will try it :)

timaschew avatar Mar 09 '16 23:03 timaschew

I wonder how to delete a file via blob or tree API... ... just found a solution here: http://stackoverflow.com/a/23714505

But I'm confused now: What does it mean to generate a new tree exactly? I don't get this:

The tree creation API will take nested entries as well. If both a tree and a nested path modifying that tree are specified, it will overwrite the contents of that tree with the new path contents and write a new tree out.

Do I need to list all files, otherwise unlisted files will be deleted?

timaschew avatar Mar 10 '16 20:03 timaschew

I didn't find time to try out use the blobs and do in one commit, but here is the Promise/Generator version to do it with multiple commits: https://gist.github.com/timaschew/c5daed16394522b756fc86039b8d19d6

By the way: I started to write some tests for this workflow, but it doesn' t work, because I cannot stub octokat with sinon:

TypeError: Attempted to wrap undefined property forks as function

Here is a demo for the test case: https://tonicdev.com/timaschew/570f420ae3cbbb11004b29e9

timaschew avatar Apr 14 '16 07:04 timaschew

I've wrote my own stub/spy for octocat: https://gist.github.com/timaschew/44de7de88d875971a5369a0046d5ba7d

timaschew avatar May 06 '16 09:05 timaschew

Does someone have that aforementioned snipped on getting this to work with async / await I can't seem to nab it down.

tyrantkhan avatar Jun 29 '17 01:06 tyrantkhan