scripting / Scripting-News

I'm starting to use GitHub for work on my blog. Why not? It's got good communication and collaboration tools. Why not hook it up to a blog?
120 stars 10 forks source link

Sample GitHub API code for uploading files? #2

Open scripting opened 8 years ago

scripting commented 8 years ago

Short version

I'm looking for simple JavaScript example code to upload a single text file to a GitHub repository from a Node.js app.

Background

Andrew Shell had an interesting idea -- mirror my blog posts to a GitHub repository.

I like it because it provides an easy to manage archive, and takes care of distribution of updates.

I looked at the API and the JavaScript toolkits and how authentication works, and there are a lot of pieces to understand to get what I imagine will ultimately be a very small piece of code.

So I thought I'd ask if anyone has such code?

Of course I will re-share whatever I end up using.

reachfh commented 8 years ago

This may be useful: https://github.com/prose/prose

scotthansonde commented 8 years ago

Do you mean something like GitHub pages that would store and be able to serve a mirror of your blog?

I do this with my main blog http://www.papascott.de (in fact, GitHub serves the blog). The repository is at https://github.com/papascott/papascott.github.io (the HTML files are in the master branch). I generate the HTML files locally and push to GitHub when I post something new. I do this manually, but (since I know a little bit about 1999.io) I'm sure the simple-git node package could do this, maybe through a publish callback.

scripting commented 8 years ago

I wasn't thinking of GitHub pages because that adds another public rendering to confuse people and search engines. It's about wanting to create a backup flow of the site's content. It's for distribution.

I think I remember that GitHub pages require that you get the content to flow into a repository? Is that correct? If so, that's the part of the problem that I want to solve here.

It would definitely connect in, esp at first, into the callbacks mechanism in nodeStorage server.

xicubed commented 8 years ago

interesting commenting system you can use on static sites https://jimpick.com/2016/05/05/introducing-lambda-comments/

scotthansonde commented 8 years ago

After a couple of months abstinence I'm back working with 1999.io :-) And I've got pushing to GitHub working in a callback.

The JavaScript is pretty easy...

var simpleGit = require('simple-git')('/home/shanson/source');
simpleGit.add(gitfilename).commit('Post '+ filename + ' updated at '+ timestamp,[gitfilename]).push();

where gitfilename is the relative path to my file in my repository.

Authenticating with GitHub unattended is slightly trickier. I made a SSH key without a passphrase that I use only for pushing to this one repository. I found the instructions Deploying Private Repos Automatically with Github pretty clear.

UPDATE I found when I was committing only one file this worked fine, but with a real-life update where 5 files get changed at once, not all the git commands would be carried out. There are always one or two not committed or not pushed. Probably a sync/async issue. For my own purposes I can get around this by running git every 5 minutes with cron, but that's not a real solution.