Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/gitlab

src/pipelines/list.ts

51 lines · 1.2 KB · TypeScript
import { normalizeProjectLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, pickQuery, slimPipeline } from '../shape.ts'

const QUERY_KEYS = [
	'ref',
	'status',
	'source',
	'sha',
	'name',
	'username',
	'order_by',
	'sort',
	'page',
	'per_page',
] as const

/**
 * List pipelines for a GitLab project.
 *
 * @example
 * import listGitlabPipelines from 'kody:@kody/gitlab/pipelines/list'
 * const result = await listGitlabPipelines({
 *   project: 'example-group/example-project',
 *   ref: 'main',
 * })
 */
export default async function listGitlabPipelines(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const { projectId, projectPath } = normalizeProjectLocator(params)
	const query = pickQuery(params, QUERY_KEYS)

	const response = await request<unknown[]>({
		...auth,
		path: `/projects/${projectId}/pipelines`,
		query,
		throwOnError: true,
	})

	const items = Array.isArray(response.data)
		? response.data.map((item) => slimPipeline(asRecord(item)))
		: []

	return {
		items,
		projectPath,
		page: response.headers['x-page'] ?? null,
		nextPage: response.headers['x-next-page'] ?? null,
		total: response.headers['x-total'] ?? null,
		auth: response.auth,
	}
}