@@ -2,7 +2,7 @@ const Git = require("nodegit");
2
2
const PATH = require ( "path" ) ;
3
3
const FS = require ( 'fs-extra' )
4
4
const Models = require ( '../../../../conf/sequelize' ) ;
5
- const { readFile } = require ( '../../../utils/fsExtra' ) ;
5
+ const { readFile, removeDir } = require ( '../../../utils/fsExtra' ) ;
6
6
7
7
const { getFiles, repoFilesSort, getEntrysInfo, getFilesCommitInfo, getFileCommit } = require ( './util' ) ;
8
8
@@ -155,8 +155,31 @@ module.exports = {
155
155
}
156
156
} ,
157
157
delete : async ( ctx ) => {
158
- const { owner, repo } = ctx . params ;
159
- ctx . body = { owner, repo } ;
158
+ const { owner } = ctx . params ;
159
+ let { repo } = ctx . params ;
160
+ const { reposPath } = ctx . state . conf ;
161
+ repo = repo . replace ( / .g i t $ / , '' ) ;
162
+ let transaction ;
163
+ try {
164
+ // 托管事务
165
+ transaction = await Models . sequelize . transaction ( ) ;
166
+ const namespaces = await Models . namespaces . findOne ( { where : { name : owner } } ) ;
167
+ if ( ! namespaces . id ) ctx . throw ( 404 , 'Owner does not exist!' ) ;
168
+ await Models . projects . destroy ( { where : { namespace_id : namespaces . id , name : repo } } ) ;
169
+ const projects = Models . projects . findOne ( { where : { name : repo , namespace_id : namespaces . id } } ) ;
170
+ if ( ! projects . id ) ctx . throw ( 404 , 'Repo does not exist!' ) ;
171
+ await Models . user_interacted_projects . destroy ( { where : { project_id : projects . id , creator_id : namespaces . owner_id } } ) ;
172
+ // remove repo
173
+ await removeDir ( PATH . join ( reposPath , owner , `${ repo } .git` ) ) ;
174
+ // transaction commit 事务提交
175
+ await transaction . commit ( ) ;
176
+ ctx . body = { message : `Successfully deleted ${ repo } !` } ;
177
+ } catch ( err ) {
178
+ // 事务回滚
179
+ if ( transaction ) await transaction . rollback ( ) ;
180
+ ctx . response . status = err . statusCode || err . status || 500 ;
181
+ ctx . body = { message : err . message , ...err }
182
+ }
160
183
} ,
161
184
reposTree : async ( ctx ) => {
162
185
const { id } = ctx . params ;
0 commit comments