Skip to content

Commit 471a6b7

Browse files
committed
Added code search API method. Original idea by @zamith. Fixes octokit#228.
1 parent 3ae6ddf commit 471a6b7

File tree

7 files changed

+84
-5
lines changed

7 files changed

+84
-5
lines changed

api/v3.0.0/issues.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ var issues = module.exports = {
173173
* - body (String): Optional.
174174
* - assignee (String): Optional. Login for the user that this issue should be assigned to.
175175
* - milestone (Number): Optional. Milestone to associate this issue with. Validation rule: ` ^[0-9]+$ `.
176-
* - labels (Json): Required. Array of strings - Labels to associate with this issue.
176+
* - labels (Json): Optional. Array of strings - Labels to associate with this issue.
177177
**/
178178
this.create = function(msg, block, callback) {
179179
var self = this;

api/v3.0.0/orgs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ var orgs = module.exports = {
159159
* - org (String): Required.
160160
* - page (Number): Optional. Page number of the results to fetch. Validation rule: ` ^[0-9]+$ `.
161161
* - per_page (Number): Optional. A custom page size up to 100. Default is 30. Validation rule: ` ^[0-9]+$ `.
162-
* - filter (String): Optional. Validation rule: ` (all|2fa_disabled) `, Default: ` all `.
162+
* - filter (String): Optional. Validation rule: ` ^(all|2fa_disabled)$ `.
163163
**/
164164
this.getMembers = function(msg, block, callback) {
165165
var self = this;

api/v3.0.0/pullRequests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var pullRequests = module.exports = {
3030
* - user (String): Required.
3131
* - repo (String): Required.
3232
* - state (String): Optional. open, closed, or all Validation rule: ` ^(open|closed|all)$ `.
33-
* - head (String): Optional. Filter pulls by head user and branch name in the format of user:ref-name. Example: github:new-script-format.
34-
* - base (String): Optional. Filter pulls by base branch name. Example: gh-pages.
33+
* - head (String): Required. The branch (or git ref) where your changes are implemented.
34+
* - base (String): Required. The branch (or git ref) you want your changes pulled into. This should be an existing branch on the current repository. You cannot submit a pull request to one repo that requests a merge to a base of another repo.
3535
* - page (Number): Optional. Page number of the results to fetch. Validation rule: ` ^[0-9]+$ `.
3636
* - per_page (Number): Optional. A custom page size up to 100. Default is 30. Validation rule: ` ^[0-9]+$ `.
3737
* - sort (String): Optional. Possible values are: `created`, `updated`, `popularity`, `long-running`, Default: `created` Validation rule: ` ^(created|updated|popularity|long-running)$ `.

api/v3.0.0/routes.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,24 @@
35593559
},
35603560

35613561
"search": {
3562+
"code": {
3563+
"url": "/search/code",
3564+
"method": "GET",
3565+
"params": {
3566+
"$q": null,
3567+
"sort": {
3568+
"type": "String",
3569+
"required": false,
3570+
"validation": "^indexed$",
3571+
"invalidmsg": "indexed only",
3572+
"description": "indexed only"
3573+
},
3574+
"$order": null,
3575+
"$page": null,
3576+
"$per_page": null
3577+
}
3578+
},
3579+
35623580
"issues": {
35633581
"url": "/search/issues",
35643582
"method": "GET",

api/v3.0.0/search.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,50 @@ var search = module.exports = {
1919
};
2020

2121
(function() {
22+
/** section: github
23+
* search#code(msg, callback) -> null
24+
* - msg (Object): Object that contains the parameters and their values to be sent to the server.
25+
* - callback (Function): function to call when the request is finished with an error as first argument and result data as second argument.
26+
*
27+
* ##### Params on the `msg` object:
28+
*
29+
* - headers (Object): Optional. Key/ value pair of request headers to pass along with the HTTP request. Valid headers are: 'If-Modified-Since', 'If-None-Match', 'Cookie', 'User-Agent', 'Accept', 'X-GitHub-OTP'.
30+
* - q (String): Required. Search Term
31+
* - sort (String): Optional. indexed only Validation rule: ` ^indexed$ `.
32+
* - order (String): Optional. asc or desc Validation rule: ` ^(asc|desc)$ `.
33+
* - page (Number): Optional. Page number of the results to fetch. Validation rule: ` ^[0-9]+$ `.
34+
* - per_page (Number): Optional. A custom page size up to 100. Default is 30. Validation rule: ` ^[0-9]+$ `.
35+
**/
36+
this.code = function(msg, block, callback) {
37+
var self = this;
38+
this.client.httpSend(msg, block, function(err, res) {
39+
if (err)
40+
return self.sendError(err, null, msg, callback);
41+
42+
var ret;
43+
try {
44+
ret = res.data && JSON.parse(res.data);
45+
}
46+
catch (ex) {
47+
if (callback)
48+
callback(new error.InternalServerError(ex.message), res);
49+
return;
50+
}
51+
52+
if (!ret)
53+
ret = {};
54+
if (!ret.meta)
55+
ret.meta = {};
56+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
57+
if (res.headers[header])
58+
ret.meta[header] = res.headers[header];
59+
});
60+
61+
if (callback)
62+
callback(null, ret);
63+
});
64+
};
65+
2266
/** section: github
2367
* search#issues(msg, callback) -> null
2468
* - msg (Object): Object that contains the parameters and their values to be sent to the server.

api/v3.0.0/searchTest.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,26 @@ describe("[search]", function() {
2626
});*/
2727
});
2828

29+
it("should successfully execute GET /search/code/:q (code)", function(next) {
30+
client.search.code(
31+
{
32+
q: ['test', 'repo:mikedeboertest/1423836276146'].join('+')
33+
},
34+
function(err, res) {
35+
Assert.equal(err, null);
36+
Assert.equal(res.items.length, 1);
37+
var file = res.items[0];
38+
Assert.equal(file.name, "TEST.md");
39+
40+
next();
41+
}
42+
);
43+
});
44+
2945
it("should successfully execute GET /search/issues/:q (issues)", function(next) {
3046
client.search.issues(
3147
{
32-
q: ['macaroni', 'repo:mikedeboertest/node_chat', 'state:open'].join('+')
48+
q: ['debate', 'repo:mikedeboertest/node_chat', 'state:open'].join('+')
3349
},
3450
function(err, res) {
3551
Assert.equal(err, null);

api/v3.0.0/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var user = module.exports = {
5858
callback(null, ret);
5959
});
6060
};
61+
6162
/** section: github
6263
* user#getFrom(msg, callback) -> null
6364
* - msg (Object): Object that contains the parameters and their values to be sent to the server.

0 commit comments

Comments
 (0)