Skip to content

Commit 5efb011

Browse files
Update GitLab REST client library (#18523)
* [server] update gitlab REST client library * clean up workspace packages * Update gitlab rest client * Fix yarn.lock * Update chainguard node image * Catch up with GitLab client library changes. Several places relied on a particular shape of errors. * [gitlab] fix project typings --------- Co-authored-by: Jean Pierre <[email protected]>
1 parent f92950f commit 5efb011

13 files changed

+59
-236
lines changed

components/server/leeway.Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ COPY components-server--app /installer/
88
WORKDIR /app
99
RUN /installer/install.sh
1010

11-
# NodeJS v16.19
12-
FROM cgr.dev/chainguard/node@sha256:95bb4763acb8e9702c956e093932be97ab118db410a0619bb3fdd334c9198006
11+
FROM cgr.dev/chainguard/node:18.17.1@sha256:fbaecf4d6ac9883699078c0b501aad22c866f9ce039d009212c0eed260914875
1312
ENV NODE_OPTIONS="--unhandled-rejections=warn --max_old_space_size=2048"
1413

1514
EXPOSE 3000

components/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@authzed/authzed-node": "^0.12.1",
3838
"@bufbuild/connect": "^0.8.1",
3939
"@bufbuild/connect-express": "^0.8.1",
40-
"@gitbeaker/node": "^35.7.0",
40+
"@gitbeaker/rest": "^39.12.0",
4141
"@gitpod/content-service": "0.1.5",
4242
"@gitpod/gitpod-db": "0.1.5",
4343
"@gitpod/gitpod-protocol": "0.1.5",

components/server/src/gitlab/api.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { injectable, inject } from "inversify";
88
import { User } from "@gitpod/gitpod-protocol";
99

10-
import { Gitlab } from "@gitbeaker/node";
10+
import { Gitlab } from "@gitbeaker/rest";
1111
import {
1212
Projects,
1313
Users,
@@ -19,10 +19,12 @@ import {
1919
MergeRequests,
2020
Issues,
2121
RepositoryFiles,
22+
NamespaceSchema,
23+
UserSchema,
24+
ExpandedUserSchema,
25+
ProjectSchema,
26+
SimpleProjectSchema,
2227
} from "@gitbeaker/core";
23-
import { ProjectExtendedSchema } from "@gitbeaker/core/dist/types/resources/Projects";
24-
import { NamespaceSchema } from "@gitbeaker/core/dist/types/resources/Namespaces";
25-
import { UserExtendedSchema, UserSchema } from "@gitbeaker/core/dist/types/resources/Users";
2628
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
2729
import { GitLabScope } from "./scopes";
2830
import { AuthProviderParams } from "../auth/auth-provider";
@@ -60,21 +62,11 @@ export class GitLabApi {
6062
const response = (await operation(userApi)) as R;
6163
return response as R;
6264
} catch (error) {
63-
if (error && typeof error?.response?.status === "number" && error?.response?.status !== 200) {
64-
return new GitLab.ApiError(`GitLab responded with code ${error.response.status}`, error);
65-
}
66-
if (error && error?.name === "HTTPError") {
67-
// e.g.
68-
// {
69-
// "name": "HTTPError",
70-
// "timings": { },
71-
// "description": "404 Commit Not Found"
72-
// }
73-
74-
return new GitLab.ApiError(
75-
`GitLab Request Error: ${error?.description?.name || JSON.stringify(error?.description)}`,
76-
error,
77-
);
65+
const status = error?.cause?.response?.status;
66+
const statusText = error?.cause?.response?.statusText;
67+
if (error && typeof status === "number" && status !== 200) {
68+
const description = error?.cause?.description || `${status} - ${statusText}`;
69+
return new GitLab.ApiError(`GitLab responded: ${description}`, status);
7870
}
7971
throw error;
8072
} finally {
@@ -90,9 +82,7 @@ export class GitLabApi {
9082
path: string,
9183
): Promise<string | undefined> {
9284
const projectId = `${org}/${name}`;
93-
const result = await this.run<string>(user, (api) =>
94-
api.RepositoryFiles.showRaw(projectId, path, { ref: commitish }),
95-
);
85+
const result = await this.run<string>(user, (api) => api.RepositoryFiles.showRaw(projectId, path, commitish));
9686
if (GitLab.ApiError.is(result)) {
9787
return undefined; // e.g. 404 error, because the file isn't found
9888
}
@@ -116,10 +106,8 @@ export namespace GitLab {
116106
RepositoryFiles: RepositoryFiles;
117107
}
118108
export class ApiError extends Error {
119-
readonly httpError: { name: string; description: string } | undefined;
120-
constructor(msg?: string, httpError?: any) {
109+
constructor(msg?: string, readonly code?: number) {
121110
super(msg);
122-
this.httpError = httpError;
123111
this.name = "GitLabApiError";
124112
}
125113
}
@@ -128,16 +116,16 @@ export namespace GitLab {
128116
return !!something && something.name === "GitLabApiError";
129117
}
130118
export function isNotFound(error: ApiError): boolean {
131-
return !!error.httpError?.description.startsWith("404");
119+
return error.code === 404;
132120
}
133121
export function isInternalServerError(error: ApiError): boolean {
134-
return !!error.httpError?.description.startsWith("500");
122+
return error.code === 500;
135123
}
136124
}
137125
/**
138126
* https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/projects.md#get-single-project
139127
*/
140-
export interface Project extends ProjectExtendedSchema {
128+
interface ProjectFiltered extends Omit<ProjectSchema, "permissions"> {
141129
visibility: "public" | "private" | "internal";
142130
archived: boolean;
143131
path: string; // "diaspora-project-site"
@@ -148,15 +136,19 @@ export namespace GitLab {
148136
"id" | "name" | "path" | "kind" | "full_path" | "avatar_url" | "web_url" | "avatar_url" | "parent_id"
149137
>;
150138
owner: Pick<UserSchema, "id" | "name" | "created_at" | "avatar_url">;
139+
permissions: Permissions;
151140
merge_requests_enabled: boolean;
152141
issues_enabled: boolean;
153142
open_issues_count: number;
154143
forks_count: number;
155144
star_count: number;
156-
forked_from_project?: Project;
145+
forked_from_project?: ProjectSchema;
157146
default_branch: string;
158147
web_url: string;
159148
}
149+
// workaround for https://github.com/microsoft/TypeScript/issues/36981
150+
export type Project = ProjectFiltered & SimpleProjectSchema;
151+
160152
export interface TreeObject {
161153
id: string;
162154
mode: string;
@@ -227,7 +219,7 @@ export namespace GitLab {
227219
merge_requests_count: number;
228220
}
229221
// https://docs.gitlab.com/ee/api/users.html#list-current-user-for-normal-users
230-
export interface User extends UserExtendedSchema {
222+
export interface User extends ExpandedUserSchema {
231223
email: string;
232224
state: "active" | string;
233225
}

components/server/src/gitlab/file-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class GitlabFileProvider implements FileProvider {
3030
path: string,
3131
): Promise<string> {
3232
const result = await this.gitlabApi.run<GitLab.Commit[]>(user, async (g) => {
33-
return g.Commits.all(`${repository.owner}/${repository.name}`, { path, ref_name: revisionOrBranch });
33+
return g.Commits.all(`${repository.owner}/${repository.name}`, { path, refName: revisionOrBranch });
3434
});
3535

3636
if (GitLab.ApiError.is(result)) {

components/server/src/gitlab/gitlab-app-support.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { AuthProviderInfo, ProviderRepository, User } from "@gitpod/gitpod-protocol";
88
import { inject, injectable } from "inversify";
9-
import { Gitlab } from "@gitbeaker/node";
9+
import { Gitlab } from "@gitbeaker/rest";
1010
import { GitLab } from "./api";
1111
import { TokenProvider } from "../user/token-provider";
1212

@@ -36,7 +36,7 @@ export class GitLabAppSupport {
3636
// also cf. https://docs.gitlab.com/ee/api/members.html#valid-access-levels
3737
//
3838
const projectsWithAccess = await api.Projects.all({
39-
min_access_level: "40",
39+
minAccessLevel: 40,
4040
perPage: 100,
4141
});
4242
for (const project of projectsWithAccess) {

components/server/src/gitlab/gitlab-auth-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class GitLabAuthProvider extends GenericAuthProvider {
6666
host: this.baseURL,
6767
});
6868
const getCurrentUser = async () => {
69-
const response = await api.Users.current();
69+
const response = await api.Users.showCurrentUser();
7070
return response as unknown as GitLab.User;
7171
};
7272
const unconfirmedUserMessage = "Please confirm your GitLab account and try again.";

components/server/src/gitlab/gitlab-context-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class TestGitlabContextParser {
245245
chai.assert.fail();
246246
} catch (e) {
247247
if (GitLab.ApiError.is(e)) {
248-
expect(e.httpError?.description).equals("404 Commit Not Found");
248+
expect(e.code).equals(404);
249249
} else {
250250
chai.assert.fail("Unknown Error: " + JSON.stringify(e));
251251
}

components/server/src/gitlab/gitlab-context-parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class GitlabContextParser extends AbstractContextParser implements IConte
222222
}
223223

224224
const result = await this.gitlabApi.run<GitLab.TreeObject[]>(user, async (g) => {
225-
return g.Repositories.tree(`${owner}/${repoName}`, {
225+
return g.Repositories.allRepositoryTrees(`${owner}/${repoName}`, {
226226
ref: branchOrTag.name,
227227
path: path.dirname(branchOrTag.fullPath),
228228
});
@@ -421,7 +421,7 @@ export class GitlabContextParser extends AbstractContextParser implements IConte
421421
): Promise<IssueContext> {
422422
const ctxPromise = this.handleDefaultContext(user, host, owner, repoName);
423423
const result = await this.gitlabApi.run<GitLab.Issue>(user, async (g) => {
424-
return g.Issues.show(`${owner}/${repoName}`, nr);
424+
return g.Issues.show(nr, { projectId: `${owner}/${repoName}` });
425425
});
426426
if (GitLab.ApiError.is(result)) {
427427
throw await NotFoundError.create(await this.tokenHelper.getCurrentToken(user), user, host, owner, repoName);

components/server/src/gitlab/gitlab-file-provider.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ class TestFileProvider {
7575
const project = await api.run<GitLab.Project>(this.user, (g) =>
7676
g.Projects.create({
7777
name: `test_project_${i}`,
78-
path: `test_project_${i}`,
79-
namespace_id: 57982169,
80-
initialize_with_readme: true,
78+
namespaceId: 57982169,
79+
initializeWithReadme: true,
8180
description: "generated project to test pagination",
8281
}),
8382
);

components/server/src/gitlab/gitlab-repository-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ export class GitlabRepositoryProvider implements RepositoryProvider {
126126
const projectId = `${owner}/${repo}`;
127127
const result = await this.gitlab.run<GitLab.Commit[]>(user, async (g) => {
128128
return g.Commits.all(projectId, {
129-
ref_name: ref,
130-
per_page: maxDepth,
129+
refName: ref,
130+
perPage: maxDepth,
131131
page: 1,
132132
});
133133
});

components/ws-manager-bridge/leeway.Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ COPY components-ws-manager-bridge--app /installer/
88
WORKDIR /app
99
RUN /installer/install.sh
1010

11-
# NodeJS v16.19
12-
FROM cgr.dev/chainguard/node@sha256:95bb4763acb8e9702c956e093932be97ab118db410a0619bb3fdd334c9198006
11+
FROM cgr.dev/chainguard/node:18.17.1@sha256:fbaecf4d6ac9883699078c0b501aad22c866f9ce039d009212c0eed260914875
1312
ENV NODE_OPTIONS=--unhandled-rejections=warn
1413
EXPOSE 3000
1514
COPY --from=builder --chown=node:node /app /app/

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
"workspaces": {
2525
"packages": [
2626
"components/*",
27-
"components/ee/*",
2827
"components/*/typescript",
2928
"components/*/typescript-*",
30-
"components/supervisor/frontend",
31-
"charts/"
29+
"components/supervisor/frontend"
3230
]
3331
}
3432
}

0 commit comments

Comments
 (0)