Skip to content

Commit

Permalink
feat: support gh_token closes 1758
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jun 13, 2024
1 parent 05e067f commit 39e10ef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/core/src/modules/configs/configs.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const generateDefaultConfig: () => IConfig = () => ({
},
thirdPartyServiceIntegration: {
xLogSiteId: '',
githubToken: '',
},
clerkOptions: {
enable: false,
Expand Down
11 changes: 10 additions & 1 deletion apps/core/src/modules/configs/configs.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,23 @@ export class ClerkOptionsDto {
/**
* 第三方服务集成
*/
@JSONSchema({ title: '第三方服务集成' })
@JSONSchema({ title: '第三方服务信息' })
export class ThirdPartyServiceIntegrationDto {
@JSONSchemaPlainField('xLog SiteId', {
description: '文章发布同步到 [xLog](https://xlog.app)',
})
@IsOptional()
@IsString()
xLogSiteId?: string

@JSONSchemaPasswordField('GitHub Token', {
description:
'用于调用 GitHub API,获取仓库信息等;可选参数,如果没有遇到限流问题,可以不填写',
})
@IsOptional()
@IsString()
@SecretField
githubToken?: string
}

@JSONSchema({ title: '认证安全设置', 'ui:options': { type: 'hidden' } })
Expand Down
8 changes: 8 additions & 0 deletions apps/core/src/modules/pageproxy/pageproxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ export class PageProxyService {
* @throws {Error}
*/
async getAdminLastestVersionFromGHRelease(): Promise<string> {
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/, '')
Expand Down
29 changes: 24 additions & 5 deletions apps/core/src/modules/update/update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ 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'

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<string>((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()
Expand Down Expand Up @@ -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/, '')
}

Expand Down

0 comments on commit 39e10ef

Please sign in to comment.