Skip to content

Commit

Permalink
feat: choice between multiple git provider configs (#1233)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic authored Oct 11, 2024
1 parent 0c6134b commit f3c0d4a
Show file tree
Hide file tree
Showing 24 changed files with 603 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (m *MockGitProviderService) GetConfig(id string) (*gitprovider.GitProviderC
return args.Get(0).(*gitprovider.GitProviderConfig), args.Error(1)
}

func (m *MockGitProviderService) GetConfigForUrl(url string) (*gitprovider.GitProviderConfig, error) {
func (m *MockGitProviderService) ListConfigsForUrl(url string) ([]*gitprovider.GitProviderConfig, error) {
args := m.Called(url)
return args.Get(0).(*gitprovider.GitProviderConfig), args.Error(1)
return args.Get(0).([]*gitprovider.GitProviderConfig), args.Error(1)
}

func (m *MockGitProviderService) GetGitProviderForHttpRequest(req *http.Request) (gitprovider.GitProvider, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ func (a *Agent) getGitProvider(repoUrl string) (*apiclient.GitProvider, error) {
}

encodedUrl := url.QueryEscape(repoUrl)
gitProvider, res, err := apiClient.GitProviderAPI.GetGitProviderForUrl(ctx, encodedUrl).Execute()
gitProviders, res, err := apiClient.GitProviderAPI.ListGitProvidersForUrl(ctx, encodedUrl).Execute()
if err != nil {
return nil, apiclient_util.HandleErrorResponse(res, err)
}

if gitProvider != nil {
return gitProvider, nil
if len(gitProviders) > 0 {
return &gitProviders[0], nil
}

return nil, nil
Expand Down
53 changes: 45 additions & 8 deletions pkg/api/controllers/gitprovider/gitprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/daytonaio/daytona/pkg/api/controllers"
"github.com/daytonaio/daytona/pkg/api/controllers/gitprovider/dto"
"github.com/daytonaio/daytona/pkg/apikey"
"github.com/daytonaio/daytona/pkg/gitprovider"
"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -46,18 +47,18 @@ func ListGitProviders(ctx *gin.Context) {
ctx.JSON(200, response)
}

// GetGitProviderForUrl godoc
// ListGitProvidersForUrl godoc
//
// @Tags gitProvider
// @Summary Get Git provider
// @Description Get Git provider
// @Summary List Git providers for url
// @Description List Git providers for url
// @Produce json
// @Param url path string true "Url"
// @Success 200 {object} gitprovider.GitProviderConfig
// @Param url path string true "Url"
// @Success 200 {array} gitprovider.GitProviderConfig
// @Router /gitprovider/for-url/{url} [get]
//
// @id GetGitProviderForUrl
func GetGitProviderForUrl(ctx *gin.Context) {
// @id ListGitProvidersForUrl
func ListGitProvidersForUrl(ctx *gin.Context) {
urlParam := ctx.Param("url")

decodedUrl, err := url.QueryUnescape(urlParam)
Expand All @@ -68,12 +69,48 @@ func GetGitProviderForUrl(ctx *gin.Context) {

server := server.GetInstance(nil)

gitProvider, err := server.GitProviderService.GetConfigForUrl(decodedUrl)
gitProviders, err := server.GitProviderService.ListConfigsForUrl(decodedUrl)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get git provider for url: %w", err))
return
}

apiKeyType, ok := ctx.Get("apiKeyType")
if !ok || apiKeyType == apikey.ApiKeyTypeClient {
for _, gitProvider := range gitProviders {
gitProvider.Token = ""
}
}

ctx.JSON(200, gitProviders)
}

// GetGitProvider godoc
//
// @Tags gitProvider
// @Summary Get Git provider
// @Description Get Git provider
// @Produce plain
// @Param gitProviderId path string true "ID"
// @Success 200 {object} gitprovider.GitProviderConfig
// @Router /gitprovider/{gitProviderId} [get]
//
// @id GetGitProvider
func GetGitProvider(ctx *gin.Context) {
id := ctx.Param("gitProviderId")

server := server.GetInstance(nil)

gitProvider, err := server.GitProviderService.GetConfig(id)
if err != nil {
statusCode, message, codeErr := controllers.GetHTTPStatusCodeAndMessageFromError(err)
if codeErr != nil {
ctx.AbortWithError(statusCode, codeErr)
}
ctx.AbortWithError(statusCode, errors.New(message))
return
}

ctx.JSON(200, gitProvider)
}

Expand Down
39 changes: 35 additions & 4 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,15 @@ const docTemplate = `{
},
"/gitprovider/for-url/{url}": {
"get": {
"description": "Get Git provider",
"description": "List Git providers for url",
"produces": [
"application/json"
],
"tags": [
"gitProvider"
],
"summary": "Get Git provider",
"operationId": "GetGitProviderForUrl",
"summary": "List Git providers for url",
"operationId": "ListGitProvidersForUrl",
"parameters": [
{
"type": "string",
Expand All @@ -500,7 +500,10 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/GitProvider"
"type": "array",
"items": {
"$ref": "#/definitions/GitProvider"
}
}
}
}
Expand Down Expand Up @@ -537,6 +540,34 @@ const docTemplate = `{
}
},
"/gitprovider/{gitProviderId}": {
"get": {
"description": "Get Git provider",
"produces": [
"text/plain"
],
"tags": [
"gitProvider"
],
"summary": "Get Git provider",
"operationId": "GetGitProvider",
"parameters": [
{
"type": "string",
"description": "ID",
"name": "gitProviderId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/GitProvider"
}
}
}
},
"delete": {
"description": "Remove Git provider",
"produces": [
Expand Down
39 changes: 35 additions & 4 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,15 @@
},
"/gitprovider/for-url/{url}": {
"get": {
"description": "Get Git provider",
"description": "List Git providers for url",
"produces": [
"application/json"
],
"tags": [
"gitProvider"
],
"summary": "Get Git provider",
"operationId": "GetGitProviderForUrl",
"summary": "List Git providers for url",
"operationId": "ListGitProvidersForUrl",
"parameters": [
{
"type": "string",
Expand All @@ -497,7 +497,10 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/GitProvider"
"type": "array",
"items": {
"$ref": "#/definitions/GitProvider"
}
}
}
}
Expand Down Expand Up @@ -534,6 +537,34 @@
}
},
"/gitprovider/{gitProviderId}": {
"get": {
"description": "Get Git provider",
"produces": [
"text/plain"
],
"tags": [
"gitProvider"
],
"summary": "Get Git provider",
"operationId": "GetGitProvider",
"parameters": [
{
"type": "string",
"description": "ID",
"name": "gitProviderId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/GitProvider"
}
}
}
},
"delete": {
"description": "Remove Git provider",
"produces": [
Expand Down
29 changes: 25 additions & 4 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,25 @@ paths:
summary: Remove Git provider
tags:
- gitProvider
get:
description: Get Git provider
operationId: GetGitProvider
parameters:
- description: ID
in: path
name: gitProviderId
required: true
type: string
produces:
- text/plain
responses:
"200":
description: OK
schema:
$ref: '#/definitions/GitProvider'
summary: Get Git provider
tags:
- gitProvider
/gitprovider/{gitProviderId}/{namespaceId}/{repositoryId}/branches:
get:
description: Get Git repository branches
Expand Down Expand Up @@ -1351,8 +1370,8 @@ paths:
- gitProvider
/gitprovider/for-url/{url}:
get:
description: Get Git provider
operationId: GetGitProviderForUrl
description: List Git providers for url
operationId: ListGitProvidersForUrl
parameters:
- description: Url
in: path
Expand All @@ -1365,8 +1384,10 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/GitProvider'
summary: Get Git provider
items:
$ref: '#/definitions/GitProvider'
type: array
summary: List Git providers for url
tags:
- gitProvider
/gitprovider/id-for-url/{url}:
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"strings"

"github.com/daytonaio/daytona/pkg/apikey"
"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
)
Expand All @@ -29,8 +30,18 @@ func AuthMiddleware() gin.HandlerFunc {

if !server.ApiKeyService.IsValidApiKey(token) {
ctx.AbortWithError(401, errors.New("unauthorized"))
return
}

apiKeyType := apikey.ApiKeyTypeClient

if server.ApiKeyService.IsWorkspaceApiKey(token) {
apiKeyType = apikey.ApiKeyTypeWorkspace
} else if server.ApiKeyService.IsProjectApiKey(token) {
apiKeyType = apikey.ApiKeyTypeProject
}

ctx.Set("apiKeyType", apiKeyType)
ctx.Next()
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (a *ApiServer) Start() error {
gitProviderController.GET("/:gitProviderId/:namespaceId/:repositoryId/pull-requests", gitprovider.GetRepoPRs)
gitProviderController.POST("/context", gitprovider.GetGitContext)
gitProviderController.POST("/context/url", gitprovider.GetUrlFromRepository)
gitProviderController.GET("/for-url/:url", gitprovider.ListGitProvidersForUrl)
gitProviderController.GET("/id-for-url/:url", gitprovider.GetGitProviderIdForUrl)
}

Expand All @@ -257,7 +258,7 @@ func (a *ApiServer) Start() error {
projectGroup.Use(middlewares.ProjectAuthMiddleware())
{
projectGroup.POST(workspaceController.BasePath()+"/:workspaceId/:projectId/state", workspace.SetProjectState)
projectGroup.GET(gitProviderController.BasePath()+"/for-url/:url", gitprovider.GetGitProviderForUrl)
projectGroup.GET(gitProviderController.BasePath()+"/:gitProviderId", gitprovider.GetGitProvider)
}

a.httpServer = &http.Server{
Expand Down
3 changes: 2 additions & 1 deletion pkg/apiclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Class | Method | HTTP request | Description
*ContainerRegistryAPI* | [**SetContainerRegistry**](docs/ContainerRegistryAPI.md#setcontainerregistry) | **Put** /container-registry/{server} | Set container registry credentials
*DefaultAPI* | [**HealthCheck**](docs/DefaultAPI.md#healthcheck) | **Get** /health | Health check
*GitProviderAPI* | [**GetGitContext**](docs/GitProviderAPI.md#getgitcontext) | **Post** /gitprovider/context | Get Git context
*GitProviderAPI* | [**GetGitProviderForUrl**](docs/GitProviderAPI.md#getgitproviderforurl) | **Get** /gitprovider/for-url/{url} | Get Git provider
*GitProviderAPI* | [**GetGitProvider**](docs/GitProviderAPI.md#getgitprovider) | **Get** /gitprovider/{gitProviderId} | Get Git provider
*GitProviderAPI* | [**GetGitProviderIdForUrl**](docs/GitProviderAPI.md#getgitprovideridforurl) | **Get** /gitprovider/id-for-url/{url} | Get Git provider ID
*GitProviderAPI* | [**GetGitUser**](docs/GitProviderAPI.md#getgituser) | **Get** /gitprovider/{gitProviderId}/user | Get Git context
*GitProviderAPI* | [**GetNamespaces**](docs/GitProviderAPI.md#getnamespaces) | **Get** /gitprovider/{gitProviderId}/namespaces | Get Git namespaces
Expand All @@ -101,6 +101,7 @@ Class | Method | HTTP request | Description
*GitProviderAPI* | [**GetRepositories**](docs/GitProviderAPI.md#getrepositories) | **Get** /gitprovider/{gitProviderId}/{namespaceId}/repositories | Get Git repositories
*GitProviderAPI* | [**GetUrlFromRepository**](docs/GitProviderAPI.md#geturlfromrepository) | **Post** /gitprovider/context/url | Get URL from Git repository
*GitProviderAPI* | [**ListGitProviders**](docs/GitProviderAPI.md#listgitproviders) | **Get** /gitprovider | List Git providers
*GitProviderAPI* | [**ListGitProvidersForUrl**](docs/GitProviderAPI.md#listgitprovidersforurl) | **Get** /gitprovider/for-url/{url} | List Git providers for url
*GitProviderAPI* | [**RemoveGitProvider**](docs/GitProviderAPI.md#removegitprovider) | **Delete** /gitprovider/{gitProviderId} | Remove Git provider
*GitProviderAPI* | [**SetGitProvider**](docs/GitProviderAPI.md#setgitprovider) | **Put** /gitprovider | Set Git provider
*PrebuildAPI* | [**DeletePrebuild**](docs/PrebuildAPI.md#deleteprebuild) | **Delete** /project-config/{configName}/prebuild/{prebuildId} | Delete prebuild
Expand Down
Loading

0 comments on commit f3c0d4a

Please sign in to comment.