-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.js
285 lines (258 loc) · 8.4 KB
/
github.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const request = require('request-promise-native');
const path = require('path');
const fs = require('fs-extra');
const { logger } = require('./logger');
const { logErrorInstance } = require('./errorHandlers');
const { extractZipArchive } = require('./archive');
const { GITHUB_RELEASE_TYPES } = require('./lib/constants');
const { i18n } = require('./lib/lang');
const { DEFAULT_USER_AGENT_HEADERS } = require('./http/requestOptions');
const GITHUB_AUTH_HEADERS = {
authorization:
global && global.githubToken ? `Bearer ${global.githubToken}` : null,
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
/**
* @param {String} filePath - path where config file is stored
* @param {String} repoPath - path to the github repository ({username}/{reponame})
* @param {boolean} isCustomPath - true if users pass a path flag to the CLI
* @param {String | undefined} - branch to fetch from. if undefined, default branch used
* @returns {Buffer|Null} Zip data buffer
*/
async function fetchJsonFromRepository(
repoPath,
filePath,
ref,
isCustomPath = false
) {
try {
const URI = `https://raw.githubusercontent.com/${repoPath}/${ref}/${filePath}`;
logger.debug(`Fetching ${URI}...`);
return await request.get(URI, {
json: true,
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
});
} catch (err) {
if (isCustomPath && err.statusCode === 404) {
logger.error(
i18n(`cli.lib.prompts.createProjectPrompt.errors.failedToFetchJson`)
);
process.exit(1);
} else {
logger.error('An error occured fetching JSON file.');
}
logErrorInstance(err);
}
return null;
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
/**
* https://developer.github.com/v3/repos/releases/#get-the-latest-release
* @param {String} repoPath - Path to GitHub repository to fetch. ({username}/{reponame})
* @param {String} tag - Git tag to fetch for. If omitted latest will be fetched.
*/
async function fetchReleaseData(repoPath, tag = '') {
tag = tag.trim().toLowerCase();
if (tag.length && tag[0] !== 'v') {
tag = `v${tag}`;
}
const URI = tag
? `https://api.github.com/repos/${repoPath}/releases/tags/${tag}`
: `https://api.github.com/repos/${repoPath}/releases/latest`;
try {
return await request.get(URI, {
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
json: true,
});
} catch (err) {
logger.error(
`Failed fetching release data for ${tag || 'latest'} project.`
);
if (tag && err.statusCode === 404) {
logger.error(`project ${tag} not found.`);
}
}
return null;
}
/**
* @param {String} repoPath - Path to GitHub repository to download. ({username}/{reponame})
* @param {String} tag - Git tag to fetch for. If omitted latest will be fetched.
* @param {String} releaseType - type of content
* @returns {Buffer|Null} Zip data buffer
*/
async function downloadGithubRepoZip(
repoPath,
tag = '',
releaseType = GITHUB_RELEASE_TYPES.RELEASE,
ref
) {
try {
let zipUrl;
if (releaseType === GITHUB_RELEASE_TYPES.REPOSITORY) {
logger.log(`Fetching ${releaseType} with name ${repoPath}...`);
zipUrl = `https://api.github.com/repos/${repoPath}/zipball${
ref ? `/${ref}` : ''
}`;
} else {
const releaseData = await fetchReleaseData(repoPath, tag);
if (!releaseData) return;
({ zipball_url: zipUrl } = releaseData);
const { name } = releaseData;
logger.log(`Fetching ${name}...`);
}
const zip = await request.get(zipUrl, {
encoding: null,
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
});
logger.debug('Completed project fetch.');
return zip;
} catch (err) {
logger.error('An error occured fetching the project source.');
logErrorInstance(err);
}
return null;
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
/**
* Writes a copy of the boilerplate project to dest.
* @param {String} dest - Dir to write project src to.
* @param {String} type - Type of project to create.
* @param {String} repoPath - Path to GitHub repository to clone. ({username}/{reponame})
* @param {String} sourceDir - Directory in project that should get copied.
* @param {Object} options
* @returns {Boolean} `true` if successful, `false` otherwise.
*/
async function cloneGitHubRepo(dest, type, repoPath, sourceDir, options = {}) {
const { themeVersion, projectVersion, releaseType, ref } = options;
const tag = projectVersion || themeVersion;
const zip = await downloadGithubRepoZip(repoPath, tag, releaseType, ref);
const repoName = repoPath.split('/')[1];
const success = await extractZipArchive(zip, repoName, dest, { sourceDir });
if (success) {
logger.success(`Your new ${type} has been created in ${dest}`);
}
return success;
}
async function getGitHubRepoContentsAtPath(repoPath, path, ref) {
const refQuery = ref ? `?ref=${ref}` : '';
const contentsRequestUrl = `https://api.github.com/repos/${repoPath}/contents/${path}${refQuery}`;
return request.get(contentsRequestUrl, {
json: true,
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
});
}
async function fetchGitHubRepoContentFromDownloadUrl(dest, downloadUrl) {
const resp = await request.get(downloadUrl, {
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
});
fs.outputFileSync(dest, resp, 'utf8');
}
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
/**
* Writes files from a HubSpot public repository to the destination folder
* @param {String} dest - Dir to write contents to
* @param {String} repoPath - Path to GitHub repository to fetch contents from ({username}/{reponame})
* @param {String} path - Path to obtain contents from within repository
* @returns {Boolean} `true` if successful, `false` otherwise.
*/
async function downloadGitHubRepoContents(
repoPath,
contentPath,
dest,
options = {
filter: false,
ref: '',
}
) {
fs.ensureDirSync(path.dirname(dest));
try {
const contentsResp = await getGitHubRepoContentsAtPath(
repoPath,
contentPath,
options.ref
);
const downloadContentRecursively = async contentPiece => {
const {
path: contentPiecePath,
download_url,
type: contentPieceType,
} = contentPiece;
const downloadPath = path.join(
dest,
contentPiecePath.replace(contentPath, '')
);
if (
typeof options.filter === 'function' &&
!options.filter(contentPiecePath, downloadPath)
) {
return Promise.resolve(null);
}
logger.debug(
`Downloading content piece: ${contentPiecePath} from ${download_url} to ${downloadPath}`
);
if (contentPieceType === 'dir') {
const innerDirContent = await getGitHubRepoContentsAtPath(
repoPath,
contentPiecePath,
options.ref
);
return Promise.all(innerDirContent.map(downloadContentRecursively));
} else {
return fetchGitHubRepoContentFromDownloadUrl(
downloadPath,
download_url,
{
headers: {
...DEFAULT_USER_AGENT_HEADERS,
...GITHUB_AUTH_HEADERS,
},
}
);
}
};
let contentPromises;
if (Array.isArray(contentsResp)) {
contentPromises = contentsResp.map(downloadContentRecursively);
return Promise.all(contentPromises);
} else {
contentPromises = downloadContentRecursively(contentsResp);
return Promise.resolve(contentPromises);
}
} catch (e) {
if (e.statusCode === 404) {
if (e.error && e.error.message) {
throw new Error(`Failed to fetch contents: ${e.error.message}`);
}
}
if (e.statusCode >= 500 && e.statusCode <= 599) {
if (e.error && e.error.message) {
throw new Error(`Failed to fetch contents: ${e.error.message}`);
} else {
throw new Error('Failed to fetch contents: Check the status of GitHub');
}
}
throw new Error(e);
}
}
module.exports = {
cloneGitHubRepo,
downloadGitHubRepoContents,
fetchJsonFromRepository,
fetchReleaseData,
};