Skip to content

Commit

Permalink
Merge pull request #259 from opensumi/feat/gitee-auth
Browse files Browse the repository at this point in the history
feat: support gitee oauth
  • Loading branch information
hacke2 authored Feb 10, 2025
2 parents f543406 + 878c16b commit 58a0ff7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
14 changes: 2 additions & 12 deletions packages/code-api/src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CodePlatform, ICodePlatform } from './types';
import merge from 'lodash/merge';

export interface ICodePlatformConfig {
platform: CodePlatform | string;
Expand Down Expand Up @@ -219,18 +220,7 @@ export class CodePlatformRegistry {
if (!provider) {
return;
}
if (Array.isArray(data.hostname)) {
provider.hostname.push(...data.hostname);
}
if (data.origin) {
provider.origin = data.origin;
}
if (data.endpoint) {
provider.endpoint = data.endpoint;
}
if (data.token) {
provider.token = data.token;
}
merge(provider, data);
}

load(configs: Record<string, ICodePlatformConfig>) {
Expand Down
59 changes: 57 additions & 2 deletions packages/code-api/src/gitee/gitee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class GiteeAPIService implements ICodeAPIService {

private redirectUrl = this.config.redirectUrl;

private client_id = '';
private client_id = this.config.clientId;

get PRIVATE_TOKEN() {
if (!this._PRIVATE_TOKEN) {
Expand Down Expand Up @@ -104,9 +104,64 @@ export class GiteeAPIService implements ICodeAPIService {
throw new Error('Method not implemented.');
}

private sleep(t: number) {
return new Promise(res => {
setTimeout(() => {
res(undefined);
}, t);
});
}

private async checkAccessToken(): Promise<boolean> {
return new Promise<boolean>(async (resolve, reject) => {
resolve(true);
// 一开始就弹出弹窗会有颜色闪烁问题,先临时 sleep 处理,后面再优化
await this.sleep(300);
const btn = await this.helper.showDialog({
message: '检测到 OAuth 未授权',
type: MessageType.Info,
closable: false,
buttons: ['去授权'],
});

let popupWindow;
if (btn === '去授权') {
popupWindow = window.open(
`${this.config.origin}/oauth/authorize?client_id=${this.client_id}&response_type=code&redirect_uri=${this.redirectUrl}`,
'_blank',
'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=800,height=520,top=150,left=150'
);
}
const handleMessage = async (event: MessageEvent) => {
try {
const { data } = event;
if (isObject(data) && data.type === 'gitee') {
const {
data: { token },
} = data;
popupWindow?.close();
if (!token) {
resolve(false);
return;
}

// 清理过期的 token;
this.clearToken();

this.helper.GITEE_TOKEN = token;
this.helper.reinitializeCodeService(true);
if(this.helper.GITEE_TOKEN){
// 需重新加载否则scm插件报错
window.location.reload();
}
resolve(true);
}
} catch (error) {
console.log(error);
reject(error);
}
};
window.addEventListener('message', handleMessage);
});
}

Expand Down Expand Up @@ -470,6 +525,6 @@ export class GiteeAPIService implements ICodeAPIService {

clearToken() {
this._PRIVATE_TOKEN = null;
this.helper.ATOMGIT_TOKEN = null;
this.helper.GITEE_TOKEN = null;
}
}

0 comments on commit 58a0ff7

Please sign in to comment.