@@ -2,45 +2,91 @@ const got = require('got');
22const ArgParser = require ( '../ArgParser' ) ;
33
44/**
5- * getInternalGitIssues seraches text in the repos and return matched git issues
6- * api/getInternalGitIssues?text=jdk_util
7- * @param {string } text Required. Search text string. i.e., jdk_math
8- * @return {object } matched git issues
5+ * getInternalGitIssues searches text across user-configured Git hosts and repos.
6+ *
7+ * This endpoint supports multiple GitHub-style hosts (e.g., github.ibm.com, github.com)
8+ * as defined in the user-provided trssConf.json file loaded via --configFile.
9+ *
10+ * Example:
11+ * GET api/getInternalGitIssues?text=jdk_util
12+ *
13+ * Config format (per host):
14+ * {
15+ * "github.ibm.com": {
16+ * "token": "<optional_token>",
17+ * "repos": ["owner1/repo1", "owner2/repo2"]
18+ * },
19+ * "github.com": {
20+ * "token": "<optional_token>",
21+ * "repos": ["adoptium/aqa-test-tools"]
22+ * }
23+ * }
24+ *
25+ * Behavior:
26+ * - Loops through each host defined in config
27+ * - Builds GitHub search queries for each repo in that host
28+ * - Calls the host's /search/issues endpoint using got()
29+ * - Collects and merges issue results from all hosts
30+ * - Returns a combined array of GitHub issue objects
31+ *
32+ * @param {string } text - Required search query string
33+ * @return {Array<object> } Combined list of matched issues from all configured hosts
934 */
1035
1136module . exports = async ( req , res ) => {
1237 try {
1338 const { text } = req . query ;
14- if ( text ) {
15- const url = 'https://api.github.ibm.com' ;
16- const repos = 'repo:runtimes/backlog+repo:runtimes/infrastructure' ;
17- const credentails = ArgParser . getConfig ( ) ;
18- if ( credentails [ url ] && credentails [ url ] . token ) {
19- const response = await got (
20- `${ url } /search/issues?q=${ text } +${ repos } ` ,
21- {
22- method : 'get' ,
23- headers : {
24- Authorization : `Bearer ${ credentails [ url ] . token } ` ,
25- Accept : 'application/vnd.github+json' ,
26- } ,
27- responseType : 'json' ,
28- }
29- ) ;
30- if ( response && response . body ) {
31- res . send ( response . body . items ) ;
32- } else {
33- res . send ( [ ] ) ;
39+ if ( ! text ) return res . send ( 'expect text query parameter' ) ;
40+
41+ const config = ArgParser . getConfig ( ) ;
42+ if ( ! config )
43+ return res . send ( 'No config file loaded. Use --configFile=<path>' ) ;
44+
45+ const allIssues = [ ] ;
46+
47+ // Loop through each host in the config
48+ for ( const host of Object . keys ( config ) ) {
49+ const hostCfg = config [ host ] ;
50+
51+ // Validate host entry
52+ if ( ! hostCfg || ! hostCfg . repos || ! Array . isArray ( hostCfg . repos ) ) {
53+ console . warn ( `Skipping host ${ host } : repos[] missing` ) ;
54+ continue ;
55+ }
56+
57+ const repoQuery = hostCfg . repos . map ( ( r ) => `repo:${ r } ` ) . join ( '+' ) ;
58+
59+ const baseUrl = host . startsWith ( 'http' ) ? host : `https://${ host } ` ;
60+
61+ const url = `${ baseUrl } /search/issues?q=${ text } +${ repoQuery } ` ;
62+
63+ try {
64+ const response = await got ( url , {
65+ method : 'get' ,
66+ headers : {
67+ ...( hostCfg . token
68+ ? { Authorization : `Bearer ${ hostCfg . token } ` }
69+ : { } ) ,
70+ Accept : 'application/vnd.github+json' ,
71+ } ,
72+ responseType : 'json' ,
73+ } ) ;
74+
75+ if ( response . body ?. items ) {
76+ allIssues . push ( ...response . body . items ) ;
3477 }
35- } else {
36- res . send ( `${ url } and/or token not appear in trssConf.json` ) ;
78+ } catch ( innerErr ) {
79+ console . error (
80+ `Error fetching from host ${ host } :` ,
81+ innerErr . message
82+ ) ;
3783 }
38- } else {
39- res . send ( 'expect text query parameter' ) ;
4084 }
85+
86+ return res . send ( allIssues ) ;
4187 } catch ( error ) {
4288 const rawError = error . response ?. data || error . message ;
4389 console . error ( 'Error fetching repos:' , rawError ) ;
44- res . status ( 500 ) . json ( { error : String ( rawError ) } ) ;
90+ return res . status ( 500 ) . json ( { error : String ( rawError ) } ) ;
4591 }
4692} ;
0 commit comments