/
github.js
141 lines (129 loc) · 5.24 KB
/
github.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright (c) 2013-2018 Memba Sarl. All rights reserved.
* Sources at https://github.com/Memba
*/
/* jshint node: true */
'use strict';
var request = require('request');
var config = require('../config');
var version = require('../../package.json').version;
var github = 'https://api.github.com';
/*
var authentication = {
user: process.env.USERNAME,
pass: process.env.PASSWORD
};
*/
// Set a personal access token at https://github.com/settings/tokens
var headers = {
Authorization: 'token ' + process.env.TOKEN,
'user-agent': 'Memba.Blog/' + version
};
var repo = config.get('github:repository');
var branch = config.get('github:branch');
module.exports = {
/**
* Get content from github
* @see https://developer.github.com/v3/repos/contents/#get-contents
* @param path
* @param callback
*/
getContent: function (path, callback) {
var uri = github + '/repos/' + repo + '/contents/' + path;
var qs = { ref: branch };
request({ method: 'GET', uri: uri, headers: headers, /*auth: authentication,*/ json: true, qs: qs }, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body); // json: true in request options ensures that body is parsed to return an object
} else {
callback(error || new Error('Getting Github content returned status ' + response.statusCode + (body && body.message ? ': ' + body.message : '')));
}
});
},
/**
* Create content into github
* @see https://developer.github.com/v3/repos/contents/#create-a-file
* @param path
* @param content
* @param callback
*/
createContent: function (path, content, callback) {
var uri = github + '/repos/' + repo + '/contents/' + path;
var data = {
branch: branch,
content: Buffer.from(content).toString('base64'),
message: 'System creation'
};
// Note content creation requires PUT instead of POST
request({ method: 'PUT', uri: uri, headers: headers, /*auth: authentication,*/ json: true, body: data }, function (error, response, body) {
if (!error && response.statusCode === 201) {
callback(null, body);
} else {
callback(error || new Error('Creating Github content returned status ' + response.statusCode + (body && body.message ? ': ' + body.message : '')));
}
});
},
/**
* Update content into github
* @see https://developer.github.com/v3/repos/contents/#update-a-file
* @param path
* @param content
* @param sha
* @param callback
*/
updateContent: function (path, content, sha, callback) {
var uri = github + '/repos/' + repo + '/contents/' + path;
var data = {
branch: branch,
content: Buffer.from(content).toString('base64'),
message: 'System update',
sha: sha
};
request({ method: 'PUT', uri: uri, headers: headers, /*auth: authentication,*/ json: true, body: data }, function (error, response, body) {
if (!error && (response.statusCode === 200 || response.statusCode === 201)) {
// updating content returns 200, except when updating deleted content which return 201 (like creation)
callback(null, body);
} else {
callback(error || new Error('Updating Github content returned status ' + response.statusCode + (body && body.message ? ': ' + body.message : '')));
}
});
},
/**
* Delete content from github
* @see https://developer.github.com/v3/repos/contents/#delete-a-file
* @param path
* @param sha
* @param callback
*/
deleteContent: function (path, sha, callback) {
var uri = github + '/repos/' + repo + '/contents/' + path;
var data = {
branch: branch,
message: 'System deletion',
sha: sha
};
request({ method: 'DELETE', uri: uri, headers: headers, /*auth: authentication,*/ json: true, body: data }, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(error || new Error('Deleting Github content returned status ' + response.statusCode + (body && body.message ? ': ' + body.message : '')));
}
});
},
/**
* Gets commits (especially to find author and date)
* @see https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository
* @param path
* @param callback
*/
getCommits: function (path, callback) {
var uri = github + '/repos/' + repo + '/commits';
var qs = { path: path, sha: branch };
request({ method: 'GET', uri: uri, headers: headers, /*auth: authentication,*/ json: true, qs: qs }, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(error || new Error('Getting Github commits returned status ' + response.statusCode + (body && body.message ? ': ' + body.message : '')));
}
});
}
};