Skip to content

Commit eb0a1b1

Browse files
Merge pull request #1067 from bharati-21/feature/144-search-issues
Feature: Search issues using GitHub Search API
2 parents 6ef31fc + ee7649d commit eb0a1b1

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

controllers/issues.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
1010

1111
const getIssues = async (req, res) => {
1212
try {
13-
const issues = await issuesService.getOrgIssues();
13+
const { q: queryString } = req.query;
14+
let issues = {};
15+
if (queryString) {
16+
const searchedIssues = await issuesService.searchOrgIssues(queryString);
17+
issues.data = searchedIssues?.data?.items ?? [];
18+
} else {
19+
issues = await issuesService.getOrgIssues();
20+
}
21+
1422
let issuesData = issues.data.length > 0 ? issues.data : [];
1523
issuesData = issuesData.filter((issue) => !Object.keys(issue).includes("pull_request"));
1624
issuesData = issuesData.map(async (issue) => {

services/githubService.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const extractPRdetails = (data) => {
4848
* @param searchParams {Object} - List of params to create github API URL
4949
* @param resultsOptions {Object} - Ordering and pagination of results
5050
*/
51-
const getGithubURL = (searchParams, resultsOptions = {}) => {
51+
const getGithubURL = (searchParams, resultsOptions = {}, searchString) => {
5252
const baseURL = config.get("githubApi.baseUrl");
5353
const issuesAndPRsPath = "/search/issues";
5454

@@ -65,7 +65,11 @@ const getGithubURL = (searchParams, resultsOptions = {}) => {
6565
const paramsStrArr = paramsObjArr.map(([key, value]) => `${key}:${value}`);
6666

6767
// The string that can be entrered as text on Github website for simple search
68-
const prsSearchText = paramsStrArr.join(" ");
68+
let prsSearchText = paramsStrArr.join(" ");
69+
70+
if (searchString) {
71+
prsSearchText = `${searchString} ${prsSearchText}`;
72+
}
6973

7074
urlObj.searchParams.append("q", prsSearchText);
7175

@@ -178,7 +182,7 @@ const fetchMergedPRs = async (params = {}) => {
178182
};
179183

180184
const fetchOpenIssues = async (params = {}) => {
181-
const { perPage = 100, page = 1, searchParams = {}, resultOptions = {} } = params;
185+
const { perPage = 100, page = 1, searchParams = {}, resultOptions = {}, searchString = "" } = params;
182186

183187
try {
184188
const url = getGithubURL(
@@ -192,7 +196,8 @@ const fetchOpenIssues = async (params = {}) => {
192196
...resultOptions,
193197
per_page: perPage,
194198
page,
195-
}
199+
},
200+
searchString
196201
);
197202
return getFetch(url);
198203
} catch (err) {
@@ -239,6 +244,7 @@ const fetchIssues = async () => {
239244
createdURL,
240245
{
241246
filter: "all",
247+
state: "open",
242248
},
243249
{
244250
Accept: "application/vnd.github+json",

services/issuesService.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const getOrgIssues = async () => {
99
return data;
1010
};
1111

12+
const searchOrgIssues = async (searchString) => {
13+
const data = await githubService.fetchOpenIssues({
14+
searchString,
15+
});
16+
17+
return data;
18+
};
19+
1220
module.exports = {
1321
getOrgIssues,
22+
searchOrgIssues,
1423
};

test/unit/services/githubService.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,21 @@ describe("githubService", function () {
138138
expect(response).to.be.equal(`https://api.github.com/orgs/Real-Dev-Squad/issues`);
139139
});
140140
});
141+
142+
describe("fetchOpenIssues with search query param", function () {
143+
it("Should generate the correct url to fetch open issues with search param", async function () {
144+
const searchString = "website";
145+
const response = await githubService.fetchOpenIssues({
146+
searchString,
147+
});
148+
149+
const baseURL = config.get("githubApi.baseUrl");
150+
const org = config.get("githubApi.org");
151+
const path = "search/issues";
152+
const searchParams = `org%3A${org}+type%3Aissue+is%3Aopen&sort=created&per_page=100&page=1`;
153+
154+
const url = `${baseURL}/${path}?q=${searchString}+${searchParams}`;
155+
expect(response).to.be.equal(url);
156+
});
157+
});
141158
});

0 commit comments

Comments
 (0)