-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect hostnames when resolving git remotes and URL args to repos
- Loading branch information
Showing
8 changed files
with
143 additions
and
78 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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cli/cli/api" | ||
"github.com/cli/cli/context" | ||
"github.com/cli/cli/internal/ghrepo" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func issueFromArg(ctx context.Context, apiClient *api.Client, cmd *cobra.Command, arg string) (*api.Issue, ghrepo.Interface, error) { | ||
issue, baseRepo, err := issueFromURL(apiClient, arg) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if issue != nil { | ||
return issue, baseRepo, nil | ||
} | ||
|
||
baseRepo, err = determineBaseRepo(apiClient, cmd, ctx) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("could not determine base repo: %w", err) | ||
} | ||
|
||
issueNumber, err := strconv.Atoi(strings.TrimPrefix(arg, "#")) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("invalid issue format: %q", arg) | ||
} | ||
|
||
issue, err = issueFromNumber(apiClient, baseRepo, issueNumber) | ||
return issue, baseRepo, err | ||
} | ||
|
||
var issueURLRE = regexp.MustCompile(`^/([^/]+)/([^/]+)/issues/(\d+)`) | ||
|
||
func issueFromURL(apiClient *api.Client, s string) (*api.Issue, ghrepo.Interface, error) { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
return nil, nil, nil | ||
} | ||
|
||
if u.Scheme != "https" && u.Scheme != "http" { | ||
return nil, nil, nil | ||
} | ||
|
||
m := issueURLRE.FindStringSubmatch(u.Path) | ||
if m == nil { | ||
return nil, nil, nil | ||
} | ||
|
||
repo := ghrepo.NewWithHost(m[1], m[2], u.Hostname()) | ||
issueNumber, _ := strconv.Atoi(m[3]) | ||
issue, err := issueFromNumber(apiClient, repo, issueNumber) | ||
return issue, repo, err | ||
} | ||
|
||
func issueFromNumber(apiClient *api.Client, repo ghrepo.Interface, issueNumber int) (*api.Issue, error) { | ||
return api.IssueByNumber(apiClient, repo, issueNumber) | ||
} |
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
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
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
Oops, something went wrong.