Skip to content

Commit

Permalink
Merge pull request #32 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Dec 9, 2023
2 parents b617e36 + 366be4d commit d7c6e48
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 77 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ jobs:
- name: Run Jest tests
run: npm run test
env:
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
GITEE_OWNER: ${{ secrets.GITEE_OWNER }}
GITEE_REPO: ${{ secrets.GITEE_REPO }}
GITEE_NUMBER: ${{ secrets.GITEE_NUMBER }}
TEST_GITEE_TOKEN: ${{ secrets.TEST_GITEE_TOKEN }}
TEST_GITEE_OWNER: ${{ secrets.TEST_GITEE_OWNER }}
TEST_GITEE_REPO: ${{ secrets.TEST_GITEE_REPO }}
TEST_GITEE_NUMBER: ${{ secrets.TEST_GITEE_NUMBER }}
TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }}
TEST_GITLAB_TOKEN: ${{ secrets.TEST_GITLAB_TOKEN }}


- name: Upload coverage reports to Codecov
Expand Down
26 changes: 22 additions & 4 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,35 @@ export class StartTest {
return createRequest({
requestType: RequestType.axios,
request: axios,
accessToken: process.env.GITEE_TOKEN as string,
accessToken: process.env.TEST_GITEE_TOKEN as string,
baseUrl: "https://gitee.com",
});
}

static getGiteeOptions(): GiteeStorageOptions {
return {
request: this.getGiteeRequest(),
owner: process.env.GITEE_OWNER as string,
repo: process.env.GITEE_REPO as string,
number: process.env.GITEE_NUMBER as string,
owner: process.env.TEST_GITEE_OWNER as string,
repo: process.env.TEST_GITEE_REPO as string,
number: process.env.TEST_GITEE_NUMBER as string,
}
}

static getGithubRequest() {
return createRequest({
requestType: RequestType.axios,
request: axios,
accessToken: process.env.TEST_GITHUB_TOKEN as string,
baseUrl: "https://api.github.com",
});
}

static getGitlabRequest() {
return createRequest({
requestType: RequestType.axios,
request: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
baseUrl: "https://gitlab.com",
});
}
}
113 changes: 92 additions & 21 deletions src/__tests__/ping.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,94 @@
import { StartTest } from "./helper/start-test";

test('Test Ping Gitee', async () => {
const request = StartTest.getGiteeRequest();
const res = await request.ping("gitee");
expect(Object.keys(res)).toEqual([
'id', 'login',
'name', 'avatar_url',
'url', 'html_url',
'remark', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'blog',
'weibo', 'bio',
'public_repos', 'public_gists',
'followers', 'following',
'stared', 'watched',
'created_at', 'updated_at',
'email'
]);
}, 30000);
describe('Test Ping Function', () => {
test('Test Ping Gitee', async () => {
const request = StartTest.getGiteeRequest();
const res = await request.ping("gitee");
expect(Object.keys(res)).toEqual([
'id', 'login',
'name', 'avatar_url',
'url', 'html_url',
'remark', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'blog',
'weibo', 'bio',
'public_repos', 'public_gists',
'followers', 'following',
'stared', 'watched',
'created_at', 'updated_at',
'email'
]);
});
test('Test Ping Github', async () => {
const request = StartTest.getGithubRequest();
const res = await request.ping("github");
expect(Object.keys(res)).toEqual([
'login', 'id',
'node_id', 'avatar_url',
'gravatar_id', 'url',
'html_url', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'site_admin',
'name', 'company',
'blog', 'location',
'email', 'hireable',
'bio', 'twitter_username',
'public_repos', 'public_gists',
'followers', 'following',
'created_at', 'updated_at'
]);
});
test('Test Ping Gitlab', async () => {
const request = StartTest.getGitlabRequest();
const res = await request.ping("gitlab");
expect(Object.keys(res)).toEqual([
'id',
'username',
'name',
'state',
'locked',
'avatar_url',
'web_url',
'created_at',
'bio',
'location',
'public_email',
'skype',
'linkedin',
'twitter',
'discord',
'website_url',
'organization',
'job_title',
'pronouns',
'bot',
'work_information',
'local_time',
'last_sign_in_at',
'confirmed_at',
'last_activity_on',
'email',
'theme_id',
'color_scheme_id',
'projects_limit',
'current_sign_in_at',
'identities',
'can_create_group',
'can_create_project',
'two_factor_enabled',
'external',
'private_profile',
'commit_email',
'shared_runners_minutes_limit',
'extra_shared_runners_minutes_limit',
'scim_identities'
]);
});
});

30 changes: 11 additions & 19 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { AxiosInstance } from 'axios';
import { GiteeUser, GithubUser, GitlabUser, StoragePlatformUserMap, TinyRequest } from './interfaces';
import { StoragePlatform } from '../enums';
import { TinyRequest } from './tiny-request';

export class AxiosRequest implements TinyRequest {
constructor(private axios: AxiosInstance, private baseUrl: string, private accessToken: string) { }
export class AxiosRequest extends TinyRequest {
constructor(
protected axios: AxiosInstance,
protected baseUrl: string,
protected accessToken: string
) {
super(axios, baseUrl, accessToken);
}

async get<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.get(url, {
headers: {
'Authorization': this.accessToken
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data as T);
Expand All @@ -22,21 +27,8 @@ export class AxiosRequest implements TinyRequest {
post(url: string) {
this.axios.post(url, undefined, {
headers: {
'Authorization': this.accessToken
'Authorization': `Bearer ${this.accessToken}`
}
});
}

async ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]> {
switch (platform) {
case StoragePlatform.gitee:
return this.get<GiteeUser>(`${this.baseUrl}/api/v5/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
case StoragePlatform.github:
return this.get<GithubUser>(`${this.baseUrl}/api/v3/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
case StoragePlatform.gitlab:
return this.get<GitlabUser>(`${this.baseUrl}/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
default:
throw new Error('unsupported platform');
}
}
}
10 changes: 5 additions & 5 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface WxRequestOptions {
fail: (errMsg: string, errNo: number) => void;
}

export interface TinyRequest {
get<T>(url: string): Promise<T>;
post(url: string): void;
ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
};
// export interface TinyRequest {
// get<T>(url: string): Promise<T>;
// post(url: string): void;
// ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
// };

export type RequestInstance = WxInstance | AxiosInstance;

Expand Down
3 changes: 2 additions & 1 deletion src/request-lib/request-factories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AxiosInstance } from "axios";
import { RequestInstance, TinyRequest, WxInstance } from "./interfaces";
import { RequestInstance, WxInstance } from "./interfaces";
import { AxiosRequest } from "./axios-request";
import { WxRequest } from "./wx-request";
import { TinyRequest } from "./tiny-request";

abstract class TinyRequestFactory {
abstract createRequest(instance: RequestInstance, baseUrl: string, accessToken: string): TinyRequest;
Expand Down
32 changes: 32 additions & 0 deletions src/request-lib/tiny-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { StoragePlatform } from "../enums";
import { RequestInstance, StoragePlatformUserMap } from "./interfaces";

export abstract class TinyRequest {
constructor(
protected instance: RequestInstance,
protected baseUrl: string,
protected accessToken: string
) { }

abstract get<T>(url: string): Promise<T>;
abstract post(url: string): void;

async ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]> {
let url: string;
switch (platform) {
case StoragePlatform.gitee:
url = `${this.baseUrl}/api/v5/user`;
break;
case StoragePlatform.github:
url = `${this.baseUrl}/user`;
break;
case StoragePlatform.gitlab:
url = `${this.baseUrl}/api/v3/user`;
break;
default:
throw new Error('Unsupported platform');
}

return this.get<StoragePlatformUserMap[typeof StoragePlatform[P]]>(url);
}
}
34 changes: 13 additions & 21 deletions src/request-lib/wx-request.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { StoragePlatform } from '../enums';
import { WxInstance, TinyRequest, GiteeUser, GithubUser, GitlabUser, StoragePlatformUserMap } from './interfaces';
import { WxInstance } from './interfaces';
import { TinyRequest } from './tiny-request';

export class WxRequest implements TinyRequest {
constructor(private wx: WxInstance, private baseUrl: string, private accessToken: string) { }
export class WxRequest extends TinyRequest {
constructor(
protected wx: WxInstance,
protected baseUrl: string,
protected accessToken: string
) {
super(wx, baseUrl, accessToken);
}

async get<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
this.wx.request({
url,
method: 'GET',
header: {
'Authorization': this.accessToken
'Authorization': `Bearer ${this.accessToken}`
},
success: (res: {data: string | Object | ArrayBuffer, statusCode: number}) => {
success: (res: { data: string | Object | ArrayBuffer, statusCode: number }) => {
resolve(res as T);
},
fail: (errMsg: string, errNo: number) => {
Expand All @@ -27,22 +33,8 @@ export class WxRequest implements TinyRequest {
// url,
// method: 'POST',
// header: {
// 'Authorization': this.accessToken,
// 'PRIVATE-TOKEN': this.accessToken
// 'Authorization': `Bearer ${this.accessToken}`
// }
// });
}

async ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]> {
switch (platform) {
case StoragePlatform.gitee:
return this.get<GiteeUser>(`${this.baseUrl}/api/v5/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
case StoragePlatform.github:
return this.get<GithubUser>(`${this.baseUrl}/api/v3/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
case StoragePlatform.gitlab:
return this.get<GitlabUser>(`${this.baseUrl}/user`) as Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
default:
throw new Error('unsupported platform');
}
}
}
2 changes: 1 addition & 1 deletion src/storage-lib/gitee-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export abstract class GiteeStorage<T> implements TinyStorage<T> {
const {owner, repo, number} = this.options;
const url = `${this.baseUrl}/api/v5/repos/${owner}/${repo}/issues/${number}/comments`;
const response = await this.options.request.get<GiteeResponse[]>(url);
return response.map(item => ({
return response.map((item: { id: any; body: string; created_at: any; updated_at: any; }) => ({
id: item.id,
...JSON.parse(item.body),
created_at: item.created_at,
Expand Down
2 changes: 1 addition & 1 deletion src/storage-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TinyRequest } from "../request-lib/interfaces";
import { TinyRequest } from "../request-lib/tiny-request";

export interface TinyStorage<T> {
findById(): void;
Expand Down

0 comments on commit d7c6e48

Please sign in to comment.