Skip to content

Commit

Permalink
Move gitops function to Build Service and remove dependency on Applic…
Browse files Browse the repository at this point in the history
…ation Service (#283)
  • Loading branch information
mmorhun authored Apr 26, 2024
1 parent bb1475e commit f61c0d5
Show file tree
Hide file tree
Showing 18 changed files with 560 additions and 257 deletions.
45 changes: 45 additions & 0 deletions controllers/component_build_controller_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"sort"
"strings"
Expand All @@ -43,6 +44,50 @@ import (
// That way it can be mocked in tests
var DevfileSearchForDockerfile = devfile.SearchForDockerfile

// getGitProvider returns git provider name based on the repository url, e.g. github, gitlab, etc or git-privider annotation
func getGitProvider(component appstudiov1alpha1.Component) (string, error) {
allowedGitProviders := map[string]bool{"github": true, "gitlab": true, "bitbucket": true}
gitProvider := ""

sourceUrl := component.Spec.Source.GitSource.URL

if strings.HasPrefix(sourceUrl, "git@") {
// [email protected]:redhat-appstudio/application-service.git
sourceUrl = strings.TrimPrefix(sourceUrl, "git@")
host := strings.Split(sourceUrl, ":")[0]
gitProvider = strings.Split(host, ".")[0]
} else {
// https://github.com/redhat-appstudio/application-service
u, err := url.Parse(sourceUrl)
if err != nil {
return "", err
}
uParts := strings.Split(u.Hostname(), ".")
if len(uParts) == 1 {
gitProvider = uParts[0]
} else {
gitProvider = uParts[len(uParts)-2]
}
}

var err error
if !allowedGitProviders[gitProvider] {
// Self-hosted git provider, check for git-provider annotation on the component
gitProviderAnnotationValue := component.GetAnnotations()[GitProviderAnnotationName]
if gitProviderAnnotationValue != "" {
if allowedGitProviders[gitProviderAnnotationValue] {
gitProvider = gitProviderAnnotationValue
} else {
err = fmt.Errorf("unsupported \"%s\" annotation value: %s", GitProviderAnnotationName, gitProviderAnnotationValue)
}
} else {
err = fmt.Errorf("self-hosted git provider is not specified via \"%s\" annotation in the component", GitProviderAnnotationName)
}
}

return gitProvider, err
}

// GetPipelineForComponent searches for the build pipeline to use on the component.
func (r *ComponentBuildReconciler) GetPipelineForComponent(ctx context.Context, component *appstudiov1alpha1.Component) (*tektonapi.PipelineRef, []tektonapi.Param, error) {
var pipelineSelectors []buildappstudiov1alpha1.BuildPipelineSelector
Expand Down
Loading

0 comments on commit f61c0d5

Please sign in to comment.