forked from jdalrymple/gitbeaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtectedBranches.ts
159 lines (144 loc) · 4.75 KB
/
ProtectedBranches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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<string, unknown> {
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<C extends boolean = false> extends BaseResource<C> {
all<E extends boolean = false, P extends PaginationTypes = 'offset'>(
projectId: string | number,
options?: { search?: string } & Sudo & ShowExpanded<E> & PaginationRequestOptions<P>,
): Promise<GitlabAPIResponse<ProtectedBranchSchema[], C, E, P>> {
return RequestHelper.get<ProtectedBranchSchema[]>()(
this,
endpoint`projects/${projectId}/protected_branches`,
options,
);
}
create<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: CreateProtectedBranchOptions & Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<ProtectedBranchSchema, C, E, void>> {
const { sudo, showExpanded, ...opts } = options || {};
return RequestHelper.post<ProtectedBranchSchema>()(
this,
endpoint`projects/${projectId}/protected_branches`,
{
searchParams: {
...opts,
name: branchName,
},
sudo,
showExpanded,
},
);
}
// Convenience method - create
protect<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: CreateProtectedBranchOptions & Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<ProtectedBranchSchema, C, E, void>> {
return this.create(projectId, branchName, options);
}
edit<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: EditProtectedBranchOptions & Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<ProtectedBranchSchema, C, E, void>> {
return RequestHelper.patch<ProtectedBranchSchema>()(
this,
endpoint`projects/${projectId}/protected_branches/${branchName}`,
options,
);
}
show<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<ProtectedBranchSchema, C, E, void>> {
return RequestHelper.get<ProtectedBranchSchema>()(
this,
endpoint`projects/${projectId}/protected_branches/${branchName}`,
options,
);
}
remove<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<void, C, E, void>> {
return RequestHelper.del()(
this,
endpoint`projects/${projectId}/protected_branches/${branchName}`,
options,
);
}
// Convenience method - remove
unprotect<E extends boolean = false>(
projectId: string | number,
branchName: string,
options?: Sudo & ShowExpanded<E>,
): Promise<GitlabAPIResponse<void, C, E, void>> {
return this.remove(projectId, branchName, options);
}
}