Skip to content

Commit

Permalink
fix gitlab update...
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxingguang authored and starxg committed Jul 7, 2021
1 parent 7b8fa77 commit 4f764a1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/components/SettingsTab.component.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/components/SettingsTab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gist/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
17 changes: 14 additions & 3 deletions src/gist/GitLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/gist/Gitee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 47 additions & 29 deletions test/GitLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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();

})

0 comments on commit 4f764a1

Please sign in to comment.