Skip to content

Instantly share code, notes, and snippets.

@scripting
Created November 22, 2017 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/98a6ab727ccacac41029b50e07b3c4e2 to your computer and use it in GitHub Desktop.
Save scripting/98a6ab727ccacac41029b50e07b3c4e2 to your computer and use it in GitHub Desktop.
A simple JavaScript demo app, running in Node, creates a new file in a repo on GitHub using the REST API
const request = require ("request");
const fs = require ("fs");
var myPost = {
username: "scripting",
repo: "test1",
path: "ideas/plot.html",
content: "<!DOCTYPE html>\n<html><body>A programmer writes a demo app. Everyone lives happily ever after.</body></html>",
type: "text/html",
committer: {
name: "Dave Winer",
email: "dave.winer@gmail.com"
},
message: "Yet another testing change",
userAgent: "Dave's GitHub Upload Demo App"
};
fs.readFile ("config.json", function (err, data) {
var config = JSON.parse (data);
var bodyStruct = {
message: myPost.message,
committer: {
name: myPost.committer.name,
email: myPost.committer.email
},
content: new Buffer (myPost.content).toString ('base64')
};
var username = myPost.username;
var url = "https://" + username + ":" + config.password + "@api.github.com/repos/" + username + "/" + myPost.repo + "/contents/" + myPost.path;
const theRequest = {
method: "PUT",
url: url,
body: JSON.stringify (bodyStruct),
headers: {
"User-Agent": myPost.userAgent,
"Content-Type": myPost.type
}
};
request (theRequest, function (err, response, body) {
if (err) {
console.log (err.message);
}
else {
console.log (JSON.stringify (response, undefined, 4));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment