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 ) ;
0 commit comments