33 * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information.
44 *--------------------------------------------------------------------------------------*/
55
6+ import { CancellationToken } from '../../../../base/common/cancellation.js' ;
67import { Disposable } from '../../../../base/common/lifecycle.js' ;
8+ import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js' ;
79import { IEnvironmentMainService } from '../../../../platform/environment/electron-main/environmentMainService.js' ;
810import { IProductService } from '../../../../platform/product/common/productService.js' ;
11+ import { asJson , IRequestService } from '../../../../platform/request/common/request.js' ;
912import { IUpdateService , StateType } from '../../../../platform/update/common/update.js' ;
1013import { ICortexideUpdateService } from '../common/cortexideUpdateService.js' ;
1114import { CortexideCheckUpdateResponse } from '../common/cortexideUpdateServiceTypes.js' ;
@@ -19,6 +22,8 @@ export class CortexideMainUpdateService extends Disposable implements ICortexide
1922 @IProductService private readonly _productService : IProductService ,
2023 @IEnvironmentMainService private readonly _envMainService : IEnvironmentMainService ,
2124 @IUpdateService private readonly _updateService : IUpdateService ,
25+ @IConfigurationService private readonly _configurationService : IConfigurationService ,
26+ @IRequestService private readonly _requestService : IRequestService ,
2227 ) {
2328 super ( )
2429 }
@@ -40,8 +45,6 @@ export class CortexideMainUpdateService extends Disposable implements ICortexide
4045
4146 this . _updateService . checkForUpdates ( false ) // implicity check, then handle result ourselves
4247
43- console . log ( 'updateState' , this . _updateService . state )
44-
4548 if ( this . _updateService . state . type === StateType . Uninitialized ) {
4649 // The update service hasn't been initialized yet
4750 return { message : explicit ? 'Checking for updates soon...' : null , action : explicit ? 'reinstall' : undefined } as const
@@ -83,7 +86,8 @@ export class CortexideMainUpdateService extends Disposable implements ICortexide
8386 }
8487
8588 if ( this . _updateService . state . type === StateType . Disabled ) {
86- return await this . _manualCheckGHTagIfDisabled ( explicit )
89+ const channel = this . _configurationService . getValue < 'stable' | 'beta' | 'nightly' > ( 'update.updateChannel' ) || 'stable' ;
90+ return await this . _manualCheckGHTagIfDisabled ( explicit , channel )
8791 }
8892 return null
8993 }
@@ -93,11 +97,31 @@ export class CortexideMainUpdateService extends Disposable implements ICortexide
9397
9498
9599
96- private async _manualCheckGHTagIfDisabled ( explicit : boolean ) : Promise < CortexideCheckUpdateResponse > {
100+ private async _manualCheckGHTagIfDisabled ( explicit : boolean , channel : 'stable' | 'beta' | 'nightly' ) : Promise < CortexideCheckUpdateResponse > {
97101 try {
98- const response = await fetch ( 'https://api.github.com/repos/cortexide/cortexide/releases/latest' ) ;
102+ let releaseUrl : string ;
103+ if ( channel === 'beta' ) {
104+ releaseUrl = 'https://api.github.com/repos/OpenCortexIDE/cortexide/releases?per_page=1' ;
105+ } else if ( channel === 'nightly' ) {
106+ releaseUrl = 'https://api.github.com/repos/OpenCortexIDE/cortexide/releases?per_page=1' ;
107+ } else {
108+ releaseUrl = 'https://api.github.com/repos/OpenCortexIDE/cortexide/releases/latest' ;
109+ }
110+
111+ const context = await this . _requestService . request ( { url : releaseUrl , type : 'GET' } , CancellationToken . None ) ;
112+ if ( context . res . statusCode !== 200 ) {
113+ throw new Error ( `GitHub API returned ${ context . res . statusCode } ` ) ;
114+ }
115+
116+ const jsonData = await asJson ( context ) ;
117+ const data = channel === 'stable'
118+ ? jsonData
119+ : Array . isArray ( jsonData ) ? jsonData [ 0 ] : jsonData ;
120+
121+ if ( ! data || ! data . tag_name ) {
122+ throw new Error ( 'Invalid release data' ) ;
123+ }
99124
100- const data = await response . json ( ) ;
101125 const version = data . tag_name ;
102126
103127 const myVersion = this . _productService . version
@@ -110,23 +134,17 @@ export class CortexideMainUpdateService extends Disposable implements ICortexide
110134
111135 // explicit
112136 if ( explicit ) {
113- if ( response . ok ) {
114- if ( ! isUpToDate ) {
115- message = 'A new version of CortexIDE is available! Please reinstall (auto-updates are disabled on this OS) - it only takes a second!'
116- action = 'reinstall'
117- }
118- else {
119- message = 'CortexIDE is up-to-date!'
120- }
137+ if ( ! isUpToDate ) {
138+ message = 'A new version of CortexIDE is available! Please reinstall (auto-updates are disabled on this OS) - it only takes a second!'
139+ action = 'reinstall'
121140 }
122141 else {
123- message = `An error occurred when fetching the latest GitHub release tag. Please try again in ~5 minutes, or reinstall.`
124- action = 'reinstall'
142+ message = 'CortexIDE is up-to-date!'
125143 }
126144 }
127145 // not explicit
128146 else {
129- if ( response . ok && ! isUpToDate ) {
147+ if ( ! isUpToDate ) {
130148 message = 'A new version of CortexIDE is available! Please reinstall (auto-updates are disabled on this OS) - it only takes a second!'
131149 action = 'reinstall'
132150 }
0 commit comments