Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webserver): add GraphQL endpoints for gitlab integration #1967

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 64 additions & 23 deletions ee/tabby-webserver/graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
input UpdateRepositoryProviderInput {
id: ID!
displayName: String!
accessToken: String!
}

type GitlabRepositoryProvider {
id: ID!
displayName: String!
connected: Boolean!
}

enum Language {
RUST
PYTHON
Expand Down Expand Up @@ -36,6 +48,11 @@ type JobRun {
stderr: String!
}

type GitlabProvidedRepositoryEdge {
node: GitlabProvidedRepository!
cursor: String!
}

enum AuthMethod {
NONE
PLAIN
Expand Down Expand Up @@ -71,6 +88,11 @@ type LicenseInfo {
expiresAt: DateTimeUtc
}

type GitlabRepositoryProviderEdge {
node: GitlabRepositoryProvider!
cursor: String!
}

type GithubRepositoryProviderConnection {
edges: [GithubRepositoryProviderEdge!]!
pageInfo: PageInfo!
Expand Down Expand Up @@ -130,6 +152,11 @@ type Invitation {
createdAt: DateTimeUtc!
}

type GitlabProvidedRepositoryConnection {
edges: [GitlabProvidedRepositoryEdge!]!
pageInfo: PageInfo!
}

type EmailSetting {
smtpUsername: String!
smtpServer: String!
Expand All @@ -139,17 +166,17 @@ type EmailSetting {
authMethod: AuthMethod!
}

type TokenAuthResponse {
accessToken: String!
refreshToken: String!
}

type JobStats {
success: Int!
failed: Int!
pending: Int!
}

type TokenAuthResponse {
accessToken: String!
refreshToken: String!
}

type NetworkSetting {
externalUrl: String!
}
Expand Down Expand Up @@ -201,10 +228,14 @@ type Mutation {
deleteEmailSetting: Boolean!
uploadLicense(license: String!): Boolean!
resetLicense: Boolean!
createGithubRepositoryProvider(input: CreateGithubRepositoryProviderInput!): ID!
createGithubRepositoryProvider(input: CreateRepositoryProviderInput!): ID!
deleteGithubRepositoryProvider(id: ID!): Boolean!
updateGithubRepositoryProvider(input: UpdateGithubRepositoryProviderInput!): Boolean!
updateGithubRepositoryProvider(input: UpdateRepositoryProviderInput!): Boolean!
updateGithubProvidedRepositoryActive(id: ID!, active: Boolean!): Boolean!
createGitlabRepositoryProvider(input: CreateRepositoryProviderInput!): ID!
deleteGitlabRepositoryProvider(id: ID!): Boolean!
updateGitlabRepositoryProvider(input: UpdateRepositoryProviderInput!): Boolean!
updateGitlabProvidedRepositoryActive(id: ID!, active: Boolean!): Boolean!
}

type UserEventEdge {
Expand All @@ -217,12 +248,6 @@ type RepositoryEdge {
cursor: String!
}

input UpdateGithubRepositoryProviderInput {
id: ID!
displayName: String!
accessToken: String!
}

"DateTime"
scalar DateTimeUtc

Expand Down Expand Up @@ -250,6 +275,8 @@ type Query {
invitations(after: String, before: String, first: Int, last: Int): InvitationConnection!
githubRepositoryProviders(ids: [ID!], after: String, before: String, first: Int, last: Int): GithubRepositoryProviderConnection!
githubRepositories(providerIds: [ID!]!, after: String, before: String, first: Int, last: Int): GithubProvidedRepositoryConnection!
gitlabRepositoryProviders(ids: [ID!], after: String, before: String, first: Int, last: Int): GitlabRepositoryProviderConnection!
gitlabRepositories(providerIds: [ID!]!, after: String, before: String, first: Int, last: Int): GitlabProvidedRepositoryConnection!
jobRuns(ids: [ID!], jobs: [String!], after: String, before: String, first: Int, last: Int): JobRunConnection!
jobRunStats(jobs: [String!]): JobStats!
emailSetting: EmailSetting
Expand Down Expand Up @@ -299,6 +326,11 @@ type RefreshTokenResponse {
refreshExpiresAt: DateTimeUtc!
}

type GitlabRepositoryProviderConnection {
edges: [GitlabRepositoryProviderEdge!]!
pageInfo: PageInfo!
}

type RepositoryConnection {
edges: [RepositoryEdge!]!
pageInfo: PageInfo!
Expand All @@ -320,6 +352,15 @@ enum LicenseType {
ENTERPRISE
}

type GitlabProvidedRepository {
id: ID!
vendorId: String!
gitlabRepositoryProviderId: ID!
name: String!
gitUrl: String!
active: Boolean!
}

type OAuthCredential {
provider: OAuthProvider!
clientId: String!
Expand Down Expand Up @@ -347,7 +388,7 @@ input PasswordResetInput {
password2: String!
}

input CreateGithubRepositoryProviderInput {
input CreateRepositoryProviderInput {
displayName: String!
accessToken: String!
}
Expand All @@ -363,10 +404,6 @@ type User {
isPasswordSet: Boolean!
}

input RequestInvitationInput {
email: String!
}

type Worker {
kind: WorkerKind!
name: String!
Expand All @@ -378,23 +415,27 @@ type Worker {
cudaDevices: [String!]!
}

enum OAuthProvider {
GITHUB
GOOGLE
}

type InvitationEdge {
node: Invitation!
cursor: String!
}

enum OAuthProvider {
GITHUB
GOOGLE
}

type PageInfo {
hasPreviousPage: Boolean!
hasNextPage: Boolean!
startCursor: String
endCursor: String
}

input RequestInvitationInput {
email: String!
}

type GithubProvidedRepositoryEdge {
node: GithubProvidedRepository!
cursor: String!
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::Context;
use crate::{juniper::relay::NodeType, schema::Result};

#[derive(GraphQLInputObject, Validate)]
pub struct CreateGithubRepositoryProviderInput {
pub struct CreateRepositoryProviderInput {
boxbeam marked this conversation as resolved.
Show resolved Hide resolved
#[validate(regex(
code = "displayName",
path = "crate::schema::constants::REPOSITORY_NAME_REGEX"
Expand All @@ -18,7 +18,7 @@ pub struct CreateGithubRepositoryProviderInput {
}

#[derive(GraphQLInputObject, Validate)]
pub struct UpdateGithubRepositoryProviderInput {
pub struct UpdateRepositoryProviderInput {
pub id: ID,
#[validate(regex(
code = "displayName",
Expand Down
131 changes: 124 additions & 7 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
},
email::{EmailService, EmailSetting, EmailSettingInput},
git_repository::GitRepository,
github_repository_provider::{
CreateGithubRepositoryProviderInput, GithubProvidedRepository, GithubRepositoryProvider,
UpdateGithubRepositoryProviderInput,
},
github_repository_provider::{GithubProvidedRepository, GithubRepositoryProvider},
job::JobStats,
license::{IsLicenseValid, LicenseInfo, LicenseService, LicenseType},
repository::RepositoryService,
Expand All @@ -53,7 +50,13 @@
use crate::{
axum::FromAuth,
juniper::relay::{self, Connection},
schema::repository::FileEntrySearchResult,
schema::{
github_repository_provider::{
CreateRepositoryProviderInput, UpdateRepositoryProviderInput,
},
gitlab_repository_provider::{GitlabProvidedRepository, GitlabRepositoryProvider},
repository::FileEntrySearchResult,
},
};

pub trait ServiceLocator: Send + Sync {
Expand Down Expand Up @@ -286,6 +289,68 @@
.await
}

async fn gitlab_repository_providers(
ctx: &Context,
ids: Option<Vec<ID>>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> Result<Connection<GitlabRepositoryProvider>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
ctx.locator
.repository()
.gitlab()
.list_gitlab_repository_providers(
ids.unwrap_or_default(),
after,
before,
first,
last,
)
.await
},

Check warning on line 318 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L306-L318

Added lines #L306 - L318 were not covered by tests
)
.await
}

async fn gitlab_repositories(
ctx: &Context,
provider_ids: Vec<ID>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> Result<Connection<GitlabProvidedRepository>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
ctx.locator
.repository()
.gitlab()
.list_gitlab_provided_repositories_by_provider(
provider_ids,
after,
before,
first,
last,
)
.await
},

Check warning on line 349 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L337-L349

Added lines #L337 - L349 were not covered by tests
)
.await
}

async fn job_runs(
ctx: &Context,
ids: Option<Vec<ID>>,
Expand Down Expand Up @@ -733,7 +798,7 @@

async fn create_github_repository_provider(
ctx: &Context,
input: CreateGithubRepositoryProviderInput,
input: CreateRepositoryProviderInput,
) -> Result<ID> {
check_admin(ctx).await?;
input.validate()?;
Expand All @@ -758,7 +823,7 @@

async fn update_github_repository_provider(
ctx: &Context,
input: UpdateGithubRepositoryProviderInput,
input: UpdateRepositoryProviderInput,
) -> Result<bool> {
check_admin(ctx).await?;
input.validate()?;
Expand All @@ -782,6 +847,58 @@
.await?;
Ok(true)
}

async fn create_gitlab_repository_provider(
ctx: &Context,
input: CreateRepositoryProviderInput,
) -> Result<ID> {
check_admin(ctx).await?;
input.validate()?;
let id = ctx
.locator
.repository()
.gitlab()
.create_gitlab_repository_provider(input.display_name, input.access_token)
.await?;
Ok(id)
}

async fn delete_gitlab_repository_provider(ctx: &Context, id: ID) -> Result<bool> {
check_admin(ctx).await?;
ctx.locator
.repository()
.gitlab()
.delete_gitlab_repository_provider(id)
.await?;
Ok(true)
}

async fn update_gitlab_repository_provider(
ctx: &Context,
input: UpdateRepositoryProviderInput,
) -> Result<bool> {
check_admin(ctx).await?;
input.validate()?;
ctx.locator
.repository()
.gitlab()
.update_gitlab_repository_provider(input.id, input.display_name, input.access_token)
.await?;
Ok(true)
}

async fn update_gitlab_provided_repository_active(
ctx: &Context,
id: ID,
active: bool,
) -> Result<bool> {
ctx.locator
.repository()
.gitlab()
.update_gitlab_provided_repository_active(id, active)
.await?;
Ok(true)
}
}

async fn check_analytic_access(ctx: &Context, users: &[ID]) -> Result<(), CoreError> {
Expand Down
Loading