diff --git a/src/woodpecker/woodpecker.service.ts b/src/woodpecker/woodpecker.service.ts index e3fb310..f9b542b 100644 --- a/src/woodpecker/woodpecker.service.ts +++ b/src/woodpecker/woodpecker.service.ts @@ -28,12 +28,38 @@ export class WoodpeckerService { : isRecord(body) && Array.isArray(body.repos) ? body.repos : []; + const normalizedRepos = repos.map((repo) => (isRecord(repo) ? repo : {})); + const pipelines = await Promise.all( + normalizedRepos.map((repo) => this.getLatestPipeline(repo).catch(() => getPipeline(repo))), + ); return { - repos: repos.map((repo) => normalizeRepo(repo)).sort((a, b) => b.createdAt - a.createdAt), + repos: normalizedRepos + .map((repo, index) => normalizeRepo(repo, pipelines[index])) + .sort((a, b) => b.createdAt - a.createdAt), }; } + private async getLatestPipeline(repo: WoodpeckerRepo): Promise { + const embeddedPipeline = getPipeline(repo); + if (getPipelineTime(embeddedPipeline) > 0) return embeddedPipeline; + + const repoId = getString(repo.id); + if (!repoId) return {}; + + const body = await this.request( + `/api/repos/${encodeURIComponent(repoId)}/pipelines?page=1&limit=1`, + ); + const pipelines = Array.isArray(body) + ? body + : isRecord(body) && Array.isArray(body.pipelines) + ? body.pipelines + : []; + const latest = pipelines[0]; + + return isRecord(latest) ? latest : {}; + } + private async request(path: string): Promise { const response = await fetch(`${this.baseUrl}${path}`, { headers: { @@ -61,8 +87,7 @@ export class WoodpeckerService { } } -function normalizeRepo(repo: WoodpeckerRepo) { - const pipeline = getPipeline(repo); +function normalizeRepo(repo: WoodpeckerRepo, pipeline = getPipeline(repo)) { const createdAt = getPipelineTime(pipeline); const repository = getString(repo.full_name) || getString(repo.fullName) || joinRepoName(repo) || getString(repo.name) || '-';