From 4f764a165ef0400d365ff21d338ba41c4447cd63 Mon Sep 17 00:00:00 2001 From: huangxingguang <591269@gmail.com> Date: Wed, 7 Jul 2021 16:08:43 +0800 Subject: [PATCH] fix gitlab update... --- src/components/SettingsTab.component.pug | 5 +- src/components/SettingsTab.component.ts | 2 + src/gist/GitHub.ts | 2 +- src/gist/GitLab.ts | 17 +++++- src/gist/Gitee.ts | 6 +- test/GitLab.ts | 76 +++++++++++++++--------- 6 files changed, 71 insertions(+), 37 deletions(-) diff --git a/src/components/SettingsTab.component.pug b/src/components/SettingsTab.component.pug index 314bbd4..fdc9056 100644 --- a/src/components/SettingsTab.component.pug +++ b/src/components/SettingsTab.component.pug @@ -13,6 +13,7 @@ div.sync-config-settings-tab option(ngValue='Off') Off option(ngValue='GitHub') GitHub option(ngValue='Gitee') Gitee + option(ngValue='GitLab') GitLab .row(*ngIf='config.store.syncConfig.type !== "Off"') .col-md-7 @@ -43,10 +44,10 @@ div.sync-config-settings-tab [(ngModel)]='config.store.syncConfig.gist', (ngModelChange)='config.save()', ) - .input-group-append(*ngIf='config.store.syncConfig.type === "GitHub"') + .input-group-append(*ngIf='config.store.syncConfig.type !== "Gitee"') button.btn.btn-secondary( (click)='viewGist()', - [disabled]="config.store.syncConfig.gist === null || config.store.syncConfig.gist.trim() === ''", + [disabled]="config.store.syncConfig.gist === null || config.store.syncConfig.gist.toString().trim() === ''", ) i.fa.fa-fw.fa-eye diff --git a/src/components/SettingsTab.component.ts b/src/components/SettingsTab.component.ts index 46f77fd..e7b0e54 100644 --- a/src/components/SettingsTab.component.ts +++ b/src/components/SettingsTab.component.ts @@ -120,6 +120,8 @@ export class SyncConfigSettingsTabComponent implements OnInit { viewGist(): void { if (this.config.store.syncConfig.type === 'GitHub') { this.platform.openExternal('https://gist.github.com/' + this.config.store.syncConfig.gist) + } else if (this.config.store.syncConfig.type === 'GitLab') { + this.platform.openExternal('https://gitlab.com/-/snippets/' + this.config.store.syncConfig.gist) } } diff --git a/src/gist/GitHub.ts b/src/gist/GitHub.ts index 76525ce..5a402b2 100644 --- a/src/gist/GitHub.ts +++ b/src/gist/GitHub.ts @@ -9,7 +9,7 @@ class GitHub extends Gist { return new Promise(async (resolve, reject) => { this.request({ - method: 'PATCH', + method: 'GET', url, headers: { Authorization: `Bearer ${this.token}` diff --git a/src/gist/GitLab.ts b/src/gist/GitLab.ts index 745b6e4..78c4c55 100644 --- a/src/gist/GitLab.ts +++ b/src/gist/GitLab.ts @@ -25,7 +25,13 @@ class GitLab extends Gist { const data = { title: "sync terminus config", visibility: "private", - files: gists.map(e => { return { file_path: e.name, content: e.value } }) + files: gists.map(e => { + let obj: any = { file_path: e.name, content: e.value }; + if (gist) { + obj.action = 'update'; + } + return obj + }) }; const url = gist ? `${this.baseUrl}/${gist}` : this.baseUrl; @@ -66,9 +72,14 @@ class GitLab extends Gist { url: `${this.baseUrl}/${gist}/files/main/${path}/raw`, headers: { Authorization: `Bearer ${this.token}` - } + }, + responseType: 'text' }).then(res => { - resolve(res.data); + if (typeof res.data === 'object') { + resolve(JSON.stringify(res.data)); + } else { + resolve(res.data); + } }).catch(reject); }); } diff --git a/src/gist/Gitee.ts b/src/gist/Gitee.ts index d72add3..95a0a81 100644 --- a/src/gist/Gitee.ts +++ b/src/gist/Gitee.ts @@ -44,12 +44,14 @@ class Gitee extends Gist { public: false, id: gist || '' }; + const method = gist ? 'PATCH' : 'POST'; + const url = gist ? `${this.baseUrl}/${gist}` : this.baseUrl; return new Promise(async (resolve, reject) => { try { const result = await axios.request({ - method: 'POST', - url: this.baseUrl, + method, + url, data, }) resolve(result.data.id); diff --git a/test/GitLab.ts b/test/GitLab.ts index 99b5694..40725c8 100644 --- a/test/GitLab.ts +++ b/test/GitLab.ts @@ -3,48 +3,48 @@ import GitLab from '../src/gist/GitLab' import { gitlabToken } from './token' -// test('id is invalid', async () => { +test('id is invalid', async () => { -// const gitLab = new GitLab('123456'); + const gitLab = new GitLab('123456'); -// try { -// await gitLab.get('abc'); -// } catch (error) { -// expect(error).toEqual('id is invalid'); -// } + try { + await gitLab.get('abc'); + } catch (error) { + expect(error).toEqual('id is invalid'); + } -// }) +}) -// test('sync unauthorized', async () => { +test('sync unauthorized', async () => { -// const gitLab = new GitLab('123456'); + const gitLab = new GitLab('123456'); -// try { -// await gitLab.sync(null, [new GistFile('test', 'test')]); -// } catch (error) { -// expect(error).toEqual('401 Unauthorized'); -// } + try { + await gitLab.sync(null, [new GistFile('test', 'test')]); + } catch (error) { + expect(error).toEqual('401 Unauthorized'); + } -// }) +}) -// test('del', async () => { +test('del', async () => { -// const gitLab = new GitLab(gitlabToken); + const gitLab = new GitLab(gitlabToken); -// try { -// await gitLab.del('test') -// } catch (error) { -// expect(error).toEqual('id is invalid'); -// } + try { + await gitLab.del('test') + } catch (error) { + expect(error).toEqual('id is invalid'); + } -// try { -// await gitLab.del('2144014') -// } catch (error) { -// expect(error).toEqual('Request failed with status code 404'); -// } + try { + await gitLab.del('2144014') + } catch (error) { + expect(error).toEqual('Request failed with status code 404'); + } -// }) +}) test('add and get and del', async () => { @@ -62,3 +62,21 @@ test('add and get and del', async () => { expect(await gitLab.del(id)).toBeTruthy(); }) + + +test('update', async () => { + + const gitLab = new GitLab(gitlabToken); + + const id = await gitLab.sync(null, [new GistFile('test', 'test')]); + + expect(id).not.toBeNull(); + + + const id2 = await gitLab.sync(id, [new GistFile('test', 'test')]); + + expect(id).toEqual(id2); + + expect(await gitLab.del(id)).toBeTruthy(); + +})