1
1
const { fetch } = require ( '../utils/fetch' )
2
2
const { fetchUser } = require ( '../models/users' )
3
3
4
+ /**
5
+ * Extracts only the necessary details required from the object returned by Github API
6
+ * @param data {Object} - Object returned by Github API
7
+ */
8
+
9
+ const extractPRdetails = ( data ) => {
10
+ const allPRs = [ ]
11
+ data . items . forEach ( ( { title, html_url : url , state, created_at : createdAt , updated_at : updatedAt , repository_url : repositoryUrl , labels, assignees } ) => {
12
+ const allAssignees = assignees . map ( object => object . login )
13
+ const allLabels = labels . map ( object => object . name )
14
+ const repositoryUrlSplit = repositoryUrl . split ( '/' )
15
+ const repository = repositoryUrlSplit [ repositoryUrlSplit . length - 1 ]
16
+ allPRs . push ( {
17
+ title,
18
+ state,
19
+ createdAt,
20
+ updatedAt,
21
+ repository,
22
+ url,
23
+ labels : allLabels ,
24
+ assignees : allAssignees
25
+ } )
26
+ } )
27
+ return allPRs
28
+ }
29
+
4
30
/**
5
31
* Creates the custom API URL with the required params in the format
6
32
* expected by Github
7
33
* https://docs.github.com/en/free-pro-team@latest/rest/reference/search
8
34
* @access private
9
- * @param {Object } searchParams - List of params to create github API URL
10
- * @param {Object } resultsOptions - Ordering and pagination of results
35
+ * @param searchParams {Object} - List of params to create github API URL
36
+ * @param resultsOptions {Object} - Ordering and pagination of results
11
37
*/
12
38
const getGithubURL = ( searchParams , resultsOptions = { } ) => {
13
39
const baseURL = config . get ( 'githubApi.baseUrl' )
@@ -43,7 +69,7 @@ const getGithubURL = (searchParams, resultsOptions = {}) => {
43
69
44
70
/** Create the fetch object to call on github url
45
71
* @access private
46
- * @param {string } url - URL on github to call
72
+ * @param url {string} - URL on github to call
47
73
*/
48
74
function getFetch ( url ) {
49
75
return fetch ( url , 'get' , null , null , null , {
@@ -62,13 +88,10 @@ function getFetch (url) {
62
88
const fetchPRsByUser = async ( username ) => {
63
89
try {
64
90
const { user } = await fetchUser ( { username } )
65
- const url = `${ config . get ( 'githubApi.baseUrl' ) } /search/issues?q=org:${ config . get ( 'githubApi.org' ) } +author:${ user . github_id } +type:pr`
66
- return fetch ( url , 'get' , null , null , null , {
67
- auth : {
68
- username : config . get ( 'githubOauth.clientId' ) ,
69
- password : config . get ( 'githubOauth.clientSecret' )
70
- }
91
+ const url = getGithubURL ( {
92
+ author : user . github_id
71
93
} )
94
+ return getFetch ( url )
72
95
} catch ( err ) {
73
96
logger . error ( `Error while fetching pull requests: ${ err } ` )
74
97
throw err
@@ -120,5 +143,6 @@ const fetchOpenPRs = async (pageNumber) => {
120
143
module . exports = {
121
144
fetchPRsByUser,
122
145
fetchOpenPRs,
123
- fetchStalePRs
146
+ fetchStalePRs,
147
+ extractPRdetails
124
148
}
0 commit comments