@@ -144,6 +144,25 @@ export const status: Handler = async (c) => {
144
144
} ) ;
145
145
} ;
146
146
147
+ const doBuild = ( oid : string , build : Deno . Command | null ) => {
148
+ const buildMap = new Map < string , Promise < void > > ( ) ;
149
+
150
+ const p = buildMap . get ( oid ) ||
151
+ ( async ( ) => {
152
+ try {
153
+ await build ?. spawn ( ) ?. status ;
154
+ await persist ( oid ) ;
155
+ } catch ( e ) {
156
+ console . error ( "Building failed with:" , e ) ;
157
+ } finally {
158
+ buildMap . delete ( oid ) ;
159
+ }
160
+ } ) ( ) ;
161
+
162
+ buildMap . set ( oid , p ) ;
163
+
164
+ return p ;
165
+ } ;
147
166
export interface PublishAPI {
148
167
body : {
149
168
message : string ;
@@ -186,25 +205,6 @@ const persist = async (oid: string) => {
186
205
// TODO: maybe tag with versions!
187
206
// TODO: handle rebase conflicts
188
207
export const publish = ( { build } : Options ) : Handler => {
189
- const buildMap = new Map < string , Promise < void > > ( ) ;
190
-
191
- const doBuild = ( oid : string ) => {
192
- const p = buildMap . get ( oid ) ||
193
- ( async ( ) => {
194
- try {
195
- await build ?. spawn ( ) ?. status ;
196
- await persist ( oid ) ;
197
- } catch ( e ) {
198
- console . error ( "Building failed with:" , e ) ;
199
- } finally {
200
- buildMap . delete ( oid ) ;
201
- }
202
- } ) ( ) ;
203
-
204
- buildMap . set ( oid , p ) ;
205
-
206
- return p ;
207
- } ;
208
208
209
209
return async ( c ) => {
210
210
const body = ( await c . req . json ( ) ) as PublishAPI [ "body" ] ;
@@ -223,7 +223,7 @@ export const publish = ({ build }: Options): Handler => {
223
223
const result = await git . push ( ) ;
224
224
225
225
// Runs build pipeline asynchronously
226
- doBuild ( commit . commit ) ;
226
+ doBuild ( commit . commit , build ) ;
227
227
228
228
return Response . json ( result ) ;
229
229
} ;
@@ -436,6 +436,56 @@ interface Options {
436
436
site : string ;
437
437
}
438
438
439
+ export interface RevertAPI {
440
+ body : {
441
+ commitHash : string ;
442
+ message ?: string ;
443
+ author ?: {
444
+ name ?: string ;
445
+ email ?: string ;
446
+ } ;
447
+ } ;
448
+ response : {
449
+ status : StatusResult ;
450
+ commit : string ;
451
+ } ;
452
+ }
453
+
454
+ export const revert = ( { build } : Options ) : Handler => {
455
+
456
+ return async ( c ) => {
457
+ const { commitHash
, message
, author
= { name :
"decobot" , email :
"[email protected] " } } =
458
+ await c . req . json ( ) as RevertAPI [ "body" ] ;
459
+
460
+ await git . fetch ( [ "-p" ] ) . submoduleUpdate ( [ "--depth" , "1" ] ) ;
461
+
462
+ await resetToMergeBase ( ) ;
463
+
464
+ // Create revert commit
465
+ await git . revert ( commitHash , {
466
+ "--no-commit" : null , // Stage changes without committing
467
+ } ) ;
468
+
469
+ // Commit the revert with custom message
470
+ const commitMessage = message || `Revert "${ commitHash } "` ;
471
+ const commit = await git . commit ( commitMessage , {
472
+ "--author" : `${ author . name } <${ author . email } >` ,
473
+ "--no-verify" : null ,
474
+ } ) ;
475
+
476
+ // Runs build pipeline asynchronously
477
+ doBuild ( commit . commit , build ) ;
478
+
479
+ // Push the revert commit
480
+ await git . push ( ) ;
481
+
482
+ return Response . json ( {
483
+ status : await git . status ( ) ,
484
+ commit : commit . commit
485
+ } ) ;
486
+ } ;
487
+ } ;
488
+
439
489
export const createGitAPIS = ( options : Options ) => {
440
490
const app = new Hono ( ) ;
441
491
@@ -446,6 +496,7 @@ export const createGitAPIS = (options: Options) => {
446
496
app . post ( "/publish" , publish ( options ) ) ;
447
497
app . post ( "/discard" , discard ) ;
448
498
app . post ( "/rebase" , rebase ) ;
499
+ app . post ( "/revert" , revert ( options ) ) ;
449
500
450
501
return app ;
451
502
} ;
0 commit comments