From 39e10efcd1113e475b03cb055321a1b58f9306b3 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 14 Jun 2024 00:17:01 +0800 Subject: [PATCH] feat: support `gh_token` closes 1758 Signed-off-by: Innei --- .../src/modules/configs/configs.default.ts | 1 + apps/core/src/modules/configs/configs.dto.ts | 11 ++++++- .../modules/pageproxy/pageproxy.service.ts | 8 +++++ .../core/src/modules/update/update.service.ts | 29 +++++++++++++++---- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/apps/core/src/modules/configs/configs.default.ts b/apps/core/src/modules/configs/configs.default.ts index 7ee3f102a1..09bba0d043 100644 --- a/apps/core/src/modules/configs/configs.default.ts +++ b/apps/core/src/modules/configs/configs.default.ts @@ -65,6 +65,7 @@ export const generateDefaultConfig: () => IConfig = () => ({ }, thirdPartyServiceIntegration: { xLogSiteId: '', + githubToken: '', }, clerkOptions: { enable: false, diff --git a/apps/core/src/modules/configs/configs.dto.ts b/apps/core/src/modules/configs/configs.dto.ts index 79bb42372a..6cace61ab8 100644 --- a/apps/core/src/modules/configs/configs.dto.ts +++ b/apps/core/src/modules/configs/configs.dto.ts @@ -350,7 +350,7 @@ export class ClerkOptionsDto { /** * 第三方服务集成 */ -@JSONSchema({ title: '第三方服务集成' }) +@JSONSchema({ title: '第三方服务信息' }) export class ThirdPartyServiceIntegrationDto { @JSONSchemaPlainField('xLog SiteId', { description: '文章发布同步到 [xLog](https://xlog.app)', @@ -358,6 +358,15 @@ export class ThirdPartyServiceIntegrationDto { @IsOptional() @IsString() xLogSiteId?: string + + @JSONSchemaPasswordField('GitHub Token', { + description: + '用于调用 GitHub API,获取仓库信息等;可选参数,如果没有遇到限流问题,可以不填写', + }) + @IsOptional() + @IsString() + @SecretField + githubToken?: string } @JSONSchema({ title: '认证安全设置', 'ui:options': { type: 'hidden' } }) diff --git a/apps/core/src/modules/pageproxy/pageproxy.service.ts b/apps/core/src/modules/pageproxy/pageproxy.service.ts index c3fd9fae5a..e5974d65fe 100644 --- a/apps/core/src/modules/pageproxy/pageproxy.service.ts +++ b/apps/core/src/modules/pageproxy/pageproxy.service.ts @@ -29,9 +29,17 @@ export class PageProxyService { * @throws {Error} */ async getAdminLastestVersionFromGHRelease(): Promise { + const { githubToken } = await this.configs.get( + 'thirdPartyServiceIntegration', + ) // tag_name: v3.6.x const { tag_name } = await fetch( `https://api.github.com/repos/${PKG.dashboard.repo}/releases/latest`, + { + headers: { + Authorization: githubToken || `Bearer ${githubToken}`, + }, + }, ).then((data) => data.json()) return tag_name.replace(/^v/, '') diff --git a/apps/core/src/modules/update/update.service.ts b/apps/core/src/modules/update/update.service.ts index 15c7d28280..59c6e4ebde 100644 --- a/apps/core/src/modules/update/update.service.ts +++ b/apps/core/src/modules/update/update.service.ts @@ -8,6 +8,7 @@ import { Injectable } from '@nestjs/common' import { LOCAL_ADMIN_ASSET_PATH } from '~/constants/path.constant' import { HttpService } from '~/processors/helper/helper.http.service' import { spawnShell } from '~/utils' +import { ConfigsService } from '../configs/configs.service' import type { Subscriber } from 'rxjs' import { dashboard } from '~/../package.json' @@ -15,22 +16,33 @@ const { repo } = dashboard @Injectable() export class UpdateService { - constructor(protected readonly httpService: HttpService) {} + constructor( + protected readonly httpService: HttpService, + protected readonly configService: ConfigsService, + ) {} downloadAdminAsset(version: string) { const observable$ = new Observable((subscriber) => { ;(async () => { + const { githubToken } = await this.configService.get( + 'thirdPartyServiceIntegration', + ) const endpoint = `https://api.github.com/repos/${repo}/releases/tags/v${version}` subscriber.next(`Getting release info from ${endpoint}.\n`) - const json = await fetch(endpoint) - .then((res) => res.json()) + const result = await this.httpService.axiosRef + .get(endpoint, { + headers: { + Authorization: githubToken || `Bearer ${githubToken}`, + }, + }) + .catch((error) => { subscriber.next(chalk.red(`Fetching error: ${error.message}`)) subscriber.complete() return null }) - + const json = result?.data if (!json) { subscriber.next(chalk.red('Fetching error, json is empty. \n')) subscriber.complete() @@ -131,7 +143,14 @@ export class UpdateService { async getLatestAdminVersion() { const endpoint = `https://api.github.com/repos/${repo}/releases/latest` - const res = await this.httpService.axiosRef.get(endpoint) + const { githubToken } = await this.configService.get( + 'thirdPartyServiceIntegration', + ) + const res = await this.httpService.axiosRef.get(endpoint, { + headers: { + Authorization: githubToken || `Bearer ${githubToken}`, + }, + }) return res.data.tag_name.replace(/^v/, '') }