Skip to content

Commit

Permalink
Implement custom gitlab instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ffNethive authored and starxg committed Feb 20, 2022
1 parent d443b8a commit 51c0d30
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import GitHub from "gist/GitHub";
import GitLab from "gist/GitLab";


export function syncGist(type: 'Gitee' | 'GitHub' | 'GitLab', token: string, gistId: string, files: Array<GistFile>): Promise<string> {
export function syncGist(type: 'Gitee' | 'GitHub' | 'GitLab', token: string, baseUrl: string, gistId: string, files: Array<GistFile>): Promise<string> {

return new Promise(async (resolve, reject) => {
try {
Expand All @@ -15,7 +15,7 @@ export function syncGist(type: 'Gitee' | 'GitHub' | 'GitLab', token: string, gis
} else if (type === 'GitHub') {
gist = new GitHub(token);
} else if (type === 'GitLab') {
gist = new GitLab(token);
gist = new GitLab(token, baseUrl);
} else {
throw "unknown the type " + type;
}
Expand All @@ -28,7 +28,7 @@ export function syncGist(type: 'Gitee' | 'GitHub' | 'GitLab', token: string, gis
});
}

export function getGist(type: string, token: string, gistId: string): Promise<Map<string, GistFile>> {
export function getGist(type: string, token: string, baseUrl: string, gistId: string): Promise<Map<string, GistFile>> {

return new Promise(async (resolve, reject) => {
try {
Expand All @@ -39,7 +39,7 @@ export function getGist(type: string, token: string, gistId: string): Promise<Ma
} else if (type === 'GitHub') {
gist = new GitHub(token);
} else if (type === 'GitLab') {
gist = new GitLab(token);
gist = new GitLab(token, baseUrl);
} else {
throw "unknown the type " + type;
}
Expand Down
9 changes: 9 additions & 0 deletions src/components/SettingsTab.component.pug
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ div.sync-config-settings-tab

.row(*ngIf='config.store.syncConfig.type !== "Off"')
.col-md-7
.form-line(*ngIf='config.store.syncConfig.type == "GitLab"')
.header
.title Instance URL
input.form-control(
type='text',
placeholder="https://gitlab.com",
[(ngModel)]='config.store.syncConfig.baseUrl',
(ngModelChange)='config.save()',
)
.form-line
.header
.title Token
Expand Down
7 changes: 4 additions & 3 deletions src/components/SettingsTab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PasswordStorageService } from 'services/PasswordStorage.service';
import CryptoJS from 'crypto-js'
import * as yaml from 'js-yaml'
import { GistFile } from 'gist/Gist';
import GitLab from 'gist/GitLab';

/** @hidden */
@Component({
Expand Down Expand Up @@ -49,7 +50,7 @@ export class SyncConfigSettingsTabComponent implements OnInit {

async sync(isUploading: boolean): Promise<void> {

const { type, token, gist, encryption } = this.config.store.syncConfig;
const { type,baseUrl, token, gist, encryption } = this.config.store.syncConfig;
const selfConfig = JSON.parse(JSON.stringify(this.config.store.syncConfig));

if (!token) {
Expand Down Expand Up @@ -82,11 +83,11 @@ export class SyncConfigSettingsTabComponent implements OnInit {
// ssh password
files.push(new GistFile('ssh.auth.json', JSON.stringify(await this.getSSHPluginAllPasswordInfos(token))));

this.config.store.syncConfig.gist = await syncGist(type, token, gist, files);
this.config.store.syncConfig.gist = await syncGist(type, token, baseUrl, gist, files);

} else {

const result = await getGist(type, token, gist);
const result = await getGist(type, token, baseUrl, gist);

if (result.has('config.json')) {
const config = yaml.load(result.get('config.json').value) as any;
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class SyncConfigProvider extends ConfigProvider {
defaults = {
syncConfig: {
type: 'Off',
baseUrl: '',
token: '',
gist: '',
lastSyncTime: '-',
Expand Down
6 changes: 5 additions & 1 deletion src/gist/Gist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ class GistFile {
abstract class Gist {

protected readonly token: string;
protected customBaseUrl: string;

constructor(token: string) {

constructor(token: string)
constructor(token: string, customBaseUrl?: string) {
this.token = token;
this.customBaseUrl = customBaseUrl;
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/gist/GitLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ import { Gist, GistFile } from "./Gist";
import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
class GitLab extends Gist {

private readonly baseUrl = "https://gitlab.com/api/v4/snippets";
public static readonly defaultBaseUrl = "https://gitlab.com";
private readonly apiPath = "/api/v4/snippets";


constructor(token: string, customBaseUrl?: string) {
super(token);
this.customBaseUrl = GitLab.defaultBaseUrl;
if (customBaseUrl) {
this.customBaseUrl = customBaseUrl;
}


}



get(gist: string): Promise<Map<string, GistFile>> {
const url = `${this.baseUrl}/${gist}`;
const url = `${this.customBaseUrl}${this.apiPath}/${gist}`;

return new Promise(async (resolve, reject) => {
this.request({
Expand Down Expand Up @@ -34,7 +48,7 @@ class GitLab extends Gist {
})
};

const url = gist ? `${this.baseUrl}/${gist}` : this.baseUrl;
const url = gist ? `${this.customBaseUrl}${this.apiPath}/${gist}` : `${this.customBaseUrl}${this.apiPath}`;
const method = gist ? 'PUT' : 'POST';

return new Promise(async (resolve, reject) => {
Expand All @@ -55,7 +69,7 @@ class GitLab extends Gist {
return new Promise(async (resolve, reject) => {
this.request({
method: 'DELETE',
url: `${this.baseUrl}/${gist}`,
url: `${this.customBaseUrl}${this.apiPath}/${gist}`,
headers: {
Authorization: `Bearer ${this.token}`
}
Expand All @@ -69,7 +83,7 @@ class GitLab extends Gist {
return new Promise(async (resolve, reject) => {
this.request({
method: 'GET',
url: `${this.baseUrl}/${gist}/files/main/${path}/raw`,
url: `${this.customBaseUrl}${this.apiPath}/${gist}/files/main/${path}/raw`,
headers: {
Authorization: `Bearer ${this.token}`
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormsModule } from '@angular/forms'
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
import { ConfigProvider } from 'terminus-core';
import { SyncConfigProvider } from 'config';
import { SyncConfigSettingsTabComponent } from 'components/settingsTab.component'
import { SyncConfigSettingsTabComponent } from 'components/SettingsTab.component'
import { ToggleComponent } from 'components/toggle.component'

@NgModule({
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'
import { SyncConfigSettingsTabComponent } from 'components/settingsTab.component'
import { SyncConfigSettingsTabComponent } from 'components/SettingsTab.component'
import { SettingsTabProvider } from 'terminus-settings'
@Injectable()
export class SyncConfigSettingsTabProvider extends SettingsTabProvider {
Expand Down

0 comments on commit 51c0d30

Please sign in to comment.