Skip to content

Commit 7d6c79f

Browse files
committed
add heroku magic
1 parent 6b524c1 commit 7d6c79f

File tree

10 files changed

+327
-12
lines changed

10 files changed

+327
-12
lines changed

server-refactored-v3/src/apps/apps.service.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,122 @@ export class AppsService {
391391
}
392392
}
393393

394+
public async getAllAppsList(contextName: string) {
395+
this.logger.debug('getAppsList');
396+
this.kubectl.setCurrentContext(contextName);
397+
const apps = await this.kubectl.getAllAppsList(contextName);
398+
const appslist = [] as IApp[];
399+
for (const app of apps.items) {
400+
appslist.push(app.spec);
401+
}
402+
return appslist;
403+
}
404+
405+
406+
public async getAppsByRepoAndBranch(repository: string, branch: string) {
407+
this.logger.debug('getAppsByBranch: ' + branch);
408+
409+
const appslist = await this.getAllAppsList(
410+
process.env.KUBERO_CONTEXT || 'default',
411+
);
412+
const apps: IApp[] = [];
413+
for (const app of appslist) {
414+
if (app.branch === branch && repository === app.gitrepo?.ssh_url) {
415+
apps.push(app);
416+
}
417+
}
418+
return apps;
419+
}
420+
421+
422+
// delete a pr app in all pipelines that have review apps enabled and the same ssh_url
423+
public async deletePRApp(branch: string, title: string, ssh_url: string) {
424+
this.logger.debug('destroyPRApp');
425+
const websaveTitle = title.toLowerCase().replace(/[^a-z0-9-]/g, '-'); //TODO improve websave title
426+
427+
428+
const appslist = await this.getAllAppsList(
429+
process.env.KUBERO_CONTEXT || 'default',
430+
);
431+
432+
for (const app of appslist) {
433+
if (
434+
app.phase === 'review' &&
435+
app.gitrepo &&
436+
app.gitrepo.ssh_url === ssh_url &&
437+
app.branch === branch
438+
) {
439+
const user = {
440+
username: 'unknown',
441+
} as IUser;
442+
443+
this.deleteApp(app.pipeline, app.phase, websaveTitle, user);
444+
}
445+
}
446+
}
447+
448+
public async rebuildApp(app: IApp) {
449+
this.logger.debug(
450+
'rebuild App: ' +
451+
app.name +
452+
' in ' +
453+
app.pipeline +
454+
' phase: ' +
455+
app.phase,
456+
);
457+
458+
const contextName = await this.pipelinesService.getContext(
459+
app.pipeline,
460+
app.phase,
461+
);
462+
463+
if (contextName) {
464+
if (
465+
app.deploymentstrategy == 'docker' ||
466+
app.buildstrategy == undefined ||
467+
app.buildstrategy == 'plain'
468+
) {
469+
this.kubectl.restartApp(
470+
app.pipeline,
471+
app.phase,
472+
app.name,
473+
'web',
474+
contextName,
475+
);
476+
this.kubectl.restartApp(
477+
app.pipeline,
478+
app.phase,
479+
app.name,
480+
'worker',
481+
contextName,
482+
);
483+
} else {
484+
// rebuild for buildstrategy git/dockerfile or git/nixpacks
485+
this.triggerImageBuild(app.pipeline, app.phase, app.name);
486+
}
487+
488+
const m = {
489+
name: 'restartApp',
490+
user: '',
491+
resource: 'app',
492+
action: 'restart',
493+
severity: 'normal',
494+
message:
495+
'Rebuild app: ' +
496+
app.name +
497+
' in ' +
498+
app.pipeline +
499+
' phase: ' +
500+
app.phase,
501+
pipelineName: app.pipeline,
502+
phaseName: app.phase,
503+
appName: app.name,
504+
data: {},
505+
} as INotification;
506+
this.NotificationsService.send(m);
507+
}
508+
}
509+
394510
public async getTemplate(
395511
pipelineName: string,
396512
phaseName: string,

server-refactored-v3/src/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class AuthService {
4141
.update(pass)
4242
.digest('hex');
4343
const passwordMatch = await bcrypt.compare(pass, user.password);
44-
//if (passwordMatch) {
44+
//if (passwordMatch) {
4545
if (user.password === password || passwordMatch) {
4646
const { password, ...result } = user;
4747
return result;

server-refactored-v3/src/deployments/templates/buildpacks.yaml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ spec:
137137
# secret:
138138
# defaultMode: 0384
139139
# secretName: kubero-pull-secret
140-
`
140+
`;

server-refactored-v3/src/deployments/templates/dockerfile.yaml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ spec:
122122
secret:
123123
defaultMode: 384
124124
secretName: kubero-pull-secret
125-
`;
125+
`;

server-refactored-v3/src/deployments/templates/nixpacks.yaml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,4 @@ spec:
138138
secret:
139139
defaultMode: 384
140140
secretName: kubero-pull-secret
141-
`;
141+
`;

server-refactored-v3/src/kubernetes/kubernetes.service.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,28 @@ export class KubernetesService {
429429
return appslist;
430430
}
431431

432+
public async getAllAppsList(
433+
context: string,
434+
): Promise<IKubectlAppList> {
435+
this.kc.setCurrentContext(context);
436+
try {
437+
const appslist = await this.customObjectsApi.listClusterCustomObject(
438+
'application.kubero.dev',
439+
'v1alpha1',
440+
'kuberoapps',
441+
442+
);
443+
return appslist.body as IKubectlAppList;
444+
} catch (error) {
445+
//this.logger.debug(error);
446+
this.logger.debug('getAppsList: error getting apps');
447+
}
448+
const appslist = {} as IKubectlAppList;
449+
appslist.items = [];
450+
return appslist;
451+
}
452+
453+
432454
public async restartApp(
433455
pipelineName: string,
434456
phaseName: string,
@@ -1454,13 +1476,13 @@ export class KubernetesService {
14541476
let job = '';
14551477
switch (jobname) {
14561478
case 'buildpacks':
1457-
job = buildpacksTemplate
1479+
job = buildpacksTemplate;
14581480
break;
14591481
case 'dockerfile':
1460-
job = dockerfileTemplate
1482+
job = dockerfileTemplate;
14611483
break;
14621484
case 'nixpacks':
1463-
job = nixpacksTemplate
1485+
job = nixpacksTemplate;
14641486
break;
14651487
default:
14661488
break;

server-refactored-v3/src/repo/repo.controller.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
Param,
6+
Post,
7+
Req,
8+
UseGuards,
9+
} from '@nestjs/common';
210
import { RepoService } from './repo.service';
311
import {
412
ApiBearerAuth,
@@ -182,7 +190,13 @@ export class RepoController {
182190
required: true,
183191
enum: ['github', 'gitlab', 'bigbucket', 'gitea', 'gogs'],
184192
})
185-
async repositoryWebhook(@Body() body: any) {
186-
return 'Not implemented';
193+
async repositoryWebhook(
194+
@Param('provider') provider: string,
195+
@Body() body: any,
196+
@Req() req: Request,
197+
) {
198+
const ret: string = 'ok';
199+
this.repoService.handleWebhook(provider, req.headers, body);
200+
return ret;
187201
}
188202
}

server-refactored-v3/src/repo/repo.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ export interface IDeployKeyPair {
55
privKey: string;
66
privKeyBase64: string;
77
}
8+
export interface IWebhook {
9+
repoprovider: 'gitea' | 'gitlab' | 'github' | 'bitbucket' | 'gogs' | 'onedev';
10+
action: 'opened' | 'reopened' | 'closed' | undefined;
11+
event: string;
12+
delivery: string;
13+
body: any;
14+
branch: string;
15+
verified: boolean;
16+
repo: {
17+
ssh_url: string;
18+
};
19+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Module } from '@nestjs/common';
22
import { RepoController } from './repo.controller';
33
import { RepoService } from './repo.service';
4+
import { AppsService } from 'src/apps/apps.service';
45

56
@Module({
67
controllers: [RepoController],
7-
providers: [RepoService],
8+
providers: [RepoService, AppsService],
89
})
910
export class RepoModule {}

0 commit comments

Comments
 (0)