feat(helm): reuse existing git sources in Kubernetes Helm-from-git install [BE-13046] (#2900)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Chaim Lev-Ari
2026-06-15 22:01:31 +03:00
committed by GitHub
parent 491df61fbf
commit d9673e33ec
18 changed files with 348 additions and 151 deletions
@@ -6,7 +6,9 @@ import (
"fmt"
"net/http"
portainer "github.com/portainer/portainer/api"
gittypes "github.com/portainer/portainer/api/git/types"
"github.com/portainer/portainer/api/gitops/sources"
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response"
@@ -19,19 +21,32 @@ type fileResponse struct {
}
type repositoryFilePreviewPayload struct {
Repository string `json:"repository" example:"https://github.com/openfaas/faas" validate:"required"`
Reference string `json:"reference" example:"refs/heads/master"`
Username string `json:"username" example:"myGitUsername"`
Password string `json:"password" example:"myGitPassword"`
// SourceID resolves URL and auth from the stored Source record.
// When set, the inline Repository/Username/Password/TLSSkipVerify fields are ignored.
SourceID portainer.SourceID `json:"sourceID" example:"1"`
Reference string `json:"reference" example:"refs/heads/master"`
// Path to file whose content will be read
TargetFile string `json:"targetFile" example:"docker-compose.yml"`
// TLSSkipVerify skips SSL verification when cloning the Git repository
TLSSkipVerify bool `example:"false"`
// URL of a Git repository to preview.
// Deprecated: use SourceID instead
Repository string `json:"repository" example:"https://github.com/openfaas/faas"`
// Username for git authentication.
// Deprecated: use SourceID instead
Username string `json:"username" example:"myGitUsername"`
// Password for git authentication.
// Deprecated: use SourceID instead
Password string `json:"password" example:"myGitPassword"`
// TLSSkipVerify skips SSL verification when cloning the Git repository.
// Deprecated: use SourceID instead
TLSSkipVerify bool `json:"tlsSkipVerify" example:"false"`
}
func (payload *repositoryFilePreviewPayload) Validate(r *http.Request) error {
if len(payload.Repository) == 0 || !validate.IsURL(payload.Repository) {
return errors.New("invalid repository URL. Must correspond to a valid URL format")
if payload.SourceID == 0 {
if len(payload.Repository) == 0 || !validate.IsURL(payload.Repository) {
return errors.New("invalid repository URL. Must correspond to a valid URL format")
}
}
if len(payload.Reference) == 0 {
@@ -56,6 +71,7 @@ func (payload *repositoryFilePreviewPayload) Validate(r *http.Request) error {
// @param body body repositoryFilePreviewPayload true "Template details"
// @success 200 {object} fileResponse "Success"
// @failure 400 "Invalid request"
// @failure 404 "Source not found"
// @failure 500 "Server error"
// @router /gitops/repo/file/preview [post]
func (handler *Handler) gitOperationRepoFilePreview(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
@@ -65,6 +81,25 @@ func (handler *Handler) gitOperationRepoFilePreview(w http.ResponseWriter, r *ht
return httperror.BadRequest("Invalid request payload", err)
}
repoURL := payload.Repository
username := payload.Username
password := payload.Password
tlsSkipVerify := payload.TLSSkipVerify
if payload.SourceID != 0 {
src, httpErr := sources.ValidateGitSourceAccess(handler.dataStore, payload.SourceID)
if httpErr != nil {
return httpErr
}
repoURL = src.Git.URL
if src.Git.Authentication != nil {
username = src.Git.Authentication.Username
password = src.Git.Authentication.Password
}
tlsSkipVerify = src.Git.TLSSkipVerify
}
projectPath, err := handler.fileService.GetTemporaryPath()
if err != nil {
return httperror.InternalServerError("Unable to create temporary folder", err)
@@ -73,11 +108,11 @@ func (handler *Handler) gitOperationRepoFilePreview(w http.ResponseWriter, r *ht
err = handler.gitService.CloneRepository(
context.TODO(),
projectPath,
payload.Repository,
repoURL,
payload.Reference,
payload.Username,
payload.Password,
payload.TLSSkipVerify,
username,
password,
tlsSkipVerify,
)
if err != nil {
if errors.Is(err, gittypes.ErrAuthenticationFailure) {