Skip to content

Commit

Permalink
feat(backend): correct pipelines with external stages
Browse files Browse the repository at this point in the history
  • Loading branch information
dr460nf1r3 committed Jan 1, 2025
1 parent aaa2cb5 commit ddfe4ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
5 changes: 3 additions & 2 deletions backend/src/gitlab/gitlab.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InMemoryCache } from '../cache.source';
import { MINUTE } from 'nestjs-omacache';
import { PipelineWebhook } from './interfaces';
import { ConfigService } from '@nestjs/config';
import { PipelineWithExternalStatus } from '@./shared-lib';

@Controller('gitlab')
export class GitlabController {
Expand All @@ -27,12 +28,12 @@ export class GitlabController {
@InMemoryCache({
key: 'some',
kind: 'temporal',
ttl: 10 * MINUTE,
ttl: MINUTE,
paramIndex: [0],
})
@AllowAnonymous()
@Get('pipelines/:page')
async getPipelines(@Param('page', ParseIntPipe) page?: number) {
async getPipelines(@Param('page', ParseIntPipe) page?: number): Promise<PipelineWithExternalStatus[]> {
return await this.gitlabService.getLastPipelines({ page });
}
}
69 changes: 11 additions & 58 deletions backend/src/gitlab/gitlab.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CommitStatusSchema, Gitlab, PipelineSchema } from '@gitbeaker/rest';
import { PipelineWebhook } from './interfaces';
import { PipelineWithExternalStatus } from '@./shared-lib';

@Injectable()
export class GitlabService {
PIPELINE_URL = 'https://gitlab.com/api/v4/projects/:id/pipelines';
STATUS_URL = 'https://gitlab.com/api/v4/projects/:id/repository/commits/:commit/statuses';

api = new Gitlab({
token: this.configService.getOrThrow<string>('CAUR_GITLAB_TOKEN'),
});

private readonly chaoticId = this.configService.getOrThrow<string>('CAUR_GITLAB_ID_CAUR');
private readonly garudaId = this.configService.getOrThrow<string>('CAUR_GITLAB_ID_GARUDA');
private readonly botEmail = this.configService.getOrThrow<string>('CAUR_AUTO_COMMIT_AUTHOR');
private readonly gitlabWebhookToken = this.configService.getOrThrow<string>('CAUR_GITLAB_WEBHOOK_TOKEN');

constructor(
private readonly configService: ConfigService,
Expand Down Expand Up @@ -44,71 +41,26 @@ export class GitlabService {
});
}

async getLastPipelines(options: { page?: number }) {
const fetchPromises: Promise<{ commit: CommitStatusSchema[]; pipeline: PipelineSchema }>[] = [];
const returnValue = {
succeeded: [],
failed: [],
cancelled: [],
};

async getLastPipelines(options: { page?: number }): Promise<PipelineWithExternalStatus[]> {
try {
const succeededPipelines: Promise<PipelineSchema[]> = this.api.Pipelines.all(this.chaoticId, {
maxPages: 1,
page: options.page,
status: 'success',
});
const failedPipelines: Promise<PipelineSchema[]> = this.api.Pipelines.all(this.chaoticId, {
maxPages: 1,
page: options.page,
status: 'failed',
});
const cancelledPipelines: Promise<PipelineSchema[]> = this.api.Pipelines.all(this.chaoticId, {
const fetchPromises: Promise<{ commit: CommitStatusSchema[]; pipeline: PipelineSchema }>[] = [];

let allPipelines: PipelineSchema[] = await this.api.Pipelines.all(this.chaoticId, {
maxPages: 1,
page: options.page,
status: 'canceled',
perPage: 50,
});
allPipelines = allPipelines.filter((pipeline) => pipeline.status !== 'skipped');

const [succeeded, failed, cancelled] = await Promise.all([
succeededPipelines,
failedPipelines,
cancelledPipelines,
]);

for (const pipeline of succeeded) {
this.getCommitStatus(pipeline, fetchPromises);
}
for (const pipeline of failed) {
this.getCommitStatus(pipeline, fetchPromises);
}
for (const pipeline of cancelled) {
for (const pipeline of allPipelines) {
this.getCommitStatus(pipeline, fetchPromises);
}

const promiseResults = await Promise.all(fetchPromises);

for (const { commit, pipeline } of promiseResults) {
switch (pipeline.status) {
case 'success':
returnValue.succeeded.push({ pipeline, commit });
break;
case 'failed':
returnValue.failed.push({ pipeline, commit });
break;
case 'canceled':
returnValue.cancelled.push({ pipeline, commit });
break;
}
}
Logger.debug(returnValue, 'GitlabService');
returnValue.succeeded.sort((a, b) => b.pipeline.id - a.pipeline.id);
returnValue.failed.sort((a, b) => b.pipeline.id - a.pipeline.id);
returnValue.cancelled.sort((a, b) => b.pipeline.id - a.pipeline.id);
return promiseResults.sort((a, b) => b.pipeline.id - a.pipeline.id);
} catch (err) {
Logger.error(err, 'GitlabService');
}

return returnValue;
}

private getCommitStatus(
Expand All @@ -121,8 +73,9 @@ export class GitlabService {
promiseArray.push(
new Promise((resolve) => {
this.api.Commits.allStatuses(this.chaoticId, pipeline.sha).then((statuses) => {
const onlyExternal: CommitStatusSchema[] = statuses.filter((status) => this.isExternalStage(status.name));
resolve({
commit: statuses.filter((status) => this.isExternalStage(status.name)),
commit: onlyExternal.filter((status) => status.pipeline_id === pipeline.id),
pipeline,
});
});
Expand Down
9 changes: 8 additions & 1 deletion shared-lib/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { MessageContent } from './tdlib-types';
import { CommitStatusSchema, PipelineSchema } from '@gitbeaker/rest';

export const CACHE_ROUTER_TTL = 60 * 5 * 1000;
export const CACHE_TELEGRAM_TTL = 120 * 1000;
export const CAUR_ALLOWED_CORS = [
'https://aur.chaotic.cx',
'https://caur-frontend-pages.dev',
'https://v2.caur-frontend.pages.dev/',
'https://v2.caur-frontend.pages.dev',
];
//export const CAUR_API_URL = 'http://localhost:8010/proxy';
//export const CAUR_BACKEND_URL = 'http://localhost:8011/proxy';
Expand Down Expand Up @@ -233,6 +234,7 @@ export interface Build {
timeToEnd?: number;
replaced?: boolean;
}

export type BuildClass = string | number;

export enum BuildStatus {
Expand Down Expand Up @@ -300,3 +302,8 @@ export interface NamcapAnalysis {
'library-no-package-associated': string[];
'link-level-dependence': string[];
}

export interface PipelineWithExternalStatus {
commit: CommitStatusSchema[];
pipeline: PipelineSchema;
}

0 comments on commit ddfe4ec

Please sign in to comment.