Permalink
Browse files

feat(cache): make cache deletable from repository settings

  • Loading branch information...
jkuri committed Sep 24, 2017
1 parent a56d810 commit 6e229d8b1addc00a6a5eb35ee639917795385f2e
@@ -49,7 +49,11 @@ import {
getConfigRawFile,
parseConfigFromRaw
} from './config';
import { getHttpJsonResponse } from './utils';
import {
getHttpJsonResponse,
getCacheFilesFromPattern,
deleteCacheFilesFromPattern
} from './utils';
import { startBuild } from './process-manager';
import * as multer from 'multer';

@@ -444,6 +448,24 @@ export function repositoryRoutes(): express.Router {
.catch(err => res.status(200).json({ data: false }));
});

router.get('/get-cache/:id', (req: express.Request, res: express.Response) => {
getRepository(req.params.id)
.then(repo => {
const searchPattern = `cache_${repo.full_name.replace('/', '-')}*`;
res.status(200).json({ data: getCacheFilesFromPattern(searchPattern) });
});
});

router.get('/delete-cache/:id', (req: express.Request, res: express.Response) => {
getRepository(req.params.id)
.then(repo => {
const searchPattern = `cache_${repo.full_name.replace('/', '-')}*`;
return deleteCacheFilesFromPattern(searchPattern);
})
.then(() => res.status(200).json({ data: true }))
.catch(err => res.status(200).json({ data: false }));
});

return router;
}

@@ -10,13 +10,14 @@ import {
writeFile
} from './fs';
import { readFileSync, writeFileSync } from 'fs';
import { ensureDirSync } from 'fs-extra';
import { ensureDirSync, statSync, remove } from 'fs-extra';
import { Observable } from 'rxjs';
import * as uuid from 'uuid';
import * as request from 'request';
import * as temp from 'temp';
import { blue, yellow, magenta, cyan, bold, red } from 'chalk';
import * as nodeRsa from 'node-rsa';
import * as glob from 'glob';

const defaultConfig = {
url: null,
@@ -109,6 +110,29 @@ export function getConfig(): string {
return config;
}

export function getCacheFilesFromPattern(pattern: string): any[] {
const cacheFolder = getFilePath('cache');
const search = glob.sync(join(cacheFolder, pattern));

return [].concat(search.map(result => {
return {
filename: result.split('/').pop(),
size: getHumanSize(statSync(result).size)
};
}));
}

export function deleteCacheFilesFromPattern(pattern): Promise<void> {
return new Promise((resolve, reject) => {
const cacheFolder = getFilePath('cache');
const search = glob.sync(join(cacheFolder, pattern));

Promise.all(search.map(result => remove(result)))
.then(() => resolve())
.catch(err => reject(err));
});
}

export function getHumanSize(bytes: number, decimals = 2): string {
if (!bytes) {
return '0 Bytes';
@@ -18,7 +18,7 @@
</a>
</div>
<div class="nav-right">
<a class="nav-item notification-item is-hidden-mobile" *ngIf="user" (click)="toggleNotify()">
<!-- <a class="nav-item notification-item is-hidden-mobile" *ngIf="user" (click)="toggleNotify()">
<img src="/images/icons/notification.svg">
<div class="notify-num-container" *ngIf="notifications?.length">
<span>{{ notifications?.length }}</span>
@@ -28,7 +28,7 @@
<span [ngClass]="{ error: notify.type === 'error' }">{{ notify.message }}</span>
</div>
</div>
</a>
</a> -->

<a class="nav-item user-item" (click)="toggleMenu()" [class.is-open]="menuDropped" *ngIf="user">
<img class="nav-avatar" [src]="user.avatar">
@@ -188,6 +188,46 @@ <h4>Environment Variables</h4>
</form>
</div>

<div class="column is-12 is-marginless is-paddingless">
<h2>Repository Cache</h2>
</div>

<div class="column is-6">
<form class="form">
<h4>Cached Dependencies and Directories</h4>
<p>Using cache can speed up builds as it stores contents that does not often change.</p>

<div *ngIf="cache?.length && !fetchingCache">
<div class="columns list-item list-item-mini" *ngFor="let c of cache">
<div class="column is-9">
<span class="bold">{{ c.filename }}</span>
</div>
<div class="column is-3 justify-center">
<span>{{ c.size }}</span>
</div>
</div>
</div>

<div class="message" *ngIf="!cache?.length">
<p>
<i class="ion ion-information"></i>
No cached files or folders found for this repository.
</p>
</div>

<div class="message green" *ngIf="cacheDeleted">
<p>
<i class="ion ion-checkmark-round"></i>
Cache has been deleted.
</p>
</div>

<button type="button" class="button is-fullwidth mtb20" *ngIf="cache?.length" [class.is-loading]="deletingCache" (click)="deleteCache()">Delete Cache</button>
</form>
</div>

<div class="column is-6"></div>

<div class="column is-12 is-marginless is-paddingless">
<h2>Repository Configuration</h2>
</div>
@@ -59,6 +59,10 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
configFile: string;
runningConfigBuild: boolean;
configBuildStatus: boolean;
cache: any[];
cacheDeleted: boolean;
fetchingCache: boolean;
deletingCache: boolean;

constructor(
private route: ActivatedRoute,
@@ -106,6 +110,7 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
this.fetch();
this.fetchBuilds();
this.fetchBadge();
this.fetchCache();

if (this.userId) {
this.fetchTokens();
@@ -289,6 +294,25 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
this.router.navigate(['build', buildId]);
}

fetchCache(): void {
this.fetchingCache = true;
this.cache = [];

this.api.fetchCacheForRepository(Number(this.id)).subscribe(cache => {
this.cache = cache;
this.fetchingCache = false;
});
}

deleteCache(): void {
this.deletingCache = true;
this.api.deleteCacheForRepository(Number(this.id)).subscribe(status => {
this.cacheDeleted = true;
this.deletingCache = false;
this.fetchCache();
});
}

checkRepositoryConfig(): void {
this.checkingConfig = true;
this.checkConfigResult = null;
@@ -196,6 +196,14 @@ export class ApiService {
return this.post(`${this.url}/repositories/run-build-config`, data, true);
}

fetchCacheForRepository(id: number): Observable<any> {
return this.get(`${this.url}/repositories/get-cache/${id}`, null, true);
}

deleteCacheForRepository(id: number): Observable<any> {
return this.get(`${this.url}/repositories/delete-cache/${id}`, null, true);
}

private get(url: string, searchParams: URLSearchParams = null, auth = false): Observable<any> {
let headers = new Headers();
if (auth) {

0 comments on commit 6e229d8

Please sign in to comment.