8

Imagine a static web site compiled with jekyll and hosted on github pages. Is it possible to have some javascript on this page that asks the user some input, and then "git push" this input into some github repository ?

In other words, how to write in javascript the following program:

  • Ask the user for his name, put the name in variable s
  • Download (or pull) from a given github repo a text file "allnames.txt"
  • Add s at the end of allnames.txt
  • Commit and push the file "allnames.txt" back into github

Of course, I understand the potential security risks, because the javascript code would need to embed private ssh keys. But this would be a fun way to add dynamicity to a static web site, and may be someone knows of some solution to make this secure ?

Thank you !

4 Answers 4

3

Check out Staticman

From the Staticman site:

Staticman handles user-generated content for you and transforms it into data files that sit in your GitHub repository, along with the rest of your content.

Staticman works perfectly with Jekyll sites hosted on GitHub Pages, as a push to your main branch will regenerate the site automatically. If you want to moderate entries before they are published, a pull request will be created for your approval; otherwise, files will be pushed to your main branch straight away.

Sign up to request clarification or add additional context in comments.

2 Comments

Great ! It gives everything I need so why reinvent the wheel ? It requires an external web service tho but it looks like there is no easy option without... Thx !
@xtof54 You may follow the steps on Staticman's website for a quick start. Staticman consists of a GitHub bot (a GitHub user named "staticmanapp") and an API server (api.staticman.net). Within a blogging framework, it suffices to add Staticman to the site config and customize staticman.yml. At the GitHub user level, simply invite the GitHub bot and programmatically accept it by a GET request (details in the doc). You may even consider build your own Staticman instance if you want.
2

If you know the internals of git (blobs, trees, commits), then you should be able to implement a solution with the GitHub REST API. This is not going to be done in 5 minutes. As for security, you should be able to use OAuth, so no need for private ssh key.

Have a look at: https://developer.github.com/v3/git/

2 Comments

Thanks, I should definitely have a deeper look at the REST API; I'm not sooo sure OAuth is adequate in the use case I have in mind, but well, both of you are definitely right, thanks !
I just found github.com/voxpelli/node-github-publish, which is very close to what you want to do.
2

Alternative to using GitHub APIs, you can use one of the git clients written in JavaScript to do the actions you described above within the browser.

Here are a few examples:

https://github.com/creationix/js-git

https://github.com/danlucraft/git.js

1 Comment

Thanks a lot for the links ! However, it looks like git.js only works in-browser through http://, which is not enough for pushing, and js-github may eventually write to the repository with appropriate auth token... It's definitely not so easy to succeed, but both your answers give very valuable path to investigate; thanks a lot !
1

An old post from Ivan Zuzak describes how he uses Github comment to build a dynamic commenting system (source). I recently recreated the system he described (using Github issues for comments) using some extra services: Zapier, the Zapier Github integration and the free version of Zoho forms. This enabled me let a custom webform submit data to Github issues, through Zapier (so without asking users to create a Github account).

I used the Github issues as comments on my blog, but you might be able to use the same solution for something else (like your list of names).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.