-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cdsctl): workflow init detect vcs server (#6810)
Signed-off-by: Yvonnick Esnault <[email protected]>
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -102,6 +103,38 @@ func interactiveChooseVCSServer(proj *sdk.Project, gitRepo repo.Repo) (string, e | |
return "", cli.NewError("your CDS project must be linked to a repositories manager to perform this operation") | ||
case 1: | ||
return proj.VCSServers[0].Name, nil | ||
default: | ||
fetchURL, err := gitRepo.FetchURL(context.Background()) | ||
if err != nil { | ||
return "", cli.WrapError(err, "Unable to get remote URL") | ||
} | ||
if !strings.HasPrefix(fetchURL, "https://") && !strings.HasPrefix(fetchURL, "ssh://") && !strings.HasPrefix(fetchURL, "git@") { | ||
return "", cli.NewError("Unable to parse remote URL %v", fetchURL) | ||
} | ||
|
||
for _, v := range proj.VCSServers { | ||
if (strings.HasPrefix(fetchURL, "https://github.com/") || strings.HasPrefix(fetchURL, "[email protected]")) && v.Type == "github" && v.URL == "" { | ||
fmt.Printf(" * using repositories manager %s", cli.Magenta(v.Name)) | ||
return v.Name, nil | ||
} | ||
if v.URL != "" { | ||
rmURL, err := url.Parse(v.URL) | ||
if err != nil { | ||
return "", cli.WrapError(err, "Unable to get VCS Configuration") | ||
} | ||
rmHost := rmURL.Hostname() | ||
fetchURLParsed, err := url.Parse(fetchURL) | ||
if err != nil { | ||
return "", cli.NewError("Unable to parse remote URL %v", fetchURL) | ||
} | ||
originHost := fetchURLParsed.Hostname() | ||
if originHost == rmHost { | ||
fmt.Printf(" * using repositories manager %s (%s)", cli.Magenta(v.Name), cli.Magenta(rmURL.String())) | ||
fmt.Println() | ||
return v.Name, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Ask the user to choose the repository | ||
|