Skip to content

Commit 617680e

Browse files
authored
Merge pull request #1353 from merico-dev/fix-app-golang-error
fix: scm cant get repo name when use url
2 parents 44ff61b + d177779 commit 617680e

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

internal/pkg/plugin/installer/reposcaffolding/option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func NewOptions(options configmanager.RawOptions) (*Options, error) {
2424
func (opts *Options) renderTplConfig() map[string]interface{} {
2525
// default render value from repo
2626
renderConfig := map[string]any{
27-
"AppName": opts.DestinationRepo.Repo,
27+
"AppName": opts.DestinationRepo.GetRepoName(),
2828
"Repo": map[string]string{
29-
"Name": opts.DestinationRepo.Repo,
29+
"Name": opts.DestinationRepo.GetRepoName(),
3030
"Owner": opts.DestinationRepo.GetRepoOwner(),
3131
},
3232
}

pkg/util/scm/git/repoinfo.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,26 @@ func (r *RepoInfo) SetDefault() error {
5555
}
5656

5757
func (r *RepoInfo) GetRepoOwner() string {
58+
// if org or owner is not empty, return org/owner
5859
if r.Org != "" {
5960
return r.Org
6061
}
61-
return r.Owner
62+
if r.Owner != "" {
63+
return r.Owner
64+
}
65+
// else return owner extract from url
66+
if r.CloneURL != "" {
67+
owner, _, err := r.CloneURL.extractRepoOwnerAndName()
68+
if err != nil {
69+
log.Warnf("git GetRepoName failed %s", err)
70+
}
71+
return owner
72+
}
73+
return ""
6274
}
6375

6476
func (r *RepoInfo) GetRepoPath() string {
65-
return fmt.Sprintf("%s/%s", r.GetRepoOwner(), r.Repo)
77+
return fmt.Sprintf("%s/%s", r.GetRepoOwner(), r.GetRepoName())
6678
}
6779

6880
func (r *RepoInfo) GetRepoName() string {
@@ -82,7 +94,7 @@ func (r *RepoInfo) GetRepoName() string {
8294
func (r *RepoInfo) GetCloneURL() string {
8395
var cloneURL string
8496
if r.CloneURL != "" {
85-
cloneURL = string(r.CloneURL)
97+
cloneURL = string(r.CloneURL.addGithubURLScheme())
8698
} else {
8799
cloneURL = string(r.buildScmURL())
88100
}
@@ -107,10 +119,9 @@ func (r *RepoInfo) getBranchWithDefault() string {
107119
if branch != "" {
108120
return branch
109121
}
110-
switch r.RepoType {
111-
case "github":
122+
if r.IsGithubRepo() {
112123
branch = "main"
113-
case "gitlab":
124+
} else {
114125
branch = "master"
115126
}
116127
return branch
@@ -143,7 +154,6 @@ func (r *RepoInfo) updateFieldsFromURLField() error {
143154
// 1. config basic info for different repo type
144155
if r.IsGithubRepo() {
145156
r.RepoType = "github"
146-
r.CloneURL = r.CloneURL.addGithubURLScheme()
147157
} else {
148158
r.RepoType = "gitlab"
149159
// extract gitlab baseURL from url string
@@ -205,7 +215,7 @@ func (r *RepoInfo) checkValid() error {
205215
// extractRepoOwnerAndName will get repoOwner and repoName from ScmURL
206216
func (u ScmURL) extractRepoOwnerAndName() (string, string, error) {
207217
var paths string
208-
ScmURLStr := string(u)
218+
ScmURLStr := string(u.addGithubURLScheme())
209219
c, err := url.ParseRequestURI(ScmURLStr)
210220
if err != nil {
211221
if strings.Contains(ScmURLStr, "git@") {
@@ -232,7 +242,7 @@ func (u ScmURL) extractRepoOwnerAndName() (string, string, error) {
232242
return repoOwner, repoName, nil
233243
}
234244

235-
// formatGithubCloneURL will add "https://" in github url config if it doesn't contain schme
245+
// addGithubURLScheme will add "https://" in github url config if it doesn't contain schme
236246
func (u ScmURL) addGithubURLScheme() ScmURL {
237247
cloneURL := string(u)
238248
if !strings.Contains(cloneURL, "git@") && !strings.HasPrefix(cloneURL, "http") {

pkg/util/scm/git/repoinfo_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ var _ = Describe("ScmURL type", func() {
397397
Expect(err.Error()).Should(ContainSubstring("git url repo path is not valid"))
398398
})
399399
})
400+
When("url is path doesn't have scheme", func() {
401+
BeforeEach(func() {
402+
cloneURL = "test.com/test_user/test_repo"
403+
})
404+
It("should add scheme auto", func() {
405+
owner, repo, err := cloneURL.extractRepoOwnerAndName()
406+
Expect(err).Error().ShouldNot(HaveOccurred())
407+
Expect(owner).Should(Equal("test_user"))
408+
Expect(repo).Should(Equal("test_repo"))
409+
})
410+
})
411+
400412
})
401413
When("cloneURL is git ssh format", func() {
402414
When("ssh format is valid", func() {

0 commit comments

Comments
 (0)