Skip to content

Commit 39e10ef

Browse files
committed
feat: support gh_token closes 1758
Signed-off-by: Innei <i@innei.in>
1 parent 05e067f commit 39e10ef

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

apps/core/src/modules/configs/configs.default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const generateDefaultConfig: () => IConfig = () => ({
6565
},
6666
thirdPartyServiceIntegration: {
6767
xLogSiteId: '',
68+
githubToken: '',
6869
},
6970
clerkOptions: {
7071
enable: false,

apps/core/src/modules/configs/configs.dto.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,23 @@ export class ClerkOptionsDto {
350350
/**
351351
* 第三方服务集成
352352
*/
353-
@JSONSchema({ title: '第三方服务集成' })
353+
@JSONSchema({ title: '第三方服务信息' })
354354
export class ThirdPartyServiceIntegrationDto {
355355
@JSONSchemaPlainField('xLog SiteId', {
356356
description: '文章发布同步到 [xLog](https://xlog.app)',
357357
})
358358
@IsOptional()
359359
@IsString()
360360
xLogSiteId?: string
361+
362+
@JSONSchemaPasswordField('GitHub Token', {
363+
description:
364+
'用于调用 GitHub API,获取仓库信息等;可选参数,如果没有遇到限流问题,可以不填写',
365+
})
366+
@IsOptional()
367+
@IsString()
368+
@SecretField
369+
githubToken?: string
361370
}
362371

363372
@JSONSchema({ title: '认证安全设置', 'ui:options': { type: 'hidden' } })

apps/core/src/modules/pageproxy/pageproxy.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ export class PageProxyService {
2929
* @throws {Error}
3030
*/
3131
async getAdminLastestVersionFromGHRelease(): Promise<string> {
32+
const { githubToken } = await this.configs.get(
33+
'thirdPartyServiceIntegration',
34+
)
3235
// tag_name: v3.6.x
3336
const { tag_name } = await fetch(
3437
`https://api.github.com/repos/${PKG.dashboard.repo}/releases/latest`,
38+
{
39+
headers: {
40+
Authorization: githubToken || `Bearer ${githubToken}`,
41+
},
42+
},
3543
).then((data) => data.json())
3644

3745
return tag_name.replace(/^v/, '')

apps/core/src/modules/update/update.service.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,41 @@ import { Injectable } from '@nestjs/common'
88
import { LOCAL_ADMIN_ASSET_PATH } from '~/constants/path.constant'
99
import { HttpService } from '~/processors/helper/helper.http.service'
1010
import { spawnShell } from '~/utils'
11+
import { ConfigsService } from '../configs/configs.service'
1112
import type { Subscriber } from 'rxjs'
1213
import { dashboard } from '~/../package.json'
1314

1415
const { repo } = dashboard
1516

1617
@Injectable()
1718
export class UpdateService {
18-
constructor(protected readonly httpService: HttpService) {}
19+
constructor(
20+
protected readonly httpService: HttpService,
21+
protected readonly configService: ConfigsService,
22+
) {}
1923
downloadAdminAsset(version: string) {
2024
const observable$ = new Observable<string>((subscriber) => {
2125
;(async () => {
26+
const { githubToken } = await this.configService.get(
27+
'thirdPartyServiceIntegration',
28+
)
2229
const endpoint = `https://api.github.com/repos/${repo}/releases/tags/v${version}`
2330

2431
subscriber.next(`Getting release info from ${endpoint}.\n`)
2532

26-
const json = await fetch(endpoint)
27-
.then((res) => res.json())
33+
const result = await this.httpService.axiosRef
34+
.get(endpoint, {
35+
headers: {
36+
Authorization: githubToken || `Bearer ${githubToken}`,
37+
},
38+
})
39+
2840
.catch((error) => {
2941
subscriber.next(chalk.red(`Fetching error: ${error.message}`))
3042
subscriber.complete()
3143
return null
3244
})
33-
45+
const json = result?.data
3446
if (!json) {
3547
subscriber.next(chalk.red('Fetching error, json is empty. \n'))
3648
subscriber.complete()
@@ -131,7 +143,14 @@ export class UpdateService {
131143
async getLatestAdminVersion() {
132144
const endpoint = `https://api.github.com/repos/${repo}/releases/latest`
133145

134-
const res = await this.httpService.axiosRef.get(endpoint)
146+
const { githubToken } = await this.configService.get(
147+
'thirdPartyServiceIntegration',
148+
)
149+
const res = await this.httpService.axiosRef.get(endpoint, {
150+
headers: {
151+
Authorization: githubToken || `Bearer ${githubToken}`,
152+
},
153+
})
135154
return res.data.tag_name.replace(/^v/, '')
136155
}
137156

0 commit comments

Comments
 (0)