Repository navigation
feat: oss index categories endpoint #2351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright (c) 2025 The Linux Foundation and each contributor. | ||
| // SPDX-License-Identifier: MIT | ||
| import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'; | ||
| import { Type } from '@sinclair/typebox'; | ||
|
|
||
| import { fetchPipe } from '../../../clients/tinybird.js'; | ||
| import { NotFoundError } from '../../../lib/errors.js'; | ||
| import { | ||
| CategoryTopProject, | ||
| isCategoryTopProjectTuple, | ||
| isTopCollectionTuple, | ||
| sortQuery, | ||
| TopCollection, | ||
| toCategoryTopProject, | ||
| toTopCollection, | ||
| type CategoryTopProjectTuple, | ||
| type TopCollectionTuple, | ||
| } from '../../../lib/oss-index.js'; | ||
| import { inRange, isCount, isString } from '../../../lib/security.js'; | ||
|
|
||
| const groupPipePath = '/v0/pipes/category_groups_list.json'; | ||
| const categoriesPipePath = '/v0/pipes/categories_oss_index.json'; | ||
|
|
||
| interface GroupRow { | ||
| name: string; | ||
| slug: string; | ||
| type: string; | ||
| } | ||
|
|
||
| interface Row { | ||
| id: string; | ||
| name: string; | ||
| slug: string; | ||
| totalContributors: number; | ||
| softwareValue: number; | ||
| avgScore: number; | ||
| topCollections: TopCollectionTuple[]; | ||
| topProjects: CategoryTopProjectTuple[]; | ||
| } | ||
|
|
||
| const isGroupRow = (row: GroupRow) => | ||
| isString(row.name) && isString(row.slug) && isString(row.type); | ||
|
|
||
| const isRow = (row: Row) => | ||
| isString(row.id) && | ||
| isString(row.name) && | ||
| isString(row.slug) && | ||
| isCount(row.totalContributors) && | ||
| isCount(row.softwareValue) && | ||
| inRange(row.avgScore, 1) && | ||
| Array.isArray(row.topCollections) && | ||
| row.topCollections.every(isTopCollectionTuple) && | ||
| Array.isArray(row.topProjects) && | ||
| row.topProjects.every(isCategoryTopProjectTuple); | ||
|
|
||
| const Query = Type.Object({ | ||
| categoryGroupSlug: Type.String({ | ||
| minLength: 1, | ||
| description: 'Slug of the category group, for example `runtime-horizontal`.', | ||
| }), | ||
| ...sortQuery.properties, | ||
| }); | ||
|
|
||
| const OssIndexCategory = Type.Object( | ||
| { | ||
| id: Type.String({ description: 'Identifier of the category.' }), | ||
| name: Type.String({ description: 'Display name of the category.' }), | ||
| slug: Type.String({ description: 'URL slug of the category.' }), | ||
| totalContributors: Type.Integer({ | ||
| minimum: 0, | ||
| description: 'Contributors across the category.', | ||
| }), | ||
| softwareValue: Type.Integer({ minimum: 0, description: 'Software value of the category.' }), | ||
| avgScore: Type.Number({ | ||
| minimum: 0, | ||
| maximum: 1, | ||
| description: 'Average health score of the category, from 0 to 1.', | ||
| }), | ||
| topCollections: Type.Array(TopCollection, { | ||
| description: 'Leading collections of the category, ordered by contributors.', | ||
| }), | ||
| topProjects: Type.Array(CategoryTopProject, { | ||
| description: 'Leading projects of the category, ordered by contributors.', | ||
| }), | ||
| }, | ||
| { title: 'OssIndexCategory' }, | ||
| ); | ||
|
|
||
| const OssIndexCategories = Type.Object( | ||
| { | ||
| name: Type.String({ description: 'Display name of the category group.' }), | ||
| slug: Type.String({ description: 'URL slug of the category group.' }), | ||
| type: Type.String({ | ||
| description: 'Type of the group, for example `vertical` or `horizontal`.', | ||
| }), | ||
| categories: Type.Array(OssIndexCategory, { | ||
| description: 'Categories of the group in the requested order. Empty when it has none.', | ||
| }), | ||
| }, | ||
| { title: 'OssIndexCategories' }, | ||
| ); | ||
|
|
||
| const categoryRoutes: FastifyPluginAsyncTypebox = async (scope) => { | ||
| scope.get( | ||
| '/oss-index/categories', | ||
| { | ||
| schema: { | ||
| tags: ['OSS Index'], | ||
| summary: 'List the categories of an OSS Index category group', | ||
| description: | ||
| 'Returns a category group with each of its categories, their totals and their leading collections and projects. ' + | ||
| '`sort` orders the categories by contributors or by software value. ' + | ||
| 'Answers 404 when no group has the given slug.', | ||
| querystring: Query, | ||
| response: { 200: OssIndexCategories }, | ||
| }, | ||
| }, | ||
| async (request) => { | ||
| const { categoryGroupSlug, sort } = request.query; | ||
| const [group] = await fetchPipe<GroupRow>( | ||
| request, | ||
| groupPipePath, | ||
| { slug: categoryGroupSlug }, | ||
| isGroupRow, | ||
| ); | ||
| if (!group) { | ||
| throw new NotFoundError('Category group not found'); | ||
| } | ||
|
|
||
| const rows = await fetchPipe<Row>( | ||
| request, | ||
| categoriesPipePath, | ||
| { categoryGroupSlug, orderBy: sort ?? 'totalContributors' }, | ||
| isRow, | ||
| ); | ||
| return { | ||
| name: group.name, | ||
| slug: group.slug, | ||
| type: group.type, | ||
| categories: rows.map((row) => ({ | ||
| ...row, | ||
| topCollections: row.topCollections.map(toTopCollection), | ||
| topProjects: row.topProjects.map(toCategoryTopProject), | ||
| })), | ||
| }; | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| export default categoryRoutes; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.