1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("async-file");
|
4 | const cli_ux_1 = require("cli-ux");
|
5 | const git = require("./git");
|
6 | const got = require('got');
|
7 | async function uploadArchive(url, filePath) {
|
8 | const request = got.stream.put(url, {
|
9 | headers: {
|
10 | 'content-length': (await fs.stat(filePath)).size,
|
11 | },
|
12 | });
|
13 | fs.createReadStream(filePath).pipe(request);
|
14 | return new Promise((resolve, reject) => {
|
15 | request.on('error', reject);
|
16 | request.on('response', resolve);
|
17 | });
|
18 | }
|
19 | async function prepareSource(ref, command) {
|
20 | const filePath = await git.createArchive(ref);
|
21 | const { body: source } = await command.heroku.post('/sources');
|
22 | await uploadArchive(source.source_blob.put_url, filePath);
|
23 | return Promise.resolve(source);
|
24 | }
|
25 | async function createSourceBlob(ref, command) {
|
26 | try {
|
27 | const githubRepository = await git.githubRepository();
|
28 | const { user, repo } = githubRepository;
|
29 | const { body: archiveLink } = await command.heroku.get(`https://kolkrabbi.heroku.com/github/repos/${user}/${repo}/tarball/${ref}`);
|
30 | if (await command.heroku.request(archiveLink.archive_link, { method: 'HEAD' })) {
|
31 | return archiveLink.archive_link;
|
32 | }
|
33 | }
|
34 | catch (error) {
|
35 |
|
36 | cli_ux_1.ux.debug(`Commit not found in pipeline repository: ${error}`);
|
37 | }
|
38 | const sourceBlob = await prepareSource(ref, command);
|
39 | return sourceBlob.source_blob.get_url;
|
40 | }
|
41 | exports.createSourceBlob = createSourceBlob;
|