Skip to content

Commit 37084d7

Browse files
knsh14clayreimann
authored andcommitted
feature(gist): add comment-related features to Gist
* Add API to list all of a gist's comments * Allow CRUD-ing comments on gists
1 parent da54c02 commit 37084d7

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

lib/Gist.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,63 @@ class Gist extends Requestable {
107107
isStarred(cb) {
108108
return this._request204or404(`/gists/${this.__id}/star`, null, cb);
109109
}
110+
111+
112+
113+
/**
114+
* List the comments on a gist
115+
* @see https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
116+
* @param {Requestable.callback} [cb] - will receive the array of comments
117+
* @return {Promise} - the promise for the http request
118+
*/
119+
listComments(cb) {
120+
return this._requestAllPages(`/gists/${this.__id}/comments`, null, cb);
121+
}
122+
123+
/**
124+
* Fetch a comment of the gist
125+
* @see https://developer.github.com/v3/gists/comments/#get-a-single-comment
126+
* @param {number} comment - the id of the comment
127+
* @param {Requestable.callback} [cb] - will receive the comment
128+
* @return {Promise} - the Promise for the http request
129+
*/
130+
getComment(comment, cb) {
131+
return this._request('GET', `/gists/${this.__id}/comments/${comment}`, null, cb);
132+
}
133+
134+
/**
135+
* Create Comment to a gist.
136+
* @see https://developer.github.com/v3/gists/comments/#create-a-comment
137+
* @param {string} comment - the comment to add
138+
* @param {Requestable.callback} [cb] - the function that receives the API result
139+
* @return {Promise} - the Promise for the http request
140+
*/
141+
createComment(comment, cb) {
142+
return this._request('POST', `/gists/${this.__id}/comments`, {body: comment}, cb);
143+
}
144+
145+
/**
146+
* Edit a Gist Comment
147+
* @see https://developer.github.com/v3/gists/comments/#edit-a-comment
148+
* @param {number} comment - the id of the comment
149+
* @param {string} body - the new comment
150+
* @param {Requestable.callback} [cb] - will receive the modified issue
151+
* @return {Promise} - the promise for the http request
152+
*/
153+
editComment(comment, body, cb) {
154+
return this._request('PATCH', `/gists/${this.__id}/comments/${comment}`, {body: body}, cb);
155+
}
156+
157+
/**
158+
* Delete a comment of a gist.
159+
* @see https://developer.github.com/v3/gists/comments/#delete-a-comment
160+
* @param {number} comment - the id of the comment
161+
* @param {Requestable.callback} [cb] - will receive true if the request succeeds
162+
* @return {Promise} - the Promise for the http request
163+
*/
164+
deleteComment(comment, cb) {
165+
return this._request('DELETE', `/gists/${this.__id}/comments/${comment}`, null, cb);
166+
}
110167
}
111168

112169
module.exports = Gist;

test/gist.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Gist', function() {
99
let gist;
1010
let gistId;
1111
let github;
12+
let commentId;
1213

1314
before(function() {
1415
github = new Github({
@@ -64,6 +65,40 @@ describe('Gist', function() {
6465
}));
6566
}));
6667
});
68+
69+
it('should create a comment a gist', function(done) {
70+
gist.createComment("Comment test", assertSuccessful(done, function(err, comment) {
71+
expect(comment).to.have.own('body', 'Comment test');
72+
commentId = comment.id
73+
done();
74+
}));
75+
});
76+
77+
it('should list comments', function(done) {
78+
gist.listComments(assertSuccessful(done, function(err, comments) {
79+
expect(comments).to.be.an.array();
80+
done();
81+
}));
82+
});
83+
84+
it('should get comment', function(done) {
85+
gist.getComment(commentId, assertSuccessful(done, function(err, issue) {
86+
expect(issue).to.have.own('id', commentId);
87+
done();
88+
}));
89+
});
90+
91+
it('should edit comment', function(done) {
92+
const newComment = 'new comment test';
93+
gist.editComment(commentId, newComment, assertSuccessful(done, function(err, comment) {
94+
expect(comment).to.have.own('body', newComment);
95+
done();
96+
}));
97+
});
98+
99+
it('should delete comment', function(done) {
100+
gist.deleteComment(commentId, assertSuccessful(done));
101+
});
67102
});
68103

69104
describe('deleting', function() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy