1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Package repository is a set of types and functions for modeling and
// interacting with GitHub repositories.
package repository
import (
"errors"
"fmt"
"os"
"strings"
"github.com/cli/go-gh/v2/internal/git"
"github.com/cli/go-gh/v2/pkg/auth"
"github.com/cli/go-gh/v2/pkg/ssh"
)
// Repository holds information representing a GitHub repository.
type Repository struct {
Host string
Name string
Owner string
}
// Parse extracts the repository information from the following
// string formats: "OWNER/REPO", "HOST/OWNER/REPO", and a full URL.
// If the format does not specify a host, use the config to determine a host.
func Parse(s string) (Repository, error) {
var r Repository
if git.IsURL(s) {
u, err := git.ParseURL(s)
if err != nil {
return r, err
}
host, owner, name, err := git.RepoInfoFromURL(u)
if err != nil {
return r, err
}
r.Host = host
r.Name = name
r.Owner = owner
return r, nil
}
parts := strings.SplitN(s, "/", 4)
for _, p := range parts {
if len(p) == 0 {
return r, fmt.Errorf(`expected the "[HOST/]OWNER/REPO" format, got %q`, s)
}
}
switch len(parts) {
case 3:
r.Host = parts[0]
r.Owner = parts[1]
r.Name = parts[2]
return r, nil
case 2:
r.Host, _ = auth.DefaultHost()
r.Owner = parts[0]
r.Name = parts[1]
return r, nil
default:
return r, fmt.Errorf(`expected the "[HOST/]OWNER/REPO" format, got %q`, s)
}
}
// Parse extracts the repository information from the following
// string formats: "OWNER/REPO", "HOST/OWNER/REPO", and a full URL.
// If the format does not specify a host, use the host provided.
func ParseWithHost(s, host string) (Repository, error) {
var r Repository
if git.IsURL(s) {
u, err := git.ParseURL(s)
if err != nil {
return r, err
}
host, owner, name, err := git.RepoInfoFromURL(u)
if err != nil {
return r, err
}
r.Host = host
r.Owner = owner
r.Name = name
return r, nil
}
parts := strings.SplitN(s, "/", 4)
for _, p := range parts {
if len(p) == 0 {
return r, fmt.Errorf(`expected the "[HOST/]OWNER/REPO" format, got %q`, s)
}
}
switch len(parts) {
case 3:
r.Host = parts[0]
r.Owner = parts[1]
r.Name = parts[2]
return r, nil
case 2:
r.Host = host
r.Owner = parts[0]
r.Name = parts[1]
return r, nil
default:
return r, fmt.Errorf(`expected the "[HOST/]OWNER/REPO" format, got %q`, s)
}
}
// Current uses git remotes to determine the GitHub repository
// the current directory is tracking.
func Current() (Repository, error) {
var r Repository
override := os.Getenv("GH_REPO")
if override != "" {
return Parse(override)
}
remotes, err := git.Remotes()
if err != nil {
return r, err
}
if len(remotes) == 0 {
return r, errors.New("unable to determine current repository, no git remotes configured for this repository")
}
translator := ssh.NewTranslator()
for _, r := range remotes {
if r.FetchURL != nil {
r.FetchURL = translator.Translate(r.FetchURL)
}
if r.PushURL != nil {
r.PushURL = translator.Translate(r.PushURL)
}
}
hosts := auth.KnownHosts()
filteredRemotes := remotes.FilterByHosts(hosts)
if len(filteredRemotes) == 0 {
return r, errors.New("unable to determine current repository, none of the git remotes configured for this repository point to a known GitHub host")
}
rem := filteredRemotes[0]
r.Host = rem.Host
r.Owner = rem.Owner
r.Name = rem.Repo
return r, nil
}
|