From 63129121b9e9cfcc10fa10b0c9cadd5b74907617 Mon Sep 17 00:00:00 2001 From: go_bucket Date: Fri, 6 Dec 2019 08:47:25 +0530 Subject: [PATCH 1/3] adding sort and order --- index.js | 2 +- lib/search.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0588d36..b1cc395 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ module.exports = (project, options) => { } if (options.projects !== undefined && project in options.projects) { - return search(options.projects[project].q, options.auth) + return search(options.projects[project].q, options.auth, options) } else { return search(`${project.includes('/') ? 'repo' : 'org'}:${project} state:open label:"good first issue"`, options.auth) } diff --git a/lib/search.js b/lib/search.js index f599547..775a9e5 100644 --- a/lib/search.js +++ b/lib/search.js @@ -14,7 +14,7 @@ const PAGE = 1 // page_number is 1-based index // MAX_PAGE_ALLOWED = Math.ceil(MAX_RESULTS_POSSIBLE / PER_PAGE) const MAX_PAGE_ALLOWED = 34 -async function search (q, auth) { +async function search (q, auth, options) { const octokit = auth ? Octokit({ auth }) : Octokit() const { data } = await octokit.search.issuesAndPullRequests(getSearchParams({ q })) @@ -24,8 +24,13 @@ async function search (q, auth) { const lastPageAllowed = pageCount > MAX_PAGE_ALLOWED ? MAX_PAGE_ALLOWED : pageCount // page_number is 1-based index const page = Math.floor(Math.random() * Math.floor(lastPageAllowed)) + 1 + let sort, order + if (options) { + sort = options.sort ? options.sort : SORT + order = options.order ? options.order : ORDER + } - const pageRes = await octokit.search.issuesAndPullRequests(getSearchParams({ q, page })) + const pageRes = await octokit.search.issuesAndPullRequests(getSearchParams({ q, page, sort, order })) return transform(pageRes.data) } else { return transform(data) From 5b21586d71c0576c96f9192f2f863985ce19733d Mon Sep 17 00:00:00 2001 From: Govind Date: Fri, 6 Dec 2019 12:00:01 +0530 Subject: [PATCH 2/3] Added sort and order to index and search and also added them to node.js example --- examples/node.js | 4 +++- index.js | 2 +- lib/search.js | 17 ++++++----------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/examples/node.js b/examples/node.js index 71fafc8..55e3427 100644 --- a/examples/node.js +++ b/examples/node.js @@ -8,7 +8,9 @@ const options = { q: 'org:nodejs is:issue is:open label:"good first issue"', description: "Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine." } - } + }, + sort: 'comments', + order: 'asc' } const log = async () => { diff --git a/index.js b/index.js index b1cc395..389f46f 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ module.exports = (project, options) => { } if (options.projects !== undefined && project in options.projects) { - return search(options.projects[project].q, options.auth, options) + return search(options.projects[project].q, options.auth, options.sort, options.order) } else { return search(`${project.includes('/') ? 'repo' : 'org'}:${project} state:open label:"good first issue"`, options.auth) } diff --git a/lib/search.js b/lib/search.js index 775a9e5..68e053f 100644 --- a/lib/search.js +++ b/lib/search.js @@ -14,7 +14,7 @@ const PAGE = 1 // page_number is 1-based index // MAX_PAGE_ALLOWED = Math.ceil(MAX_RESULTS_POSSIBLE / PER_PAGE) const MAX_PAGE_ALLOWED = 34 -async function search (q, auth, options) { +async function search (q, auth, sort, order) { const octokit = auth ? Octokit({ auth }) : Octokit() const { data } = await octokit.search.issuesAndPullRequests(getSearchParams({ q })) @@ -24,11 +24,6 @@ async function search (q, auth, options) { const lastPageAllowed = pageCount > MAX_PAGE_ALLOWED ? MAX_PAGE_ALLOWED : pageCount // page_number is 1-based index const page = Math.floor(Math.random() * Math.floor(lastPageAllowed)) + 1 - let sort, order - if (options) { - sort = options.sort ? options.sort : SORT - order = options.order ? options.order : ORDER - } const pageRes = await octokit.search.issuesAndPullRequests(getSearchParams({ q, page, sort, order })) return transform(pageRes.data) @@ -39,11 +34,11 @@ async function search (q, auth, options) { function getSearchParams (searchParams) { return { - q: searchParams['q'], - sort: searchParams['sort'] || SORT, - order: searchParams['order'] || ORDER, - per_page: searchParams['per_page'] || PER_PAGE, - page: searchParams['page'] || PAGE + q: searchParams.q, + sort: searchParams.sort || SORT, + order: searchParams.order || ORDER, + per_page: searchParams.per_page || PER_PAGE, + page: searchParams.page || PAGE } } From e6ddc608010804d744ee8c53b6ef1a8a24dfb9d8 Mon Sep 17 00:00:00 2001 From: Govind S Date: Fri, 6 Dec 2019 12:15:11 +0530 Subject: [PATCH 3/3] Updated example in readme to show sort and order --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0eaac5a..3511e3c 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,7 @@ gfi('golang/dep') console.error(error) }) ``` - -Passing in a custom set of organizations to search: +Passing in a custom set of organizations to search and using sort and order: ```js const gfi = require('libgfi') @@ -62,7 +61,9 @@ const options = { q: 'org:nodejs is:issue is:open label:"good first issue"', //GitHub search query description: "Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine." // Human readable description } - } + }, + sort: 'comments', + order: 'asc' } let log = async () => { @@ -71,6 +72,7 @@ let log = async () => { log() ``` +The valid options available in sort and order can be found at [Github's documentation](https://developer.github.com/v3/search/#search-issues-and-pull-requests). Passing any of the allowed parameters as strings should work. Passing in GitHub credentials (see Octokit's [authentication documentation](https://octokit.github.io/rest.js/#authentication) for more details) for authentication to exponentially increase the rate limit: