1- import { Controller , Get , Res } from '@nestjs/common' ;
1+ import { BadRequestException , Controller , Get , Param , Query , Res } from '@nestjs/common' ;
22import { AppService } from './app.service' ;
33import { Response } from 'express' ;
44import { AbstractController } from '~/_common/abstracts/abstract.controller' ;
55import { Public } from './_common/decorators/public.decorator' ;
6- import { ApiBasicAuth , ApiBearerAuth , ApiOperation , ApiResponse } from '@nestjs/swagger' ;
6+ import { ApiBearerAuth , ApiOperation , ApiResponse } from '@nestjs/swagger' ;
7+
8+ interface GithubUpdate {
9+ name ?: string ;
10+ commit ?: {
11+ sha ?: string ;
12+ url ?: string ;
13+ } ;
14+ zipball_url ?: string ;
15+ tarball_url ?: string ;
16+ node_id ?: string ;
17+ }
718
819@Public ( )
920@Controller ( )
@@ -22,4 +33,34 @@ export class AppController extends AbstractController {
2233 ...this . appService . getInfo ( ) ,
2334 } ) ;
2435 }
36+
37+ @Get ( '/get-update/:project(sesame-orchestrator|sesame-daemon|sesame-app-manager)' )
38+ public async update (
39+ @Res ( ) res : Response ,
40+ @Param ( 'project' ) project : string ,
41+ @Query ( 'current' ) current : string ,
42+ ) : Promise < Response > {
43+ const update = await fetch ( `https://api.github.com/repos/Libertech-FR/${ project } /tags` )
44+ const data : GithubUpdate = await update . json ( )
45+ const lastVersion = data [ 0 ] . name . replace ( / ^ v / , '' )
46+ const pkgInfo = this . appService . getInfo ( )
47+ const currentVersion = current || pkgInfo . version
48+
49+ if ( project !== pkgInfo . name ) {
50+ if ( ! / [ 0 - 9 ] + \. [ 0 - 9 ] + \. [ 0 - 9 ] + / . test ( current ) ) {
51+ throw new BadRequestException ( 'Invalid version for current parameter' )
52+ }
53+ }
54+
55+ const [ currentMajor , currentMinor , currentPatch ] = lastVersion . split ( '.' ) . map ( Number )
56+ const [ lastMajor , lastMinor , lastPatch ] = currentVersion . split ( '.' ) . map ( Number )
57+ const updateAvailable = currentMajor > lastMajor || currentMinor > lastMinor || currentPatch > lastPatch
58+
59+ return res . json ( {
60+ project,
61+ updateAvailable,
62+ currentVersion,
63+ lastVersion,
64+ } ) ;
65+ }
2566}
0 commit comments