Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87f562b

Browse files
committed
Here comes Github.js.
It's a higher-level wrapper around the Github API. And it's simple.
0 parents  commit 87f562b

File tree

11 files changed

+7360
-0
lines changed

11 files changed

+7360
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_site/*
2+
.DS_Store

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Github.js
2+
=============
3+
4+
Ever wanted to store a file on Github right from the browser? Here you are:
5+
6+
```js
7+
var github = new Github({
8+
username: "YOU_USER",
9+
password: "YOUR_PASSWORD",
10+
auth: "basic"
11+
});
12+
13+
// Expose API for a given repository
14+
15+
var repo = github.getRepo(reponame);
16+
17+
// Store contents at a certain path (assumes UTF-8)
18+
// Files that doesn't exist are created on the fly.
19+
20+
repo.write('path/to/file', 'YOUR_NEW_CONTENTS', function(err) {
21+
22+
});
23+
```
24+
25+
Not only can you can write files, you can of course read them:
26+
27+
```js
28+
// Retrieve contents of a certain file (assumes UTF-8)
29+
30+
repo.read('path/to/file', function(err, data) {
31+
32+
});
33+
```

github.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Github.js 0.1.0
2+
// (c) 2012 Michael Aufreiter, Development Seed
3+
// Github.js is freely distributable under the MIT license.
4+
// For all details and documentation:
5+
// http://substance.io/michael/github
6+
7+
(function() {
8+
var Github;
9+
var API_URL = 'https://api.github.com';
10+
11+
// Github API
12+
// -------
13+
14+
Github = window.Github = function(options) {
15+
var username = options.username;
16+
var password = options.password;
17+
18+
// Util
19+
// -------
20+
21+
function _request(method, route, data, cb) {
22+
$.ajax({
23+
type: method,
24+
url: API_URL + route,
25+
data: JSON.stringify(data),
26+
dataType: 'json',
27+
contentType: 'application/x-www-form-urlencoded',
28+
success: function(res) { cb(null, res); },
29+
error: function(err) { cb(err); },
30+
headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) }
31+
});
32+
}
33+
34+
// USER API
35+
// -------
36+
37+
Github.User = function(options) {
38+
this.username = options.username;
39+
40+
// TODO: implement
41+
};
42+
43+
// Repository API
44+
// -------
45+
46+
Github.Repository = function(options) {
47+
48+
// Show repository information
49+
// -------
50+
51+
var repoPath = "/repos/" + username + "/" + options.name;
52+
53+
this.show = function(cb) {
54+
_request("GET", repoPath, null, function(err, res) {
55+
cb();
56+
});
57+
};
58+
59+
60+
// Read file at given path
61+
// -------
62+
63+
this.read = function(path, cb) {
64+
65+
};
66+
67+
// Write file contents on a given path
68+
// -------
69+
70+
this.write = function(path, content, cb) {
71+
72+
// Get latest commit from master
73+
function getLatestCommit(cb) {
74+
_request("GET", repoPath + "/git/refs/heads/master", null, function(err, res) {
75+
if (err) return cb(err);
76+
cb(null, res.object.sha);
77+
});
78+
}
79+
80+
// Retrieve the tree a commit points to
81+
82+
function getTree(commit, cb) {
83+
_request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
84+
if (err) return cb(err);
85+
cb(null, res.sha);
86+
});
87+
}
88+
89+
// Post a new blob object, getting a blob SHA back
90+
91+
function postBlob(content, cb) {
92+
var data = {
93+
"content": "Content of the blob",
94+
"encoding": "utf-8"
95+
};
96+
_request("POST", repoPath + "/git/blobs", data, function(err, res) {
97+
if (err) return cb(err);
98+
cb(null, res.sha);
99+
});
100+
}
101+
102+
// Post a new tree object having a file path pointer replaced
103+
// with a new blob SHA getting a tree SHA back
104+
105+
function postTree(baseTree, path, blob, cb) {
106+
var data = {
107+
"base_tree": baseTree,
108+
"tree": [
109+
{
110+
"path": path,
111+
"mode": "100644",
112+
"type": "blob",
113+
"sha": blob
114+
}
115+
]
116+
};
117+
_request("POST", repoPath + "/git/trees", data, function(err, res) {
118+
if (err) return cb(err);
119+
cb(null, res.sha);
120+
});
121+
};
122+
123+
// Create a new commit object with the current commit SHA as the parent
124+
// and the new tree SHA, getting a commit SHA back
125+
126+
function createCommit(parent, tree, cb) {
127+
var data = {
128+
"message": "Spooky. Isn't it?",
129+
"author": {
130+
"name": "Ghost"
131+
},
132+
"parents": [
133+
parent
134+
],
135+
"tree": tree
136+
};
137+
138+
_request("POST", repoPath + "/git/commits", data, function(err, res) {
139+
if (err) return cb(err);
140+
cb(null, res.sha);
141+
});
142+
}
143+
144+
// Update the reference of your head to point to the new commit SHA
145+
146+
function updateHead(commit, cb) {
147+
_request("PATCH", repoPath + "/git/refs/heads/master", { "sha": commit }, function(err, res) {
148+
cb(err);
149+
});
150+
}
151+
152+
// Write it.
153+
154+
getLatestCommit(function(err, latestCommit) {
155+
getTree(latestCommit, function(err, tree) {
156+
postBlob("It's supposed to be content, baby!", function(err, blob) {
157+
postTree(tree, path, blob, function(err, tree) {
158+
createCommit(latestCommit, tree, function(err, commit) {
159+
updateHead(commit, function(err) {
160+
cb(err);
161+
});
162+
});
163+
});
164+
});
165+
});
166+
});
167+
};
168+
};
169+
170+
// Top Level API
171+
// -------
172+
173+
this.getRepo = function(repo) {
174+
return new Github.Repository({name: repo});
175+
};
176+
177+
this.getUser = function() {
178+
return new Github.User({name: repo});
179+
};
180+
};
181+
}).call(this);

test/.DS_Store

6 KB
Binary file not shown.

test/github-test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(function() {
2+
3+
// Helpers
4+
// -------
5+
6+
function randomString(length) {
7+
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
8+
var res = '';
9+
for (var i=0; i<length; i++) {
10+
var rnum = Math.floor(Math.random() * chars.length);
11+
res += chars.substring(rnum,rnum+1);
12+
}
13+
return res;
14+
}
15+
16+
var USERNAME = "github-api-test",
17+
PASSWORD = "api-test-12",
18+
REPO = "github-api-test";
19+
20+
suite('Github', function(){
21+
setup(function() {
22+
// ...
23+
});
24+
25+
suite('#Github API', function(){
26+
test('should return repo information', function() {
27+
var github = new Github({username: USERNAME, password: PASSWORD, auth: "basic"});
28+
var repo = github.getRepo(REPO);
29+
30+
repo.show(function(err, repo) {
31+
// TODO: implement
32+
});
33+
});
34+
35+
test('should be capable of writing files', function() {
36+
var github = new Github({username: USERNAME, password: PASSWORD, auth: "basic"});
37+
var repo = github.getRepo(REPO);
38+
39+
repo.write(randomString(20) + ".md", "h3. Hello World!", function(err, commit) {
40+
// TODO:
41+
42+
repo.write("path/to/"+randomString(20) + ".md", "h3. Hello World!", function(err, commit) {
43+
// TODO:
44+
});
45+
});
46+
});
47+
48+
test('should be capable of wrting files to directories that do not yet exist', function() {
49+
50+
});
51+
52+
});
53+
});
54+
55+
}).call(this);
56+
57+

test/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2+
"http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Commit.js Tests
7+
sts</title>
8+
<link rel="stylesheet" href="vendor/mocha.css" />
9+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
10+
<script src="vendor/mocha.js"></script>
11+
<script src="vendor/chai.js"></script>
12+
<script src="vendor/base64.js"></script>
13+
14+
<script>
15+
window.assert = chai.assert;
16+
mocha.setup('tdd');
17+
$(function () {
18+
mocha
19+
.run()
20+
.globals(['Github']) // acceptable globals
21+
});
22+
</script>
23+
24+
<!-- include dependency files here... -->
25+
26+
<!-- include source files here... -->
27+
<script src="../github.js"></script>
28+
29+
<!-- include test files here... -->
30+
<script src="github-test.js"></script>
31+
32+
</head>
33+
<body>
34+
<div id="mocha"></div>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)