-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
739 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...otlin/routing/v1/organizations/repositories/CreateOrganizationRepositoryRestController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 🐻❄️📦 charted-server: Free, open source, and reliable Helm Chart registry made in Kotlin. | ||
* Copyright 2022-2023 Noelware, LLC. <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.noelware.charted.server.routing.v1.organizations.repositories | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.util.* | ||
import io.swagger.v3.oas.models.PathItem | ||
import org.noelware.charted.common.types.helm.RepoType | ||
import org.noelware.charted.common.types.responses.ApiResponse | ||
import org.noelware.charted.models.flags.ApiKeyScope.Repositories | ||
import org.noelware.charted.models.repositories.Repository | ||
import org.noelware.charted.modules.openapi.kotlin.dsl.idOrName | ||
import org.noelware.charted.modules.openapi.kotlin.dsl.json | ||
import org.noelware.charted.modules.openapi.kotlin.dsl.schema | ||
import org.noelware.charted.modules.openapi.toPaths | ||
import org.noelware.charted.modules.postgresql.controllers.getByIdOrNameOrNull | ||
import org.noelware.charted.modules.postgresql.controllers.organizations.OrganizationDatabaseController | ||
import org.noelware.charted.modules.postgresql.controllers.repositories.CreateRepositoryPayload | ||
import org.noelware.charted.modules.postgresql.controllers.repositories.RepositoryDatabaseController | ||
import org.noelware.charted.modules.postgresql.ktor.OwnerIdAttributeKey | ||
import org.noelware.charted.modules.postgresql.tables.OrganizationTable | ||
import org.noelware.charted.modules.search.SearchModule | ||
import org.noelware.charted.server.extensions.addAuthenticationResponses | ||
import org.noelware.charted.server.extensions.putAndRemove | ||
import org.noelware.charted.server.plugins.sessions.Sessions | ||
import org.noelware.charted.server.plugins.sessions.preconditions.canAccessOrganization | ||
import org.noelware.charted.server.plugins.sessions.preconditions.canEditMetadata | ||
import org.noelware.charted.server.routing.APIVersion | ||
import org.noelware.charted.server.routing.RestController | ||
|
||
class CreateOrganizationRepositoryRestController( | ||
private val organizations: OrganizationDatabaseController, | ||
private val repositories: RepositoryDatabaseController, | ||
private val search: SearchModule? = null | ||
): RestController("/organizations/{idOrName}/repositories", HttpMethod.Put) { | ||
override val apiVersion: APIVersion = APIVersion.V1 | ||
override fun Route.init() { | ||
install(Sessions) { | ||
this += Repositories.Create | ||
|
||
condition(::canAccessOrganization) | ||
condition { call -> canEditMetadata(call, organizations) } | ||
} | ||
} | ||
|
||
override suspend fun call(call: ApplicationCall) { | ||
val organization = organizations.getByIdOrNameOrNull(call.parameters.getOrFail("idOrName"), OrganizationTable::name)!! | ||
return call.attributes.putAndRemove(OwnerIdAttributeKey, organization.id) { | ||
val repository = repositories.create(call, call.receive()) | ||
search?.indexRepository(repository) | ||
|
||
call.respond(HttpStatusCode.Created, ApiResponse.ok(repository)) | ||
} | ||
} | ||
|
||
override fun toPathDsl(): PathItem = toPaths("/organizations/{idOrName}/repositories") { | ||
put { | ||
description = "Creates a repository that is owned by an organization" | ||
|
||
idOrName() | ||
requestBody { | ||
json { | ||
schema( | ||
CreateRepositoryPayload( | ||
"helm library to provide common stuff", | ||
false, | ||
"# Hello, world!\n> we do magic stuff here~!", | ||
"common", | ||
RepoType.LIBRARY, | ||
), | ||
) | ||
} | ||
} | ||
|
||
addAuthenticationResponses() | ||
response(HttpStatusCode.Created) { | ||
contentType(ContentType.Application.Json) { | ||
schema<ApiResponse.Ok<Repository>>() | ||
} | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...lin/routing/v1/organizations/repositories/GetAllOrganizationRepositoriesRestController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 🐻❄️📦 charted-server: Free, open source, and reliable Helm Chart registry made in Kotlin. | ||
* Copyright 2022-2023 Noelware, LLC. <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.noelware.charted.server.routing.v1.organizations.repositories | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.util.* | ||
import io.swagger.v3.oas.models.PathItem | ||
import org.noelware.charted.common.types.responses.ApiResponse | ||
import org.noelware.charted.models.repositories.Repository | ||
import org.noelware.charted.modules.openapi.kotlin.dsl.idOrName | ||
import org.noelware.charted.modules.openapi.kotlin.dsl.schema | ||
import org.noelware.charted.modules.openapi.toPaths | ||
import org.noelware.charted.modules.postgresql.controllers.getEntityByIdOrNameOrNull | ||
import org.noelware.charted.modules.postgresql.controllers.organizations.OrganizationDatabaseController | ||
import org.noelware.charted.modules.postgresql.controllers.repositories.RepositoryDatabaseController | ||
import org.noelware.charted.modules.postgresql.tables.OrganizationTable | ||
import org.noelware.charted.modules.postgresql.tables.RepositoryTable | ||
import org.noelware.charted.server.extensions.currentUser | ||
import org.noelware.charted.server.plugins.sessions.Sessions | ||
import org.noelware.charted.server.plugins.sessions.preconditions.canAccessOrganization | ||
import org.noelware.charted.server.routing.APIVersion | ||
import org.noelware.charted.server.routing.RestController | ||
import kotlin.reflect.typeOf | ||
|
||
class GetAllOrganizationRepositoriesRestController( | ||
private val organizations: OrganizationDatabaseController, | ||
private val controller: RepositoryDatabaseController | ||
): RestController("/organizations/{idOrName}/repositories") { | ||
override val apiVersion: APIVersion = APIVersion.V1 | ||
override fun Route.init() { | ||
install(Sessions) { | ||
allowNonAuthorizedRequests = true | ||
|
||
condition { call -> canAccessOrganization(call, false) } | ||
} | ||
} | ||
|
||
override suspend fun call(call: ApplicationCall) { | ||
val idOrName = call.parameters.getOrFail("idOrName") | ||
val org = organizations.getEntityByIdOrNameOrNull(idOrName, OrganizationTable::name) ?: return call.respond( | ||
HttpStatusCode.BadRequest, | ||
ApiResponse.err( | ||
"UNKNOWN_USER", | ||
"Unknown user with username or snowflake [$idOrName]", | ||
), | ||
) | ||
|
||
val repos = controller.all(RepositoryTable::owner to org.id.value) | ||
if (call.currentUser == null) { | ||
return call.respond(HttpStatusCode.OK, ApiResponse.ok(repos.filterNot { it.private })) | ||
} | ||
|
||
if (org.owner.id.value != call.currentUser?.id || !org.members.any { it.account.id.value == call.currentUser!!.id }) { | ||
return call.respond(HttpStatusCode.OK, ApiResponse.ok(repos.filterNot { it.private })) | ||
} | ||
|
||
call.respond(HttpStatusCode.OK, ApiResponse.ok(repos)) | ||
} | ||
|
||
override fun toPathDsl(): PathItem = toPaths("/organizations/{idOrName}/repositories") { | ||
get { | ||
description = "Returns all of an organization's repositories" | ||
|
||
idOrName() | ||
response(HttpStatusCode.OK) { | ||
contentType(ContentType.Application.Json) { | ||
schema(typeOf<ApiResponse.Ok<List<Repository>>>(), ApiResponse.ok(listOf<Repository>())) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.