|
1 |
| -import Fs from 'fs'; |
2 |
| -import Path from 'path'; |
| 1 | +import FormData from 'form-data'; |
| 2 | +import randomstring from 'randomstring'; |
3 | 3 | import { BaseService, RequestHelper } from '../infrastructure';
|
4 |
| -import { assertEventOptions } from './Events'; |
5 |
| -import { RequestOptions } from '../infrastructure/RequestHelper'; |
6 |
| - |
7 |
| -/** TODO annotate options */ |
8 |
| -type ProjectOptions = temporaryAny; |
9 | 4 |
|
10 | 5 | class Projects extends BaseService {
|
11 |
| - all(options?: RequestOptions) { |
| 6 | + all(options?: PaginatedRequestOptions) { |
12 | 7 | return RequestHelper.get(this, 'projects', options);
|
13 | 8 | }
|
14 | 9 |
|
15 |
| - archive(projectId: ProjectId) { |
| 10 | + archive(projectId: ProjectId, options?: Sudo) { |
16 | 11 | const pId = encodeURIComponent(projectId);
|
17 | 12 |
|
18 |
| - return RequestHelper.post(this, `projects/${pId}/archive`); |
| 13 | + return RequestHelper.post(this, `projects/${pId}/archive`, options); |
19 | 14 | }
|
20 |
| - /** |
21 |
| - * @see https://docs.gitlab.com/ee/api/projects.html#create-project-for-user |
22 |
| - */ |
23 |
| - create(options: temporaryAny) { |
24 |
| - const url = options.userId ? `projects/user/${encodeURIComponent(options.userId)}` : 'projects'; |
| 15 | + |
| 16 | + create({ userId, ...options }: { userId?: UserId } & BaseRequestOptions) { |
| 17 | + const url = userId ? `projects/user/${encodeURIComponent(userId)}` : 'projects'; |
25 | 18 |
|
26 | 19 | return RequestHelper.post(this, url, options);
|
27 | 20 | }
|
28 | 21 |
|
29 |
| - edit(projectId: ProjectId, options: temporaryAny) { |
| 22 | + edit(projectId: ProjectId, options?: BaseRequestOptions) { |
30 | 23 | const pId = encodeURIComponent(projectId);
|
31 | 24 |
|
32 | 25 | return RequestHelper.put(this, `projects/${pId}`, options);
|
33 | 26 | }
|
34 | 27 |
|
35 |
| - events(projectId: ProjectId, options: ProjectOptions) { |
36 |
| - assertEventOptions(options.action, options.targetType); |
37 |
| - |
| 28 | + events(projectId: ProjectId, options?: BaseRequestOptions & EventOptions) { |
38 | 29 | const pId = encodeURIComponent(projectId);
|
39 | 30 |
|
40 | 31 | return RequestHelper.get(this, `projects/${pId}/events`, options);
|
41 | 32 | }
|
42 | 33 |
|
43 |
| - fork(projectId: ProjectId, options: ProjectOptions) { |
| 34 | + fork(projectId: ProjectId, options?: BaseRequestOptions) { |
44 | 35 | const pId = encodeURIComponent(projectId);
|
45 | 36 |
|
46 | 37 | return RequestHelper.post(this, `projects/${pId}/fork`, options);
|
47 | 38 | }
|
48 | 39 |
|
49 |
| - forks(projectId: ProjectId, options: ProjectOptions) { |
| 40 | + forks(projectId: ProjectId, options?: BaseRequestOptions) { |
50 | 41 | const pId = encodeURIComponent(projectId);
|
51 | 42 |
|
52 | 43 | return RequestHelper.get(this, `projects/${pId}/forks`, options);
|
53 | 44 | }
|
54 | 45 |
|
55 |
| - languages(projectId: ProjectId) { |
| 46 | + languages(projectId: ProjectId, options?: Sudo) { |
| 47 | + const pId = encodeURIComponent(projectId); |
| 48 | + |
| 49 | + return RequestHelper.get(this, `projects/${pId}/languages`, options); |
| 50 | + } |
| 51 | + |
| 52 | + mirrorPull(projectId: ProjectId, options?: Sudo) { |
56 | 53 | const pId = encodeURIComponent(projectId);
|
57 | 54 |
|
58 |
| - return RequestHelper.get(this, `projects/${pId}/languages`); |
| 55 | + return RequestHelper.post(this, `projects/${pId}/mirror/pull`, options); |
59 | 56 | }
|
60 | 57 |
|
61 |
| - mirrorPull(projectId: ProjectId) { |
| 58 | + remove(projectId: ProjectId, options?: Sudo) { |
62 | 59 | const pId = encodeURIComponent(projectId);
|
63 | 60 |
|
64 |
| - return RequestHelper.post(this, `projects/${pId}/mirror/pull`); |
| 61 | + return RequestHelper.del(this, `projects/${pId}`, options); |
65 | 62 | }
|
66 | 63 |
|
67 |
| - remove(projectId: ProjectId) { |
| 64 | + removeFork(projectId: ProjectId, options?: Sudo) { |
68 | 65 | const pId = encodeURIComponent(projectId);
|
69 | 66 |
|
70 |
| - return RequestHelper.delete(this, `projects/${pId}`); |
| 67 | + return RequestHelper.del(this, `projects/${pId}/fork`, options); |
71 | 68 | }
|
72 | 69 |
|
73 | 70 | search(projectName: string) {
|
74 | 71 | return RequestHelper.get(this, 'projects', { search: projectName });
|
75 | 72 | }
|
76 | 73 |
|
77 |
| - share(projectId: ProjectId, groupId: GroupId, groupAccess: GroupAccess, options: ProjectOptions) { |
| 74 | + share(projectId: ProjectId, groupId: GroupId, groupAccess: number, options?: BaseRequestOptions) { |
78 | 75 | const pId = encodeURIComponent(projectId);
|
79 | 76 |
|
80 |
| - if (!groupId || !groupAccess) throw new Error('Missing required arguments'); |
81 |
| - |
82 | 77 | return RequestHelper.post(this, `projects/${pId}/share`, { groupId, groupAccess, ...options });
|
83 | 78 | }
|
84 | 79 |
|
85 |
| - show(projectId: ProjectId, options: ProjectOptions) { |
| 80 | + show(projectId: ProjectId, options?: BaseRequestOptions) { |
86 | 81 | const pId = encodeURIComponent(projectId);
|
87 | 82 |
|
88 | 83 | return RequestHelper.get(this, `projects/${pId}`, options);
|
89 | 84 | }
|
90 | 85 |
|
91 |
| - star(projectId: ProjectId) { |
| 86 | + star(projectId: ProjectId, options?: Sudo) { |
92 | 87 | const pId = encodeURIComponent(projectId);
|
93 | 88 |
|
94 |
| - return RequestHelper.post(this, `projects/${pId}/star`); |
| 89 | + return RequestHelper.post(this, `projects/${pId}/star`, options); |
95 | 90 | }
|
96 | 91 |
|
97 |
| - statuses(projectId: ProjectId, sha: string, state: string, options: ProjectOptions) { |
| 92 | + statuses(projectId: ProjectId, sha: string, state: string, options?: BaseRequestOptions) { |
98 | 93 | const pId = encodeURIComponent(projectId);
|
99 | 94 |
|
100 | 95 | return RequestHelper.post(this, `projects/${pId}/statuses/${sha}`, { state, ...options });
|
101 | 96 | }
|
102 | 97 |
|
103 |
| - transfer(projectId: ProjectId, namespace: string) { |
| 98 | + transfer(projectId: ProjectId, namespaceId: NamespaceId) { |
104 | 99 | const pId = encodeURIComponent(projectId);
|
105 |
| - return RequestHelper.put(this, `projects/${pId}/transfer`, { namespace }); |
| 100 | + return RequestHelper.put(this, `projects/${pId}/transfer`, { namespace: namespaceId }); |
106 | 101 | }
|
107 | 102 |
|
108 |
| - unarchive(projectId: ProjectId) { |
| 103 | + unarchive(projectId: ProjectId, options?: Sudo) { |
109 | 104 | const pId = encodeURIComponent(projectId);
|
110 | 105 |
|
111 |
| - return RequestHelper.post(this, `projects/${pId}/unarchive`); |
| 106 | + return RequestHelper.post(this, `projects/${pId}/unarchive`, options); |
112 | 107 | }
|
113 | 108 |
|
114 |
| - unshare(projectId: ProjectId, groupId: GroupId) { |
| 109 | + unshare(projectId: ProjectId, groupId: GroupId, options?: Sudo) { |
115 | 110 | const [pId, gId] = [projectId, groupId].map(encodeURIComponent);
|
116 | 111 |
|
117 |
| - return RequestHelper.delete(this, `projects/${pId}/share${gId}`); |
| 112 | + return RequestHelper.del(this, `projects/${pId}/share/${gId}`, options); |
118 | 113 | }
|
119 | 114 |
|
120 |
| - unstar(projectId: ProjectId) { |
| 115 | + unstar(projectId: ProjectId, options?: Sudo) { |
121 | 116 | const pId = encodeURIComponent(projectId);
|
122 | 117 |
|
123 |
| - return RequestHelper.post(this, `projects/${pId}/unstar`); |
| 118 | + return RequestHelper.post(this, `projects/${pId}/unstar`, options); |
124 | 119 | }
|
125 | 120 |
|
126 |
| - updatePushRule(projectId: ProjectId, options: ProjectOptions) { |
| 121 | + updatePushRule(projectId: ProjectId, options?: BaseRequestOptions) { |
127 | 122 | const pId = encodeURIComponent(projectId);
|
128 | 123 |
|
129 | 124 | return RequestHelper.put(this, `projects/${pId}/push_rule`, options);
|
130 | 125 | }
|
131 | 126 |
|
132 |
| - upload(projectId: ProjectId, filePath: string, { fileName = Path.basename(filePath) } = {}) { |
133 |
| - const pId = encodeURIComponent(projectId); |
134 |
| - const file = Fs.readFileSync(filePath); |
135 |
| - |
136 |
| - return RequestHelper.post( |
137 |
| - this, |
138 |
| - `projects/${pId}/uploads`, |
139 |
| - { |
140 |
| - file: { |
141 |
| - value: file, |
142 |
| - options: { |
143 |
| - filename: fileName, |
144 |
| - contentType: 'application/octet-stream', |
145 |
| - }, |
146 |
| - }, |
147 |
| - }, |
148 |
| - true, |
149 |
| - ); |
| 127 | + upload(projectId, content, metadata: ProjectUploadMetadata = {}, options?: Sudo) { |
| 128 | + const pId = encodeURIComponent(projectId); |
| 129 | + const form = new FormData(); |
| 130 | + |
| 131 | + const defaultMetadata: ProjectUploadMetadata = { |
| 132 | + filename: randomstring.generate(8), |
| 133 | + contentType: 'application/octet-stream', |
| 134 | + }; |
| 135 | + |
| 136 | + form.append('file', content, Object.assign(defaultMetadata, metadata)); |
| 137 | + |
| 138 | + return RequestHelper.post(this, `projects/${pId}/uploads`, { ...options, form }); |
150 | 139 | }
|
151 | 140 | }
|
152 | 141 |
|
|
0 commit comments