-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
180 lines (155 loc) · 5.16 KB
/
client.ts
File metadata and controls
180 lines (155 loc) · 5.16 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
import * as tar from 'tar';
import * as zlib from 'zlib';
import * as https from 'https';
import { Stream } from 'stream';
import mkdirp = require('mkdirp');
import { HttpClient } from '@contentstack/cli-utilities';
import GithubError from './error';
export default class GitHubClient {
readonly gitHubRepoUrl: string;
readonly gitHubUserUrl: string;
private readonly httpClient: HttpClient;
static parsePath(path?: string) {
const result = {
username: '',
repo: '',
};
if (path) {
const parts = path.split('/');
result.username = parts[0];
if (parts.length === 2) {
result.repo = parts[1];
}
}
return result;
}
constructor(public username: string, defaultStackPattern: string) {
this.gitHubRepoUrl = `https://api.github.com/repos/${username}`;
this.gitHubUserUrl = `https://api.github.com/search/repositories?q=org%3A${username}+in:name+${defaultStackPattern}`;
this.httpClient = HttpClient.create();
}
async getAllRepos(count = 100) {
try {
const response = await this.httpClient.get(`${this.gitHubUserUrl}&per_page=${count}`);
return response.data.items;
} catch (error) {
throw this.buildError(error);
}
}
async getLatest(repo: string, destination: string): Promise<void> {
const tarballUrl = await this.getLatestTarballUrl(repo);
const releaseStream = await this.streamRelease(tarballUrl);
await mkdirp(destination);
return this.extract(destination, releaseStream);
}
makeHeadApiCall(repo: string) {
return new Promise<any>((resolve, reject) => {
const { host, pathname } = new URL(this.gitHubRepoUrl);
const options = {
host,
method: 'HEAD',
path: `${pathname}/${repo}/contents`,
headers: { 'user-agent': 'node.js' },
};
https.request(options, resolve).on('error', reject).end();
});
}
makeGetApiCall(repo: string) {
return new Promise<any>((resolve, reject) => {
const { host, pathname } = new URL(this.gitHubRepoUrl);
const options = {
host,
method: 'GET',
path: `${pathname}/${repo}/contents`,
headers: { 'user-agent': 'node.js' },
};
https.request(options, (response) => {
let responseBody = '';
const data: any = { statusCode: response.statusCode, };
if (data.statusCode === 403) {
const xRateLimitReset = response.rawHeaders[response.rawHeaders.indexOf('X-RateLimit-Reset') + 1];
const startDate = (new Date()).getTime() / 1000;
const diffInSeconds = Number(xRateLimitReset) - startDate;
data.statusMessage = `Exceeded requests limit. Please try again after ${(diffInSeconds / 60).toFixed(1)} minutes.`;
}
response.on('data', (chunk) => {
responseBody += chunk.toString();
});
response.on('end', () => {
const body = JSON.parse(responseBody);
resolve({ ...data, data: body });
});
}).on('error', reject).end();
});
}
async checkIfRepoExists(repo: string) {
try {
/**
* Old code. Keeping it for reference.
*
* `const response: any = await this.httpClient.send('HEAD', `${this.gitHubRepoUrl}/${repo}/contents`);`
*
* `return response.status === 200;`
*/
const response: Record<string, any> = await this.makeHeadApiCall(repo);
return response.statusCode === 200;
} catch (error) {
console.log('Error', error);
// do nothing
}
return false;
}
async getMasterLocaleFromRepo(repo: string): Promise<string | null> {
try {
const response = await this.httpClient.get(
`https://raw.githubusercontent.com/${this.username}/${repo}/main/stack/locales/master-locale.json`,
);
if (response.data) {
const localeData = response.data;
const localeKey = Object.keys(localeData)[0];
if (localeKey && localeData[localeKey]?.code) {
return localeData[localeKey].code;
}
}
} catch (error) {
console.log('Could not fetch master locale from repository', error);
}
return null;
}
async getLatestTarballUrl(repo: string) {
try {
const response = await this.httpClient.get(`${this.gitHubRepoUrl}/${repo}/releases/latest`);
return response.data.tarball_url;
} catch (error) {
throw this.buildError(error);
}
}
async streamRelease(url: string): Promise<Stream> {
const response = await this.httpClient
.options({
responseType: 'stream',
})
.get(url);
this.httpClient.resetConfig();
return response.data as Stream;
}
async extract(destination: string, stream: Stream): Promise<void> {
return new Promise((resolve, reject) => {
stream
.pipe(zlib.createUnzip())
.pipe(
tar.extract({
cwd: destination,
strip: 1,
}),
)
.on('end', () => resolve())
.on('error', reject);
});
}
private buildError(error: any) {
const message = error.response.data?.error_message || error.response.statusText;
const status = error.response.status;
return new GithubError(message, status);
}
}