1
1
import env from '../env/Env.js' ;
2
2
import loggerHelper from '../helpers/logger.helper.js' ;
3
- import repository from ' ../repositories/threatmodelrepository.js' ;
3
+ import repositories from " ../repositories" ;
4
4
import responseWrapper from './responseWrapper.js' ;
5
5
import { serverError } from './errors.js' ;
6
6
7
7
const logger = loggerHelper . get ( 'controllers/threatmodelcontroller.js' ) ;
8
8
9
9
const repos = ( req , res ) => responseWrapper . sendResponseAsync ( async ( ) => {
10
+ const repository = repositories . get ( ) ;
11
+
10
12
const page = req . query . page || 1 ;
11
13
let reposResp ;
12
14
let repos ;
@@ -21,15 +23,23 @@ const repos = (req, res) => responseWrapper.sendResponseAsync(async () => {
21
23
repos = reposResp [ 0 ] ;
22
24
}
23
25
const headers = reposResp [ 1 ] ;
26
+ const pageLinks = reposResp [ 2 ] ;
24
27
logger . debug ( 'API repos request: ' + req ) ;
25
28
29
+ const pagination = getPagination ( headers , pageLinks , page ) ;
30
+
26
31
return {
27
32
repos : repos . map ( ( x ) => x . full_name ) ,
28
- pagination : getPagination ( headers , page )
33
+ pagination : pagination
29
34
} ;
30
35
} , req , res , logger ) ;
31
36
37
+
38
+
32
39
const branches = ( req , res ) => responseWrapper . sendResponseAsync ( async ( ) => {
40
+
41
+ const repository = repositories . get ( ) ;
42
+
33
43
const repoInfo = {
34
44
organisation : req . params . organisation ,
35
45
repo : req . params . repo ,
@@ -40,15 +50,21 @@ const branches = (req, res) => responseWrapper.sendResponseAsync(async () => {
40
50
const branchesResp = await repository . branchesAsync ( repoInfo , req . provider . access_token ) ;
41
51
const branches = branchesResp [ 0 ] ;
42
52
const headers = branchesResp [ 1 ] ;
53
+ const pageLinks = branchesResp [ 2 ] ;
54
+
43
55
const branchNames = branches . map ( ( x ) => x . name ) ;
44
56
57
+ const pagination = getPagination ( headers , pageLinks , repoInfo . page ) ;
58
+
45
59
return {
46
60
branches : branchNames ,
47
- pagination : getPagination ( headers , repoInfo . page )
61
+ pagination : pagination
48
62
} ;
49
63
} , req , res , logger ) ;
50
64
51
65
const models = ( req , res ) => responseWrapper . sendResponseAsync ( async ( ) => {
66
+ const repository = repositories . get ( ) ;
67
+
52
68
const branchInfo = {
53
69
organisation : req . params . organisation ,
54
70
repo : req . params . repo ,
@@ -70,6 +86,7 @@ const models = (req, res) => responseWrapper.sendResponseAsync(async () => {
70
86
} , req , res , logger ) ;
71
87
72
88
const model = ( req , res ) => responseWrapper . sendResponseAsync ( async ( ) => {
89
+ const repository = repositories . get ( ) ;
73
90
const modelInfo = {
74
91
organisation : req . params . organisation ,
75
92
repo : req . params . repo ,
@@ -83,6 +100,8 @@ const model = (req, res) => responseWrapper.sendResponseAsync(async () => {
83
100
} , req , res , logger ) ;
84
101
85
102
const create = async ( req , res ) => {
103
+ const repository = repositories . get ( ) ;
104
+
86
105
const modelBody = {
87
106
organisation : req . params . organisation ,
88
107
repo : req . params . repo ,
@@ -102,6 +121,8 @@ const create = async (req, res) => {
102
121
} ;
103
122
104
123
const update = async ( req , res ) => {
124
+ const repository = repositories . get ( ) ;
125
+
105
126
const modelBody = {
106
127
organisation : req . params . organisation ,
107
128
repo : req . params . repo ,
@@ -121,6 +142,8 @@ const update = async (req, res) => {
121
142
} ;
122
143
123
144
const deleteModel = async ( req , res ) => {
145
+ const repository = repositories . get ( ) ;
146
+
124
147
const modelInfo = {
125
148
organisation : req . params . organisation ,
126
149
repo : req . params . repo ,
@@ -138,10 +161,28 @@ const deleteModel = async (req, res) => {
138
161
}
139
162
} ;
140
163
141
- const getPagination = ( headers , page ) => {
142
- const pagination = { page, next : false , prev : false } ;
143
- const linkHeader = headers . link ;
164
+ const getPagination = ( headers , pageLinks , page ) => {
165
+
166
+ if ( headers === undefined || headers === null || ( Object . keys ( headers ) . length === 0 ) || headers ?. link === null ) {
167
+ if ( pageLinks === undefined || pageLinks === null || ( Object . keys ( pageLinks ) . length === 0 ) ) {
168
+ return { page, next : false , prev : false } ;
169
+ }
170
+ return getPaginationFromPageLinks ( pageLinks , page ) ;
171
+ }
172
+ return getPaginationFromHeaders ( headers , page ) ;
173
+
174
+ } ;
144
175
176
+ const getPaginationFromPageLinks = ( pageLinks , page ) => {
177
+ const pagination = { page, next : false , prev : false } ;
178
+ pagination . next = pageLinks . next ;
179
+ pagination . prev = pageLinks . prev ;
180
+ return pagination ;
181
+ } ;
182
+
183
+ const getPaginationFromHeaders = ( headers , page ) => {
184
+ const pagination = { page, next : false , prev : false } ;
185
+ const linkHeader = headers . link ;
145
186
if ( linkHeader ) {
146
187
linkHeader . split ( ',' ) . forEach ( ( link ) => {
147
188
const isLinkType = ( type ) => link . split ( ';' ) [ 1 ] . split ( '=' ) [ 1 ] === type ;
@@ -155,7 +196,6 @@ const getPagination = (headers, page) => {
155
196
}
156
197
} ) ;
157
198
}
158
-
159
199
return pagination ;
160
200
} ;
161
201
0 commit comments