Skip to content

Commit

Permalink
Use TransformURL over MutateGitURLsInText for the agent (#1207)
Browse files Browse the repository at this point in the history
This removes the requirement for a git url to end in .git

## Related Issue

Fixes #482

## Type of change

- [X] Bug fix (non-breaking change which fixes an issue)
  • Loading branch information
Racer159 authored and jeff-mccoy committed Jan 15, 2023
1 parent 0798db7 commit 929bc64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/internal/agent/hooks/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func mutateGitRepo(r *v1.AdmissionRequest) (result *operations.Result, err error
// Mutate the git URL if necessary
if isCreate || (isUpdate && !isPatched) {
// Mutate the git URL so that the hostname matches the hostname in the Zarf state
patchedURL = git.New(state.GitServer).MutateGitURLsInText(patchedURL)
patchedURL, err = git.New(state.GitServer).TransformURL(patchedURL)
if err != nil {
message.Warnf("Unable to transform the git url, using the original url we have: %s", patchedURL)
}
message.Debugf("original git URL of (%s) got mutated to (%s)", src.Spec.URL, patchedURL)
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/git/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (g *Git) prepRepoForPush() (*git.Repository, error) {
}

remoteURL := remote.Config().URLs[0]
targetURL, err := g.transformURL(remoteURL)
targetURL, err := g.TransformURL(remoteURL)
if err != nil {
return nil, fmt.Errorf("unable to transform the git url: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/internal/packager/git/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ var gitURLRegex = regexp.MustCompile(`^(?P<proto>[a-z]+:\/\/)(?P<hostPath>.+?)\/
func (g *Git) MutateGitURLsInText(text string) string {
extractPathRegex := regexp.MustCompilePOSIX(`https?://[^/]+/(.*\.git)`)
output := extractPathRegex.ReplaceAllStringFunc(text, func(match string) string {
output, err := g.transformURL(match)
output, err := g.TransformURL(match)
if err != nil {
message.Warnf("Unable to transform the git url, using the original url we have: %s", match)
output = match
}
return output
})
Expand Down Expand Up @@ -52,10 +51,11 @@ func (g *Git) TransformURLtoRepoName(url string) (string, error) {
return newRepoName, nil
}

func (g *Git) transformURL(url string) (string, error) {
// TransformURL takes a git url and returns a Zarf-compatible url.
func (g *Git) TransformURL(url string) (string, error) {
repoName, err := g.TransformURLtoRepoName(url)
if err != nil {
return "", err
return url, err
}
output := fmt.Sprintf("%s/%s/%s", g.Server.Address, g.Server.PushUsername, repoName)
message.Debugf("Rewrite git URL: %s -> %s", url, output)
Expand Down

0 comments on commit 929bc64

Please sign in to comment.