-
-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathgithub.service.ts
More file actions
207 lines (188 loc) · 5.68 KB
/
Copy pathgithub.service.ts
File metadata and controls
207 lines (188 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"use strict";
import * as GitHubApi from "@octokit/rest";
import * as HttpsProxyAgent from "https-proxy-agent";
import * as vscode from "vscode";
import Commons from "../commons";
import { CloudSettings } from "../models/cloudSettings.model";
import { state } from "../state";
import { File } from "./file.service";
interface IEnv {
[key: string]: string | undefined;
http_proxy: string;
HTTP_PROXY: string;
}
interface IFixGistResponse extends Omit<GitHubApi.GistsGetResponse, "files"> {
files: any | GitHubApi.GistsGetResponseFiles;
}
export class GitHubService {
public userName: string = null;
public name: string = null;
private github: GitHubApi = null;
private GIST_JSON_EMPTY: any = {
description: "Visual Studio Code Sync Settings Gist",
public: false,
files: {
"settings.json": {
content: "// Empty"
},
"launch.json": {
content: "// Empty"
},
"keybindings.json": {
content: "// Empty"
},
"extensions.json": {
content: "// Empty"
},
"locale.json": {
content: "// Empty"
},
"keybindingsMac.json": {
content: "// Empty"
},
cloudSettings: {
content: "// Empty"
}
}
};
constructor(userToken: string, basePath: string) {
const githubApiConfig: GitHubApi.Options = {};
const proxyURL: string =
vscode.workspace.getConfiguration("http").get("proxy") ||
(process.env as IEnv).http_proxy ||
(process.env as IEnv).HTTP_PROXY;
if (basePath) {
githubApiConfig.baseUrl = basePath;
}
if (proxyURL) {
githubApiConfig.agent = new HttpsProxyAgent(proxyURL);
}
if (userToken !== null && userToken !== "") {
githubApiConfig.auth = `token ${userToken}`;
}
try {
this.github = new GitHubApi(githubApiConfig);
} catch (err) {
console.error(err);
}
if (userToken !== null && userToken !== "") {
this.github.users
.getAuthenticated({})
.then(res => {
this.userName = res.data.login;
this.name = res.data.name;
console.log(
"Sync : Connected with user : " + "'" + this.userName + "'"
);
})
.catch(err => {
console.error(err);
});
}
}
public AddFile(list: File[], GIST_JSON_B: any) {
for (const file of list) {
if (file.content !== "") {
GIST_JSON_B.files[file.gistName] = {};
GIST_JSON_B.files[file.gistName].content = file.content;
}
}
return GIST_JSON_B;
}
public async CreateEmptyGIST(
publicGist: boolean,
gistDescription: string
): Promise<string> {
if (publicGist) {
this.GIST_JSON_EMPTY.public = true;
} else {
this.GIST_JSON_EMPTY.public = false;
}
if (gistDescription !== null && gistDescription !== "") {
this.GIST_JSON_EMPTY.description = gistDescription;
}
try {
const res = await this.github.gists.create(this.GIST_JSON_EMPTY);
if (res.data && res.data.id) {
return res.data.id.toString();
} else {
console.error("ID is null");
console.log("Sync : " + "Response from GitHub is: ");
console.log(res);
}
} catch (err) {
console.error(err);
throw err;
}
}
// This should return GitHubApi.Response<GitHubApi.GistsGetResponse> but Types are wrong
public async ReadGist(
GIST: string
): Promise<GitHubApi.Response<IFixGistResponse>> {
const promise = this.github.gists.get({ gist_id: GIST });
const res = await promise.catch(err => {
if (String(err).includes("HttpError: Not Found")) {
return Commons.LogException(err, "Sync: Invalid Gist ID", true);
}
Commons.LogException(err, state.commons.ERROR_MESSAGE, true);
});
if (res) {
return res;
}
}
public async IsGistNewer(
GIST: string,
localLastDownload: Date
): Promise<boolean> {
const gist = await this.ReadGist(GIST);
if (!gist) {
return;
}
let gistCloudSetting: CloudSettings = null;
try {
gistCloudSetting = JSON.parse(gist.data.files.cloudSettings.content);
const gistLastUpload = new Date(gistCloudSetting.lastUpload);
if (!localLastDownload) {
return false;
}
return gistLastUpload > new Date(localLastDownload);
} catch (err) {
return false;
}
}
public UpdateGIST(
gistObject: any,
files: File[],
localFiles: File[] = files
): any {
const allFiles: string[] = Object.keys(gistObject.data.files);
for (const fileName of allFiles) {
let exists = false;
for (const settingFile of localFiles) {
if (settingFile.gistName === fileName) {
exists = true;
}
}
if (!exists && !fileName.startsWith("keybindings")) {
gistObject.data.files[fileName] = null;
}
}
gistObject.data = this.AddFile(files, gistObject.data);
return gistObject;
}
public async SaveGIST(gistObject: any): Promise<boolean> {
gistObject.gist_id = gistObject.id;
// tslint:disable-next-line:comment-format
//TODO : use github.gists.update when issue is fixed.
const promise = this.github.request("PATCH /gists/:gist_id", gistObject);
const res = await promise.catch(err => {
if (String(err).includes("HttpError: Not Found")) {
return Commons.LogException(err, "Sync: Invalid Gist ID", true);
}
Commons.LogException(err, state.commons.ERROR_MESSAGE, true);
});
if (res) {
return true;
}
}
}