Skip to content

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

Package listing

@kody/gitlab

src/issues/list.ts

57 lines · 1.6 KB · TypeScript
import { normalizeProjectLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, pickQuery, slimIssue } from '../shape.ts'

const QUERY_KEYS = [
	'state',
	'labels',
	'search',
	'scope',
	'assignee_id',
	'author_id',
	'milestone',
	'order_by',
	'sort',
	'page',
	'per_page',
] as const

/**
 * List GitLab issues. When a project locator is present, lists that project.
 * Otherwise lists issues visible to the current user (`GET /issues`).
 *
 * @example
 * import listGitlabIssues from 'kody:@kody/gitlab/issues/list'
 * const result = await listGitlabIssues({ project: 'example-group/example-project', state: 'opened' })
 */
export default async function listGitlabIssues(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const hasProjectInput =
		params.project !== undefined ||
		params.projectId !== undefined ||
		params.projectPath !== undefined ||
		params.project_id !== undefined ||
		params.projectUrl !== undefined
	const project = hasProjectInput ? normalizeProjectLocator(params) : null
	const query = pickQuery(params, QUERY_KEYS)
	const path = project ? `/projects/${project.projectId}/issues` : '/issues'

	const response = await request<unknown[]>({
		...auth,
		path,
		query,
		throwOnError: true,
	})

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

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