Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

feat(repo): Create project from URL #709

Merged
merged 2 commits into from
May 11, 2021

Conversation

zemzale
Copy link
Collaborator

@zemzale zemzale commented May 11, 2021

Description
This is gonna allow for creating projects using full URL which is gonna
override the host URL too.

The command doesn't have unit tests and probably should be refactored, so I opened issue about that #708

Related Issue
Resolves #618

How Has This Been Tested?
Locally tested

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation
  • Chore (Related to CI or Packaging to platforms)

This is gonna allow for creating projects using full URL which is gonna
override the host URL too.

Issue profclems#618
@codecov
Copy link

codecov bot commented May 11, 2021

Codecov Report

Merging #709 (84844a7) into trunk (a17c562) will decrease coverage by 0.13%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            trunk     #709      +/-   ##
==========================================
- Coverage   59.61%   59.48%   -0.14%     
==========================================
  Files          90       90              
  Lines        6503     6518      +15     
==========================================
  Hits         3877     3877              
- Misses       2254     2269      +15     
  Partials      372      372              
Impacted Files Coverage Δ
commands/project/create/project_create.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a17c562...84844a7. Read the comment docs.

Copy link
Owner

@profclems profclems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @zemzale.

I tested this and got a few issues

@zemzale
Copy link
Collaborator Author

zemzale commented May 11, 2021

Indeed these cases wont work. I will fix these issues, shouldn't be too hard, I just didn't have the chance to test against a private instance.

@profclems
Copy link
Owner

I think it should be easier with this function in the glrepo package

func FromFullName(nwo string) (Interface, error) {
nwo = strings.TrimSpace(nwo)
// check if it's a valid git URL and parse it
if git.IsValidURL(nwo) {
u, err := git.ParseURL(nwo)
if err != nil {
return nil, err
}
return FromURL(u)
}
// check if it is valid URL and parse it
if utils.IsValidURL(nwo) {
u, _ := url.Parse(nwo)
return FromURL(u)
}
repo := nwo[strings.LastIndex(nwo, "/")+1:]
nwoWithoutRepo := strings.TrimSuffix(nwo[:strings.LastIndex(nwo, "/")+1], "/")
parts := strings.SplitN(nwoWithoutRepo, "/", 2)
if repo == "" {
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
for _, p := range parts {
if p == "" {
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
}
switch len(parts) {
case 2: // GROUP/NAMESPACE/REPO or HOST/OWNER/REPO or //HOST/GROUP/NAMESPACE/REPO
// First, checks if the first part matches the default instance host (i.e. gitlab.com) or the
// overridden default host (mostly from the GITLAB_HOST env variable)
if parts[0] == glinstance.Default() || parts[0] == glinstance.OverridableDefault() {
return NewWithHost(parts[1], repo, normalizeHostname(parts[0])), nil
}
// Dots (.) are allowed in group names by GitLab.
// So we check if if the first part contains a dot.
// However, it could be that the user is specifying a hostname but we can't be sure of that
// So we check in the list of authenticated hosts and see if it matches any
// if not, we assume it is a group name that contains a dot
if strings.ContainsRune(parts[0], '.') {
var rI Interface
cfg, err := config.Init()
if err == nil {
hosts, _ := cfg.Hosts()
for _, host := range hosts {
if host == parts[0] {
rI = NewWithHost(parts[1], repo, normalizeHostname(parts[0]))
break
}
}
if rI != nil {
return rI, nil
}
}
}
// if the first part is not a valid URL, and does not match an
// authenticated hostname then we assume it is in
// the format GROUP/NAMESPACE/REPO
return NewWithGroup(parts[0], parts[1], repo, ""), nil
case 1: // OWNER/REPO
return New(parts[0], repo), nil
default:
return nil, fmt.Errorf(`expected the "[HOST/]OWNER/[NAMESPACE/]REPO" format, got %q`, nwo)
}
}

This ensures supoport for

- ssh urls
- urls with .git prefix
- respect hostname in url provided
@profclems
Copy link
Owner

@zemzale I just pushed a commit to fix the issues I listed

@profclems profclems merged commit 33450b8 into profclems:trunk May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the project create command currently does not support full urls as parameter
2 participants