Skip to content

Commit

Permalink
add gitlab api
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Dec 21, 2023
1 parent 20bcee8 commit 5176c85
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 98 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/authenticate-gitee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { StartTest } from "./helper/start-test";

describe('Test Authenticate Gitee', () => {
test('Test Authenticate Gitee', async () => {
const request = StartTest.createGiteeRequest();
const res = await request.authenticate();
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'
])
});
});
26 changes: 26 additions & 0 deletions src/__tests__/authenticate-github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { StartTest } from "./helper/start-test";

describe('Test Authenticate Github', () => {
test('Test Authenticate Github', async () => {
const request = StartTest.createGithubRequest();
const res = await request.authenticate();
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'
]);
});
});
50 changes: 50 additions & 0 deletions src/__tests__/authenticate-gitlab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { StartTest } from "./helper/start-test";

describe('Test Authenticate Gitlab', () => {
test('Test Authenticate Gitlab', async () => {
const request = StartTest.createGitlabRequest();
const res = await request.authenticate();
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'
]);
});
});
95 changes: 0 additions & 95 deletions src/__tests__/authenticate.test.ts

This file was deleted.

88 changes: 88 additions & 0 deletions src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { ChatStorage } from "./helper/chat-storage";

describe('Use Gitlab Test Chat Storage', () => {
const chatStorage = new ChatStorage();

test('Test deleteAll Chat', async () => {
await chatStorage.deleteAll();
const detail = await chatStorage.find();
expect(detail.length).toEqual(0);
});

test('Test create & findById Chat', async () => {
await chatStorage.create({
participants: ['test-user', 'test-user-update'],
messages: [
{
from: 'test-user',
to: 'test-user-update',
message: 'hello'
}
]
});
const findResult = await chatStorage.find();
expect(findResult.length).toEqual(1);

const findByIdResult = await chatStorage.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});

test('Test updateById Chat', async () => {
const findResult = await chatStorage.find();
const updateResult = await chatStorage.updateById(findResult[0].id, {
participants: ['test-user', 'test-user-update'],
messages: [
{
from: 'test-user',
to: 'test-user-update',
message: 'hello world'
}
]
});
const findByIdResult = await chatStorage.findById(findResult[0].id);
expect(updateResult.participants).toEqual(findByIdResult.participants);
expect(updateResult.messages).toEqual(findByIdResult.messages);
expect(updateResult.id).toEqual(findByIdResult.id);
expect(updateResult.created_at).toEqual(findByIdResult.created_at);
expect(updateResult.updated_at).toEqual(findByIdResult.updated_at);
});

test('Test deleteById Chat failed', async () => {
try {
await chatStorage.deleteById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "404 Not found"
});
}
});

test('Test updateById Chat failed', async () => {
try {
await chatStorage.updateById(123, {
participants: ['test-user', 'test-user-update'],
messages: [
{
from: 'test-user',
to: 'test-user-update',
message: 'hello world'
}
]
});
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "404 Not found"
});
}
});

test('Test findById Chat failed', async () => {
try {
await chatStorage.findById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "404 Not found"
});
}
});
});
6 changes: 6 additions & 0 deletions src/__tests__/helper/chat-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseModel } from "../../storage-lib";

export interface ChatModel extends BaseModel {
participants: string[];
messages: Array<{from: string; to: string; message: string}>;
}
13 changes: 13 additions & 0 deletions src/__tests__/helper/chat-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GitlabStorage } from "../../storage-lib";
import { ChatModel } from "./chat-model";
import { StartTest } from "./start-test";

const options = {
request: StartTest.createGitlabRequest(),
issueNumber: StartTest.GITLAB_NUMBER
}
export class ChatStorage extends GitlabStorage<ChatModel> {
constructor() {
super(options);
}
}
4 changes: 3 additions & 1 deletion src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class StartTest {

static GITHUB_NUMBER = process.env.TEST_GITHUB_NUMBER as string;

static GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER as string;

static ENCRYPT_KEY = "MySecretPassphrase";

constructor() {
Expand Down Expand Up @@ -50,7 +52,7 @@ export class StartTest {
request: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
platform: 'gitlab',
projectId: ''
projectId: process.env.TEST_GITLAB_PROJECT_ID as string
});
}
}
6 changes: 5 additions & 1 deletion src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export abstract class BaseRequest {
return this.sendRequest<T>('PATCH', url, body);
}

put<T>(url: string, body: string): Promise<T> {
return this.sendRequest<T>('PUT', url, body);
}

async authenticate() {
switch(this.options.platform) {
case StoragePlatform.gitee:
Expand Down Expand Up @@ -72,7 +76,7 @@ export abstract class BaseRequest {
case StoragePlatform.github:
return `${this.baseUrl}/repos/${this.options.owner}/${this.options.repo}`;
case StoragePlatform.gitlab:
return `${this.baseUrl}/api/v4/user`;
return `${this.baseUrl}/api/v4/projects/${this.options.projectId}`;
default:
throw new Error('Unsupported Platform');
}
Expand Down
Loading

0 comments on commit 5176c85

Please sign in to comment.