-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
5 additions
and
8 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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package gh | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"path/filepath" | ||
|
@@ -11,22 +10,20 @@ import ( | |
// RepositoryOwnerAndNameFromPath is from the github-release-source branch | ||
// once that one is merged we should that one instead of this one | ||
func RepositoryOwnerAndNameFromPath(urlStr string) (owner, repo string, err error) { | ||
defer func() { | ||
if err != nil { | ||
err = fmt.Errorf("failed to parse owner and repo name from URI %q: %w", urlStr, err) | ||
} | ||
}() | ||
wrapError := func(urlStr string, err error) error { | ||
return fmt.Errorf("failed to parse owner and repo name from URI %q: %w", urlStr, err) | ||
} | ||
urlStr = strings.TrimPrefix(urlStr, "[email protected]:") | ||
u, err := url.Parse(urlStr) | ||
if err != nil { | ||
return "", "", err | ||
return "", "", wrapError(urlStr, err) | ||
} | ||
if filepath.Ext(u.Path) == ".git" { | ||
u.Path = strings.TrimSuffix(u.Path, ".git") | ||
} | ||
owner, repo, found := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/") | ||
if !found || owner == "" || repo == "" { | ||
return owner, repo, errors.New("path missing expected parts") | ||
return owner, repo, wrapError(urlStr, err) | ||
} | ||
return owner, repo, nil | ||
} |