Add EndpointType

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-12 17:32:37 +00:00
parent ef676488b7
commit 4890eb4732
6 changed files with 48 additions and 11 deletions

View file

@ -103,6 +103,7 @@ func (a *APIController) handleWorkflowJobEvent(ctx context.Context, w http.Respo
handleError(ctx, w, gErrors.NewBadRequestError("invalid post body: %s", err))
return
}
slog.Debug("received workflow job event", "body", string(body))
signature := r.Header.Get("X-Hub-Signature-256")
hookType := r.Header.Get("X-Github-Hook-Installation-Target-Type")
@ -154,6 +155,9 @@ func (a *APIController) WebhookHandler(w http.ResponseWriter, r *http.Request) {
}
headers := r.Header.Clone()
for k, v := range headers {
slog.Debug("header", "key", k, "value", v)
}
event := runnerParams.Event(headers.Get("X-Github-Event"))
switch event {

View file

@ -97,6 +97,7 @@ func (s *sqlDatabase) sqlToCommonGithubEndpoint(ep GithubEndpoint) (params.Githu
UploadBaseURL: ep.UploadBaseURL,
CACertBundle: ep.CACertBundle,
CreatedAt: ep.CreatedAt,
EndpointType: ep.EndpointType,
UpdatedAt: ep.UpdatedAt,
}, nil
}

View file

@ -381,6 +381,8 @@ type GithubEndpoint struct {
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
EndpointType params.EndpointType
Description string `gorm:"type:text"`
APIBaseURL string `gorm:"type:text collate nocase"`
UploadBaseURL string `gorm:"type:text collate nocase"`

View file

@ -409,6 +409,17 @@ func (s *sqlDatabase) migrateDB() error {
}
}
if s.conn.Migrator().HasTable(&GithubEndpoint{}) {
if !s.conn.Migrator().HasColumn(&GithubEndpoint{}, "endpoint_type") {
if err := s.conn.Migrator().AutoMigrate(&GithubEndpoint{}); err != nil {
return errors.Wrap(err, "migrating github endpoints")
}
if err := s.conn.Exec("update github_endpoints set endpoint_type = 'github' where endpoint_type is null").Error; err != nil {
return errors.Wrap(err, "updating github endpoints")
}
}
}
var needsCredentialMigration bool
if !s.conn.Migrator().HasTable(&GithubCredentials{}) || !s.conn.Migrator().HasTable(&GithubEndpoint{}) {
needsCredentialMigration = true

View file

@ -44,6 +44,7 @@ type (
RunnerStatus string
WebhookEndpointType string
GithubAuthType string
EndpointType string
PoolBalancerType string
ScaleSetState string
ScaleSetMessageType string
@ -76,6 +77,11 @@ const (
PoolBalancerTypeNone PoolBalancerType = ""
)
const (
GithubEndpointType EndpointType = "github"
GiteaEndpointType EndpointType = "gitea"
)
const (
// LXDProvider represents the LXD provider.
LXDProvider ProviderType = "lxd"
@ -1138,5 +1144,7 @@ type GithubEndpoint struct {
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
EndpointType EndpointType `json:"endpoint_type,omitempty"`
Credentials []GithubCredentials `json:"credentials,omitempty"`
}

View file

@ -281,6 +281,7 @@ type CreateGithubEndpointParams struct {
APIBaseURL string `json:"api_base_url,omitempty"`
UploadBaseURL string `json:"upload_base_url,omitempty"`
BaseURL string `json:"base_url,omitempty"`
EndpointType string `json:"endpoint_type,omitempty"`
CACertBundle []byte `json:"ca_cert_bundle,omitempty"`
}
@ -289,6 +290,14 @@ func (c CreateGithubEndpointParams) Validate() error {
return runnerErrors.NewBadRequestError("missing api_base_url")
}
if c.EndpointType != "" {
switch c.EndpointType {
case string(GithubEndpointType), string(GiteaEndpointType):
default:
return runnerErrors.NewBadRequestError("invalid endpoint_type: %s", c.EndpointType)
}
}
url, err := url.Parse(c.APIBaseURL)
if err != nil || url.Scheme == "" || url.Host == "" {
return runnerErrors.NewBadRequestError("invalid api_base_url")
@ -299,19 +308,21 @@ func (c CreateGithubEndpointParams) Validate() error {
return runnerErrors.NewBadRequestError("invalid api_base_url")
}
if c.UploadBaseURL == "" {
return runnerErrors.NewBadRequestError("missing upload_base_url")
}
if c.EndpointType == string(GithubEndpointType) {
if c.UploadBaseURL == "" {
return runnerErrors.NewBadRequestError("missing upload_base_url")
}
url, err = url.Parse(c.UploadBaseURL)
if err != nil || url.Scheme == "" || url.Host == "" {
return runnerErrors.NewBadRequestError("invalid upload_base_url")
}
url, err = url.Parse(c.UploadBaseURL)
if err != nil || url.Scheme == "" || url.Host == "" {
return runnerErrors.NewBadRequestError("invalid upload_base_url")
}
switch url.Scheme {
case httpsScheme, httpScheme:
default:
return runnerErrors.NewBadRequestError("invalid api_base_url")
switch url.Scheme {
case httpsScheme, httpScheme:
default:
return runnerErrors.NewBadRequestError("invalid api_base_url")
}
}
if c.BaseURL == "" {