@@ -112,7 +112,7 @@ async function _ensureEditPermissionAndGetInfo(projectId, currentUser) {
112
112
) {
113
113
throw new errors . ForbiddenError ( 'You don\'t have access on this project' ) ;
114
114
}
115
- if ( dbProject . archived ) {
115
+ if ( dbProject . archived === 'true' ) {
116
116
throw new errors . ForbiddenError ( 'You can\'t access on this archived project' ) ;
117
117
}
118
118
return dbProject ;
@@ -140,33 +140,21 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
140
140
or a time-sequence cornercase encountered here` ) ;
141
141
}
142
142
try {
143
- let oldIssues = await models . Issue . query ( { repoUrl : oldRepo . url } ) ;
144
- let oldCopilotPaymentPromise = oldIssues . filter ( issue => issue . challengeUUID )
145
- . map ( issue => models . CopilotPayment . query ( { challengeUUID : issue . challengeUUID } )
146
- . then ( payments => {
147
- if ( ! payments || payments . length === 0 ) {
148
- /* eslint-disable-next-line no-console */
149
- console . log ( `No CopilotPayment correspond to Issue with challengeUUID ${ issue . challengeUUID } .
150
- The corresponding CopilotPayment may have been removed.
151
- Or, there is bug in old version.` ) ;
152
- return null ;
153
- }
154
- if ( payments . length > 1 ) {
155
- throw new Error ( `Duplicate CopilotPayment correspond to one Issue with challengeUUID ${ issue . challengeUUID } .
156
- There must be bug in old version` ) ;
157
- }
158
- return payments [ 0 ] ;
159
- } ) ) ;
160
- let oldCopilotPayment = await Promise . all ( oldCopilotPaymentPromise ) . filter ( payment => payment ) ;
161
-
162
- await models . Repository . update ( { id : oldRepo . id } , { projectId : project . id , archived : false } ) ;
163
- await oldIssues . forEach ( issue => models . Issue . update ( { id : issue . id } , { projectId : project . id } ) ) ;
164
- await oldCopilotPayment . forEach (
165
- payment => models . CopilotPayment . update ( { id : payment . id } , { project : project . id } )
143
+ const oldIssues = await dbHelper . queryIssueIdChallengeUUIDByRepoUrl ( repoUrl ) ;
144
+ const issueIds = oldIssues . map ( issue => issue . id ) ;
145
+ const challengeUUIDs = oldIssues . map ( issue => issue . challengeUUID ) . filter ( challengeUUID => challengeUUID ) ;
146
+ const paymentIds = await Promise . all (
147
+ challengeUUIDs . map ( challengeUUID => dbHelper . queryPaymentIdByChallengeUUID ( challengeUUID ) )
148
+ ) ;
149
+
150
+ await dbHelper . update ( models . Repository , oldRepo . id , { projectId : project . id , archived : false } ) ;
151
+ await Promise . all ( issueIds . map ( issueId => dbHelper . update ( models . Issue , issueId , { projectId : project . id } ) ) ) ;
152
+ await Promise . all (
153
+ paymentIds . map ( paymentId => dbHelper . update ( models . CopilotPayment , paymentId , { project : project . id } ) )
166
154
) ;
167
155
}
168
156
catch ( err ) {
169
- throw new Error ( `Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
157
+ throw new Error ( `Update ProjectId for Repository, Issue, CopilotPayment failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
170
158
}
171
159
} else {
172
160
try {
@@ -181,7 +169,7 @@ async function _createOrMigrateRepository(repoUrl, project, currentUser) {
181
169
await addWikiRules ( { projectId : project . id } , currentUser , repoUrl ) ;
182
170
}
183
171
catch ( err ) {
184
- throw new Error ( `Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
172
+ throw new Error ( `Project created. Adding the webhook, issue labels, and wiki rules failed. Repo ${ repoUrl } . Internal Error: ${ err } ` ) ;
185
173
}
186
174
}
187
175
}
@@ -213,6 +201,8 @@ async function create(project, currentUser) {
213
201
project . copilot = project . copilot ? project . copilot . toLowerCase ( ) : null ;
214
202
project . id = helper . generateIdentifier ( ) ;
215
203
204
+ const createdProject = await dbHelper . create ( models . Project , project ) ;
205
+
216
206
// TODO: The following db operation should/could be moved into one transaction
217
207
for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
218
208
try {
@@ -222,7 +212,6 @@ async function create(project, currentUser) {
222
212
throw new Error ( `Create or migrate repository failed. Repo ${ repoUrl } . Internal Error: ${ err . message } ` ) ;
223
213
}
224
214
}
225
- const createdProject = await dbHelper . create ( models . Project , project ) ;
226
215
227
216
return createdProject ;
228
217
}
@@ -267,12 +256,12 @@ async function update(project, currentUser) {
267
256
} ) ;
268
257
269
258
// TODO: move the following logic into one dynamoose transaction
270
- const repoUrl2Repo = await dbHelper . queryRepositoriesByProjectId ( dbProject . id )
271
- . map ( repo => { return { [ repo . url ] : repo } ; } ) ;
259
+ const repos = await dbHelper . queryRepositoriesByProjectId ( dbProject . id ) ;
272
260
273
261
for ( const repoUrl of repoUrls ) { // eslint-disable-line no-restricted-syntax
274
- if ( repoUrl in repoUrl2Repo ) {
275
- await models . Repository . update ( { id : repoUrl2Repo [ repoUrl ] . id } , { archived : project . archived } ) ;
262
+ if ( repos . find ( repo => repo . url === repoUrl ) ) {
263
+ const repoId = repos . find ( repo => repo . url === repoUrl ) . id
264
+ await dbHelper . update ( models . Repository , repoId , { archived : project . archived } ) ;
276
265
} else {
277
266
try {
278
267
await _createOrMigrateRepository ( repoUrl , project , currentUser ) ;
@@ -319,7 +308,6 @@ async function getAll(query, currentUser) {
319
308
query . lastKey = parseInt ( query . lastKey , 10 ) ;
320
309
}
321
310
const slicedProjects = _ . slice ( projects , query . lastKey , query . lastKey + query . perPage ) ;
322
- // console.log(projects);
323
311
for ( const project of slicedProjects ) { // eslint-disable-line
324
312
project . repoUrls = await dbHelper . populateRepoUrls ( project . id ) ;
325
313
}
0 commit comments