import { BaseResource } from '@gitbeaker/requester-utils'; import { RequestHelper, endpoint } from '../infrastructure'; import type { GitlabAPIResponse, OneOf, PaginationRequestOptions, PaginationTypes, ShowExpanded, Sudo, } from '../infrastructure'; import { AccessLevel } from '../constants'; export type ProtectedBranchAccessLevel = | AccessLevel.NO_ACCESS | AccessLevel.DEVELOPER | AccessLevel.MAINTAINER | AccessLevel.ADMIN; export interface ExtendedProtectedBranchAccessLevelSchema { id: number; access_level: ProtectedBranchAccessLevel; access_level_description: string; user_id?: number | null; group_id?: number | null; } export interface ProtectedBranchSchema extends Record { id: number; name: string; push_access_levels?: ExtendedProtectedBranchAccessLevelSchema[]; merge_access_levels?: ExtendedProtectedBranchAccessLevelSchema[]; unprotect_access_levels?: ExtendedProtectedBranchAccessLevelSchema[]; allow_force_push: boolean; code_owner_approval_required: boolean; } export type CreateProtectedBranchAllowOptions = OneOf<{ userId: number; groupId: number; accessLevel: ProtectedBranchAccessLevel; }>; export type EditProtectedBranchAllowOptions = { _destroy?: boolean; } & ( | { userId: number } | { groupId: number } | { accessLevel: ProtectedBranchAccessLevel; id: number; } ); export type CreateProtectedBranchOptions = { allowForcePush?: boolean; allowedToMerge?: CreateProtectedBranchAllowOptions[]; allowedToPush?: CreateProtectedBranchAllowOptions[]; allowedToUnprotect?: CreateProtectedBranchAllowOptions[]; codeOwnerApprovalRequired?: boolean; mergeAccessLevel?: ProtectedBranchAccessLevel; pushAccessLevel?: ProtectedBranchAccessLevel; unprotectAccessLevel?: ProtectedBranchAccessLevel; }; export type EditProtectedBranchOptions = { allowForcePush?: boolean; allowedToMerge?: EditProtectedBranchAllowOptions[]; allowedToPush?: EditProtectedBranchAllowOptions[]; allowedToUnprotect?: EditProtectedBranchAllowOptions[]; codeOwnerApprovalRequired?: boolean; }; export class ProtectedBranches extends BaseResource { all( projectId: string | number, options?: { search?: string } & Sudo & ShowExpanded & PaginationRequestOptions

, ): Promise> { return RequestHelper.get()( this, endpoint`projects/${projectId}/protected_branches`, options, ); } create( projectId: string | number, branchName: string, options?: CreateProtectedBranchOptions & Sudo & ShowExpanded, ): Promise> { const { sudo, showExpanded, ...opts } = options || {}; return RequestHelper.post()( this, endpoint`projects/${projectId}/protected_branches`, { searchParams: { ...opts, name: branchName, }, sudo, showExpanded, }, ); } // Convenience method - create protect( projectId: string | number, branchName: string, options?: CreateProtectedBranchOptions & Sudo & ShowExpanded, ): Promise> { return this.create(projectId, branchName, options); } edit( projectId: string | number, branchName: string, options?: EditProtectedBranchOptions & Sudo & ShowExpanded, ): Promise> { return RequestHelper.patch()( this, endpoint`projects/${projectId}/protected_branches/${branchName}`, options, ); } show( projectId: string | number, branchName: string, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.get()( this, endpoint`projects/${projectId}/protected_branches/${branchName}`, options, ); } remove( projectId: string | number, branchName: string, options?: Sudo & ShowExpanded, ): Promise> { return RequestHelper.del()( this, endpoint`projects/${projectId}/protected_branches/${branchName}`, options, ); } // Convenience method - remove unprotect( projectId: string | number, branchName: string, options?: Sudo & ShowExpanded, ): Promise> { return this.remove(projectId, branchName, options); } }