Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize github/gitee token process #919

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api/githubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const githubRequest = async (endpoint: string, options: RequestInit = {})

try {
const response = await fetch(`https://api.github.com${endpoint}`, {
headers: {
Authorization: `Bearer ${token}`,
},
...options,
});
return response.json();
Expand Down
3 changes: 2 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export const OSS_XLAB_ENDPOINT = 'https://oss.open-digger.cn';
export const HYPERTRONS_CRX_NEW_ISSUE = 'https://github.com/hypertrons/hypertrons-crx/issues/new/choose';

export const HYPERCRX_GITHUB = 'https://github.com/hypertrons/hypertrons-crx';
export const OSS_URL = 'https://hypercrx-fastpr.oss-cn-beijing.aliyuncs.com/fast-pr-url-rules.cjs';

export const FAST_PR_CONFIG_URL = 'https://hypercrx.cn/configs/fast-pr-url-rules.cjs';
48 changes: 40 additions & 8 deletions src/helpers/gitee-token.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
export const saveGiteeToken = (token: string) => {
chrome.storage.sync.set({ gitee_token: token });
};
const GITEE_TOKEN_KEY = 'gitee_token';

export const getGiteeToken = (): Promise<string | null> => {
return new Promise((resolve) => {
chrome.storage.sync.get('gitee_token', (result) => {
resolve(result.gitee_token || null);
});
export const saveGiteeToken = (token: string, expireAt: number, refreshToken: string) => {
return chrome.storage.sync.set({
[GITEE_TOKEN_KEY]: {
token,
expireAt,
refreshToken,
},
});
};

export const getGiteeToken = async (): Promise<string | null> => {
const result = await chrome.storage.sync.get(GITEE_TOKEN_KEY);
if (!result || !result[GITEE_TOKEN_KEY]) {
return null;
}
const tokenInfo = result[GITEE_TOKEN_KEY];
if (!tokenInfo.expireAt || tokenInfo.expireAt > Date.now()) {
return tokenInfo.token || null;
} else {
console.log('Gitee token expired and need refesh');
const refreshReq = await fetch(
`https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token=${tokenInfo.refreshToken}`,
{ method: 'POST' }
);
const refreshData = await refreshReq.json();
if (!refreshData) {
console.log('Gitee token refresh failed');
return null;
}
await saveGiteeToken(
refreshData.access_token,
Date.now() + (refreshData.expires_in - 120) * 1000,
refreshData.refresh_token
);
return refreshData.access_token;
}
};

export const removeGiteeToken = () => {
return chrome.storage.sync.remove(GITEE_TOKEN_KEY);
};
12 changes: 9 additions & 3 deletions src/helpers/github-token.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const GITHUB_TOKEN_KEY = 'github_token';

export const saveGithubToken = (token: string) => {
chrome.storage.sync.set({ github_token: token });
return chrome.storage.sync.set({ [GITHUB_TOKEN_KEY]: token });
};

export const getGithubToken = (): Promise<string | null> => {
return new Promise((resolve) => {
chrome.storage.sync.get('github_token', (result) => {
resolve(result.github_token || null);
chrome.storage.sync.get(GITHUB_TOKEN_KEY, (result) => {
resolve(result[GITHUB_TOKEN_KEY] || null);
});
});
};

export const removeGithubToken = () => {
return chrome.storage.sync.remove(GITHUB_TOKEN_KEY);
};
69 changes: 21 additions & 48 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,25 @@
"openrank_icon": "openrank",
"contributors_participants_icon": "contributors and participants",
"merged_lines_icon": "code lines change",
"github_token_configuration": "GitHub Token Configuration",
"github_token_tooltip": "Enter your GitHub Token here for authentication.",
"github_token_description": "Providing a GitHub Token ensures that HyperCRX can securely and effectively access and operate on your GitHub data for personalized analysis.",
"github_token_how_to_generate": "How to generate a GitHub Token?",
"github_token_step1": "1. Log in to GitHub and go to Settings.",
"github_token_step2": "2. Select Developer settings.",
"github_token_step3": "3. Choose Personal access tokens, click Tokens (classic), then Generate a personal access token.",
"github_token_step4": "4. Set token details:",
"github_token_note": "Note",
"github_token_note_description": "A descriptive name, e.g., \"HyperCRX Token\".",
"github_token_expiration": "Expiration",
"github_token_expiration_description": "Choose the validity period.",
"github_token_scopes": "Scopes",
"github_token_scopes_description": "Select permission scopes, such as repo and workflow.",
"github_token_step5": "5. Click the Generate token button.",
"github_token_step6": "6. Copy the generated token and paste it into the input box below.",
"github_token_placeholder": "GitHub Token",
"github_token_save": "Save",
"github_token_edit": "Edit",
"github_token_test": "Test Token",
"github_token_error_empty": "Token cannot be empty",
"github_token_success_save": "Token saved successfully",
"github_token_success_valid": "Token is valid. Username: {{username}}",
"github_token_error_invalid": "Invalid token or request failed",
"gitee_token_configuration": "Gitee Token Configuration",
"gitee_token_tooltip": "Enter your Gitee Token here for authentication.",
"gitee_token_description": "Providing a Gitee Token ensures that HyperCRX can securely and effectively access and operate on your Gitee data for personalized analysis.",
"gitee_token_how_to_generate": "How to generate a Gitee Token?",
"gitee_token_step1": "1. Log in to Gitee and go to Personal Settings.",
"gitee_token_step2": "2. Find and click on the Private Token option in the left menu.",
"gitee_token_step3": "3. Click the Generate New Token button.",
"gitee_token_step4": "4. Set token details:",
"gitee_token_note": "Note",
"gitee_token_note_description": "A descriptive name, e.g., \"HyperCRX Token\".",
"gitee_token_scopes": "Scopes",
"gitee_token_scopes_description": "Select permission scopes, such as user_info and projects.",
"gitee_token_step5": "5. Click the Generate token button.",
"gitee_token_step6": "6. Copy the generated token and paste it into the input box below.",
"gitee_token_placeholder": "Gitee Token",
"gitee_token_save": "Save",
"gitee_token_edit": "Edit",
"gitee_token_test": "Test Token",
"gitee_token_error_empty": "Token cannot be empty",
"gitee_token_success_save": "Token saved successfully",
"gitee_token_success_valid": "Token is valid. Username: {{username}}",
"gitee_token_error_invalid": "Invalid token or request failed",

"github_account_configuration": "GitHub Account Binding",
"github_account_tooltip": "Authorize HyperCRX to access your GitHub account.",
"github_account_description": "Authorizing your GitHub account allows HyperCRX to securely and efficiently access GitHub data and perform other operations on your behalf, such as FastPR. The authorization information will only be securely stored in your browser; HyperCRX will not store or leak your authorization information.",
"github_account_bind": "Bind Account",
"github_account_unbind": "Unbind Account",
"github_account_no_bind": "No binding account found.",
"github_account_binded": "🎉Bind with GitHub account: {{username}}",
"github_account_bind_fail": "GitHub account binding failed.",

"gitee_account_configuration": "Gitee Account Binding",
"gitee_account_tooltip": "Authorize HyperCRX to access your Gitee account.",
"gitee_account_description": "Authorizing your Gitee account allows HyperCRX to securely and efficiently access Gitee data and perform other operations on your behalf, such as FastPR. The authorization information will only be securely stored in your browser; HyperCRX will not store or leak your authorization information.",
"gitee_account_bind": "Bind Account",
"gitee_account_unbind": "Unbind Account",
"gitee_account_no_bind": "No binding account found.",
"gitee_account_binded": "🎉Bind with Gitee account: {{username}}",
"gitee_account_bind_fail": "Gitee account binding failed.",

"fast_pr": "FastPR",
"pr_title_label": "PR Title",
"pr_title_rule": "Please enter the PR title",
Expand Down Expand Up @@ -151,6 +124,6 @@
"success_create_pr": "PR created successfully.\nPR URL: {{url}}",
"error_get_user_info": "Failed to get user info: {{status}}",
"error_get_file": "The path is a directory, not a file.",
"github_token_not_found": "GitHub token not found.\nPlease enter the token in the Settings page of the extension.",
"gitee_token_not_found": "Gitee token not found.\nPlease enter the token in the Settings page of the extension."
"github_token_not_found": "GitHub token not found.\nPlease bind your account in the Settings page of the extension.",
"gitee_token_not_found": "Gitee token not found.\nPlease bind your account in the Settings page of the extension."
}
69 changes: 21 additions & 48 deletions src/locales/zh_CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,52 +72,25 @@
"openrank_icon": "openrank值",
"contributors_participants_icon": "contributors和participants数",
"merged_lines_icon": "代码变化量",
"github_token_configuration": "GitHub 令牌配置",
"github_token_tooltip": "在此输入您的 GitHub 令牌以进行身份验证。",
"github_token_description": "提供 GitHub 令牌可确保 HyperCRX 能够安全有效地访问和操作您的 GitHub 数据以进行个性化分析。",
"github_token_how_to_generate": "如何生成 GitHub 令牌?",
"github_token_step1": "1. 登录 GitHub 并进入设置。",
"github_token_step2": "2. 选择开发者设置。",
"github_token_step3": "3. 选择个人访问令牌,Tokens (classic),然后生成个人访问令牌。",
"github_token_step4": "4. 设置令牌详细信息:",
"github_token_note": "注意",
"github_token_note_description": "描述性名称,例如:“HyperCRX Token”。",
"github_token_expiration": "过期时间",
"github_token_expiration_description": "选择有效期。",
"github_token_scopes": "权限范围",
"github_token_scopes_description": "选择权限范围,例如 repo 和 workflow。",
"github_token_step5": "5. 点击生成令牌按钮。",
"github_token_step6": "6. 复制生成的令牌并将其粘贴到下面的输入框中。",
"github_token_placeholder": "GitHub 令牌",
"github_token_save": "保存",
"github_token_edit": "编辑",
"github_token_test": "测试令牌",
"github_token_error_empty": "令牌不能为空",
"github_token_success_save": "令牌保存成功",
"github_token_success_valid": "令牌有效。用户名:{{username}}",
"github_token_error_invalid": "令牌无效或请求失败",
"gitee_token_configuration": "Gitee 令牌配置",
"gitee_token_tooltip": "在此输入您的 Gitee 令牌以进行身份验证。",
"gitee_token_description": "提供 Gitee 令牌可确保 HyperCRX 能够安全有效地访问和操作您的 Gitee 数据以进行个性化分析。",
"gitee_token_how_to_generate": "如何生成 Gitee 令牌?",
"gitee_token_step1": "1. 登录 Gitee 并进入设置。",
"gitee_token_step2": "2. 在左侧菜单中找到并点击 私人令牌 选项。",
"gitee_token_step3": "3. 点击 生成新令牌 按钮。",
"gitee_token_step4": "4. 设置令牌详细信息:",
"gitee_token_note": "注意",
"gitee_token_note_description": "描述性名称,例如:“HyperCRX Token”。",
"gitee_token_scopes": "权限范围",
"gitee_token_scopes_description": "选择权限范围,例如 user_info 和 projects。",
"gitee_token_step5": "5. 点击生成令牌按钮。",
"gitee_token_step6": "6. 复制生成的令牌并将其粘贴到下面的输入框中。",
"gitee_token_placeholder": "Gitee 令牌",
"gitee_token_save": "保存",
"gitee_token_edit": "编辑",
"gitee_token_test": "测试令牌",
"gitee_token_error_empty": "令牌不能为空",
"gitee_token_success_save": "令牌保存成功",
"gitee_token_success_valid": "令牌有效。用户名:{{username}}",
"gitee_token_error_invalid": "令牌无效或请求失败",

"github_account_configuration": "绑定 GitHub 账号",
"github_account_tooltip": "绑定 GitHub 账号",
"github_account_description": "为 HyperCRX 授权您的 GitHub 账号,可用于安全高效获取 GitHub 数据以及代理您的一些操作,例如 FastPR 功能。授权信息将安全的存储在您的浏览器本地,HyperCRX 并不会远程存储和泄露您的授权信息。",
"github_account_bind": "绑定账号",
"github_account_unbind": "解绑账号",
"github_account_no_bind": "当前无绑定账号",
"github_account_binded": "🎉当前绑定的 GitHub 账号为:{{username}}",
"github_account_bind_fail": "GitHub 账号绑定失败",

"gitee_account_configuration": "绑定 Gitee 账号",
"gitee_account_tooltip": "绑定 Gitee 账号",
"gitee_account_description": "为 HyperCRX 授权您的 Gitee 账号,可用于安全高效获取 Gitee 数据以及代理您的一些操作,例如 FastPR 功能。授权信息将安全的存储在您的浏览器本地,HyperCRX 并不会远程存储和泄露您的授权信息。",
"gitee_account_bind": "绑定账号",
"gitee_account_unbind": "解绑账号",
"gitee_account_no_bind": "当前无绑定账号",
"gitee_account_binded": "🎉当前绑定的 Gitee 账号为:{{username}}",
"gitee_account_bind_fail": "Gitee 账号绑定失败",

"fast_pr": "FastPR",
"pr_title_label": "PR 标题",
"pr_title_rule": "请输入 PR 的标题",
Expand Down Expand Up @@ -150,6 +123,6 @@
"success_create_pr": "PR创建成功。\nPR 地址为: {{url}}",
"error_get_user_info": "获取用户信息失败:{{status}}",
"error_get_file": "该路径是一个目录,而不是文件",
"github_token_not_found": "GitHub 的 token 没有找到。\n请记得在扩展程序的设置页面输入 GitHub token。",
"gitee_token_not_found": "Gitee 的 token 没有找到。\n请记得在扩展程序的设置页面输入 Gitee token。"
"github_token_not_found": "GitHub 的 token 没有找到。\n请记得在扩展程序的设置页面中绑定您的账号。",
"gitee_token_not_found": "Gitee 的 token 没有找到。\n请记得在扩展程序的设置页面中绑定您的账号。"
}
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"matches": ["<all_urls>"]
}
],
"permissions": ["storage"],
"permissions": ["storage", "identity"],
"host_permissions": ["<all_urls>"],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self';"
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Options/Options.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ a {
}

.token-options button {
padding: 8px 16px;
margin: 0 25px 20px;
padding: 8px 8px;
margin: 0 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
Expand Down
Loading
Loading