Skip to content

Commit 56245c3

Browse files
committed
support repository api
1 parent 78f5892 commit 56245c3

File tree

6 files changed

+470
-18
lines changed

6 files changed

+470
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test-cov:
2323
test-coveralls:
2424
@$(MAKE) test
2525
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
26-
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
26+
@-$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
2727

2828
test-all: test test-cov
2929

README.md

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,117 @@ Milestone.prototype.list = function (params, callback);
6161
* Create a milestone.
6262
*
6363
* @param {Object} params
64-
* - {Number} id (required) - The ID of a project
65-
* - {String} title (required) - The title of an milestone
66-
* - {String} [description] (optional) - The description of the milestone
67-
* - {String} [due_date] (optional) - The due date of the milestone
64+
* - {Number} id The ID of a project
65+
* - {String} title The title of an milestone
66+
* - {String} [description] The description of the milestone
67+
* - {String} [due_date] The due date of the milestone
6868
* @param {Function(err, row)} callback
6969
*/
7070
Milestone.prototype.create = function (params, callback);
7171

7272
/**
7373
* Update a milestone.
74+
7475
* @param {Object} params
75-
* - {Number} id (required) - The ID of a project
76-
* - {Number} milestone_id (required) - The ID of a project milestone
77-
* - {String} title (required) - The title of an milestone
78-
* - {String} [description] (optional) - The description of the milestone
79-
* - {String} [due_date] (optional) - The due date of the milestone
80-
* - {String} [closed] (optional) - The status of the milestone
76+
* - {Number} id The ID of a project
77+
* - {Number} milestone_id The ID of a project milestone
78+
* - {String} title The title of an milestone
79+
* - {String} [description] The description of the milestone
80+
* - {String} [due_date] The due date of the milestone
81+
* - {String} [closed] The status of the milestone
8182
* @param {Function(err, row)} callback
8283
*/
8384
Milestone.prototype.update = function (params, callback);
8485
```
8586

87+
### Repository
88+
89+
```js
90+
/**
91+
* Get a list of repository branches from a project, sorted by name alphabetically.
92+
*
93+
* @param {Object} params
94+
* - {String} id The ID of a project
95+
* @param {Function} callback
96+
*/
97+
Repository.prototype.getBranches = function (params, callback);
98+
99+
/**
100+
* Protects a single project repository branch.
101+
* This is an idempotent function, protecting an already protected repository branch
102+
* still returns a 200 Ok status code.
103+
*
104+
* @param {Object} params
105+
* - {String} id The ID of a project
106+
* - {String} branch The name of the branch
107+
* @param {Function} callback
108+
*/
109+
Repository.prototype.protectBranch = function (params, callback);
110+
111+
/**
112+
* Unprotects a single project repository branch.
113+
* This is an idempotent function, unprotecting an already unprotected repository branch
114+
* still returns a 200 Ok status code.
115+
*
116+
* @param {Object} params
117+
* - {String} id The ID of a project
118+
* - {String} branch The name of the branch
119+
* @param {Function} callback
120+
*/
121+
Repository.prototype.unprotectBranch = function (params, callback);
122+
123+
/**
124+
* Get a single project repository branch.
125+
*
126+
* @param {Object} params
127+
* - {String} id The ID of a project
128+
* - {String} branch The name of the branch
129+
* @param {Function} callback
130+
*/
131+
Repository.prototype.getBranch = function (params, callback);
132+
133+
/**
134+
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
135+
*
136+
* @param {Object} params
137+
* - {String} id The ID of a project
138+
* @param {Function} callback
139+
*/
140+
Repository.prototype.getTags = function (params, callback);
141+
142+
/**
143+
* Get a list of repository commits in a project.
144+
*
145+
* @param {Object} params
146+
* - {String} id The ID of a project
147+
* - {String} [ref_name] The name of a repository branch or tag or if not given the default branch
148+
* @param {Function} callback
149+
*/
150+
Repository.prototype.getCommits = function (params, callback);
151+
152+
/**
153+
* Get a list of repository files and directories in a project.
154+
*
155+
* @param {Object} params
156+
* - {String} id The ID of a project
157+
* - {String} [path] The path inside repository, default is '/'. Used to get contend of subdirectories. e.g.: `test`
158+
* - {String} [ref_name] The name of a repository branch or tag or if not given the default branch
159+
* @param {Function} callback
160+
*/
161+
Repository.prototype.getTree = function (params, callback);
162+
163+
/**
164+
* Get the raw file contents for a file.
165+
*
166+
* @param {Object} params
167+
* - {String} id The ID of a project
168+
* - {String} sha The commit or branch name
169+
* - {String} filepath The path the file
170+
* @param {Function} callback
171+
*/
172+
Repository.prototype.getBlob = function (params, callback);
173+
```
174+
86175
## Authors
87176

88177
```bash

lib/gitlab.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* gitlab - lib/gitlab.js
3-
* Copyright(c) 2012 fengmk2 <[email protected]>
3+
* Copyright(c) 2012 - 2013 fengmk2 <[email protected]>
44
* MIT Licensed
55
*/
66

@@ -18,6 +18,7 @@ var Member = require('./member');
1818
var Hook = require('./hook');
1919
var Issue = require('./issue');
2020
var User = require('./user');
21+
var Repository = require('./repository');
2122

2223
/**
2324
* Create a gitlab API client.
@@ -36,9 +37,13 @@ function Client(options) {
3637
this.hooks = new Hook(this);
3738
this.issues = new Issue(this);
3839
this.users = new User(this);
40+
this.repository = new Repository(this);
3941
}
4042

4143
Client.prototype.request = function (method, pathname, data, callback) {
44+
var dataType = data.dataType || 'json';
45+
delete data.dataType;
46+
4247
var keys = pathname.match(/\:\w+/g);
4348
if (keys) {
4449
for (var i = 0; i < keys.length; i++) {
@@ -58,13 +63,10 @@ Client.prototype.request = function (method, pathname, data, callback) {
5863
var url = this.api + pathname;
5964
urllib.request(url, {
6065
type: method,
61-
dataType: 'json',
66+
dataType: dataType,
6267
data: data,
6368
}, function (err, result, res) {
64-
if (Buffer.isBuffer(result)) {
65-
result = result.toString();
66-
}
67-
debug('%s %s %j: status: %s, result: %j, err: %j', method, url, data, res.statusCode, result, err);
69+
// debug('%s %s %j: status: %s, result: %j, err: %j', method, url, data, res.statusCode, result, err);
6870
if (err) {
6971
if (err.name === 'SyntaxError') {
7072
err.name = 'Gitlab' + res.statusCode + 'Error';
@@ -75,15 +77,21 @@ Client.prototype.request = function (method, pathname, data, callback) {
7577
err.name = 'Gitlab' + err.name;
7678
}
7779
err.headers = res && res.headers;
80+
if (Buffer.isBuffer(result)) {
81+
result = result.toString();
82+
}
7883
err.data = { resBody: result, requestData: data };
7984
err.statusCode = res && res.statusCode;
8085
err.method = method;
8186
err.url = url;
8287
return callback(err);
8388
}
8489
if (res.statusCode !== 200 && res.statusCode !== 201) {
85-
err = new Error(result ? result.message : 'Unknow Error ' + res.statusCode);
90+
err = new Error(result && result.message ? result.message : 'Unknow Error ' + res.statusCode);
8691
err.name = 'Gitlab' + res.statusCode + 'Error';
92+
if (Buffer.isBuffer(result)) {
93+
result = result.toString();
94+
}
8795
err.data = { resBody: result, requestData: data };
8896
err.headers = res.headers;
8997
err.statusCode = res.statusCode;

lib/repository.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*!
2+
* gitlab - lib/repository.js
3+
* Copyright(c) 2013 fengmk2 <[email protected]>
4+
* MIT Licensed
5+
*/
6+
7+
"use strict";
8+
9+
/**
10+
* Module dependencies.
11+
*/
12+
13+
var util = require('util');
14+
var Resource = require('./resource');
15+
16+
function Repository(client) {
17+
Resource.call(this, client, '/projects/:id/repository/:type', 'branch');
18+
}
19+
util.inherits(Repository, Resource);
20+
21+
module.exports = Repository;
22+
23+
/**
24+
* Get a list of repository branches from a project, sorted by name alphabetically.
25+
*
26+
* @param {Object} params
27+
* - {String} id The ID of a project
28+
* @param {Function} callback
29+
*/
30+
Repository.prototype.getBranches = function (params, callback) {
31+
params.type = 'branches';
32+
this.list(params, callback);
33+
};
34+
35+
/**
36+
* Protects a single project repository branch.
37+
* This is an idempotent function, protecting an already protected repository branch
38+
* still returns a 200 Ok status code.
39+
*
40+
* @param {Object} params
41+
* - {String} id The ID of a project
42+
* - {String} branch The name of the branch
43+
* @param {Function} callback
44+
*/
45+
Repository.prototype.protectBranch = function (params, callback) {
46+
params.type = 'branches';
47+
this.client.request('PUT', this.path + '/:branch/protect', params, callback);
48+
};
49+
50+
/**
51+
* Unprotects a single project repository branch.
52+
* This is an idempotent function, unprotecting an already unprotected repository branch
53+
* still returns a 200 Ok status code.
54+
*
55+
* @param {Object} params
56+
* - {String} id The ID of a project
57+
* - {String} branch The name of the branch
58+
* @param {Function} callback
59+
*/
60+
Repository.prototype.unprotectBranch = function (params, callback) {
61+
params.type = 'branches';
62+
this.client.request('PUT', this.path + '/:branch/unprotect', params, callback);
63+
};
64+
65+
/**
66+
* Get a single project repository branch.
67+
*
68+
* @param {Object} params
69+
* - {String} id The ID of a project
70+
* - {String} branch The name of the branch
71+
* @param {Function} callback
72+
*/
73+
Repository.prototype.getBranch = function (params, callback) {
74+
params.type = 'branches';
75+
this.client.request('get', this.path + '/:branch', params, callback);
76+
};
77+
78+
/**
79+
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
80+
*
81+
* @param {Object} params
82+
* - {String} id The ID of a project
83+
* @param {Function} callback
84+
*/
85+
Repository.prototype.getTags = function (params, callback) {
86+
params.type = 'tags';
87+
this.list(params, callback);
88+
};
89+
90+
/**
91+
* Get a list of repository commits in a project.
92+
*
93+
* @param {Object} params
94+
* - {String} id The ID of a project
95+
* - {String} [ref_name] The name of a repository branch or tag or if not given the default branch
96+
* @param {Function} callback
97+
*/
98+
Repository.prototype.getCommits = function (params, callback) {
99+
params.type = 'commits';
100+
this.list(params, callback);
101+
};
102+
103+
/**
104+
* Get a list of repository files and directories in a project.
105+
*
106+
* @param {Object} params
107+
* - {String} id The ID of a project
108+
* - {String} [path] The path inside repository, default is '/'. Used to get contend of subdirectories. e.g.: `test`
109+
* - {String} [ref_name] The name of a repository branch or tag or if not given the default branch
110+
* @param {Function} callback
111+
*/
112+
Repository.prototype.getTree = function (params, callback) {
113+
params.type = 'tree';
114+
this.list(params, callback);
115+
};
116+
117+
/**
118+
* Get the raw file contents for a file.
119+
*
120+
* @param {Object} params
121+
* - {String} id The ID of a project
122+
* - {String} sha The commit or branch name
123+
* - {String} filepath The path the file
124+
* @param {Function} callback
125+
*/
126+
Repository.prototype.getBlob = function (params, callback) {
127+
params.type = 'commits';
128+
params.dataType = 'buffer';
129+
this.client.request('get', this.path + '/:sha/blob', params, callback);
130+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"should": "*",
17+
"pedding": "*",
1718
"blanket": "*",
1819
"travis-cov": "*",
1920
"coveralls": "*",

0 commit comments

Comments
 (0)