diff --git a/v57/factory/factory.go b/v57/factory/factory.go index 9ae8269..f87bc24 100644 --- a/v57/factory/factory.go +++ b/v57/factory/factory.go @@ -25,6 +25,8 @@ const defaultV4Endpoint = "https://api.github.com/graphql" type Config struct { Token string Endpoint string + Owner string + Repo string DialTimeout time.Duration TLSHandshakeTimeout time.Duration Timeout time.Duration @@ -95,6 +97,25 @@ func SkipAuth(enable bool) Option { } } +func Owner(owner string) Option { + return func(c *Config) error { + c.Owner = owner + return nil + } +} + +func OwnerRepo(ownerrepo string) Option { + return func(c *Config) error { + splitted := strings.Split(ownerrepo, "/") + if len(splitted) != 2 { + return errors.New("invalid owner/repo format") + } + c.Owner = splitted[0] + c.Repo = splitted[1] + return nil + } +} + // NewGithubClient returns github.com/google/go-github/v57/github.Client with environment variable resolution func NewGithubClient(opts ...Option) (*github.Client, error) { c := &Config{ @@ -226,7 +247,7 @@ func newHTTPClientUsingGitHubApp(c *Config, ep string) (*http.Client, error) { privateKey := []byte(repairKey(envPrivateKey)) var installationID int64 if envInstallaitonID == "" { - installationID, err = detectInstallationID(appID, privateKey, ep) + installationID, err = detectInstallationID(c, appID, privateKey, ep) if err != nil { return nil, err } @@ -246,8 +267,8 @@ func newHTTPClientUsingGitHubApp(c *Config, ep string) (*http.Client, error) { return hc, nil } -func detectInstallationID(appID int64, privateKey []byte, ep string) (int64, error) { - owner, repo, err := detectOwnerRepo() +func detectInstallationID(c *Config, appID int64, privateKey []byte, ep string) (int64, error) { + owner, repo, err := detectOwnerRepo(c) if err != nil { return 0, err } @@ -294,7 +315,10 @@ func detectInstallationID(appID int64, privateKey []byte, ep string) (int64, err return 0, fmt.Errorf("could not installation for %s", owner) } -func detectOwnerRepo() (string, string, error) { +func detectOwnerRepo(c *Config) (string, string, error) { + if c.Owner != "" { + return c.Owner, c.Repo, nil + } if hostownerrepo := os.Getenv("GH_REPO"); hostownerrepo != "" { splitted := strings.Split(hostownerrepo, "/") switch { diff --git a/v57/factory/factory_test.go b/v57/factory/factory_test.go index 7ff2e6d..b1b6c06 100644 --- a/v57/factory/factory_test.go +++ b/v57/factory/factory_test.go @@ -145,6 +145,8 @@ func TestNewGithubClientUsingMock(t *testing.T) { func TestDetectOwnerRepo(t *testing.T) { tests := []struct { + owner string + repo string GH_REPO string GITHUB_REPOSITORY string GITHUB_REPOSITORY_OWNER string @@ -152,19 +154,26 @@ func TestDetectOwnerRepo(t *testing.T) { wantRepo string wantErr bool }{ - {"", "", "", "", "", true}, - {"example/myrepo", "", "", "example", "myrepo", false}, - {"git.example.com/example/myrepo", "", "", "example", "myrepo", false}, - {"", "example/myrepo", "", "example", "myrepo", false}, - {"example/ourrepo", "example/myrepo", "", "example", "ourrepo", false}, - {"", "", "example", "example", "", false}, + {"", "", "", "", "", "", "", true}, + {"", "", "example/myrepo", "", "", "example", "myrepo", false}, + {"", "", "git.example.com/example/myrepo", "", "", "example", "myrepo", false}, + {"", "", "", "example/myrepo", "", "example", "myrepo", false}, + {"", "", "example/ourrepo", "example/myrepo", "", "example", "ourrepo", false}, + {"", "", "", "", "example", "example", "", false}, + {"owner", "", "", "", "", "owner", "", false}, + {"owner", "repo", "", "", "", "owner", "repo", false}, + {"owner", "repo", "example/myrepo", "", "", "owner", "repo", false}, } for i, tt := range tests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { t.Setenv("GH_REPO", tt.GH_REPO) t.Setenv("GITHUB_REPOSITORY", tt.GITHUB_REPOSITORY) t.Setenv("GITHUB_REPOSITORY_OWNER", tt.GITHUB_REPOSITORY_OWNER) - gotOwner, gotRepo, err := detectOwnerRepo() + c := &Config{ + Owner: tt.owner, + Repo: tt.repo, + } + gotOwner, gotRepo, err := detectOwnerRepo(c) if err != nil { if !tt.wantErr { t.Errorf("got error: %v", err)