diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9150a5..a2851ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/src/__tests__/helper/start-test.ts b/src/__tests__/helper/start-test.ts index bc250eb..ff8090a 100644 --- a/src/__tests__/helper/start-test.ts +++ b/src/__tests__/helper/start-test.ts @@ -13,7 +13,7 @@ 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", }); } @@ -21,9 +21,27 @@ export class StartTest { 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", + }); + } } \ No newline at end of file diff --git a/src/__tests__/ping.test.ts b/src/__tests__/ping.test.ts index b97ecaa..0b974e3 100644 --- a/src/__tests__/ping.test.ts +++ b/src/__tests__/ping.test.ts @@ -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); \ No newline at end of file +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' + ]); + }); +}); + diff --git a/src/request-lib/axios-request.ts b/src/request-lib/axios-request.ts index dd1b36c..02919cd 100644 --- a/src/request-lib/axios-request.ts +++ b/src/request-lib/axios-request.ts @@ -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(url: string): Promise { return new Promise((resolve, reject) => { this.axios.get(url, { headers: { - 'Authorization': this.accessToken + 'Authorization': `Bearer ${this.accessToken}` } }).then((res) => { resolve(res.data as T); @@ -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

(platform: P): Promise { - switch (platform) { - case StoragePlatform.gitee: - return this.get(`${this.baseUrl}/api/v5/user`) as Promise; - case StoragePlatform.github: - return this.get(`${this.baseUrl}/api/v3/user`) as Promise; - case StoragePlatform.gitlab: - return this.get(`${this.baseUrl}/user`) as Promise; - default: - throw new Error('unsupported platform'); - } - } } diff --git a/src/request-lib/interfaces.ts b/src/request-lib/interfaces.ts index 9831d6f..3ba8604 100644 --- a/src/request-lib/interfaces.ts +++ b/src/request-lib/interfaces.ts @@ -14,11 +14,11 @@ export interface WxRequestOptions { fail: (errMsg: string, errNo: number) => void; } -export interface TinyRequest { - get(url: string): Promise; - post(url: string): void; - ping

(platform: P): Promise; -}; +// export interface TinyRequest { +// get(url: string): Promise; +// post(url: string): void; +// ping

(platform: P): Promise; +// }; export type RequestInstance = WxInstance | AxiosInstance; diff --git a/src/request-lib/request-factories.ts b/src/request-lib/request-factories.ts index 25164be..12d3881 100644 --- a/src/request-lib/request-factories.ts +++ b/src/request-lib/request-factories.ts @@ -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; diff --git a/src/request-lib/tiny-request.ts b/src/request-lib/tiny-request.ts new file mode 100644 index 0000000..0c07098 --- /dev/null +++ b/src/request-lib/tiny-request.ts @@ -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(url: string): Promise; + abstract post(url: string): void; + + async ping

(platform: P): Promise { + 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(url); + } +} \ No newline at end of file diff --git a/src/request-lib/wx-request.ts b/src/request-lib/wx-request.ts index fa1a145..885e241 100644 --- a/src/request-lib/wx-request.ts +++ b/src/request-lib/wx-request.ts @@ -1,8 +1,14 @@ -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(url: string): Promise { return new Promise((resolve, reject) => { @@ -10,9 +16,9 @@ export class WxRequest implements TinyRequest { 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) => { @@ -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

(platform: P): Promise { - switch (platform) { - case StoragePlatform.gitee: - return this.get(`${this.baseUrl}/api/v5/user`) as Promise; - case StoragePlatform.github: - return this.get(`${this.baseUrl}/api/v3/user`) as Promise; - case StoragePlatform.gitlab: - return this.get(`${this.baseUrl}/user`) as Promise; - default: - throw new Error('unsupported platform'); - } - } } diff --git a/src/storage-lib/gitee-storage.ts b/src/storage-lib/gitee-storage.ts index 189e378..0ecc1fa 100644 --- a/src/storage-lib/gitee-storage.ts +++ b/src/storage-lib/gitee-storage.ts @@ -14,7 +14,7 @@ export abstract class GiteeStorage implements TinyStorage { 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(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, diff --git a/src/storage-lib/interfaces.ts b/src/storage-lib/interfaces.ts index 34c9435..579c12c 100644 --- a/src/storage-lib/interfaces.ts +++ b/src/storage-lib/interfaces.ts @@ -1,4 +1,4 @@ -import { TinyRequest } from "../request-lib/interfaces"; +import { TinyRequest } from "../request-lib/tiny-request"; export interface TinyStorage { findById(): void;