Skip to content

Commit

Permalink
Update v58
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Mar 22, 2024
1 parent e798055 commit 68fb84a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
32 changes: 28 additions & 4 deletions v58/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/v58/github.Client with environment variable resolution
func NewGithubClient(opts ...Option) (*github.Client, error) {
c := &Config{
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 16 additions & 7 deletions v58/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,35 @@ 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
wantOwner string
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)
Expand Down

0 comments on commit 68fb84a

Please sign in to comment.