2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
|
|
|
import * as cp from 'child_process';
|
|
|
|
import {EventEmitter} from 'events';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as shell from 'shelljs';
|
2018-05-10 08:56:07 -04:00
|
|
|
import {HIDDEN_DIR_PREFIX} from '../common/constants';
|
2018-08-27 11:07:25 -04:00
|
|
|
import {assertNotMissingOrEmpty, computeShortSha, Logger} from '../common/utils';
|
2017-06-18 18:15:07 -04:00
|
|
|
import {ChangedPrVisibilityEvent, CreatedBuildEvent} from './build-events';
|
2018-08-15 08:47:45 -04:00
|
|
|
import {PreviewServerError} from './preview-error';
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
// Classes
|
|
|
|
export class BuildCreator extends EventEmitter {
|
2018-05-12 10:39:16 -04:00
|
|
|
|
2018-08-27 11:07:25 -04:00
|
|
|
private logger = new Logger('BuildCreator');
|
2018-05-12 10:39:16 -04:00
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Constructor
|
|
|
|
constructor(protected buildsDir: string) {
|
|
|
|
super();
|
2017-02-27 14:04:43 -05:00
|
|
|
assertNotMissingOrEmpty('buildsDir', buildsDir);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods - Public
|
2018-05-10 08:56:07 -04:00
|
|
|
public create(pr: number, sha: string, archivePath: string, isPublic: boolean): Promise<void> {
|
2017-06-25 15:13:03 -04:00
|
|
|
// Use only part of the SHA for more readable URLs.
|
2018-05-10 08:56:07 -04:00
|
|
|
sha = computeShortSha(sha);
|
2017-06-25 15:13:03 -04:00
|
|
|
|
2017-06-26 06:31:15 -04:00
|
|
|
const {newPrDir: prDir} = this.getCandidatePrDirs(pr, isPublic);
|
2017-02-06 13:40:28 -05:00
|
|
|
const shaDir = path.join(prDir, sha);
|
|
|
|
let dirToRemoveOnError: string;
|
|
|
|
|
2017-06-18 18:15:07 -04:00
|
|
|
return Promise.resolve().
|
|
|
|
// If the same PR exists with different visibility, update the visibility first.
|
2017-06-26 06:31:15 -04:00
|
|
|
then(() => this.updatePrVisibility(pr, isPublic)).
|
2017-06-18 18:15:07 -04:00
|
|
|
then(() => Promise.all([this.exists(prDir), this.exists(shaDir)])).
|
2017-02-06 13:40:28 -05:00
|
|
|
then(([prDirExisted, shaDirExisted]) => {
|
|
|
|
if (shaDirExisted) {
|
2017-08-03 04:40:39 -04:00
|
|
|
const publicOrNot = isPublic ? 'public' : 'non-public';
|
2018-08-15 08:47:45 -04:00
|
|
|
throw new PreviewServerError(409, `Request to overwrite existing ${publicOrNot} directory: ${shaDir}`);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dirToRemoveOnError = prDirExisted ? shaDir : prDir;
|
|
|
|
|
|
|
|
return Promise.resolve().
|
|
|
|
then(() => shell.mkdir('-p', shaDir)).
|
|
|
|
then(() => this.extractArchive(archivePath, shaDir)).
|
2017-06-18 18:15:07 -04:00
|
|
|
then(() => this.emit(CreatedBuildEvent.type, new CreatedBuildEvent(+pr, sha, isPublic))).
|
|
|
|
then(() => undefined);
|
2017-02-06 13:40:28 -05:00
|
|
|
}).
|
|
|
|
catch(err => {
|
|
|
|
if (dirToRemoveOnError) {
|
|
|
|
shell.rm('-rf', dirToRemoveOnError);
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:47:45 -04:00
|
|
|
if (!(err instanceof PreviewServerError)) {
|
|
|
|
err = new PreviewServerError(500, `Error while creating preview at: ${shaDir}\n${err}`);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
public updatePrVisibility(pr: number, makePublic: boolean): Promise<boolean> {
|
2017-06-26 06:31:15 -04:00
|
|
|
const {oldPrDir: otherVisPrDir, newPrDir: targetVisPrDir} = this.getCandidatePrDirs(pr, makePublic);
|
|
|
|
|
|
|
|
return Promise.
|
|
|
|
all([this.exists(otherVisPrDir), this.exists(targetVisPrDir)]).
|
|
|
|
then(([otherVisPrDirExisted, targetVisPrDirExisted]) => {
|
|
|
|
if (!otherVisPrDirExisted) {
|
|
|
|
// No visibility change: Either the visibility is up-to-date or the PR does not exist.
|
|
|
|
return false;
|
|
|
|
} else if (targetVisPrDirExisted) {
|
|
|
|
// Error: Directories for both visibilities exist.
|
2018-08-15 08:47:45 -04:00
|
|
|
throw new PreviewServerError(409,
|
|
|
|
`Request to move '${otherVisPrDir}' to existing directory '${targetVisPrDir}'.`);
|
2017-06-26 06:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Visibility change: Moving `otherVisPrDir` to `targetVisPrDir`.
|
|
|
|
return Promise.resolve().
|
|
|
|
then(() => shell.mv(otherVisPrDir, targetVisPrDir)).
|
|
|
|
then(() => this.listShasByDate(targetVisPrDir)).
|
|
|
|
then(shas => this.emit(ChangedPrVisibilityEvent.type, new ChangedPrVisibilityEvent(+pr, shas, makePublic))).
|
|
|
|
then(() => true);
|
|
|
|
}).
|
|
|
|
catch(err => {
|
2018-08-15 08:47:45 -04:00
|
|
|
if (!(err instanceof PreviewServerError)) {
|
|
|
|
err = new PreviewServerError(500, `Error while making PR ${pr} ${makePublic ? 'public' : 'hidden'}.\n${err}`);
|
2017-06-26 06:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Methods - Protected
|
|
|
|
protected exists(fileOrDir: string): Promise<boolean> {
|
|
|
|
return new Promise(resolve => fs.access(fileOrDir, err => resolve(!err)));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected extractArchive(inputFile: string, outputDir: string): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
const cmd = `tar --extract --gzip --directory "${outputDir}" --file "${inputFile}"`;
|
|
|
|
|
|
|
|
cp.exec(cmd, (err, _stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stderr) {
|
2018-05-12 10:39:16 -04:00
|
|
|
this.logger.warn(stderr);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-06-08 05:55:15 -04:00
|
|
|
shell.chmod('-R', 'a-w', outputDir);
|
2017-02-06 13:40:28 -05:00
|
|
|
shell.rm('-f', inputFile);
|
|
|
|
resolve();
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-06-18 18:15:07 -04:00
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
protected getCandidatePrDirs(pr: number, isPublic: boolean): {oldPrDir: string, newPrDir: string} {
|
2017-06-24 18:40:04 -04:00
|
|
|
const hiddenPrDir = path.join(this.buildsDir, HIDDEN_DIR_PREFIX + pr);
|
2018-05-10 08:56:07 -04:00
|
|
|
const publicPrDir = path.join(this.buildsDir, `${pr}`);
|
2017-06-18 18:15:07 -04:00
|
|
|
|
|
|
|
const oldPrDir = isPublic ? hiddenPrDir : publicPrDir;
|
|
|
|
const newPrDir = isPublic ? publicPrDir : hiddenPrDir;
|
|
|
|
|
|
|
|
return {oldPrDir, newPrDir};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected listShasByDate(inputDir: string): Promise<string[]> {
|
|
|
|
return Promise.resolve().
|
|
|
|
then(() => shell.ls('-l', inputDir) as any as Promise<(fs.Stats & {name: string})[]>).
|
|
|
|
// Keep directories only.
|
|
|
|
// (Also, convert to standard Array - ShellJS provides custom `sort()` method for sorting file contents.)
|
|
|
|
then(items => items.filter(item => item.isDirectory())).
|
|
|
|
// Sort by modification date.
|
|
|
|
then(items => items.sort((a, b) => a.mtime.getTime() - b.mtime.getTime())).
|
|
|
|
// Return directory names.
|
|
|
|
then(items => items.map(item => item.name));
|
|
|
|
}
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|