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: Mimicking getting data on github #924

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions src/api/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { OSS_XLAB_ENDPOINT, ErrorCode } from '../constant';
import request from '../helpers/request';

export const getMetricByName = async (owner: string, metricNameMap: Map<string, string>, metric: string) => {
export const getMetricByName = async (
platform: string,
owner: string,
metricNameMap: Map<string, string>,
metric: string
) => {
try {
return await request(`${OSS_XLAB_ENDPOINT}/github/${owner}/${metricNameMap.get(metric)}.json`);
return await request(`${OSS_XLAB_ENDPOINT}/${platform}/${owner}/${metricNameMap.get(metric)}.json`);
} catch (error) {
// the catched error being "404" means the metric file is not available so return a null
if (error === ErrorCode.NOT_FOUND) {
Expand Down Expand Up @@ -57,8 +62,8 @@ class MetaStore {
* Fetch the meta file and cache the response
* @param name repo name or user name
*/
private fetchMeta(name: string) {
const url = `${OSS_XLAB_ENDPOINT}/github/${name}/meta.json`;
private fetchMeta(platform: string, name: string) {
const url = `${OSS_XLAB_ENDPOINT}/${platform}/${name}/meta.json`;
const promise = fetch(url);
this.responseCache.set(name, promise);
}
Expand All @@ -68,9 +73,9 @@ class MetaStore {
* @param name repo name or user name
* @returns true if the meta file exists, false otherwise
*/
public async has(name: string) {
public async has(platform: string, name: string) {
if (!this.responseCache.has(name)) {
this.fetchMeta(name);
this.fetchMeta(platform, name);
}
const response = await this.responseCache.get(name)!;
if (!response.ok) {
Expand All @@ -85,8 +90,8 @@ class MetaStore {
* @param name repo name or user name
* @returns the parsed meta file if it exists, undefined otherwise
*/
public async get(name: string): Promise<CommonMeta | undefined> {
if (await this.has(name)) {
public async get(platform: string, name: string): Promise<CommonMeta | undefined> {
if (await this.has(platform, name)) {
const meta: CommonMeta = await this.responseCache
.get(name)!
// clone the response to avoid the response being used up
Expand Down
16 changes: 8 additions & 8 deletions src/api/developer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const metricNameMap = new Map([
['repo_network', 'repo_network'],
]);

export const getActivity = async (user: string) => {
return getMetricByName(user, metricNameMap, 'activity');
export const getActivity = async (platform: string, user: string) => {
return getMetricByName(platform, user, metricNameMap, 'activity');
};

export const getOpenrank = async (user: string) => {
return getMetricByName(user, metricNameMap, 'openrank');
export const getOpenrank = async (platform: string, user: string) => {
return getMetricByName(platform, user, metricNameMap, 'openrank');
};

export const getDeveloperNetwork = async (user: string) => {
return getMetricByName(user, metricNameMap, 'developer_network');
export const getDeveloperNetwork = async (platform: string, user: string) => {
return getMetricByName(platform, user, metricNameMap, 'developer_network');
};

export const getRepoNetwork = async (user: string) => {
return getMetricByName(user, metricNameMap, 'repo_network');
export const getRepoNetwork = async (platform: string, user: string) => {
return getMetricByName(platform, user, metricNameMap, 'repo_network');
};
72 changes: 36 additions & 36 deletions src/api/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,74 @@ const metricNameMap = new Map([
['activity_details', 'activity_details'],
]);

export const getActivity = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'activity');
export const getActivity = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'activity');
};

export const getOpenrank = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'openrank');
export const getOpenrank = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'openrank');
};

export const getParticipant = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'participant');
export const getParticipant = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'participant');
};

export const getContributor = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'contributor');
export const getContributor = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'contributor');
};

export const getForks = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'forks');
export const getForks = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'forks');
};

export const getStars = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'stars');
export const getStars = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'stars');
};

export const getIssuesOpened = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'issues_opened');
export const getIssuesOpened = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'issues_opened');
};

export const getIssuesClosed = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'issues_closed');
export const getIssuesClosed = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'issues_closed');
};

export const getIssueComments = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'issue_comments');
export const getIssueComments = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'issue_comments');
};

export const getPROpened = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'PR_opened');
export const getPROpened = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'PR_opened');
};

export const getPRMerged = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'PR_merged');
export const getPRMerged = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'PR_merged');
};

export const getPRReviews = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'PR_reviews');
export const getPRReviews = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'PR_reviews');
};

export const getMergedCodeAddition = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'merged_code_addition');
export const getMergedCodeAddition = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'merged_code_addition');
};

export const getMergedCodeDeletion = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'merged_code_deletion');
export const getMergedCodeDeletion = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'merged_code_deletion');
};

export const getMergedCodeSum = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'merged_code_sum');
export const getMergedCodeSum = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'merged_code_sum');
};

export const getDeveloperNetwork = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'developer_network');
export const getDeveloperNetwork = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'developer_network');
};

export const getRepoNetwork = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'repo_network');
export const getRepoNetwork = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'repo_network');
};

export const getActivityDetails = async (repo: string) => {
return getMetricByName(repo, metricNameMap, 'activity_details');
export const getActivityDetails = async (platform: string, repo: string) => {
return getMetricByName(platform, repo, metricNameMap, 'activity_details');
};
8 changes: 6 additions & 2 deletions src/feature-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import isRestorationVisit from './helpers/is-restoration-visit';
import shouldFeatureRun, { ShouldRunConditions } from './helpers/should-feature-run';
import optionsStorage from './options-storage';
import { throttle } from 'lodash-es';
import { getPlatform } from './helpers/platform-detection';

type FeatureInit = () => Promisable<void>;
type FeatureRestore = Function;
Expand Down Expand Up @@ -82,8 +83,11 @@ const globalReady = new Promise<object>(async (resolve) => {
document.documentElement.classList.add('hypercrx');

const options = await optionsStorage.getAll();
const updatedOptions = Object.fromEntries(
Object.entries(options).map(([key, value]) => [key.replace(/\//g, '-'), value])
);

resolve(options);
resolve(updatedOptions);
});

const setupPageLoad = async (id: FeatureId, config: InternalRunConfig): Promise<void> => {
Expand Down Expand Up @@ -113,6 +117,7 @@ const getFeatureID = (url: string): FeatureId => {
let name = pathComponents.pop()!.split('.')[0];
if (name === 'index') {
name = pathComponents.pop()!;
name = pathComponents.pop()! + '-' + name;
}
return `${prefix}${name}` as FeatureId;
};
Expand All @@ -124,7 +129,6 @@ const add = async (
): Promise<void> => {
/* Feature filtering and running */
const options = await globalReady;

// If the feature is disabled, skip it
if (!options[id as keyof typeof options]) {
log.info('↩️', 'Skipping', id);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/get-developer-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function getDeveloperNameByUrl() {
return pageDetect.utils.getUsername()!;
}

export async function isDeveloperWithMeta() {
return pageDetect.isUserProfile() && (await metaStore.has(getDeveloperName()));
export async function isDeveloperWithMeta(platform: string) {
return pageDetect.isUserProfile() && (await metaStore.has(platform, getDeveloperName()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PageDetect is a tool in the github-url-detection package used to detect the URL structure of the current Github page. For the Gitee platform, using it this way to determine if it is a developer profile page should be incorrect.Because Gitee and Github have different page structures and URL patterns. Similar methods to using github-url-detection to obtain information may not be effective when migrating to the Gitee platform. You can try it and see if it's like this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I have mentioned several times before in some tasks involving Gitee, and suggested that you can do something about encapsulating the function methods for obtaining user names, project names, and other information on the Gitee platform based on URLs or page elements.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you have free time afterwards, you can make corresponding attempts. At the same time, it is also possible to learn more about the specific methods and techniques used in the project during this process.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During this process, I will also do these things together.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out my problem.I'll follow up on the project.😊

Copy link
Collaborator

@wangyantong2000 wangyantong2000 Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out my problem.I'll follow up on the project.😊

Firstly, prioritize school exams. Wait until the exam is over.

}
export async function isUserProfile() {
return pageDetect.isUserProfile();
Expand Down
24 changes: 15 additions & 9 deletions src/helpers/get-repo-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ export async function isRepoRoot() {
/**
* check if the repository is public
*/
export async function isPublicRepo() {
const selector = 'meta[name="octolytics-dimension-repository_public"]';
await elementReady(selector);
// <meta name="octolytics-dimension-repository_public" content="true/false">
const isPublic = $(selector).attr('content') === 'true';
return pageDetect.isRepo() && isPublic;
export async function isPublicRepo(platform: string) {
if (platform === 'github') {
const selector = 'meta[name="octolytics-dimension-repository_public"]';
await elementReady(selector);
// <meta name="octolytics-dimension-repository_public" content="true/false">
const isPublic = $(selector).attr('content') === 'true';
return pageDetect.isRepo() && isPublic;
} else {
return true;
}
}

export async function isPublicRepoWithMeta() {
return (await isPublicRepo()) && ((await metaStore.has(getRepoName())) || (await metaStore.has(getRepoNameByPage())));
export async function isPublicRepoWithMeta(platform: string) {
return (
(await isPublicRepo(platform)) &&
((await metaStore.has(platform, getRepoNameByUrl())) || (await metaStore.has(platform, getRepoNameByPage())))
);
}
5 changes: 5 additions & 0 deletions src/helpers/is-gitee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const isGitee = (): boolean => {
return window.location.href.startsWith('https://gitee.com/');
};

export default isGitee;
8 changes: 8 additions & 0 deletions src/helpers/platform-detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import isGitee from './is-gitee';
import isGithub from './is-github';

export const getPlatform = (): 'github' | 'gitee' => {
if (isGithub()) return 'github';
if (isGitee()) return 'gitee';
throw new Error('Unsupported platform');
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as url from './giteeUrl';
import { handleMessage } from './handleMessage';
import type { FormInstance } from 'antd/lib/form';
import i18n from '../../../../helpers/i18n';
import { getGiteeToken } from '../../../../helpers/gitee-token';
import i18n from '../../../../../helpers/i18n';
import { getGiteeToken } from '../../../../../helpers/gitee-token';
import { generateBranchName, COMMIT_MESSAGE, COMMIT_MESSAGE_DOC } from './baseContent';
const t = i18n.t;
let userName: string | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { handleMessage } from './handleMessage';
import type { FormInstance } from 'antd/lib/form';
import { getGithubToken } from '../../../../helpers/github-token';
import i18n from '../../../../helpers/i18n';
import { getGithubToken } from '../../../../../helpers/github-token';
import i18n from '../../../../../helpers/i18n';
import { Octokit } from '@octokit/rest';
import { generateBranchName, COMMIT_MESSAGE, COMMIT_MESSAGE_DOC } from './baseContent';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import features from '../../../../feature-manager';
import features from '../../../../../feature-manager';
import View from './view';
import i18n from '../../../../helpers/i18n';
import i18n from '../../../../../helpers/i18n';
const featureId = features.getFeatureID(import.meta.url);
const t = i18n.t;
interface MatchedUrl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as githubService from './githubService';
import * as giteeService from './giteeService';
import { GET_FILE_URL as GITEE_FILE_URL } from './giteeUrl';
import { handleMessage } from './handleMessage';
import optionsStorage, { HypercrxOptions, defaults } from '../../../../options-storage';
import optionsStorage, { HypercrxOptions, defaults } from '../../../../../options-storage';
import { useTranslation } from 'react-i18next';
import { getGiteeToken } from '../../../../helpers/gitee-token';
import { getGithubToken } from '../../../../helpers/github-token';
import { getGiteeToken } from '../../../../../helpers/gitee-token';
import { getGithubToken } from '../../../../../helpers/github-token';
import { PR_TITLE, PR_CONTENT } from './baseContent';
interface Props {
filePath: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from 'react';
import { createRoot } from 'react-dom/client';
import $ from 'jquery';

import features from '../../../../feature-manager';
import getGithubTheme from '../../../../helpers/get-github-theme';
import { getRepoName, isPublicRepo } from '../../../../helpers/get-repo-info';
import features from '../../../../../feature-manager';
import getGithubTheme from '../../../../../helpers/get-github-theme';
import { getRepoName, isPublicRepo } from '../../../../../helpers/get-repo-info';
import View from './view';
import isGithub from '../../../../helpers/is-github';
import isGithub from '../../../../../helpers/is-github';
import { getPlatform } from '../../../../../helpers/platform-detection';

interface DocsMetaItem {
type: 'repo' | 'org';
Expand Down Expand Up @@ -65,7 +66,7 @@ const init = async (): Promise<void> => {

// TODO need a mechanism to remove extra listeners like this one
document.addEventListener('turbo:load', async () => {
if (await isPublicRepo()) {
if (await isPublicRepo(getPlatform())) {
if (repoName !== getRepoName()) {
repoName = getRepoName();
renderTo($(`#${featureId}`)[0]);
Expand All @@ -77,7 +78,7 @@ const init = async (): Promise<void> => {
};

features.add(featureId, {
include: [isGithub, isPublicRepo],
include: [isGithub, () => isPublicRepo(getPlatform())],
awaitDomReady: false,
init,
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {

import { getAnswer } from './service';
import './rcw.scss';
import exists from '../../../../helpers/exists';
import optionsStorage, { HypercrxOptions, defaults } from '../../../../options-storage';
import exists from '../../../../../helpers/exists';
import optionsStorage, { HypercrxOptions, defaults } from '../../../../../options-storage';
import { useTranslation } from 'react-i18next';
import '../../../../helpers/i18n';
import '../../../../../helpers/i18n';
interface Props {
theme: 'light' | 'dark';
currentRepo: string;
Expand Down
Loading
Loading