Skip to content

Commit 9e5879e

Browse files
committed
adding ConfigurationServiceAPI
1 parent a55b2b8 commit 9e5879e

File tree

13 files changed

+2538
-3
lines changed

13 files changed

+2538
-3
lines changed

components/gitpod-db/src/project-db.spec.db.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ class ProjectDBSpec {
5454
const foundProject = await this.projectDb.findProjectsBySearchTerm(0, 10, "creationTime", "DESC", searchTerm);
5555

5656
expect(foundProject.rows[0].id).to.eq(storedProject.id);
57+
58+
const foundProjectByName = await this.projectDb.findProjectsBySearchTerm(
59+
0,
60+
10,
61+
"creationTime",
62+
"DESC",
63+
"some-proj",
64+
);
65+
expect(foundProjectByName.rows[0].id).to.eq(storedProject.id);
5766
}
5867
}
5968

components/gitpod-db/src/typeorm/project-db-impl.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { PartialProject, Project, ProjectEnvVar, ProjectEnvVarWithValue, ProjectUsage } from "@gitpod/gitpod-protocol";
88
import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service";
99
import { inject, injectable, optional } from "inversify";
10-
import { EntityManager, FindConditions, Repository } from "typeorm";
10+
import { Brackets, EntityManager, FindConditions, Repository } from "typeorm";
1111
import { v4 as uuidv4 } from "uuid";
1212
import { ProjectDB } from "../project-db";
1313
import { DBProject } from "./entity/db-project";
@@ -80,7 +80,14 @@ export class ProjectDBImpl extends TransactionalDBImpl<ProjectDB> implements Pro
8080

8181
const queryBuilder = projectRepo
8282
.createQueryBuilder("project")
83-
.where("project.cloneUrl LIKE :searchTerm", { searchTerm: `%${searchTerm}%` })
83+
.andWhere(
84+
new Brackets((qb) => {
85+
qb.where("project.cloneUrl LIKE :searchTerm", { searchTerm: `%${searchTerm}%` }).orWhere(
86+
"project.name LIKE :searchTerm",
87+
{ searchTerm: `%${searchTerm}%` },
88+
);
89+
}),
90+
)
8491
.andWhere("project.markedDeleted = false")
8592
.skip(offset)
8693
.take(limit)

components/gitpod-protocol/src/public-api-converter.spec.ts

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import {
1414
} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
1515
import { expect } from "chai";
1616
import { PublicAPIConverter } from "./public-api-converter";
17-
import { OrgMemberInfo } from "./teams-projects-protocol";
17+
import { OrgMemberInfo, Project, PrebuildSettings as PrebuildSettingsProtocol } from "./teams-projects-protocol";
1818
import { OrganizationRole } from "@gitpod/public-api/lib/gitpod/v1/organization_pb";
19+
import {
20+
BranchMatchingStrategy,
21+
PrebuildSettings,
22+
WorkspaceSettings,
23+
} from "@gitpod/public-api/lib/gitpod/v1/configuration_pb";
1924

2025
describe("PublicAPIConverter", () => {
2126
const converter = new PublicAPIConverter();
@@ -623,4 +628,97 @@ describe("PublicAPIConverter", () => {
623628
);
624629
});
625630
});
631+
632+
describe("toConfiguration", () => {
633+
it("should convert a Project to a Configuration", () => {
634+
const project: Project = {
635+
id: "123",
636+
teamId: "456",
637+
name: "My Project",
638+
cloneUrl: "https://github.com/myorg/myproject.git",
639+
appInstallationId: "",
640+
creationTime: new Date().toISOString(),
641+
settings: {
642+
workspaceClasses: {
643+
regular: "dev",
644+
},
645+
prebuilds: {
646+
enable: true,
647+
branchMatchingPattern: "main",
648+
branchStrategy: "default-branch",
649+
prebuildInterval: 20,
650+
workspaceClass: "dev",
651+
},
652+
},
653+
};
654+
const result = converter.toConfiguration(project);
655+
expect(result.id).to.equal(project.id);
656+
expect(result.organizationId).to.equal(project.teamId);
657+
expect(result.name).to.equal(project.name);
658+
expect(result.cloneUrl).to.equal(project.cloneUrl);
659+
expect(result.workspaceSettings).to.deep.equal(
660+
new WorkspaceSettings({
661+
workspaceClass: project.settings?.workspaceClasses?.regular,
662+
}),
663+
);
664+
expect(result.prebuildSettings).to.deep.equal(
665+
new PrebuildSettings({
666+
enabled: project.settings?.prebuilds?.enable,
667+
branchMatchingPattern: project.settings?.prebuilds?.branchMatchingPattern,
668+
branchStrategy: BranchMatchingStrategy.DEFAULT_BRANCH,
669+
prebuildInterval: project.settings?.prebuilds?.prebuildInterval,
670+
workspaceClass: project.settings?.prebuilds?.workspaceClass,
671+
}),
672+
);
673+
});
674+
});
675+
676+
describe("toPrebuildSettings", () => {
677+
it("should convert a PrebuildSettingsProtocol to a PrebuildSettings", () => {
678+
const prebuilds: PrebuildSettingsProtocol = {
679+
enable: true,
680+
branchMatchingPattern: "main",
681+
branchStrategy: "default-branch",
682+
prebuildInterval: 42,
683+
workspaceClass: "dev",
684+
};
685+
const result = converter.toPrebuildSettings(prebuilds);
686+
expect(result.enabled).to.equal(prebuilds.enable);
687+
expect(result.branchMatchingPattern).to.equal(prebuilds.branchMatchingPattern);
688+
expect(result.branchStrategy).to.equal(BranchMatchingStrategy.DEFAULT_BRANCH);
689+
expect(result.prebuildInterval).to.equal(prebuilds.prebuildInterval);
690+
expect(result.workspaceClass).to.equal(prebuilds.workspaceClass);
691+
});
692+
693+
it("should return an empty PrebuildSettings if no PrebuildSettingsProtocol is provided", () => {
694+
const result = converter.toPrebuildSettings(undefined);
695+
expect(result).to.deep.equal(new PrebuildSettings());
696+
});
697+
});
698+
699+
describe("toBranchMatchingStrategy", () => {
700+
it("should convert a BranchStrategy to a BranchMatchingStrategy", () => {
701+
expect(converter.toBranchMatchingStrategy("default-branch")).to.equal(
702+
BranchMatchingStrategy.DEFAULT_BRANCH,
703+
);
704+
expect(converter.toBranchMatchingStrategy("all-branches")).to.equal(BranchMatchingStrategy.ALL_BRANCHES);
705+
expect(converter.toBranchMatchingStrategy("matched-branches")).to.equal(
706+
BranchMatchingStrategy.MATCHED_BRANCHES,
707+
);
708+
expect(converter.toBranchMatchingStrategy(undefined)).to.be.undefined;
709+
});
710+
});
711+
712+
describe("toWorkspaceSettings", () => {
713+
it("should convert a workspace class string to a WorkspaceSettings", () => {
714+
const workspaceClass = "dev";
715+
const result = converter.toWorkspaceSettings(workspaceClass);
716+
expect(result).to.deep.equal(new WorkspaceSettings({ workspaceClass }));
717+
});
718+
719+
it("should return an empty WorkspaceSettings if no workspace class string is provided", () => {
720+
const result = converter.toWorkspaceSettings(undefined);
721+
expect(result).to.deep.equal(new WorkspaceSettings());
722+
});
723+
});
626724
});

components/gitpod-protocol/src/public-api-converter.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ import {
2626
OrganizationRole,
2727
OrganizationSettings,
2828
} from "@gitpod/public-api/lib/gitpod/v1/organization_pb";
29+
import {
30+
BranchMatchingStrategy,
31+
Configuration,
32+
PrebuildSettings,
33+
WorkspaceSettings,
34+
} from "@gitpod/public-api/lib/gitpod/v1/configuration_pb";
2935
import { ApplicationError, ErrorCode, ErrorCodes } from "./messaging/error";
3036
import {
3137
CommitContext,
@@ -50,6 +56,8 @@ import {
5056
OrgMemberInfo,
5157
OrgMemberRole,
5258
OrganizationSettings as OrganizationSettingsProtocol,
59+
Project,
60+
PrebuildSettings as PrebuildSettingsProtocol,
5361
} from "./teams-projects-protocol";
5462

5563
const applicationErrorCode = "application-error-code";
@@ -376,4 +384,49 @@ export class PublicAPIConverter {
376384
result.defaultWorkspaceImage = settings.defaultWorkspaceImage || undefined;
377385
return result;
378386
}
387+
388+
toConfiguration(project: Project): Configuration {
389+
const result = new Configuration();
390+
result.id = project.id;
391+
result.organizationId = project.teamId;
392+
result.name = project.name;
393+
result.cloneUrl = project.cloneUrl;
394+
result.workspaceSettings = this.toWorkspaceSettings(project.settings?.workspaceClasses?.regular);
395+
result.prebuildSettings = this.toPrebuildSettings(project.settings?.prebuilds);
396+
return result;
397+
}
398+
399+
toPrebuildSettings(prebuilds: PrebuildSettingsProtocol | undefined): PrebuildSettings {
400+
const result = new PrebuildSettings();
401+
if (prebuilds) {
402+
result.enabled = prebuilds.enable;
403+
result.branchMatchingPattern = prebuilds.branchMatchingPattern;
404+
result.branchStrategy = this.toBranchMatchingStrategy(prebuilds.branchStrategy);
405+
result.prebuildInterval = prebuilds.prebuildInterval;
406+
result.workspaceClass = prebuilds.workspaceClass;
407+
}
408+
return result;
409+
}
410+
411+
toBranchMatchingStrategy(
412+
branchStrategy?: PrebuildSettingsProtocol.BranchStrategy,
413+
): BranchMatchingStrategy | undefined {
414+
switch (branchStrategy) {
415+
case "default-branch":
416+
return BranchMatchingStrategy.DEFAULT_BRANCH;
417+
case "all-branches":
418+
return BranchMatchingStrategy.ALL_BRANCHES;
419+
case "matched-branches":
420+
return BranchMatchingStrategy.MATCHED_BRANCHES;
421+
}
422+
return undefined;
423+
}
424+
425+
toWorkspaceSettings(workspaceClass: string | undefined): WorkspaceSettings {
426+
const result = new WorkspaceSettings();
427+
if (workspaceClass) {
428+
result.workspaceClass = workspaceClass;
429+
}
430+
return result;
431+
}
379432
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
syntax = "proto3";
2+
3+
package gitpod.v1;
4+
5+
import "google/protobuf/timestamp.proto";
6+
import "gitpod/v1/pagination.proto";
7+
8+
option go_package = "github.com/gitpod-io/gitpod/components/public-api/go/v1";
9+
10+
message Configuration {
11+
string id = 1;
12+
string organization_id = 2;
13+
string name = 3;
14+
string clone_url = 4;
15+
google.protobuf.Timestamp creation_time = 5;
16+
PrebuildSettings prebuild_settings = 6;
17+
WorkspaceSettings workspace_settings = 7;
18+
}
19+
20+
message PrebuildSettings {
21+
optional bool enabled = 1;
22+
optional string branch_matching_pattern = 2;
23+
optional BranchMatchingStrategy branch_strategy = 3;
24+
optional int32 prebuild_interval = 4;
25+
optional string workspace_class = 5;
26+
}
27+
28+
enum BranchMatchingStrategy {
29+
BRANCH_MATCHING_STRATEGY_UNSPECIFIED = 0;
30+
BRANCH_MATCHING_STRATEGY_DEFAULT_BRANCH = 1;
31+
BRANCH_MATCHING_STRATEGY_ALL_BRANCHES = 2;
32+
BRANCH_MATCHING_STRATEGY_MATCHED_BRANCHES = 3;
33+
}
34+
35+
message WorkspaceSettings { optional string workspace_class = 1; }
36+
37+
service ConfigurationService {
38+
// Creates a new configuration.
39+
rpc CreateConfiguration(CreateConfigurationRequest)
40+
returns (CreateConfigurationResponse) {};
41+
42+
// Retrieves a configuration.
43+
rpc GetConfiguration(GetConfigurationRequest)
44+
returns (GetConfigurationResponse) {};
45+
46+
// Lists configurations.
47+
rpc ListConfigurations(ListConfigurationsRequest)
48+
returns (ListConfigurationsResponse) {};
49+
50+
// Deletes a configuration.
51+
rpc DeleteConfiguration(DeleteConfigurationRequest)
52+
returns (DeleteConfigurationResponse) {};
53+
}
54+
55+
message CreateConfigurationRequest {
56+
string organization_id = 1;
57+
string name = 2;
58+
string clone_url = 3;
59+
optional PrebuildSettings prebuild_settings = 4;
60+
optional WorkspaceSettings workspace_settings = 5;
61+
}
62+
63+
message CreateConfigurationResponse { Configuration configuration = 1; }
64+
65+
message GetConfigurationRequest { string configuration_id = 1; }
66+
67+
message GetConfigurationResponse { Configuration configuration = 1; }
68+
69+
message ListConfigurationsRequest {
70+
string organization_id = 1;
71+
string search_term = 2;
72+
PaginationRequest pagination = 3;
73+
}
74+
75+
message ListConfigurationsResponse {
76+
repeated Configuration configurations = 1;
77+
PaginationResponse pagination = 2;
78+
}
79+
80+
message DeleteConfigurationRequest { string configuration_id = 1; }
81+
82+
message DeleteConfigurationResponse {}

0 commit comments

Comments
 (0)